-
Notifications
You must be signed in to change notification settings - Fork 63
/
main.go
216 lines (188 loc) · 5.79 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"flag"
"fmt"
"os"
"sort"
"time"
"github.com/faceair/clash-speedtest/speedtester"
"github.com/metacubex/mihomo/log"
"github.com/olekukonko/tablewriter"
"github.com/schollz/progressbar/v3"
"gopkg.in/yaml.v3"
)
var (
configPathsConfig = flag.String("c", "", "config file path, also support http(s) url")
filterRegexConfig = flag.String("f", ".+", "filter proxies by name, use regexp")
serverURL = flag.String("server-url", "https://speed.cloudflare.com", "server url")
downloadSize = flag.Int("download-size", 50*1024*1024, "download size for testing proxies")
uploadSize = flag.Int("upload-size", 20*1024*1024, "upload size for testing proxies")
timeout = flag.Duration("timeout", time.Second*5, "timeout for testing proxies")
concurrent = flag.Int("concurrent", 4, "download concurrent size")
outputPath = flag.String("output", "", "output config file path")
maxLatency = flag.Duration("max-latency", 800*time.Millisecond, "filter latency greater than this value")
minSpeed = flag.Float64("min-speed", 5, "filter speed less than this value(unit: MB/s)")
)
const (
colorRed = "\033[31m"
colorGreen = "\033[32m"
colorYellow = "\033[33m"
colorReset = "\033[0m"
)
func main() {
flag.Parse()
log.SetLevel(log.SILENT)
if *configPathsConfig == "" {
log.Fatalln("please specify the configuration file")
}
speedTester := speedtester.New(&speedtester.Config{
ConfigPaths: *configPathsConfig,
FilterRegex: *filterRegexConfig,
ServerURL: *serverURL,
DownloadSize: *downloadSize,
UploadSize: *uploadSize,
Timeout: *timeout,
Concurrent: *concurrent,
})
allProxies, err := speedTester.LoadProxies()
if err != nil {
log.Fatalln("load proxies failed: %v", err)
}
bar := progressbar.Default(int64(len(allProxies)), "测试中...")
results := make([]*speedtester.Result, 0)
speedTester.TestProxies(allProxies, func(result *speedtester.Result) {
bar.Add(1)
bar.Describe(result.ProxyName)
results = append(results, result)
})
sort.Slice(results, func(i, j int) bool {
return results[i].DownloadSpeed > results[j].DownloadSpeed
})
printResults(results)
if *outputPath != "" {
err = saveConfig(results)
if err != nil {
log.Fatalln("save config file failed: %v", err)
}
fmt.Printf("\nsave config file to: %s\n", *outputPath)
}
}
func printResults(results []*speedtester.Result) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{
"序号",
"节点名称",
"类型",
"延迟",
"抖动",
"丢包率",
"下载速度",
"上传速度",
})
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t")
table.SetNoWhiteSpace(true)
for i, result := range results {
idStr := fmt.Sprintf("%d.", i+1)
// 延迟颜色
latencyStr := result.FormatLatency()
if result.Latency > 0 {
if result.Latency < 800*time.Millisecond {
latencyStr = colorGreen + latencyStr + colorReset
} else if result.Latency < 1500*time.Millisecond {
latencyStr = colorYellow + latencyStr + colorReset
} else {
latencyStr = colorRed + latencyStr + colorReset
}
} else {
latencyStr = colorRed + latencyStr + colorReset
}
jitterStr := result.FormatJitter()
if result.Jitter > 0 {
if result.Jitter < 800*time.Millisecond {
jitterStr = colorGreen + jitterStr + colorReset
} else if result.Jitter < 1500*time.Millisecond {
jitterStr = colorYellow + jitterStr + colorReset
} else {
jitterStr = colorRed + jitterStr + colorReset
}
} else {
jitterStr = colorRed + jitterStr + colorReset
}
// 丢包率颜色
packetLossStr := result.FormatPacketLoss()
if result.PacketLoss < 10 {
packetLossStr = colorGreen + packetLossStr + colorReset
} else if result.PacketLoss < 20 {
packetLossStr = colorYellow + packetLossStr + colorReset
} else {
packetLossStr = colorRed + packetLossStr + colorReset
}
// 下载速度颜色 (以MB/s为单位判断)
downloadSpeed := result.DownloadSpeed / (1024 * 1024)
downloadSpeedStr := result.FormatDownloadSpeed()
if downloadSpeed >= 10 {
downloadSpeedStr = colorGreen + downloadSpeedStr + colorReset
} else if downloadSpeed >= 5 {
downloadSpeedStr = colorYellow + downloadSpeedStr + colorReset
} else {
downloadSpeedStr = colorRed + downloadSpeedStr + colorReset
}
// 上传速度颜色
uploadSpeed := result.UploadSpeed / (1024 * 1024)
uploadSpeedStr := result.FormatUploadSpeed()
if uploadSpeed >= 5 {
uploadSpeedStr = colorGreen + uploadSpeedStr + colorReset
} else if uploadSpeed >= 2 {
uploadSpeedStr = colorYellow + uploadSpeedStr + colorReset
} else {
uploadSpeedStr = colorRed + uploadSpeedStr + colorReset
}
row := []string{
idStr,
result.ProxyName,
result.ProxyType,
latencyStr,
jitterStr,
packetLossStr,
downloadSpeedStr,
uploadSpeedStr,
}
table.Append(row)
}
fmt.Println()
table.Render()
fmt.Println()
}
func saveConfig(results []*speedtester.Result) error {
filteredResults := make([]*speedtester.Result, 0)
for _, result := range results {
if *maxLatency > 0 && result.Latency > *maxLatency {
continue
}
if *minSpeed > 0 && float64(result.DownloadSpeed)/(1024*1024) < *minSpeed {
continue
}
filteredResults = append(filteredResults, result)
}
proxies := make([]map[string]any, 0)
for _, result := range filteredResults {
proxies = append(proxies, result.ProxyConfig)
}
config := &speedtester.RawConfig{
Proxies: proxies,
}
yamlData, err := yaml.Marshal(config)
if err != nil {
return err
}
return os.WriteFile(*outputPath, yamlData, 0o644)
}