interface_windows.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //go:build windows
  2. // +build windows
  3. /*
  4. Copyright © 2021-2022 Ettore Di Giacinto <[email protected]>
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. package vpn
  16. import (
  17. "net/netip"
  18. "github.com/mudler/water"
  19. "golang.org/x/sys/windows"
  20. "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
  21. )
  22. func prepareInterface(c *Config) error {
  23. // find interface created by water
  24. guid, err := windows.GUIDFromString("{00000000-FFFF-FFFF-FFE9-76E58C74063E}")
  25. if err != nil {
  26. return err
  27. }
  28. luid, err := winipcfg.LUIDFromGUID(&guid)
  29. if err != nil {
  30. return err
  31. }
  32. prefix, err := netip.ParsePrefix(c.InterfaceAddress)
  33. if err != nil {
  34. return err
  35. }
  36. addresses := append([]netip.Prefix{}, prefix)
  37. if err := luid.SetIPAddresses(addresses); err != nil {
  38. return err
  39. }
  40. iface, err := luid.IPInterface(windows.AF_INET)
  41. if err != nil {
  42. return err
  43. }
  44. iface.NLMTU = uint32(c.InterfaceMTU)
  45. if err := iface.Set(); err != nil {
  46. return err
  47. }
  48. return nil
  49. }
  50. func createInterface(c *Config) (*water.Interface, error) {
  51. config := water.Config{
  52. DeviceType: c.DeviceType,
  53. }
  54. config.Name = c.InterfaceName
  55. return water.New(config)
  56. }