Quellcode durchsuchen

Refactoring

tags/v2.2.4^2
9seconds vor 1 Monat
Ursprung
Commit
b6b900e430
4 geänderte Dateien mit 88 neuen und 89 gelöschten Zeilen
  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 Datei anzeigen

@@ -1,22 +1,16 @@
1 1
 package cli
2 2
 
3 3
 import (
4
-	"context"
5 4
 	"encoding/json"
6 5
 	"fmt"
7
-	"io"
8 6
 	"net"
9
-	"net/http"
10 7
 	"net/url"
11 8
 	"os"
12 9
 	"strconv"
13
-	"strings"
14 10
 	"sync"
15 11
 
16
-	"github.com/9seconds/mtg/v2/essentials"
17 12
 	"github.com/9seconds/mtg/v2/internal/config"
18 13
 	"github.com/9seconds/mtg/v2/internal/utils"
19
-	"github.com/9seconds/mtg/v2/mtglib"
20 14
 )
21 15
 
22 16
 type accessResponse struct {
@@ -65,7 +59,7 @@ func (a *Access) Run(cli *CLI, version string) error {
65 59
 	wg.Go(func() {
66 60
 		ip := a.PublicIPv4
67 61
 		if ip == nil {
68
-			ip = a.getIP(ntw, "tcp4")
62
+			ip = getIP(ntw, "tcp4")
69 63
 		}
70 64
 
71 65
 		if ip != nil {
@@ -77,7 +71,7 @@ func (a *Access) Run(cli *CLI, version string) error {
77 71
 	wg.Go(func() {
78 72
 		ip := a.PublicIPv6
79 73
 		if ip == nil {
80
-			ip = a.getIP(ntw, "tcp6")
74
+			ip = getIP(ntw, "tcp6")
81 75
 		}
82 76
 
83 77
 		if ip != nil {
@@ -100,45 +94,6 @@ func (a *Access) Run(cli *CLI, version string) error {
100 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 97
 func (a *Access) makeURLs(conf *config.Config, ip net.IP) *accessResponseURLs {
143 98
 	if ip == nil {
144 99
 		return nil

+ 35
- 41
internal/cli/doctor.go Datei anzeigen

@@ -2,6 +2,7 @@ package cli
2 2
 
3 3
 import (
4 4
 	"context"
5
+	"errors"
5 6
 	"fmt"
6 7
 	"maps"
7 8
 	"net"
@@ -63,28 +64,6 @@ type Doctor struct {
63 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 67
 func (d *Doctor) Run(cli *CLI, version string) error {
89 68
 	conf, err := utils.ReadConfig(d.ConfigPath)
90 69
 	if err != nil {
@@ -140,7 +119,7 @@ func (d *Doctor) checkDeprecatedConfig() bool {
140 119
 
141 120
 	if d.conf.DomainFrontingIP.Value != nil {
142 121
 		ok = false
143
-		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{
122
+		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
144 123
 			"when":        "2.3.0",
145 124
 			"old":         "domain-fronting-ip",
146 125
 			"old_section": "",
@@ -151,7 +130,7 @@ func (d *Doctor) checkDeprecatedConfig() bool {
151 130
 
152 131
 	if d.conf.DomainFrontingPort.Value != 0 {
153 132
 		ok = false
154
-		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{
133
+		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
155 134
 			"when":        "2.3.0",
156 135
 			"old":         "domain-fronting-port",
157 136
 			"old_section": "",
@@ -162,7 +141,7 @@ func (d *Doctor) checkDeprecatedConfig() bool {
162 141
 
163 142
 	if d.conf.DomainFrontingProxyProtocol.Value {
164 143
 		ok = false
165
-		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{
144
+		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
166 145
 			"when":        "2.3.0",
167 146
 			"old":         "domain-fronting-proxy-protocol",
168 147
 			"old_section": "",
@@ -173,7 +152,7 @@ func (d *Doctor) checkDeprecatedConfig() bool {
173 152
 
174 153
 	if d.conf.Network.DOHIP.Value != nil {
175 154
 		ok = false
176
-		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{
155
+		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{ //nolint: errcheck
177 156
 			"when":        "2.3.0",
178 157
 			"old":         "doh-ip",
179 158
 			"old_section": "network",
@@ -192,7 +171,7 @@ func (d *Doctor) checkDeprecatedConfig() bool {
192 171
 func (d *Doctor) checkTimeSkewness() bool {
193 172
 	response, err := ntp.Query("0.pool.ntp.org")
194 173
 	if err != nil {
195
-		tplError.Execute(os.Stdout, map[string]any{
174
+		tplError.Execute(os.Stdout, map[string]any{ //nolint: errcheck
196 175
 			"description": "cannot access ntp pool",
197 176
 			"error":       err,
198 177
 		})
@@ -202,19 +181,19 @@ func (d *Doctor) checkTimeSkewness() bool {
202 181
 	skewness := response.ClockOffset.Abs()
203 182
 	confValue := d.conf.TolerateTimeSkewness.Get(mtglib.DefaultTolerateTimeSkewness)
204 183
 	diff := float64(skewness) / float64(confValue)
205
-	context := map[string]any{
184
+	tplData := map[string]any{
206 185
 		"drift": response.ClockOffset,
207 186
 		"value": confValue,
208 187
 	}
209 188
 
210 189
 	switch {
211 190
 	case diff < 0.3:
212
-		tplOTimeSkewness.Execute(os.Stdout, context)
191
+		tplOTimeSkewness.Execute(os.Stdout, tplData) //nolint: errcheck
213 192
 		return true
214 193
 	case diff < 0.7:
215
-		tplWTimeSkewness.Execute(os.Stdout, context)
194
+		tplWTimeSkewness.Execute(os.Stdout, tplData) //nolint: errcheck
216 195
 	default:
217
-		tplETimeSkewness.Execute(os.Stdout, context)
196
+		tplETimeSkewness.Execute(os.Stdout, tplData) //nolint: errcheck
218 197
 	}
219 198
 
220 199
 	return false
@@ -229,11 +208,11 @@ func (d *Doctor) checkNetwork(ntw mtglib.Network) bool {
229 208
 	for _, dc := range dcs {
230 209
 		err := d.checkNetworkAddresses(ntw, essentials.TelegramCoreAddresses[dc])
231 210
 		if err == nil {
232
-			tplODCConnect.Execute(os.Stdout, map[string]any{
211
+			tplODCConnect.Execute(os.Stdout, map[string]any{ //nolint: errcheck
233 212
 				"dc": dc,
234 213
 			})
235 214
 		} else {
236
-			tplEDCConnect.Execute(os.Stdout, map[string]any{
215
+			tplEDCConnect.Execute(os.Stdout, map[string]any{ //nolint: errcheck
237 216
 				"dc":    dc,
238 217
 				"error": err,
239 218
 			})
@@ -274,6 +253,10 @@ func (d *Doctor) checkNetworkAddresses(ntw mtglib.Network, addresses []string) e
274 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 260
 	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
278 261
 	defer cancel()
279 262
 
@@ -288,7 +271,7 @@ func (d *Doctor) checkNetworkAddresses(ntw mtglib.Network, addresses []string) e
288 271
 			continue
289 272
 		}
290 273
 
291
-		conn.Close()
274
+		conn.Close() //nolint: errcheck
292 275
 
293 276
 		return nil
294 277
 	}
@@ -299,18 +282,29 @@ func (d *Doctor) checkNetworkAddresses(ntw mtglib.Network, addresses []string) e
299 282
 func (d *Doctor) checkSecretHost(resolver *net.Resolver, ntw mtglib.Network) bool {
300 283
 	addresses, err := resolver.LookupIPAddr(context.Background(), d.conf.Secret.Host)
301 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 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 303
 	strAddresses := []string{}
311 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 308
 				"ip":       value.IP,
315 309
 				"hostname": d.conf.Secret.Host,
316 310
 			})
@@ -320,7 +314,7 @@ func (d *Doctor) checkSecretHost(resolver *net.Resolver, ntw mtglib.Network) boo
320 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 318
 		"hostname": d.conf.Secret.Host,
325 319
 		"resolved": strings.Join(strAddresses, ", "),
326 320
 		"ip4":      ourIP4,

+ 51
- 0
internal/cli/utils.go Datei anzeigen

@@ -0,0 +1,51 @@
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 Datei anzeigen

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

Laden…
Abbrechen
Speichern