Procházet zdrojové kódy

Add verification of time skewness

tags/v2.2.4^2
9seconds před 1 měsícem
rodič
revize
a60523fed0
3 změnil soubory, kde provedl 69 přidání a 20 odebrání
  1. 1
    0
      go.mod
  2. 2
    0
      go.sum
  3. 66
    20
      internal/cli/doctor.go

+ 1
- 0
go.mod Zobrazit soubor

@@ -36,6 +36,7 @@ require (
36 36
 )
37 37
 
38 38
 require (
39
+	github.com/beevik/ntp v1.5.0 // indirect
39 40
 	github.com/beorn7/perks v1.0.1 // indirect
40 41
 	github.com/cespare/xxhash/v2 v2.3.0 // indirect
41 42
 	github.com/davecgh/go-spew v1.1.1 // indirect

+ 2
- 0
go.sum Zobrazit soubor

@@ -12,6 +12,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd
12 12
 github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
13 13
 github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6 h1:4NNbNM2Iq/k57qEu7WfL67UrbPq1uFWxW4qODCohi+0=
14 14
 github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6/go.mod h1:J29hk+f9lJrblVIfiJOtTFk+OblBawmib4uz/VdKzlg=
15
+github.com/beevik/ntp v1.5.0 h1:y+uj/JjNwlY2JahivxYvtmv4ehfi3h74fAuABB9ZSM4=
16
+github.com/beevik/ntp v1.5.0/go.mod h1:mJEhBrwT76w9D+IfOEGvuzyuudiW9E52U2BaTrMOYow=
15 17
 github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
16 18
 github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
17 19
 github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=

+ 66
- 20
internal/cli/doctor.go Zobrazit soubor

@@ -3,17 +3,35 @@ package cli
3 3
 import (
4 4
 	"fmt"
5 5
 	"os"
6
-	"strings"
7 6
 	"text/template"
8 7
 
9 8
 	"github.com/9seconds/mtg/v2/internal/config"
10 9
 	"github.com/9seconds/mtg/v2/internal/utils"
10
+	"github.com/9seconds/mtg/v2/mtglib"
11
+	"github.com/beevik/ntp"
11 12
 )
12 13
 
13 14
 var (
15
+	tplError = template.Must(
16
+		template.New("").Parse("  ‼️ {{ .description }}: {{ .error }}\n"),
17
+	)
18
+
14 19
 	tplWDeprecatedConfig = template.Must(
15
-		template.New("deprecated-config").
16
-			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.`),
20
+		template.New("").
21
+			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"),
22
+	)
23
+
24
+	tplOTimeSkewness = template.Must(
25
+		template.New("").
26
+			Parse("  ✅ Time drift is {{ .drift }}, but tolerate-time-skewness is {{ .value }}\n"),
27
+	)
28
+	tplWTimeSkewness = template.Must(
29
+		template.New("").
30
+			Parse("  ⚠️ Time drift is {{ .drift }}, but tolerate-time-skewness is {{ .value }}. Please check ntp.\n"),
31
+	)
32
+	tplETimeSkewness = template.Must(
33
+		template.New("").
34
+			Parse("  ❌ Time drift is {{ .drift }}, but tolerate-time-skewness is {{ .value }}. You will get many rejected connections!\n"),
17 35
 	)
18 36
 )
19 37
 
@@ -33,15 +51,17 @@ func (d *Doctor) Run(cli *CLI, version string) error {
33 51
 	everythingOK := true
34 52
 
35 53
 	fmt.Println("Deprecated options")
36
-	if errs := d.checkDeprecatedConfig(); len(errs) > 0 {
37
-		for _, err := range errs {
38
-			fmt.Println(err)
39
-			everythingOK = false
40
-		}
54
+	if !d.checkDeprecatedConfig() {
55
+		everythingOK = false
41 56
 	} else {
42 57
 		fmt.Println("  ✅ All good")
43 58
 	}
44 59
 
60
+	fmt.Println("Time skewness")
61
+	if !d.checkTimeSkewness() {
62
+		everythingOK = false
63
+	}
64
+
45 65
 	if !everythingOK {
46 66
 		os.Exit(1)
47 67
 	}
@@ -49,11 +69,12 @@ func (d *Doctor) Run(cli *CLI, version string) error {
49 69
 	return nil
50 70
 }
51 71
 
52
-func (d *Doctor) checkDeprecatedConfig() []string {
53
-	errors := []string{}
72
+func (d *Doctor) checkDeprecatedConfig() bool {
73
+	ok := true
54 74
 
55 75
 	if d.conf.DomainFrontingIP.Value != nil {
56
-		errors = d.addError(errors, tplWDeprecatedConfig, map[string]string{
76
+		ok = false
77
+		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{
57 78
 			"when":        "2.3.0",
58 79
 			"old":         "domain-fronting-ip",
59 80
 			"old_section": "",
@@ -63,7 +84,8 @@ func (d *Doctor) checkDeprecatedConfig() []string {
63 84
 	}
64 85
 
65 86
 	if d.conf.DomainFrontingPort.Value != 0 {
66
-		errors = d.addError(errors, tplWDeprecatedConfig, map[string]string{
87
+		ok = false
88
+		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{
67 89
 			"when":        "2.3.0",
68 90
 			"old":         "domain-fronting-port",
69 91
 			"old_section": "",
@@ -73,7 +95,8 @@ func (d *Doctor) checkDeprecatedConfig() []string {
73 95
 	}
74 96
 
75 97
 	if d.conf.DomainFrontingProxyProtocol.Value {
76
-		errors = d.addError(errors, tplWDeprecatedConfig, map[string]string{
98
+		ok = false
99
+		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{
77 100
 			"when":        "2.3.0",
78 101
 			"old":         "domain-fronting-proxy-protocol",
79 102
 			"old_section": "",
@@ -83,7 +106,8 @@ func (d *Doctor) checkDeprecatedConfig() []string {
83 106
 	}
84 107
 
85 108
 	if d.conf.Network.DOHIP.Value != nil {
86
-		errors = d.addError(errors, tplWDeprecatedConfig, map[string]string{
109
+		ok = false
110
+		tplWDeprecatedConfig.Execute(os.Stdout, map[string]string{
87 111
 			"when":        "2.3.0",
88 112
 			"old":         "doh-ip",
89 113
 			"old_section": "network",
@@ -92,14 +116,36 @@ func (d *Doctor) checkDeprecatedConfig() []string {
92 116
 		})
93 117
 	}
94 118
 
95
-	return errors
119
+	return ok
96 120
 }
97 121
 
98
-func (d *Doctor) addError(messages []string, tpl *template.Template, context map[string]string) []string {
99
-	value := &strings.Builder{}
100
-	if err := tpl.Execute(value, context); err != nil {
101
-		panic(err)
122
+func (d *Doctor) checkTimeSkewness() bool {
123
+	response, err := ntp.Query("0.pool.ntp.org")
124
+	if err != nil {
125
+		tplError.Execute(os.Stdout, map[string]any{
126
+			"description": "cannot access ntp pool",
127
+			"error":       err,
128
+		})
129
+		return false
130
+	}
131
+
132
+	skewness := response.ClockOffset.Abs()
133
+	confValue := d.conf.TolerateTimeSkewness.Get(mtglib.DefaultTolerateTimeSkewness)
134
+	diff := float64(skewness) / float64(confValue)
135
+	context := map[string]any{
136
+		"drift": response.ClockOffset,
137
+		"value": confValue,
138
+	}
139
+
140
+	switch {
141
+	case diff < 0.3:
142
+		tplOTimeSkewness.Execute(os.Stdout, context)
143
+		return true
144
+	case diff < 0.7:
145
+		tplWTimeSkewness.Execute(os.Stdout, context)
146
+	default:
147
+		tplETimeSkewness.Execute(os.Stdout, context)
102 148
 	}
103 149
 
104
-	return append(messages, value.String())
150
+	return false
105 151
 }

Načítá se…
Zrušit
Uložit