Skip to content

Commit

Permalink
fix: Improve automatic IP detection
Browse files Browse the repository at this point in the history
  • Loading branch information
heyvito committed Dec 9, 2023
1 parent 5a85505 commit bfff210
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 71 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21.1

require (
github.com/cloudflare/circl v1.3.6
github.com/heyvito/gateway v0.1.2
github.com/heyvito/defip v0.1.1
github.com/influxdata/tdigest v0.0.1
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/heyvito/gateway v0.1.2 h1:4enCoosRHawlJlrUG7f5O0/f7yYz0TYQQJOBA1SlWFU=
github.com/heyvito/gateway v0.1.2/go.mod h1:g7FIBBk+dKr9dsm+VXJM/4PUon0RdVc99sjePjKuFFw=
github.com/heyvito/defip v0.1.1 h1:yQrlTPZp2K0b7MfWUHs5WHPBlct85jb1n8226jMA7w8=
github.com/heyvito/defip v0.1.1/go.mod h1:rWEvk113oZKvLu/vYfxtQnvEF3PRYhzt2xOt4lzVM/s=
github.com/influxdata/tdigest v0.0.1 h1:XpFptwYmnEKUqmkcDjrzffswZ3nvNeevbUSLPP/ZzIY=
github.com/influxdata/tdigest v0.0.1/go.mod h1:Z0kXnxzbTC2qrx4NaIzYkE1k66+6oEDQTvL95hQFh5Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
28 changes: 0 additions & 28 deletions internal/iputil/ip_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package iputil

import (
"fmt"
"github.com/heyvito/gateway"
"net"
"net/netip"
"slices"
Expand Down Expand Up @@ -39,33 +38,6 @@ func LocalAddresses() (IPList, error) {
return addrIPs, nil
}

func GetDefaultIP(preferIPv6 bool) (addr netip.Addr, err error) {
var ips []netip.Addr
ips, err = gateway.FindDefaultIPs()
if err != nil {
return
}

if preferIPv6 {
for _, v := range ips {
if v.Is6() {
return v, nil
}
}
} else {
for _, v := range ips {
return v, nil
}
}

for _, v := range ips {
return v, nil
}

err = NoAddressErr
return
}

func ConvertNetIP(in net.IP) netip.Addr {
if ip4 := in.To4(); ip4 != nil {
return netip.AddrFrom4([4]byte(ip4[:4]))
Expand Down
61 changes: 21 additions & 40 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package eswim

import (
"fmt"
"github.com/heyvito/gateway"
"github.com/heyvito/defip"
"go.uber.org/zap"
"math"
"net/netip"
Expand Down Expand Up @@ -123,6 +123,12 @@ type Options struct {
}

func (o *Options) normalize() error {
if o.LogHandler == nil {
o.LogHandler = zap.NewNop()
}

logger := o.LogHandler.With(zap.String("facility", "options"))

if !o.InsecureDisableCrypto && len(o.CryptoKey) != 16 {
return fmt.Errorf("CryptoKey must have 16 bytes")
}
Expand Down Expand Up @@ -157,52 +163,27 @@ func (o *Options) normalize() error {
}
}

if o.LogHandler == nil {
o.LogHandler = zap.NewNop()
}

if o.HostAddresses.IPv4Address == nil || o.HostAddresses.IPv6Address == nil {
allIPs, err := gateway.FindDefaultIPs()
if err != nil {
return fmt.Errorf("failed detecting IP addresses: %w", err)
}

if o.HostAddresses.IPv4Address == nil {
o.HostAddresses.IPv4Address = filterIP(allIPs, netip.Addr.Is4)
if ip, err := defip.FindDefaultIP(defip.NetRouteKindV4); err == nil {
o.HostAddresses.IPv4Address = ip
logger.Debug("Automatically selected IPv4", zap.String("addr", ip.String()))
} else {
logger.Warn("IPv4 has not been explicitly disabled, but no usable IPv4 could be found. Disabling...")
o.HostAddresses.IPv4Address = IgnoreFamilyAddr
}
}

if o.HostAddresses.IPv6Address == nil {
o.HostAddresses.IPv6Address = filterIP(allIPs, netip.Addr.Is6)
if ip, err := defip.FindDefaultIP(defip.NetRouteKindV6); err == nil {
o.HostAddresses.IPv6Address = ip
logger.Debug("Automatically selected IPv6", zap.String("addr", ip.String()))
} else {
logger.Warn("IPv6 has not been explicitly disabled, but no usable IPv6 could be found. Disabling...")
o.HostAddresses.IPv6Address = IgnoreFamilyAddr
}
}
}

return nil
}

func filterIP(list []netip.Addr, filter func(netip.Addr) bool) *netip.Addr {
var anyLocal, anyNonLocal netip.Addr

for _, ip := range list {
isLinkLocal := ip.IsLinkLocalUnicast() || ip.IsLinkLocalUnicast()
if !filter(ip) {
continue
}

if isLinkLocal && !anyLocal.IsValid() {
anyLocal = ip
} else if !anyNonLocal.IsValid() {
anyNonLocal = ip
}

if anyLocal.IsValid() && anyNonLocal.IsValid() {
break
}
}

if anyLocal.IsValid() {
return &anyLocal
} else if anyNonLocal.IsValid() {
return &anyNonLocal
}
return nil
}

0 comments on commit bfff210

Please sign in to comment.