github.com/slackhq/nebula@v1.9.0/overlay/tun_windows.go (about)

     1  //go:build !e2e_testing
     2  // +build !e2e_testing
     3  
     4  package overlay
     5  
     6  import (
     7  	"fmt"
     8  	"net"
     9  	"os"
    10  	"path/filepath"
    11  	"runtime"
    12  	"syscall"
    13  
    14  	"github.com/sirupsen/logrus"
    15  	"github.com/slackhq/nebula/config"
    16  )
    17  
    18  func newTunFromFd(_ *config.C, _ *logrus.Logger, _ int, _ *net.IPNet) (Device, error) {
    19  	return nil, fmt.Errorf("newTunFromFd not supported in Windows")
    20  }
    21  
    22  func newTun(c *config.C, l *logrus.Logger, cidr *net.IPNet, multiqueue bool) (Device, error) {
    23  	useWintun := true
    24  	if err := checkWinTunExists(); err != nil {
    25  		l.WithError(err).Warn("Check Wintun driver failed, fallback to wintap driver")
    26  		useWintun = false
    27  	}
    28  
    29  	if useWintun {
    30  		device, err := newWinTun(c, l, cidr, multiqueue)
    31  		if err != nil {
    32  			return nil, fmt.Errorf("create Wintun interface failed, %w", err)
    33  		}
    34  		return device, nil
    35  	}
    36  
    37  	device, err := newWaterTun(c, l, cidr, multiqueue)
    38  	if err != nil {
    39  		return nil, fmt.Errorf("create wintap driver failed, %w", err)
    40  	}
    41  	return device, nil
    42  }
    43  
    44  func checkWinTunExists() error {
    45  	myPath, err := os.Executable()
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	arch := runtime.GOARCH
    51  	switch arch {
    52  	case "386":
    53  		//NOTE: wintun bundles 386 as x86
    54  		arch = "x86"
    55  	}
    56  
    57  	_, err = syscall.LoadDLL(filepath.Join(filepath.Dir(myPath), "dist", "windows", "wintun", "bin", arch, "wintun.dll"))
    58  	return err
    59  }