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 #3

Closed jc21 closed 3 years ago

jc21 commented 4 years ago

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.

mvdan commented 3 years ago

Friendly ping @GlenDC, do you think you'll have time to review and merge this soon?

GlenDC commented 3 years ago

Friendly ping @GlenDC, do you think you'll have time to review and merge this soon?

It's been a long time since I worked on this project. I'm going to reserve some time for it this week to get back into it, see where it was left, review and merge this MR.

altergui commented 3 years ago

thanks in advance @GlenDC <3 FWIW, i've tested this code as it currently is, on both a dualstack host and an ipv4-only host, and it just works as expected.

GlenDC commented 3 years ago

All looks good to me. There is one open comment as soon as we find consensus on how to resolve it I'll merge this MR and make a first sem-versionned release. I've already turned this package into a Go module on the master branch.

This patch will be part of v0.2.0, to be released as soon as this MR is merged and some testing has been done.

GlenDC commented 3 years ago

@jc21 could you also apply this patch to your MR to add your protocol support to the exip binary:

diff --git a/cmd/exip/main.go b/cmd/exip/main.go
index c010340..2f755c2 100644
--- a/cmd/exip/main.go
+++ b/cmd/exip/main.go
@@ -12,8 +12,9 @@ import (

 // CLI Flags
 var (
-   timeout = flag.Duration("t", time.Second*5, "consensus's voting timeout")
-   verbose = flag.Bool("v", false, "log errors to STDERR, when defined")
+   timeout  = flag.Duration("t", time.Second*5, "consensus's voting timeout")
+   verbose  = flag.Bool("v", false, "log errors to STDERR, when defined")
+   protocol = flag.Uint("p", 0, "IP Protocol to be used (0, 4, or 6)")
 )

 func main() {
@@ -32,6 +33,7 @@ func main() {

    // create the consensus
    consensus := externalip.DefaultConsensus(cfg, logger)
+   consensus.UseIPProtocol(*protocol)

    // retrieve the external ip
    ip, err := consensus.ExternalIP()

If you cannot do it, do not worry, I can also do it once this MR is merged.

jc21 commented 3 years ago

Sorry guys I totally destroyed my master branch. Will attempt to get it back and open another pr.