Fix ipv6/ipv4 name resolution

This commit is contained in:
9seconds
2018-07-27 10:04:10 +03:00
parent 0d0bdd9fde
commit a00408562f
2 changed files with 25 additions and 12 deletions
+17 -4
View File
@@ -1,6 +1,7 @@
package config
import (
"context"
"io/ioutil"
"net"
"net/http"
@@ -9,16 +10,28 @@ import (
"github.com/juju/errors"
)
const ifconfigAddress = "https://ifconfig.co/ip"
func getGlobalIPv4() (net.IP, error) {
return fetchIP("https://v4.ifconfig.co/ip")
return fetchIP("tcp4")
}
func getGlobalIPv6() (net.IP, error) {
return fetchIP("https://v6.ifconfig.co/ip")
return fetchIP("tcp6")
}
func fetchIP(url string) (net.IP, error) {
resp, err := http.Get(url)
func fetchIP(network string) (net.IP, error) {
dialer := &net.Dialer{DualStack: false}
client := &http.Client{
Jar: nil,
Transport: &http.Transport{
DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) {
return dialer.DialContext(ctx, network, addr)
},
},
}
resp, err := client.Get(ifconfigAddress)
if err != nil {
return nil, err
}