mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 23:44:01 +03:00
FILE / ScuroNeko/mtg
config/global_ips.go
Исходный файл и его история в репозитории.
``` // 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 ```
53 lines
1002 B
Go
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
|
|
}
|