dns_registration_windows.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //go:build !e2e_testing
  2. package overlay
  3. import (
  4. "fmt"
  5. "log/slog"
  6. "golang.org/x/sys/windows"
  7. "golang.org/x/sys/windows/registry"
  8. )
  9. var tcpipInterfaceKeys = []string{
  10. `SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces`,
  11. `SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters\Interfaces`,
  12. }
  13. // disableDNSRegistration prevents publishing overlay addresses in the host's
  14. // DNS records, where clients outside the overlay cannot reach them.
  15. // Failures are non-fatal so registry access cannot prevent tunnel creation.
  16. func disableDNSRegistration(l *slog.Logger, guid windows.GUID) {
  17. id := guid.String()
  18. for _, base := range tcpipInterfaceKeys {
  19. path := base + `\` + id
  20. if err := setDNSRegistrationValues(path); err != nil {
  21. l.Warn("Failed to disable DNS registration on the tun adapter", "error", err, "key", path)
  22. continue
  23. }
  24. l.Debug("Disabled DNS registration on the tun adapter", "key", path)
  25. }
  26. }
  27. func setDNSRegistrationValues(path string) error {
  28. // The stack may not have created the adapter's interface key yet.
  29. k, _, err := registry.CreateKey(registry.LOCAL_MACHINE, path, registry.SET_VALUE)
  30. if err != nil {
  31. return fmt.Errorf("open HKLM\\%s: %w", path, err)
  32. }
  33. defer k.Close()
  34. // Neither RegistrationEnabled nor DisableDynamicUpdate alone reliably
  35. // prevents registration, so all three settings are required.
  36. values := []struct {
  37. name string
  38. data uint32
  39. }{
  40. {"RegistrationEnabled", 0},
  41. {"DisableDynamicUpdate", 1},
  42. {"MaxNumberOfAddressesToRegister", 0},
  43. }
  44. for _, v := range values {
  45. if err := k.SetDWordValue(v.name, v.data); err != nil {
  46. return fmt.Errorf("set %s: %w", v.name, err)
  47. }
  48. }
  49. return nil
  50. }