Correct usage of ifconfig.co

This commit is contained in:
9seconds
2021-03-11 06:27:39 +03:00
parent d6566e5dfb
commit 6b9b437a5a
2 changed files with 53 additions and 27 deletions
+47 -16
View File
@@ -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
}