summaryrefslogtreecommitdiffstats
path: root/pkg/common/outbound_ip.go
blob: 66e15e5d774fef7d3d3fbfcc4b3ce1a5a273642b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package common

import (
	"net"
	"sort"
	"strings"
)

// 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 {
		defer conn.Close()
		return conn.LocalAddr().(*net.UDPAddr).IP
	}

	// 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 nil
}