|
|
@@ -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
|
}
|