/
extaddr.go
36 lines (31 loc) · 922 Bytes
/
extaddr.go
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
package portmap
import "net"
import "github.com/hlandau/portmap/gateway"
import "github.com/hlandau/portmap/natpmp"
// Attempt to obtain the external IP address from the default gateway.
//
// If the host has a globally routable IP, returns that IP.
//
// Currently this only tries NAT-PMP and does not attempt to learn the external
// IP address via UPnP.
//
// This function is not very useful because the IP address returned may still
// be an RFC1918 address, due to the possibility of a double NAT setup. There
// are better solutions for obtaining one's public IP address, such as STUN.
func ExternalAddr() (net.IP, error) {
if gr, ip := isGloballyRoutable(); gr {
return ip, nil
}
gwa, err := gateway.GetIPs()
if err != nil {
return nil, err
}
var extaddr net.IP
for _, gw := range gwa {
extaddr, err = natpmp.GetExternalAddr(gw)
if err == nil {
return extaddr, nil
}
}
return nil, err
}