浏览代码

Refactoring

tags/v2.2.4^2
9seconds 1 个月前
父节点
当前提交
b6b900e430
共有 4 个文件被更改,包括 88 次插入89 次删除
  1. 2
    47
      internal/cli/access.go
  2. 35
    41
      internal/cli/doctor.go
  3. 51
    0
      internal/cli/utils.go
  4. 0
    1
      run_profile.go

+ 2
- 47
internal/cli/access.go 查看文件

1
 package cli
1
 package cli
2
 
2
 
3
 import (
3
 import (
4
-	"context"
5
 	"encoding/json"
4
 	"encoding/json"
6
 	"fmt"
5
 	"fmt"
7
-	"io"
8
 	"net"
6
 	"net"
9
-	"net/http"
10
 	"net/url"
7
 	"net/url"
11
 	"os"
8
 	"os"
12
 	"strconv"
9
 	"strconv"
13
-	"strings"
14
 	"sync"
10
 	"sync"
15
 
11
 
16
-	"github.com/9seconds/mtg/v2/essentials"
17
 	"github.com/9seconds/mtg/v2/internal/config"
12
 	"github.com/9seconds/mtg/v2/internal/config"
18
 	"github.com/9seconds/mtg/v2/internal/utils"
13
 	"github.com/9seconds/mtg/v2/internal/utils"
19
-	"github.com/9seconds/mtg/v2/mtglib"
20
 )
14
 )
21
 
15
 
22
 type accessResponse struct {
16
 type accessResponse struct {
65
 	wg.Go(func() {
59
 	wg.Go(func() {
66
 		ip := a.PublicIPv4
60
 		ip := a.PublicIPv4
67
 		if ip == nil {
61
 		if ip == nil {
68
-			ip = a.getIP(ntw, "tcp4")
62
+			ip = getIP(ntw, "tcp4")
69
 		}
63
 		}
70
 
64
 
71
 		if ip != nil {
65
 		if ip != nil {
77
 	wg.Go(func() {
71
 	wg.Go(func() {
78
 		ip := a.PublicIPv6
72
 		ip := a.PublicIPv6
79
 		if ip == nil {
73
 		if ip == nil {
80
-			ip = a.getIP(ntw, "tcp6")
74
+			ip = getIP(ntw, "tcp6")
81
 		}
75
 		}
82
 
76
 
83
 		if ip != nil {
77
 		if ip != nil {
100
 	return nil
94
 	return nil
101
 }
95
 }
102
 
96
 
103
-func (a *Access) getIP(ntw mtglib.Network, protocol string) net.IP {
104
-	dialer := ntw.NativeDialer()
105
-	client := ntw.MakeHTTPClient(func(ctx context.Context, network, address string) (essentials.Conn, error) {
106
-		conn, err := dialer.DialContext(ctx, protocol, address)
107
-		if err != nil {
108
-			return nil, err
109
-		}
110
-		return essentials.WrapNetConn(conn), err
111
-	})
112
-
113
-	req, err := http.NewRequest(http.MethodGet, "https://ifconfig.co", nil) //nolint: noctx
114
-	if err != nil {
115
-		panic(err)
116
-	}
117
-
118
-	req.Header.Add("Accept", "text/plain")
119
-
120
-	resp, err := client.Do(req)
121
-	if err != nil {
122
-		return nil
123
-	}
124
-
125
-	if resp.StatusCode != http.StatusOK {
126
-		return nil
127
-	}
128
-
129
-	defer func() {
130
-		io.Copy(io.Discard, resp.Body) //nolint: errcheck
131
-		resp.Body.Close()              //nolint: errcheck
132
-	}()
133
-
134
-	data, err := io.ReadAll(resp.Body)
135
-	if err != nil {
136
-		return nil
137
-	}
138
-
139
-	return net.ParseIP(strings.TrimSpace(string(data)))
140
-}
141
-
142
 func (a *Access) makeURLs(conf *config.Config, ip net.IP) *accessResponseURLs {
97
 func (a *Access) makeURLs(conf *config.Config, ip net.IP) *accessResponseURLs {
143
 	if ip == nil {
98
 	if ip == nil {
144
 		return nil
99
 		return nil

+ 35
- 41
internal/cli/doctor.go 查看文件

2
 
2
 
3
 import (
3
 import (
4
 	"context"
4
 	"context"
5
+	"errors"
5
 	"fmt"
6
 	"fmt"
6
 	"maps"
7
 	"maps"
7
 	"net"
8
 	"net"
63
 	ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"` //nolint: lll
64
 	ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"` //nolint: lll
64
 }
65
 }
65
 
66
 
66
-type wrappedNetwork struct {
67
-	mtglib.Network
68
-}
69
-
70
-func (w wrappedNetwork) Dial(network, address string) (net.Conn, error) {
71
-	rv, err := w.Network.Dial(network, address)
72
-	if err != nil {
73
-		return nil, err
74
-	}
75
-
76
-	return rv.(net.Conn), nil
77
-}
78
-
79
-func (w wrappedNetwork) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
80
-	rv, err := w.Network.DialContext(ctx, network, address)
81
-	if err != nil {
82
-		return nil, err
83
-	}
84
-
85
-	return rv.(net.Conn), nil
86
-}
87
-
88
 func (d *Doctor) Run(cli *CLI, version string) error {
67
 func (d *Doctor) Run(cli *CLI, version string) error {
89
 	conf, err := utils.ReadConfig(d.ConfigPath)
68
 	conf, err := utils.ReadConfig(d.ConfigPath)
90
 	if err != nil {
69
 	if err != nil {
140
 
119
 
141
 	if d.conf.DomainFrontingIP.Value != nil {
120
 	if d.conf.DomainFrontingIP.Value != nil {
142
 		ok = false
121
 		ok = false
143
-		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{
122
+		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
144
 			"when":        "2.3.0",
123
 			"when":        "2.3.0",
145
 			"old":         "domain-fronting-ip",
124
 			"old":         "domain-fronting-ip",
146
 			"old_section": "",
125
 			"old_section": "",
151
 
130
 
152
 	if d.conf.DomainFrontingPort.Value != 0 {
131
 	if d.conf.DomainFrontingPort.Value != 0 {
153
 		ok = false
132
 		ok = false
154
-		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{
133
+		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
155
 			"when":        "2.3.0",
134
 			"when":        "2.3.0",
156
 			"old":         "domain-fronting-port",
135
 			"old":         "domain-fronting-port",
157
 			"old_section": "",
136
 			"old_section": "",
162
 
141
 
163
 	if d.conf.DomainFrontingProxyProtocol.Value {
142
 	if d.conf.DomainFrontingProxyProtocol.Value {
164
 		ok = false
143
 		ok = false
165
-		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{
144
+		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
166
 			"when":        "2.3.0",
145
 			"when":        "2.3.0",
167
 			"old":         "domain-fronting-proxy-protocol",
146
 			"old":         "domain-fronting-proxy-protocol",
168
 			"old_section": "",
147
 			"old_section": "",
173
 
152
 
174
 	if d.conf.Network.DOHIP.Value != nil {
153
 	if d.conf.Network.DOHIP.Value != nil {
175
 		ok = false
154
 		ok = false
176
-		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{
155
+		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
177
 			"when":        "2.3.0",
156
 			"when":        "2.3.0",
178
 			"old":         "doh-ip",
157
 			"old":         "doh-ip",
179
 			"old_section": "network",
158
 			"old_section": "network",
192
 func (d *Doctor) checkTimeSkewness() bool {
171
 func (d *Doctor) checkTimeSkewness() bool {
193
 	response, err := ntp.Query("0.pool.ntp.org")
172
 	response, err := ntp.Query("0.pool.ntp.org")
194
 	if err != nil {
173
 	if err != nil {
195
-		tplError.Execute(os.Stdout, map[string]any{
174
+		tplError.Execute(os.Stdout, map[string]any{ //nolint: errcheck
196
 			"description": "cannot access ntp pool",
175
 			"description": "cannot access ntp pool",
197
 			"error":       err,
176
 			"error":       err,
198
 		})
177
 		})
202
 	skewness := response.ClockOffset.Abs()
181
 	skewness := response.ClockOffset.Abs()
203
 	confValue := d.conf.TolerateTimeSkewness.Get(mtglib.DefaultTolerateTimeSkewness)
182
 	confValue := d.conf.TolerateTimeSkewness.Get(mtglib.DefaultTolerateTimeSkewness)
204
 	diff := float64(skewness) / float64(confValue)
183
 	diff := float64(skewness) / float64(confValue)
205
-	context := map[string]any{
184
+	tplData := map[string]any{
206
 		"drift": response.ClockOffset,
185
 		"drift": response.ClockOffset,
207
 		"value": confValue,
186
 		"value": confValue,
208
 	}
187
 	}
209
 
188
 
210
 	switch {
189
 	switch {
211
 	case diff < 0.3:
190
 	case diff < 0.3:
212
-		tplOTimeSkewness.Execute(os.Stdout, context)
191
+		tplOTimeSkewness.Execute(os.Stdout, tplData) //nolint: errcheck
213
 		return true
192
 		return true
214
 	case diff < 0.7:
193
 	case diff < 0.7:
215
-		tplWTimeSkewness.Execute(os.Stdout, context)
194
+		tplWTimeSkewness.Execute(os.Stdout, tplData) //nolint: errcheck
216
 	default:
195
 	default:
217
-		tplETimeSkewness.Execute(os.Stdout, context)
196
+		tplETimeSkewness.Execute(os.Stdout, tplData) //nolint: errcheck
218
 	}
197
 	}
219
 
198
 
220
 	return false
199
 	return false
229
 	for _, dc := range dcs {
208
 	for _, dc := range dcs {
230
 		err := d.checkNetworkAddresses(ntw, essentials.TelegramCoreAddresses[dc])
209
 		err := d.checkNetworkAddresses(ntw, essentials.TelegramCoreAddresses[dc])
231
 		if err == nil {
210
 		if err == nil {
232
-			tplODCConnect.Execute(os.Stdout, map[string]any{
211
+			tplODCConnect.Execute(os.Stdout, map[string]any{ //nolint: errcheck
233
 				"dc": dc,
212
 				"dc": dc,
234
 			})
213
 			})
235
 		} else {
214
 		} else {
236
-			tplEDCConnect.Execute(os.Stdout, map[string]any{
215
+			tplEDCConnect.Execute(os.Stdout, map[string]any{ //nolint: errcheck
237
 				"dc":    dc,
216
 				"dc":    dc,
238
 				"error": err,
217
 				"error": err,
239
 			})
218
 			})
274
 		checkAddresses = addresses
253
 		checkAddresses = addresses
275
 	}
254
 	}
276
 
255
 
256
+	if len(checkAddresses) == 0 {
257
+		return fmt.Errorf("no suitable addresses after IP version filtering")
258
+	}
259
+
277
 	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
260
 	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
278
 	defer cancel()
261
 	defer cancel()
279
 
262
 
288
 			continue
271
 			continue
289
 		}
272
 		}
290
 
273
 
291
-		conn.Close()
274
+		conn.Close() //nolint: errcheck
292
 
275
 
293
 		return nil
276
 		return nil
294
 	}
277
 	}
299
 func (d *Doctor) checkSecretHost(resolver *net.Resolver, ntw mtglib.Network) bool {
282
 func (d *Doctor) checkSecretHost(resolver *net.Resolver, ntw mtglib.Network) bool {
300
 	addresses, err := resolver.LookupIPAddr(context.Background(), d.conf.Secret.Host)
283
 	addresses, err := resolver.LookupIPAddr(context.Background(), d.conf.Secret.Host)
301
 	if err != nil {
284
 	if err != nil {
302
-		// TODO
285
+		tplError.Execute(os.Stdout, map[string]any{ //nolint: errcheck
286
+			"description": fmt.Sprintf("cannot resolve DNS name of %s", d.conf.Secret.Host),
287
+			"error":       err,
288
+		})
303
 		return false
289
 		return false
304
 	}
290
 	}
305
 
291
 
306
-	access := &Access{}
307
-	ourIP4 := access.getIP(ntw, "tcp4")
308
-	ourIP6 := access.getIP(ntw, "tcp6")
292
+	ourIP4 := getIP(ntw, "tcp4")
293
+	ourIP6 := getIP(ntw, "tcp6")
294
+
295
+	if ourIP4 == nil && ourIP6 == nil {
296
+		tplError.Execute(os.Stdout, map[string]any{ //nolint: errcheck
297
+			"description": "cannot detect public IP address",
298
+			"error":       errors.New("ifconfig.co is unreachable for both IPv4 and IPv6"),
299
+		})
300
+		return false
301
+	}
309
 
302
 
310
 	strAddresses := []string{}
303
 	strAddresses := []string{}
311
 	for _, value := range addresses {
304
 	for _, value := range addresses {
312
-		if value.IP.String() == ourIP4.String() || value.IP.String() == ourIP6.String() {
313
-			tplODNSSNIMatch.Execute(os.Stdout, map[string]any{
305
+		if (ourIP4 != nil && value.IP.String() == ourIP4.String()) ||
306
+			(ourIP6 != nil && value.IP.String() == ourIP6.String()) {
307
+			tplODNSSNIMatch.Execute(os.Stdout, map[string]any{ //nolint: errcheck
314
 				"ip":       value.IP,
308
 				"ip":       value.IP,
315
 				"hostname": d.conf.Secret.Host,
309
 				"hostname": d.conf.Secret.Host,
316
 			})
310
 			})
320
 		strAddresses = append(strAddresses, `"`+value.IP.String()+`"`)
314
 		strAddresses = append(strAddresses, `"`+value.IP.String()+`"`)
321
 	}
315
 	}
322
 
316
 
323
-	tplEDNSSNIMatch.Execute(os.Stdout, map[string]any{
317
+	tplEDNSSNIMatch.Execute(os.Stdout, map[string]any{ //nolint: errcheck
324
 		"hostname": d.conf.Secret.Host,
318
 		"hostname": d.conf.Secret.Host,
325
 		"resolved": strings.Join(strAddresses, ", "),
319
 		"resolved": strings.Join(strAddresses, ", "),
326
 		"ip4":      ourIP4,
320
 		"ip4":      ourIP4,

+ 51
- 0
internal/cli/utils.go 查看文件

1
+package cli
2
+
3
+import (
4
+	"context"
5
+	"io"
6
+	"net"
7
+	"net/http"
8
+	"strings"
9
+
10
+	"github.com/9seconds/mtg/v2/essentials"
11
+	"github.com/9seconds/mtg/v2/mtglib"
12
+)
13
+
14
+func getIP(ntw mtglib.Network, protocol string) net.IP {
15
+	dialer := ntw.NativeDialer()
16
+	client := ntw.MakeHTTPClient(func(ctx context.Context, network, address string) (essentials.Conn, error) {
17
+		conn, err := dialer.DialContext(ctx, protocol, address)
18
+		if err != nil {
19
+			return nil, err
20
+		}
21
+		return essentials.WrapNetConn(conn), err
22
+	})
23
+
24
+	req, err := http.NewRequest(http.MethodGet, "https://ifconfig.co", nil) //nolint: noctx
25
+	if err != nil {
26
+		panic(err)
27
+	}
28
+
29
+	req.Header.Add("Accept", "text/plain")
30
+
31
+	resp, err := client.Do(req)
32
+	if err != nil {
33
+		return nil
34
+	}
35
+
36
+	if resp.StatusCode != http.StatusOK {
37
+		return nil
38
+	}
39
+
40
+	defer func() {
41
+		io.Copy(io.Discard, resp.Body) //nolint: errcheck
42
+		resp.Body.Close()              //nolint: errcheck
43
+	}()
44
+
45
+	data, err := io.ReadAll(resp.Body)
46
+	if err != nil {
47
+		return nil
48
+	}
49
+
50
+	return net.ParseIP(strings.TrimSpace(string(data)))
51
+}

+ 0
- 1
run_profile.go 查看文件

3
 package main
3
 package main
4
 
4
 
5
 func runProfile() {
5
 func runProfile() {
6
-
7
 }
6
 }

正在加载...
取消
保存