Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve GetOutboundIP to work without internet access #1707

Merged
merged 2 commits into from
Mar 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 63 additions & 9 deletions pkg/common/outbound_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,74 @@ package common

import (
"net"

log "github.com/sirupsen/logrus"
"sort"
"strings"
)

// https://stackoverflow.com/a/37382208
// Get preferred outbound ip of this machine
// GetOutboundIP returns an outbound IP address of this machine.
// It tries to access the internet and returns the local IP address of the connection.
// If the machine cannot access the internet, it returns a preferred IP address from network interfaces.
// It returns nil if no IP address is found.
func GetOutboundIP() net.IP {
// See https://stackoverflow.com/a/37382208
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
if err == nil {
defer conn.Close()
return conn.LocalAddr().(*net.UDPAddr).IP
}
defer conn.Close()

localAddr := conn.LocalAddr().(*net.UDPAddr)
// So the machine cannot access the internet. Pick an IP address from network interfaces.
if ifs, err := net.Interfaces(); err == nil {
type IP struct {
net.IP
net.Interface
}
var ips []IP
for _, i := range ifs {
if addrs, err := i.Addrs(); err == nil {
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip.IsGlobalUnicast() {
ips = append(ips, IP{ip, i})
}
}
}
}
if len(ips) > 1 {
sort.Slice(ips, func(i, j int) bool {
ifi := ips[i].Interface
ifj := ips[j].Interface

// ethernet is preferred
if vi, vj := strings.HasPrefix(ifi.Name, "e"), strings.HasPrefix(ifj.Name, "e"); vi != vj {
return vi
}

ipi := ips[i].IP
ipj := ips[j].IP

// IPv4 is preferred
if vi, vj := ipi.To4() != nil, ipj.To4() != nil; vi != vj {
return vi
}

// en0 is preferred to en1
if ifi.Name != ifj.Name {
return ifi.Name < ifj.Name
}

// fallback
return ipi.String() < ipj.String()
})
return ips[0].IP
}
}

return localAddr.IP
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the caller will still crash the cli prog if you return nil here. The caller does an unconditional .String() in the help text..

The cli could use 127.0.0.1 or [::1] as bind address in this case, but may better fit on the caller side.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safe to call (net.IP)(nil).String().

And if the caller really don't want a nil, it can check the return.

}