From 6b9b437a5a98eb58e415eb10117b355579252979 Mon Sep 17 00:00:00 2001 From: 9seconds Date: Thu, 11 Mar 2021 06:22:01 +0300 Subject: [PATCH] Correct usage of ifconfig.co --- cli/access.go | 63 +++++++++++++++++++++++++++++---------- mtglib/network/network.go | 17 ++++------- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/cli/access.go b/cli/access.go index 4e13f89..43e13b9 100644 --- a/cli/access.go +++ b/cli/access.go @@ -12,6 +12,7 @@ import ( "os" "strconv" "strings" + "sync" ) type accessResponse struct { @@ -43,24 +44,47 @@ func (c *Access) Run(cli *CLI, version string) error { return fmt.Errorf("cannot init config: %w", err) } - ipv4 := c.conf.Network.PublicIP.IPv4.Value(nil) - ipv6 := c.conf.Network.PublicIP.IPv6.Value(nil) - - if ipv4 == nil { - ipv4 = c.getIP("tcp4") - } - - if ipv6 == nil { - ipv6 = c.getIP("tcp6") - } - - resp := accessResponse{ - IPv4: c.makeURLs(ipv4, cli), - IPv6: c.makeURLs(ipv6, cli), - } + wg := &sync.WaitGroup{} + resp := &accessResponse{} resp.Secret.Base64 = c.conf.Secret.Base64() resp.Secret.Hex = c.conf.Secret.Hex() + wg.Add(2) + + go func() { + defer wg.Done() + + ip := c.conf.Network.PublicIP.IPv4.Value(nil) + + if ip == nil { + ip = c.getIP("tcp4") + } + + if ip != nil { + ip = ip.To4() + } + + resp.IPv4 = c.makeURLs(ip, cli) + }() + + go func() { + defer wg.Done() + + ip := c.conf.Network.PublicIP.IPv4.Value(nil) + + if ip == nil { + ip = c.getIP("tcp6") + } + + if ip != nil { + ip = ip.To16() + } + + resp.IPv6 = c.makeURLs(ip, cli) + }() + + wg.Wait() + encoder := json.NewEncoder(os.Stdout) encoder.SetEscapeHTML(false) @@ -78,7 +102,14 @@ func (c *Access) getIP(protocol string) net.IP { return c.network.DialContext(ctx, protocol, address) }) - resp, err := client.Get("https://ifconfig.co") // nolint: noctx + req, err := http.NewRequest(http.MethodGet, "https://ifconfig.co", nil) // nolint: noctx + if err != nil { + panic(err) + } + + req.Header.Add("Accept", "text/plain") + + resp, err := client.Do(req) if err != nil { return nil } diff --git a/mtglib/network/network.go b/mtglib/network/network.go index 853511b..5e29be7 100644 --- a/mtglib/network/network.go +++ b/mtglib/network/network.go @@ -124,7 +124,7 @@ func (n *network) MakeHTTPClient(dialFunc DialFunc) *http.Client { dialFunc = n.DialContext } - return makeHTTPClient(n.userAgent, dialFunc) + return makeHTTPClient(n.userAgent, HTTPTimeout, dialFunc) } func NewNetwork(dialer Dialer, userAgent, dohHostname string, idleTimeout time.Duration) (Network, error) { @@ -144,21 +144,16 @@ func NewNetwork(dialer Dialer, userAgent, dohHostname string, idleTimeout time.D idleTimeout: idleTimeout, userAgent: userAgent, dns: doh.Resolver{ - Host: dohHostname, - Class: doh.IN, - HTTPClient: &http.Client{ - Timeout: DNSTimeout, - Transport: &http.Transport{ - DialContext: dialer.DialContext, - }, - }, + Host: dohHostname, + Class: doh.IN, + HTTPClient: makeHTTPClient(userAgent, DNSTimeout, dialer.DialContext), }, }, nil } -func makeHTTPClient(userAgent string, dialFunc DialFunc) *http.Client { +func makeHTTPClient(userAgent string, timeout time.Duration, dialFunc DialFunc) *http.Client { return &http.Client{ - Timeout: HTTPTimeout, + Timeout: timeout, Transport: networkHTTPTransport{ userAgent: userAgent, next: &http.Transport{