mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 11:34:01 +03:00
Correct usage of ifconfig.co
This commit is contained in:
+47
-16
@@ -12,6 +12,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type accessResponse struct {
|
type accessResponse struct {
|
||||||
@@ -43,24 +44,47 @@ func (c *Access) Run(cli *CLI, version string) error {
|
|||||||
return fmt.Errorf("cannot init config: %w", err)
|
return fmt.Errorf("cannot init config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ipv4 := c.conf.Network.PublicIP.IPv4.Value(nil)
|
wg := &sync.WaitGroup{}
|
||||||
ipv6 := c.conf.Network.PublicIP.IPv6.Value(nil)
|
resp := &accessResponse{}
|
||||||
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
resp.Secret.Base64 = c.conf.Secret.Base64()
|
resp.Secret.Base64 = c.conf.Secret.Base64()
|
||||||
resp.Secret.Hex = c.conf.Secret.Hex()
|
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 := json.NewEncoder(os.Stdout)
|
||||||
|
|
||||||
encoder.SetEscapeHTML(false)
|
encoder.SetEscapeHTML(false)
|
||||||
@@ -78,7 +102,14 @@ func (c *Access) getIP(protocol string) net.IP {
|
|||||||
return c.network.DialContext(ctx, protocol, address)
|
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 {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ func (n *network) MakeHTTPClient(dialFunc DialFunc) *http.Client {
|
|||||||
dialFunc = n.DialContext
|
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) {
|
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,
|
idleTimeout: idleTimeout,
|
||||||
userAgent: userAgent,
|
userAgent: userAgent,
|
||||||
dns: doh.Resolver{
|
dns: doh.Resolver{
|
||||||
Host: dohHostname,
|
Host: dohHostname,
|
||||||
Class: doh.IN,
|
Class: doh.IN,
|
||||||
HTTPClient: &http.Client{
|
HTTPClient: makeHTTPClient(userAgent, DNSTimeout, dialer.DialContext),
|
||||||
Timeout: DNSTimeout,
|
|
||||||
Transport: &http.Transport{
|
|
||||||
DialContext: dialer.DialContext,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeHTTPClient(userAgent string, dialFunc DialFunc) *http.Client {
|
func makeHTTPClient(userAgent string, timeout time.Duration, dialFunc DialFunc) *http.Client {
|
||||||
return &http.Client{
|
return &http.Client{
|
||||||
Timeout: HTTPTimeout,
|
Timeout: timeout,
|
||||||
Transport: networkHTTPTransport{
|
Transport: networkHTTPTransport{
|
||||||
userAgent: userAgent,
|
userAgent: userAgent,
|
||||||
next: &http.Transport{
|
next: &http.Transport{
|
||||||
|
|||||||
Reference in New Issue
Block a user