tun_windows.go 1.3 KB

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