diff --git a/pkg/handler/connect.go b/pkg/handler/connect.go index cff57a50..869e4771 100644 --- a/pkg/handler/connect.go +++ b/pkg/handler/connect.go @@ -333,8 +333,10 @@ func (c *ConnectOptions) startLocalTunServe(ctx context.Context, forwardAddress } list.Insert(s) } - if err = os.Setenv(config.EnvInboundPodTunIPv6, c.localTunIPv6.String()); err != nil { - return err + if enable, _ := util.IsIPv6Enabled(); enable { + if err = os.Setenv(config.EnvInboundPodTunIPv6, c.localTunIPv6.String()); err != nil { + return err + } } r := core.Route{ diff --git a/pkg/tun/tun_windows.go b/pkg/tun/tun_windows.go index 43238692..4a125a33 100644 --- a/pkg/tun/tun_windows.go +++ b/pkg/tun/tun_windows.go @@ -13,7 +13,6 @@ import ( "github.com/pkg/errors" log "github.com/sirupsen/logrus" "golang.org/x/sys/windows" - "golang.org/x/sys/windows/registry" wintun "golang.zx2c4.com/wintun" wireguardtun "golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg" @@ -51,8 +50,7 @@ func createTun(cfg Config) (conn net.Conn, itf *net.Interface, err error) { } } - isIPv6Enable, _ := isIPv6Enabled() - if cfg.Addr6 != "" && isIPv6Enable { + if cfg.Addr6 != "" { if ipv6, _, err = net.ParseCIDR(cfg.Addr6); err != nil { return } @@ -179,57 +177,3 @@ func (c *winTunConn) SetReadDeadline(time.Time) error { func (c *winTunConn) SetWriteDeadline(time.Time) error { return &net.OpError{Op: "set", Net: "tun", Source: nil, Addr: nil, Err: errors.New("deadline not supported")} } - -/* -* -Reference: https://learn.microsoft.com/en-us/troubleshoot/windows-server/networking/configure-ipv6-in-windows#use-registry-key-to-configure-ipv6 - -| IPv6 Functionality | Registry value and comments | -|----------------------------------------------------|-----------------------------------------------------------------------------------------------| -| Prefer IPv4 over IPv6 | Decimal 32
Hexadecimal 0x20
Binary xx1x xxxx
Recommended instead of disabling IPv6. | -| Disable IPv6 | Decimal 255
Hexadecimal 0xFF
Binary 1111 1111
| -| Disable IPv6 on all nontunnel interfaces | Decimal 16
Hexadecimal 0x10
Binary xxx1 xxxx | -| Disable IPv6 on all tunnel interfaces | Decimal 1
Hexadecimal 0x01
Binary xxxx xxx1 | -| Disable IPv6 on all nontunnel interfaces (except the loopback) and on IPv6 tunnel interface | Decimal 17
Hexadecimal 0x11
Binary xxx1 xxx1 | -| Prefer IPv6 over IPv4 | Binary xx0x xxxx | -| Re-enable IPv6 on all nontunnel interfaces | Binary xxx0 xxxx | -| Re-enable IPv6 on all tunnel interfaces | Binary xxx xxx0 | -| Re-enable IPv6 on nontunnel interfaces and on IPv6 tunnel interfaces | Binary xxx0 xxx0 | - -Enable IPv6: - - Default Value Hexadecimal 0x00 - Decimal 0 - Prefer IPv4 over IPv6 Hexadecimal 0x20 - Decimal 32 - Prefer IPv6 over IPv4 Binary xx0x xxxx -*/ -func isIPv6Enabled() (bool, error) { - key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters`, registry.QUERY_VALUE) - if err != nil { - return false, err - } - defer key.Close() - - val, valtype, err := key.GetIntegerValue("DisabledComponents") - if errors.Is(err, registry.ErrNotExist) { - return true, nil - } - - if err != nil { - return false, err - } - - if valtype != registry.DWORD { - return false, nil - } - - switch val { - case 0x00: - return true, nil - case 0x20: - return true, nil - default: - return false, nil - } -} diff --git a/pkg/util/net_others.go b/pkg/util/net_others.go new file mode 100644 index 00000000..9f9ea3b0 --- /dev/null +++ b/pkg/util/net_others.go @@ -0,0 +1,25 @@ +//go:build !windows + +package util + +import "net" + +func IsIPv6Enabled() (bool, error) { + addrs, err := net.InterfaceAddrs() + if err != nil { + return false, err + } + + ipv6Enabled := false + for _, addr := range addrs { + // Type assertion to net.IPNet to get the IP address without the mask. + if ipNet, ok := addr.(*net.IPNet); ok && ipNet.IP.To16() != nil { + if ipNet.IP.To4() == nil { // This is an IPv6 address + ipv6Enabled = true + break + } + } + } + + return ipv6Enabled, nil +} diff --git a/pkg/util/net_windows.go b/pkg/util/net_windows.go new file mode 100644 index 00000000..fd14b032 --- /dev/null +++ b/pkg/util/net_windows.go @@ -0,0 +1,62 @@ +//go:build windows + +package util + +import ( + "github.com/pkg/errors" + "golang.org/x/sys/windows/registry" +) + +/* +* +Reference: https://learn.microsoft.com/en-us/troubleshoot/windows-server/networking/configure-ipv6-in-windows#use-registry-key-to-configure-ipv6 + +| IPv6 Functionality | Registry value and comments | +|----------------------------------------------------|-----------------------------------------------------------------------------------------------| +| Prefer IPv4 over IPv6 | Decimal 32
Hexadecimal 0x20
Binary xx1x xxxx
Recommended instead of disabling IPv6. | +| Disable IPv6 | Decimal 255
Hexadecimal 0xFF
Binary 1111 1111
| +| Disable IPv6 on all nontunnel interfaces | Decimal 16
Hexadecimal 0x10
Binary xxx1 xxxx | +| Disable IPv6 on all tunnel interfaces | Decimal 1
Hexadecimal 0x01
Binary xxxx xxx1 | +| Disable IPv6 on all nontunnel interfaces (except the loopback) and on IPv6 tunnel interface | Decimal 17
Hexadecimal 0x11
Binary xxx1 xxx1 | +| Prefer IPv6 over IPv4 | Binary xx0x xxxx | +| Re-enable IPv6 on all nontunnel interfaces | Binary xxx0 xxxx | +| Re-enable IPv6 on all tunnel interfaces | Binary xxx xxx0 | +| Re-enable IPv6 on nontunnel interfaces and on IPv6 tunnel interfaces | Binary xxx0 xxx0 | + +Enable IPv6: + + Default Value Hexadecimal 0x00 + Decimal 0 + Prefer IPv4 over IPv6 Hexadecimal 0x20 + Decimal 32 + Prefer IPv6 over IPv4 Binary xx0x xxxx +*/ +func IsIPv6Enabled() (bool, error) { + key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters`, registry.QUERY_VALUE) + if err != nil { + return false, err + } + defer key.Close() + + val, valtype, err := key.GetIntegerValue("DisabledComponents") + if errors.Is(err, registry.ErrNotExist) { + return true, nil + } + + if err != nil { + return false, err + } + + if valtype != registry.DWORD { + return false, nil + } + + switch val { + case 0x00: + return true, nil + case 0x20: + return true, nil + default: + return false, nil + } +}