feat: not set ipv6 if ipv6 is not enable on linux (#248)

* feat: not set ipv6 if ipv6 is not enable on linux
This commit is contained in:
naison
2024-05-14 14:17:34 +08:00
committed by GitHub
parent e5c1ea4b9b
commit 331423c308
4 changed files with 92 additions and 59 deletions

View File

@@ -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{

View File

@@ -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<br>Hexadecimal 0x20<br>Binary xx1x xxxx<br>Recommended instead of disabling IPv6. |
| Disable IPv6 | Decimal 255<br>Hexadecimal 0xFF<br>Binary 1111 1111<br> |
| Disable IPv6 on all nontunnel interfaces | Decimal 16<br>Hexadecimal 0x10<br>Binary xxx1 xxxx |
| Disable IPv6 on all tunnel interfaces | Decimal 1<br>Hexadecimal 0x01<br>Binary xxxx xxx1 |
| Disable IPv6 on all nontunnel interfaces (except the loopback) and on IPv6 tunnel interface | Decimal 17<br>Hexadecimal 0x11<br>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
}
}

25
pkg/util/net_others.go Normal file
View File

@@ -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
}

62
pkg/util/net_windows.go Normal file
View File

@@ -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<br>Hexadecimal 0x20<br>Binary xx1x xxxx<br>Recommended instead of disabling IPv6. |
| Disable IPv6 | Decimal 255<br>Hexadecimal 0xFF<br>Binary 1111 1111<br> |
| Disable IPv6 on all nontunnel interfaces | Decimal 16<br>Hexadecimal 0x10<br>Binary xxx1 xxxx |
| Disable IPv6 on all tunnel interfaces | Decimal 1<br>Hexadecimal 0x01<br>Binary xxxx xxx1 |
| Disable IPv6 on all nontunnel interfaces (except the loopback) and on IPv6 tunnel interface | Decimal 17<br>Hexadecimal 0x11<br>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
}
}