GlenDC / go-external-ip

a Golang library to get your external ip from multiple services
MIT License
71 stars 25 forks source link

Add option to determine ipv4 or ipv6 address explicitly #6

Closed jc21 closed 3 years ago

jc21 commented 3 years ago

New version of MR #3

Fixes #2 - By rejecting connections using the TCP method we don't want for the IP protocol, the HTTP client will move on to the next connection. This eventually gets you the connection you're after. The sources will see the request from this package as either a ipv4 or ipv6 connection and return the corresponding IP protocol address.

Example usage:

consensus := externalip.DefaultConsensus(nil, nil)
connsensus.UseIPProtocol(4)
// or
connsensus.UseIPProtocol(6)
ip, err := consensus.ExternalIP()

Tested on a server that has both ipv4 and ipv6 addresses on the interface. The default will return me an ipv6 address however specifying an IP protocol explicitly will return what I wanted.

Also includes Tejas patch since I needed to add a transport anyway and this helps resolve a leak too.