tun_water_windows.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package overlay
  2. import (
  3. "fmt"
  4. "io"
  5. "net"
  6. "os/exec"
  7. "strconv"
  8. "github.com/songgao/water"
  9. )
  10. type waterTun struct {
  11. Device string
  12. Cidr *net.IPNet
  13. MTU int
  14. UnsafeRoutes []Route
  15. *water.Interface
  16. }
  17. func newWaterTun(cidr *net.IPNet, defaultMTU int, unsafeRoutes []Route) (*waterTun, error) {
  18. // NOTE: You cannot set the deviceName under Windows, so you must check tun.Device after calling .Activate()
  19. return &waterTun{
  20. Cidr: cidr,
  21. MTU: defaultMTU,
  22. UnsafeRoutes: unsafeRoutes,
  23. }, nil
  24. }
  25. func (t *waterTun) Activate() error {
  26. var err error
  27. t.Interface, err = water.New(water.Config{
  28. DeviceType: water.TUN,
  29. PlatformSpecificParams: water.PlatformSpecificParams{
  30. ComponentID: "tap0901",
  31. Network: t.Cidr.String(),
  32. },
  33. })
  34. if err != nil {
  35. return fmt.Errorf("activate failed: %v", err)
  36. }
  37. t.Device = t.Interface.Name()
  38. // TODO use syscalls instead of exec.Command
  39. err = exec.Command(
  40. `C:\Windows\System32\netsh.exe`, "interface", "ipv4", "set", "address",
  41. fmt.Sprintf("name=%s", t.Device),
  42. "source=static",
  43. fmt.Sprintf("addr=%s", t.Cidr.IP),
  44. fmt.Sprintf("mask=%s", net.IP(t.Cidr.Mask)),
  45. "gateway=none",
  46. ).Run()
  47. if err != nil {
  48. return fmt.Errorf("failed to run 'netsh' to set address: %s", err)
  49. }
  50. err = exec.Command(
  51. `C:\Windows\System32\netsh.exe`, "interface", "ipv4", "set", "interface",
  52. t.Device,
  53. fmt.Sprintf("mtu=%d", t.MTU),
  54. ).Run()
  55. if err != nil {
  56. return fmt.Errorf("failed to run 'netsh' to set MTU: %s", err)
  57. }
  58. iface, err := net.InterfaceByName(t.Device)
  59. if err != nil {
  60. return fmt.Errorf("failed to find interface named %s: %v", t.Device, err)
  61. }
  62. for _, r := range t.UnsafeRoutes {
  63. err = exec.Command(
  64. "C:\\Windows\\System32\\route.exe", "add", r.Cidr.String(), r.Via.String(), "IF", strconv.Itoa(iface.Index), "METRIC", strconv.Itoa(r.Metric),
  65. ).Run()
  66. if err != nil {
  67. return fmt.Errorf("failed to add the unsafe_route %s: %v", r.Cidr.String(), err)
  68. }
  69. }
  70. return nil
  71. }
  72. func (t *waterTun) CidrNet() *net.IPNet {
  73. return t.Cidr
  74. }
  75. func (t *waterTun) DeviceName() string {
  76. return t.Device
  77. }
  78. func (t *waterTun) WriteRaw(b []byte) error {
  79. _, err := t.Write(b)
  80. return err
  81. }
  82. func (t *waterTun) Close() error {
  83. if t.Interface == nil {
  84. return nil
  85. }
  86. return t.Interface.Close()
  87. }
  88. func (t *waterTun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
  89. return nil, fmt.Errorf("TODO: multiqueue not implemented for windows")
  90. }