github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/supervisor/daemon/wireguard/wginterface/interface_linux.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package wginterface
    19  
    20  import (
    21  	"fmt"
    22  	"net"
    23  	"os"
    24  	"path"
    25  	"strconv"
    26  
    27  	"github.com/rs/zerolog/log"
    28  	"golang.zx2c4.com/wireguard/device"
    29  	"golang.zx2c4.com/wireguard/ipc"
    30  	"golang.zx2c4.com/wireguard/tun"
    31  )
    32  
    33  func createTunnel(requestedInterfaceName string, _ []string) (tunnel tun.Device, interfaceName string, err error) {
    34  	tunnel, err = tun.CreateTUN(requestedInterfaceName, device.DefaultMTU)
    35  	if err == nil {
    36  		interfaceName = requestedInterfaceName
    37  		realInterfaceName, err2 := tunnel.Name()
    38  		if err2 == nil {
    39  			interfaceName = realInterfaceName
    40  		}
    41  	}
    42  	return tunnel, interfaceName, err
    43  }
    44  
    45  func newUAPIListener(interfaceName string) (listener net.Listener, err error) {
    46  	log.Info().Msg("Setting interface configuration")
    47  	fileUAPI, err := ipc.UAPIOpen(interfaceName)
    48  	if err != nil {
    49  		return nil, fmt.Errorf("UAPI listen error: %w", err)
    50  	}
    51  	uapi, err := ipc.UAPIListen(interfaceName, fileUAPI)
    52  	if err != nil {
    53  		return nil, fmt.Errorf("could not listen for UAPI wg configuration: %w", err)
    54  	}
    55  	return uapi, nil
    56  }
    57  
    58  func applySocketPermissions(interfaceName string, uid string) error {
    59  	numUid, err := strconv.Atoi(uid)
    60  	if err != nil {
    61  		return fmt.Errorf("failed to parse uid %s: %w", uid, err)
    62  	}
    63  	socketPath := path.Join("/var/run/wireguard", fmt.Sprintf("%s.sock", interfaceName))
    64  	err = os.Chown(socketPath, numUid, -1)
    65  	if err != nil {
    66  		return fmt.Errorf("failed to chown wireguard socket to uid %s: %w", uid, err)
    67  	}
    68  	return nil
    69  }
    70  
    71  func disableFirewall() {}