github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/supervisor/daemon/wireguard/wginterface/interface_windows.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  
    24  	"github.com/rs/zerolog/log"
    25  	"golang.zx2c4.com/wireguard/device"
    26  	"golang.zx2c4.com/wireguard/ipc"
    27  	"golang.zx2c4.com/wireguard/tun"
    28  
    29  	"github.com/mysteriumnetwork/node/supervisor/daemon/wireguard/wginterface/firewall"
    30  	"github.com/mysteriumnetwork/node/utils/cmdutil"
    31  )
    32  
    33  func createTunnel(interfaceName string, dns []string) (tunnel tun.Device, _ string, err error) {
    34  	log.Info().Msg("Creating Wintun interface")
    35  	wintun, err := tun.CreateTUN(interfaceName, device.DefaultMTU)
    36  	if err != nil {
    37  		return nil, interfaceName, fmt.Errorf("could not create Wintun tunnel: %w", err)
    38  	}
    39  
    40  	cmd := fmt.Sprintf(`netsh interface ipv4 set subinterface "%s" mtu=%d store=persistent`, interfaceName, device.DefaultMTU)
    41  	if _, err := cmdutil.PowerShell(cmd); err != nil {
    42  		return nil, interfaceName, fmt.Errorf("could not set MTU for tunnel: %w", err)
    43  	}
    44  
    45  	nativeTun := wintun.(*tun.NativeTun)
    46  
    47  	dnsIPs := []net.IP{}
    48  	for _, d := range dns {
    49  		dnsIPs = append(dnsIPs, net.ParseIP(d))
    50  	}
    51  
    52  	err = firewall.EnableFirewall(nativeTun.LUID(), false, dnsIPs)
    53  	if err != nil {
    54  		log.Warn().Err(err).Msg("Unable to enable DNS firewall rules")
    55  	}
    56  
    57  	wintunVersion, err := nativeTun.RunningVersion()
    58  	if err != nil {
    59  		log.Warn().Err(err).Msg("Unable to determine Wintun version")
    60  	} else {
    61  		log.Info().Msgf("Using Wintun/%s", wintunVersion)
    62  	}
    63  	return wintun, interfaceName, nil
    64  }
    65  
    66  func newUAPIListener(interfaceName string) (listener net.Listener, err error) {
    67  	uapi, err := ipc.UAPIListen(interfaceName)
    68  	if err != nil {
    69  		return nil, fmt.Errorf("could not listen for UAPI wg configuration: %w", err)
    70  	}
    71  	return uapi, nil
    72  }
    73  
    74  func applySocketPermissions(_ string, _ string) error {
    75  	return nil
    76  }
    77  
    78  func disableFirewall() {
    79  	firewall.DisableFirewall()
    80  }