瀏覽代碼

Correct usage of ifconfig.co

tags/v2.0.0-rc1
9seconds 5 年之前
父節點
當前提交
6b9b437a5a
共有 2 個檔案被更改,包括 52 行新增26 行删除
  1. 46
    15
      cli/access.go
  2. 6
    11
      mtglib/network/network.go

+ 46
- 15
cli/access.go 查看文件

@@ -12,6 +12,7 @@ import (
12 12
 	"os"
13 13
 	"strconv"
14 14
 	"strings"
15
+	"sync"
15 16
 )
16 17
 
17 18
 type accessResponse struct {
@@ -43,23 +44,46 @@ func (c *Access) Run(cli *CLI, version string) error {
43 44
 		return fmt.Errorf("cannot init config: %w", err)
44 45
 	}
45 46
 
46
-	ipv4 := c.conf.Network.PublicIP.IPv4.Value(nil)
47
-	ipv6 := c.conf.Network.PublicIP.IPv6.Value(nil)
47
+	wg := &sync.WaitGroup{}
48
+	resp := &accessResponse{}
49
+	resp.Secret.Base64 = c.conf.Secret.Base64()
50
+	resp.Secret.Hex = c.conf.Secret.Hex()
48 51
 
49
-	if ipv4 == nil {
50
-		ipv4 = c.getIP("tcp4")
51
-	}
52
+	wg.Add(2)
52 53
 
53
-	if ipv6 == nil {
54
-		ipv6 = c.getIP("tcp6")
55
-	}
54
+	go func() {
55
+		defer wg.Done()
56 56
 
57
-	resp := accessResponse{
58
-		IPv4: c.makeURLs(ipv4, cli),
59
-		IPv6: c.makeURLs(ipv6, cli),
60
-	}
61
-	resp.Secret.Base64 = c.conf.Secret.Base64()
62
-	resp.Secret.Hex = c.conf.Secret.Hex()
57
+		ip := c.conf.Network.PublicIP.IPv4.Value(nil)
58
+
59
+		if ip == nil {
60
+			ip = c.getIP("tcp4")
61
+		}
62
+
63
+		if ip != nil {
64
+			ip = ip.To4()
65
+		}
66
+
67
+		resp.IPv4 = c.makeURLs(ip, cli)
68
+	}()
69
+
70
+	go func() {
71
+		defer wg.Done()
72
+
73
+		ip := c.conf.Network.PublicIP.IPv4.Value(nil)
74
+
75
+		if ip == nil {
76
+			ip = c.getIP("tcp6")
77
+		}
78
+
79
+		if ip != nil {
80
+			ip = ip.To16()
81
+		}
82
+
83
+		resp.IPv6 = c.makeURLs(ip, cli)
84
+	}()
85
+
86
+	wg.Wait()
63 87
 
64 88
 	encoder := json.NewEncoder(os.Stdout)
65 89
 
@@ -78,7 +102,14 @@ func (c *Access) getIP(protocol string) net.IP {
78 102
 		return c.network.DialContext(ctx, protocol, address)
79 103
 	})
80 104
 
81
-	resp, err := client.Get("https://ifconfig.co") // nolint: noctx
105
+	req, err := http.NewRequest(http.MethodGet, "https://ifconfig.co", nil) // nolint: noctx
106
+	if err != nil {
107
+		panic(err)
108
+	}
109
+
110
+	req.Header.Add("Accept", "text/plain")
111
+
112
+	resp, err := client.Do(req)
82 113
 	if err != nil {
83 114
 		return nil
84 115
 	}

+ 6
- 11
mtglib/network/network.go 查看文件

@@ -124,7 +124,7 @@ func (n *network) MakeHTTPClient(dialFunc DialFunc) *http.Client {
124 124
 		dialFunc = n.DialContext
125 125
 	}
126 126
 
127
-	return makeHTTPClient(n.userAgent, dialFunc)
127
+	return makeHTTPClient(n.userAgent, HTTPTimeout, dialFunc)
128 128
 }
129 129
 
130 130
 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
144 144
 		idleTimeout: idleTimeout,
145 145
 		userAgent:   userAgent,
146 146
 		dns: doh.Resolver{
147
-			Host:  dohHostname,
148
-			Class: doh.IN,
149
-			HTTPClient: &http.Client{
150
-				Timeout: DNSTimeout,
151
-				Transport: &http.Transport{
152
-					DialContext: dialer.DialContext,
153
-				},
154
-			},
147
+			Host:       dohHostname,
148
+			Class:      doh.IN,
149
+			HTTPClient: makeHTTPClient(userAgent, DNSTimeout, dialer.DialContext),
155 150
 		},
156 151
 	}, nil
157 152
 }
158 153
 
159
-func makeHTTPClient(userAgent string, dialFunc DialFunc) *http.Client {
154
+func makeHTTPClient(userAgent string, timeout time.Duration, dialFunc DialFunc) *http.Client {
160 155
 	return &http.Client{
161
-		Timeout: HTTPTimeout,
156
+		Timeout: timeout,
162 157
 		Transport: networkHTTPTransport{
163 158
 			userAgent: userAgent,
164 159
 			next: &http.Transport{

Loading…
取消
儲存