feat(network): enable ipv6 connection and tunneling (#1114)

Co-authored-by: Quentin McGaw <quentin.mcgaw@gmail.com>
This commit is contained in:
EkilDeew
2022-09-14 02:18:10 +02:00
committed by GitHub
parent 6a5aa8eddb
commit 875690ab18
9 changed files with 88 additions and 27 deletions

View File

@@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"net"
"github.com/qdm12/gluetun/internal/netlink"
)
func IPIsPrivate(ip net.IP) bool {
@@ -15,7 +17,12 @@ var (
errInterfaceIPNotFound = errors.New("IP address not found for interface")
)
func (r *Routing) assignedIP(interfaceName string) (ip net.IP, err error) {
func ipMatchesFamily(ip net.IP, family int) bool {
return (family == netlink.FAMILY_V6 && ip.To4() == nil) ||
(family == netlink.FAMILY_V4 && ip.To4() != nil)
}
func (r *Routing) assignedIP(interfaceName string, family int) (ip net.IP, err error) {
iface, err := net.InterfaceByName(interfaceName)
if err != nil {
return nil, fmt.Errorf("network interface %s not found: %w", interfaceName, err)
@@ -27,9 +34,13 @@ func (r *Routing) assignedIP(interfaceName string) (ip net.IP, err error) {
for _, address := range addresses {
switch value := address.(type) {
case *net.IPAddr:
return value.IP, nil
if ipMatchesFamily(value.IP, family) {
return value.IP, nil
}
case *net.IPNet:
return value.IP, nil
if ipMatchesFamily(value.IP, family) {
return value.IP, nil
}
}
}
return nil, fmt.Errorf("%w: interface %s in %d addresses",