2
0

interface_windows.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //go:build windows
  2. // +build windows
  3. // Copyright © 2021 Ettore Di Giacinto <[email protected]>
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License along
  16. // with this program; if not, see <http://www.gnu.org/licenses/>.
  17. package vpn
  18. import (
  19. "fmt"
  20. "log"
  21. "net"
  22. "os/exec"
  23. "github.com/songgao/water"
  24. )
  25. func prepareInterface(c *Config) error {
  26. err := netsh("interface", "ip", "set", "address", "name=", c.InterfaceName, "static", c.InterfaceAddress)
  27. if err != nil {
  28. log.Println(err)
  29. }
  30. err = netsh("interface", "ipv4", "set", "subinterface", c.InterfaceName, "mtu=", fmt.Sprintf("%d", c.InterfaceMTU))
  31. if err != nil {
  32. log.Println(err)
  33. }
  34. return nil
  35. }
  36. func createInterface(c *Config) (*water.Interface, error) {
  37. // TUN on Windows requires address and network to be set on device creation stage
  38. // We also set network to 0.0.0.0/0 so we able to reach networks behind the node
  39. // https://github.com/songgao/water/blob/master/params_windows.go
  40. // https://gitlab.com/openconnect/openconnect/-/blob/master/tun-win32.c
  41. ip, _, err := net.ParseCIDR(c.InterfaceAddress)
  42. if err != nil {
  43. return nil, err
  44. }
  45. network := net.IPNet{
  46. IP: ip,
  47. Mask: net.IPv4Mask(0, 0, 0, 0),
  48. }
  49. config := water.Config{
  50. DeviceType: c.DeviceType,
  51. PlatformSpecificParams: water.PlatformSpecificParams{
  52. ComponentID: "tap0901",
  53. InterfaceName: c.InterfaceName,
  54. Network: network.String(),
  55. },
  56. }
  57. return water.New(config)
  58. }
  59. func netsh(args ...string) (err error) {
  60. cmd := exec.Command("netsh", args...)
  61. err = cmd.Run()
  62. return
  63. }