File tree 1 file changed +19
-2
lines changed
1 file changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -32,12 +32,29 @@ func getLocalIP() string {
32
32
if err != nil {
33
33
panic (err )
34
34
}
35
+ var ips []string
35
36
for _ , address := range addrs {
36
37
if ipnet , ok := address .(* net.IPNet ); ok && ! ipnet .IP .IsLoopback () {
37
38
if ipnet .IP .To4 () != nil {
38
- return ipnet .IP .String ()
39
+ ips = append ( ips , ipnet .IP .String () )
39
40
}
40
41
}
41
42
}
42
- panic ("get local ip failed" )
43
+ if len (ips ) == 0 {
44
+ panic ("get local ip failed" )
45
+ } else if len (ips ) == 1 {
46
+ return ips [0 ]
47
+ } else {
48
+ // Select the one connected to the network
49
+ // when there are multiple network interfaces
50
+
51
+ // Is there a better way?
52
+ c , err := net .Dial ("udp" , "8.8.8.8:80" )
53
+ if err != nil {
54
+ return ips [0 ]
55
+ }
56
+ defer c .Close ()
57
+ return c .LocalAddr ().(* net.UDPAddr ).IP .String ()
58
+ }
59
+
43
60
}
You can’t perform that action at this time.
0 commit comments