Quellcode durchsuchen

Correct usage of ifconfig.co

tags/v2.0.0-rc1
9seconds vor 5 Jahren
Ursprung
Commit
6b9b437a5a
2 geänderte Dateien mit 52 neuen und 26 gelöschten Zeilen
  1. 46
    15
      cli/access.go
  2. 6
    11
      mtglib/network/network.go

+ 46
- 15
cli/access.go Datei anzeigen

12
 	"os"
12
 	"os"
13
 	"strconv"
13
 	"strconv"
14
 	"strings"
14
 	"strings"
15
+	"sync"
15
 )
16
 )
16
 
17
 
17
 type accessResponse struct {
18
 type accessResponse struct {
43
 		return fmt.Errorf("cannot init config: %w", err)
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
 	encoder := json.NewEncoder(os.Stdout)
88
 	encoder := json.NewEncoder(os.Stdout)
65
 
89
 
78
 		return c.network.DialContext(ctx, protocol, address)
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
 	if err != nil {
113
 	if err != nil {
83
 		return nil
114
 		return nil
84
 	}
115
 	}

+ 6
- 11
mtglib/network/network.go Datei anzeigen

124
 		dialFunc = n.DialContext
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
 func NewNetwork(dialer Dialer, userAgent, dohHostname string, idleTimeout time.Duration) (Network, error) {
130
 func NewNetwork(dialer Dialer, userAgent, dohHostname string, idleTimeout time.Duration) (Network, error) {
144
 		idleTimeout: idleTimeout,
144
 		idleTimeout: idleTimeout,
145
 		userAgent:   userAgent,
145
 		userAgent:   userAgent,
146
 		dns: doh.Resolver{
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
 	}, nil
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
 	return &http.Client{
155
 	return &http.Client{
161
-		Timeout: HTTPTimeout,
156
+		Timeout: timeout,
162
 		Transport: networkHTTPTransport{
157
 		Transport: networkHTTPTransport{
163
 			userAgent: userAgent,
158
 			userAgent: userAgent,
164
 			next: &http.Transport{
159
 			next: &http.Transport{

Laden…
Abbrechen
Speichern