Просмотр исходного кода

Add verification of time skewness

tags/v2.2.4^2
9seconds 1 месяц назад
Родитель
Сommit
a60523fed0
3 измененных файлов: 69 добавлений и 20 удалений
  1. 1
    0
      go.mod
  2. 2
    0
      go.sum
  3. 66
    20
      internal/cli/doctor.go

+ 1
- 0
go.mod Просмотреть файл

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

+ 2
- 0
go.sum Просмотреть файл

12
 github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
12
 github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
13
 github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6 h1:4NNbNM2Iq/k57qEu7WfL67UrbPq1uFWxW4qODCohi+0=
13
 github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6 h1:4NNbNM2Iq/k57qEu7WfL67UrbPq1uFWxW4qODCohi+0=
14
 github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6/go.mod h1:J29hk+f9lJrblVIfiJOtTFk+OblBawmib4uz/VdKzlg=
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
 github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
17
 github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
16
 github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
18
 github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
17
 github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
19
 github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=

+ 66
- 20
internal/cli/doctor.go Просмотреть файл

3
 import (
3
 import (
4
 	"fmt"
4
 	"fmt"
5
 	"os"
5
 	"os"
6
-	"strings"
7
 	"text/template"
6
 	"text/template"
8
 
7
 
9
 	"github.com/9seconds/mtg/v2/internal/config"
8
 	"github.com/9seconds/mtg/v2/internal/config"
10
 	"github.com/9seconds/mtg/v2/internal/utils"
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
 var (
14
 var (
15
+	tplError = template.Must(
16
+		template.New("").Parse("  ‼️ {{ .description }}: {{ .error }}\n"),
17
+	)
18
+
14
 	tplWDeprecatedConfig = template.Must(
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
 	everythingOK := true
51
 	everythingOK := true
34
 
52
 
35
 	fmt.Println("Deprecated options")
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
 	} else {
56
 	} else {
42
 		fmt.Println("  ✅ All good")
57
 		fmt.Println("  ✅ All good")
43
 	}
58
 	}
44
 
59
 
60
+	fmt.Println("Time skewness")
61
+	if !d.checkTimeSkewness() {
62
+		everythingOK = false
63
+	}
64
+
45
 	if !everythingOK {
65
 	if !everythingOK {
46
 		os.Exit(1)
66
 		os.Exit(1)
47
 	}
67
 	}
49
 	return nil
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
 	if d.conf.DomainFrontingIP.Value != nil {
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
 			"when":        "2.3.0",
78
 			"when":        "2.3.0",
58
 			"old":         "domain-fronting-ip",
79
 			"old":         "domain-fronting-ip",
59
 			"old_section": "",
80
 			"old_section": "",
63
 	}
84
 	}
64
 
85
 
65
 	if d.conf.DomainFrontingPort.Value != 0 {
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
 			"when":        "2.3.0",
89
 			"when":        "2.3.0",
68
 			"old":         "domain-fronting-port",
90
 			"old":         "domain-fronting-port",
69
 			"old_section": "",
91
 			"old_section": "",
73
 	}
95
 	}
74
 
96
 
75
 	if d.conf.DomainFrontingProxyProtocol.Value {
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
 			"when":        "2.3.0",
100
 			"when":        "2.3.0",
78
 			"old":         "domain-fronting-proxy-protocol",
101
 			"old":         "domain-fronting-proxy-protocol",
79
 			"old_section": "",
102
 			"old_section": "",
83
 	}
106
 	}
84
 
107
 
85
 	if d.conf.Network.DOHIP.Value != nil {
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
 			"when":        "2.3.0",
111
 			"when":        "2.3.0",
88
 			"old":         "doh-ip",
112
 			"old":         "doh-ip",
89
 			"old_section": "network",
113
 			"old_section": "network",
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
 }

Загрузка…
Отмена
Сохранить