|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+package cli
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "context"
|
|
|
5
|
+ "errors"
|
|
|
6
|
+ "fmt"
|
|
|
7
|
+ "maps"
|
|
|
8
|
+ "net"
|
|
|
9
|
+ "os"
|
|
|
10
|
+ "slices"
|
|
|
11
|
+ "strconv"
|
|
|
12
|
+ "strings"
|
|
|
13
|
+ "text/template"
|
|
|
14
|
+ "time"
|
|
|
15
|
+
|
|
|
16
|
+ "github.com/9seconds/mtg/v2/essentials"
|
|
|
17
|
+ "github.com/9seconds/mtg/v2/internal/config"
|
|
|
18
|
+ "github.com/9seconds/mtg/v2/internal/utils"
|
|
|
19
|
+ "github.com/9seconds/mtg/v2/mtglib"
|
|
|
20
|
+ "github.com/9seconds/mtg/v2/network/v2"
|
|
|
21
|
+ "github.com/beevik/ntp"
|
|
|
22
|
+)
|
|
|
23
|
+
|
|
|
24
|
+var (
|
|
|
25
|
+ tplError = template.Must(
|
|
|
26
|
+ template.New("").Parse(" ‼️ {{ .description }}: {{ .error }}\n"),
|
|
|
27
|
+ )
|
|
|
28
|
+
|
|
|
29
|
+ tplWDeprecatedConfig = template.Must(
|
|
|
30
|
+ template.New("").
|
|
|
31
|
+ Parse(` ⚠️ Option {{ .old | printf "%q" }}{{ if .old_section }} from section [{{ .old_section }}]{{ end }} is deprecated and will be removed in v{{ .when }}. Please use {{ .new | printf "%q" }}{{ if .new_section }} in [{{ .new_section }}] section{{ end }} instead.` + "\n"),
|
|
|
32
|
+ )
|
|
|
33
|
+
|
|
|
34
|
+ tplOTimeSkewness = template.Must(
|
|
|
35
|
+ template.New("").
|
|
|
36
|
+ Parse(" ✅ Time drift is {{ .drift }}, but tolerate-time-skewness is {{ .value }}\n"),
|
|
|
37
|
+ )
|
|
|
38
|
+ tplWTimeSkewness = template.Must(
|
|
|
39
|
+ template.New("").
|
|
|
40
|
+ Parse(" ⚠️ Time drift is {{ .drift }}, but tolerate-time-skewness is {{ .value }}. Please check ntp.\n"),
|
|
|
41
|
+ )
|
|
|
42
|
+ tplETimeSkewness = template.Must(
|
|
|
43
|
+ template.New("").
|
|
|
44
|
+ Parse(" ❌ Time drift is {{ .drift }}, but tolerate-time-skewness is {{ .value }}. You will get many rejected connections!\n"),
|
|
|
45
|
+ )
|
|
|
46
|
+
|
|
|
47
|
+ tplODCConnect = template.Must(
|
|
|
48
|
+ template.New("").Parse(" ✅ DC {{ .dc }}\n"),
|
|
|
49
|
+ )
|
|
|
50
|
+ tplEDCConnect = template.Must(
|
|
|
51
|
+ template.New("").Parse(" ❌ DC {{ .dc }}: {{ .error }}\n"),
|
|
|
52
|
+ )
|
|
|
53
|
+
|
|
|
54
|
+ tplODNSSNIMatch = template.Must(
|
|
|
55
|
+ template.New("").Parse(" ✅ IP address {{ .ip }} matches secret hostname {{ .hostname }}\n"),
|
|
|
56
|
+ )
|
|
|
57
|
+ tplEDNSSNIMatch = template.Must(
|
|
|
58
|
+ template.New("").Parse(" ❌ Hostname {{ .hostname }} {{ if .resolved }}is resolved to {{ .resolved }} addresses, not {{ if .ip4 }}{{ .ip4 }}{{ else }}{{ .ip6 }}{{ end }}{{ else }}cannot be resolved to any host{{ end }}\n"),
|
|
|
59
|
+ )
|
|
|
60
|
+
|
|
|
61
|
+ tplOFrontingDomain = template.Must(
|
|
|
62
|
+ template.New("").Parse(" ✅ {{ .address }} is reachable\n"),
|
|
|
63
|
+ )
|
|
|
64
|
+ tplEFrontingDomain = template.Must(
|
|
|
65
|
+ template.New("").Parse(" ❌ {{ .address }}: {{ .error }}\n"),
|
|
|
66
|
+ )
|
|
|
67
|
+)
|
|
|
68
|
+
|
|
|
69
|
+type Doctor struct {
|
|
|
70
|
+ conf *config.Config
|
|
|
71
|
+
|
|
|
72
|
+ ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"` //nolint: lll
|
|
|
73
|
+}
|
|
|
74
|
+
|
|
|
75
|
+func (d *Doctor) Run(cli *CLI, version string) error {
|
|
|
76
|
+ conf, err := utils.ReadConfig(d.ConfigPath)
|
|
|
77
|
+ if err != nil {
|
|
|
78
|
+ return fmt.Errorf("cannot init config: %w", err)
|
|
|
79
|
+ }
|
|
|
80
|
+
|
|
|
81
|
+ d.conf = conf
|
|
|
82
|
+
|
|
|
83
|
+ fmt.Println("Deprecated options")
|
|
|
84
|
+ everythingOK := d.checkDeprecatedConfig()
|
|
|
85
|
+
|
|
|
86
|
+ fmt.Println("Time skewness")
|
|
|
87
|
+ everythingOK = d.checkTimeSkewness() && everythingOK
|
|
|
88
|
+
|
|
|
89
|
+ resolver, err := network.GetDNS(conf.GetDNS())
|
|
|
90
|
+ if err != nil {
|
|
|
91
|
+ return fmt.Errorf("cannot create DNS resolver: %w", err)
|
|
|
92
|
+ }
|
|
|
93
|
+
|
|
|
94
|
+ base := network.New(
|
|
|
95
|
+ resolver,
|
|
|
96
|
+ "",
|
|
|
97
|
+ conf.Network.Timeout.TCP.Get(10*time.Second),
|
|
|
98
|
+ conf.Network.Timeout.HTTP.Get(0),
|
|
|
99
|
+ conf.Network.Timeout.Idle.Get(0),
|
|
|
100
|
+ )
|
|
|
101
|
+
|
|
|
102
|
+ fmt.Println("Validate native network connectivity")
|
|
|
103
|
+ everythingOK = d.checkNetwork(base) && everythingOK
|
|
|
104
|
+
|
|
|
105
|
+ for _, url := range conf.Network.Proxies {
|
|
|
106
|
+ value, err := network.NewProxyNetwork(base, url.Get(nil))
|
|
|
107
|
+ if err != nil {
|
|
|
108
|
+ return err
|
|
|
109
|
+ }
|
|
|
110
|
+
|
|
|
111
|
+ fmt.Printf("Validate network connectivity with proxy %s\n", url.Get(nil))
|
|
|
112
|
+ everythingOK = d.checkNetwork(value) && everythingOK
|
|
|
113
|
+ }
|
|
|
114
|
+
|
|
|
115
|
+ fmt.Println("Validate fronting domain connectivity")
|
|
|
116
|
+ everythingOK = d.checkFrontingDomain(base) && everythingOK
|
|
|
117
|
+
|
|
|
118
|
+ fmt.Println("Validate SNI-DNS match")
|
|
|
119
|
+ everythingOK = d.checkSecretHost(resolver, base) && everythingOK
|
|
|
120
|
+
|
|
|
121
|
+ if !everythingOK {
|
|
|
122
|
+ os.Exit(1)
|
|
|
123
|
+ }
|
|
|
124
|
+
|
|
|
125
|
+ return nil
|
|
|
126
|
+}
|
|
|
127
|
+
|
|
|
128
|
+func (d *Doctor) checkDeprecatedConfig() bool {
|
|
|
129
|
+ ok := true
|
|
|
130
|
+
|
|
|
131
|
+ if d.conf.DomainFrontingIP.Value != nil {
|
|
|
132
|
+ ok = false
|
|
|
133
|
+ tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
|
|
|
134
|
+ "when": "2.3.0",
|
|
|
135
|
+ "old": "domain-fronting-ip",
|
|
|
136
|
+ "old_section": "",
|
|
|
137
|
+ "new": "ip",
|
|
|
138
|
+ "new_section": "domain-fronting",
|
|
|
139
|
+ })
|
|
|
140
|
+ }
|
|
|
141
|
+
|
|
|
142
|
+ if d.conf.DomainFrontingPort.Value != 0 {
|
|
|
143
|
+ ok = false
|
|
|
144
|
+ tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
|
|
|
145
|
+ "when": "2.3.0",
|
|
|
146
|
+ "old": "domain-fronting-port",
|
|
|
147
|
+ "old_section": "",
|
|
|
148
|
+ "new": "port",
|
|
|
149
|
+ "new_section": "domain-fronting",
|
|
|
150
|
+ })
|
|
|
151
|
+ }
|
|
|
152
|
+
|
|
|
153
|
+ if d.conf.DomainFrontingProxyProtocol.Value {
|
|
|
154
|
+ ok = false
|
|
|
155
|
+ tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
|
|
|
156
|
+ "when": "2.3.0",
|
|
|
157
|
+ "old": "domain-fronting-proxy-protocol",
|
|
|
158
|
+ "old_section": "",
|
|
|
159
|
+ "new": "proxy-protocol",
|
|
|
160
|
+ "new_section": "domain-fronting",
|
|
|
161
|
+ })
|
|
|
162
|
+ }
|
|
|
163
|
+
|
|
|
164
|
+ if d.conf.Network.DOHIP.Value != nil {
|
|
|
165
|
+ ok = false
|
|
|
166
|
+ tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
|
|
|
167
|
+ "when": "2.3.0",
|
|
|
168
|
+ "old": "doh-ip",
|
|
|
169
|
+ "old_section": "network",
|
|
|
170
|
+ "new": "dns",
|
|
|
171
|
+ "new_section": "network",
|
|
|
172
|
+ })
|
|
|
173
|
+ }
|
|
|
174
|
+
|
|
|
175
|
+ if ok {
|
|
|
176
|
+ fmt.Println(" ✅ All good")
|
|
|
177
|
+ }
|
|
|
178
|
+
|
|
|
179
|
+ return ok
|
|
|
180
|
+}
|
|
|
181
|
+
|
|
|
182
|
+func (d *Doctor) checkTimeSkewness() bool {
|
|
|
183
|
+ response, err := ntp.Query("0.pool.ntp.org")
|
|
|
184
|
+ if err != nil {
|
|
|
185
|
+ tplError.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
|
|
186
|
+ "description": "cannot access ntp pool",
|
|
|
187
|
+ "error": err,
|
|
|
188
|
+ })
|
|
|
189
|
+ return false
|
|
|
190
|
+ }
|
|
|
191
|
+
|
|
|
192
|
+ skewness := response.ClockOffset.Abs()
|
|
|
193
|
+ confValue := d.conf.TolerateTimeSkewness.Get(mtglib.DefaultTolerateTimeSkewness)
|
|
|
194
|
+ diff := float64(skewness) / float64(confValue)
|
|
|
195
|
+ tplData := map[string]any{
|
|
|
196
|
+ "drift": response.ClockOffset,
|
|
|
197
|
+ "value": confValue,
|
|
|
198
|
+ }
|
|
|
199
|
+
|
|
|
200
|
+ switch {
|
|
|
201
|
+ case diff < 0.3:
|
|
|
202
|
+ tplOTimeSkewness.Execute(os.Stdout, tplData) //nolint: errcheck
|
|
|
203
|
+ return true
|
|
|
204
|
+ case diff < 0.7:
|
|
|
205
|
+ tplWTimeSkewness.Execute(os.Stdout, tplData) //nolint: errcheck
|
|
|
206
|
+ default:
|
|
|
207
|
+ tplETimeSkewness.Execute(os.Stdout, tplData) //nolint: errcheck
|
|
|
208
|
+ }
|
|
|
209
|
+
|
|
|
210
|
+ return false
|
|
|
211
|
+}
|
|
|
212
|
+
|
|
|
213
|
+func (d *Doctor) checkNetwork(ntw mtglib.Network) bool {
|
|
|
214
|
+ dcs := slices.Collect(maps.Keys(essentials.TelegramCoreAddresses))
|
|
|
215
|
+ slices.Sort(dcs)
|
|
|
216
|
+
|
|
|
217
|
+ ok := true
|
|
|
218
|
+
|
|
|
219
|
+ for _, dc := range dcs {
|
|
|
220
|
+ err := d.checkNetworkAddresses(ntw, essentials.TelegramCoreAddresses[dc])
|
|
|
221
|
+ if err == nil {
|
|
|
222
|
+ tplODCConnect.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
|
|
223
|
+ "dc": dc,
|
|
|
224
|
+ })
|
|
|
225
|
+ } else {
|
|
|
226
|
+ tplEDCConnect.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
|
|
227
|
+ "dc": dc,
|
|
|
228
|
+ "error": err,
|
|
|
229
|
+ })
|
|
|
230
|
+ ok = false
|
|
|
231
|
+ }
|
|
|
232
|
+ }
|
|
|
233
|
+
|
|
|
234
|
+ return ok
|
|
|
235
|
+}
|
|
|
236
|
+
|
|
|
237
|
+func (d *Doctor) checkNetworkAddresses(ntw mtglib.Network, addresses []string) error {
|
|
|
238
|
+ checkAddresses := []string{}
|
|
|
239
|
+
|
|
|
240
|
+ switch d.conf.PreferIP.Get("prefer-ip4") {
|
|
|
241
|
+ case "only-ipv4":
|
|
|
242
|
+ for _, addr := range addresses {
|
|
|
243
|
+ host, _, err := net.SplitHostPort(addr)
|
|
|
244
|
+ if err != nil {
|
|
|
245
|
+ panic(err)
|
|
|
246
|
+ }
|
|
|
247
|
+
|
|
|
248
|
+ if ip := net.ParseIP(host); ip != nil && ip.To4() != nil {
|
|
|
249
|
+ checkAddresses = append(checkAddresses, addr)
|
|
|
250
|
+ }
|
|
|
251
|
+ }
|
|
|
252
|
+ case "only-ipv6":
|
|
|
253
|
+ for _, addr := range addresses {
|
|
|
254
|
+ host, _, err := net.SplitHostPort(addr)
|
|
|
255
|
+ if err != nil {
|
|
|
256
|
+ panic(err)
|
|
|
257
|
+ }
|
|
|
258
|
+
|
|
|
259
|
+ if ip := net.ParseIP(host); ip != nil && ip.To4() == nil {
|
|
|
260
|
+ checkAddresses = append(checkAddresses, addr)
|
|
|
261
|
+ }
|
|
|
262
|
+ }
|
|
|
263
|
+ default:
|
|
|
264
|
+ checkAddresses = addresses
|
|
|
265
|
+ }
|
|
|
266
|
+
|
|
|
267
|
+ if len(checkAddresses) == 0 {
|
|
|
268
|
+ return fmt.Errorf("no suitable addresses after IP version filtering")
|
|
|
269
|
+ }
|
|
|
270
|
+
|
|
|
271
|
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
272
|
+ defer cancel()
|
|
|
273
|
+
|
|
|
274
|
+ var (
|
|
|
275
|
+ conn net.Conn
|
|
|
276
|
+ err error
|
|
|
277
|
+ )
|
|
|
278
|
+
|
|
|
279
|
+ for _, addr := range checkAddresses {
|
|
|
280
|
+ conn, err = ntw.DialContext(ctx, "tcp", addr)
|
|
|
281
|
+ if err != nil {
|
|
|
282
|
+ continue
|
|
|
283
|
+ }
|
|
|
284
|
+
|
|
|
285
|
+ conn.Close() //nolint: errcheck
|
|
|
286
|
+
|
|
|
287
|
+ return nil
|
|
|
288
|
+ }
|
|
|
289
|
+
|
|
|
290
|
+ return err
|
|
|
291
|
+}
|
|
|
292
|
+
|
|
|
293
|
+func (d *Doctor) checkFrontingDomain(ntw mtglib.Network) bool {
|
|
|
294
|
+ host := d.conf.Secret.Host
|
|
|
295
|
+ if ip := d.conf.GetDomainFrontingIP(nil); ip != "" {
|
|
|
296
|
+ host = ip
|
|
|
297
|
+ }
|
|
|
298
|
+
|
|
|
299
|
+ port := d.conf.GetDomainFrontingPort(mtglib.DefaultDomainFrontingPort)
|
|
|
300
|
+ address := net.JoinHostPort(host, strconv.Itoa(int(port)))
|
|
|
301
|
+
|
|
|
302
|
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
303
|
+ defer cancel()
|
|
|
304
|
+
|
|
|
305
|
+ dialer := ntw.NativeDialer()
|
|
|
306
|
+
|
|
|
307
|
+ conn, err := dialer.DialContext(ctx, "tcp", address)
|
|
|
308
|
+ if err != nil {
|
|
|
309
|
+ tplEFrontingDomain.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
|
|
310
|
+ "address": address,
|
|
|
311
|
+ "error": err,
|
|
|
312
|
+ })
|
|
|
313
|
+ return false
|
|
|
314
|
+ }
|
|
|
315
|
+
|
|
|
316
|
+ conn.Close() //nolint: errcheck
|
|
|
317
|
+
|
|
|
318
|
+ tplOFrontingDomain.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
|
|
319
|
+ "address": address,
|
|
|
320
|
+ })
|
|
|
321
|
+
|
|
|
322
|
+ return true
|
|
|
323
|
+}
|
|
|
324
|
+
|
|
|
325
|
+func (d *Doctor) checkSecretHost(resolver *net.Resolver, ntw mtglib.Network) bool {
|
|
|
326
|
+ addresses, err := resolver.LookupIPAddr(context.Background(), d.conf.Secret.Host)
|
|
|
327
|
+ if err != nil {
|
|
|
328
|
+ tplError.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
|
|
329
|
+ "description": fmt.Sprintf("cannot resolve DNS name of %s", d.conf.Secret.Host),
|
|
|
330
|
+ "error": err,
|
|
|
331
|
+ })
|
|
|
332
|
+ return false
|
|
|
333
|
+ }
|
|
|
334
|
+
|
|
|
335
|
+ ourIP4 := getIP(ntw, "tcp4")
|
|
|
336
|
+ ourIP6 := getIP(ntw, "tcp6")
|
|
|
337
|
+
|
|
|
338
|
+ if ourIP4 == nil && ourIP6 == nil {
|
|
|
339
|
+ tplError.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
|
|
340
|
+ "description": "cannot detect public IP address",
|
|
|
341
|
+ "error": errors.New("ifconfig.co is unreachable for both IPv4 and IPv6"),
|
|
|
342
|
+ })
|
|
|
343
|
+ return false
|
|
|
344
|
+ }
|
|
|
345
|
+
|
|
|
346
|
+ strAddresses := []string{}
|
|
|
347
|
+ for _, value := range addresses {
|
|
|
348
|
+ if (ourIP4 != nil && value.IP.String() == ourIP4.String()) ||
|
|
|
349
|
+ (ourIP6 != nil && value.IP.String() == ourIP6.String()) {
|
|
|
350
|
+ tplODNSSNIMatch.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
|
|
351
|
+ "ip": value.IP,
|
|
|
352
|
+ "hostname": d.conf.Secret.Host,
|
|
|
353
|
+ })
|
|
|
354
|
+ return true
|
|
|
355
|
+ }
|
|
|
356
|
+
|
|
|
357
|
+ strAddresses = append(strAddresses, `"`+value.IP.String()+`"`)
|
|
|
358
|
+ }
|
|
|
359
|
+
|
|
|
360
|
+ tplEDNSSNIMatch.Execute(os.Stdout, map[string]any{ //nolint: errcheck
|
|
|
361
|
+ "hostname": d.conf.Secret.Host,
|
|
|
362
|
+ "resolved": strings.Join(strAddresses, ", "),
|
|
|
363
|
+ "ip4": ourIP4,
|
|
|
364
|
+ "ip6": ourIP6,
|
|
|
365
|
+ })
|
|
|
366
|
+
|
|
|
367
|
+ return false
|
|
|
368
|
+}
|