FILE / ScuroNeko/mtg

config/global_ips.go

Исходный файл и его история в репозитории.
FILE 33852ca4818c365778edccb7441a11decff90009
Files
mtg/config/global_ips.go
T
Evgeniy Kulikov 70be70de3c DualStack is deprecated
```
	// DualStack previously enabled RFC 6555 Fast Fallback
	// support, also known as "Happy Eyeballs", in which IPv4 is
	// tried soon if IPv6 appears to be misconfigured and
	// hanging.
	//
	// Deprecated: Fast Fallback is enabled by default. To
	// disable, set FallbackDelay to a negative value.
	DualStack bool
```
2019-02-26 21:44:47 +03:00

53 lines
1002 B
Go

package config
import (
"context"
"io/ioutil"
"net"
"net/http"
"strings"
"github.com/juju/errors"
)
const ifconfigAddress = "https://ifconfig.co/ip"
func getGlobalIPv4() (net.IP, error) {
return fetchIP("tcp4")
}
func getGlobalIPv6() (net.IP, error) {
return fetchIP("tcp6")
}
func fetchIP(network string) (net.IP, error) {
dialer := &net.Dialer{FallbackDelay: -1}
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
}
defer resp.Body.Close() // nolint: errcheck
respDataBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
respData := strings.TrimSpace(string(respDataBytes))
ip := net.ParseIP(respData)
if ip == nil {
return nil, errors.Errorf("ifconfig.co returns incorrect IP %s", respData)
}
return ip, nil
}