forked from faceair/clash-speedtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
304 lines (272 loc) · 7.65 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"context"
"encoding/csv"
"flag"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/Dreamacro/clash/adapter"
"github.com/Dreamacro/clash/adapter/provider"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
"gopkg.in/yaml.v3"
)
var (
configPathConfig = flag.String("c", "", "configuration file path, also support http(s) url")
filterRegexConfig = flag.String("f", ".*", "filter proxies by name, use regexp")
downloadSizeConfig = flag.Int("size", 1024*1024*100, "download size for testing proxies")
timeoutConfig = flag.Duration("timeout", time.Second*5, "timeout for testing proxies")
sortField = flag.String("sort", "b", "sort field for testing proxies, b for bandwidth, t for TTFB")
output = flag.String("output", "", "output result to csv file")
)
type RawConfig struct {
Providers map[string]map[string]any `yaml:"proxy-providers"`
Proxies []map[string]any `yaml:"proxies"`
}
func main() {
flag.Parse()
if *configPathConfig == "" {
log.Fatalln("Please specify the configuration file")
}
if strings.HasPrefix(*configPathConfig, "http") {
resp, err := http.Get(*configPathConfig)
if err != nil {
log.Fatalln("Failed to fetch config: %s", err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln("Failed to read config: %s", err)
}
*configPathConfig = filepath.Join(os.TempDir(), "clash_config.yaml")
if err := os.WriteFile(*configPathConfig, body, 0644); err != nil {
log.Fatalln("Failed to write config: %s", err)
}
}
if !filepath.IsAbs(*configPathConfig) {
currentDir, _ := os.Getwd()
*configPathConfig = filepath.Join(currentDir, *configPathConfig)
}
C.SetHomeDir(os.TempDir())
C.SetConfig(*configPathConfig)
proxies, err := loadProxies()
if err != nil {
log.Fatalln("Failed to load config: %s", err)
}
filteredProxies := filterProxies(*filterRegexConfig, proxies)
results := make([]Result, 0, len(filteredProxies))
format := "%s%-42s\t%-12s\t%-12s\033[0m\n"
fmt.Printf(format, "", "节点", "带宽", "延迟")
for _, name := range filteredProxies {
proxy := proxies[name]
switch proxy.Type() {
case C.Shadowsocks, C.ShadowsocksR, C.Snell, C.Socks5, C.Http, C.Vmess, C.Trojan:
result := TestProxy(name, proxy, *downloadSizeConfig, *timeoutConfig)
result.Printf(format)
results = append(results, *result)
case C.Direct, C.Reject, C.Relay, C.Selector, C.Fallback, C.URLTest, C.LoadBalance:
continue
default:
log.Fatalln("Unsupported proxy type: %s", proxy.Type())
}
}
if *sortField != "" {
switch *sortField {
case "b", "bandwidth":
sort.Slice(results, func(i, j int) bool {
return results[i].Bandwidth > results[j].Bandwidth
})
fmt.Println("\n\n===结果按照带宽排序===")
case "t", "ttfb":
sort.Slice(results, func(i, j int) bool {
return results[i].TTFB < results[j].TTFB
})
fmt.Println("\n\n===结果按照延迟排序===")
default:
log.Fatalln("Unsupported sort field: %s", *sortField)
}
fmt.Printf(format, "", "节点", "带宽", "延迟")
for _, result := range results {
result.Printf(format)
}
}
if *output != "" {
writeToCSV(*output, results)
}
}
func filterProxies(filter string, proxies map[string]C.Proxy) []string {
filterRegexp := regexp.MustCompile(filter)
filteredProxies := make([]string, 0, len(proxies))
for name := range proxies {
if filterRegexp.MatchString(name) {
filteredProxies = append(filteredProxies, name)
}
}
sort.Strings(filteredProxies)
return filteredProxies
}
func loadProxies() (map[string]C.Proxy, error) {
buf, err := os.ReadFile(C.Path.Config())
if err != nil {
return nil, err
}
rawCfg := &RawConfig{
Proxies: []map[string]any{},
}
if err := yaml.Unmarshal(buf, rawCfg); err != nil {
return nil, err
}
proxies := make(map[string]C.Proxy)
proxiesConfig := rawCfg.Proxies
providersConfig := rawCfg.Providers
for i, config := range proxiesConfig {
proxy, err := adapter.ParseProxy(config)
if err != nil {
return nil, fmt.Errorf("proxy %d: %w", i, err)
}
if _, exist := proxies[proxy.Name()]; exist {
return nil, fmt.Errorf("proxy %s is the duplicate name", proxy.Name())
}
proxies[proxy.Name()] = proxy
}
for name, config := range providersConfig {
if name == provider.ReservedName {
return nil, fmt.Errorf("can not defined a provider called `%s`", provider.ReservedName)
}
pd, err := provider.ParseProxyProvider(name, config)
if err != nil {
return nil, fmt.Errorf("parse proxy provider %s error: %w", name, err)
}
if err := pd.Initial(); err != nil {
return nil, fmt.Errorf("initial proxy provider %s error: %w", pd.Name(), err)
}
for _, proxy := range pd.Proxies() {
proxies[fmt.Sprintf("[%s] %s", name, proxy.Name())] = proxy
}
}
return proxies, nil
}
type Result struct {
Name string
Bandwidth float64
TTFB time.Duration
}
var (
red = "\033[31m"
green = "\033[32m"
)
func (r *Result) Printf(format string) {
color := ""
if r.Bandwidth < 1024*1024 {
color = red
} else if r.Bandwidth > 1024*1024*10 {
color = green
}
fmt.Printf(format, color, formatName(r.Name), formatBandwidth(r.Bandwidth), formatMillseconds(r.TTFB))
}
func TestProxy(name string, proxy C.Proxy, downloadSize int, timeout time.Duration) *Result {
client := http.Client{
Timeout: timeout,
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
return proxy.DialContext(ctx, &C.Metadata{
Host: host,
DstPort: port,
})
},
},
}
start := time.Now()
resp, err := client.Get(fmt.Sprintf("https://speed.cloudflare.com/__down?bytes=%d", downloadSize))
if err != nil {
return &Result{name, -1, -1}
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return &Result{name, -1, -1}
}
ttfb := time.Since(start)
written, _ := io.Copy(io.Discard, resp.Body)
if written == 0 {
return &Result{name, -1, -1}
}
downloadTime := time.Since(start) - ttfb
bandwidth := float64(written) / downloadTime.Seconds()
return &Result{name, bandwidth, ttfb}
}
var (
emojiRegex = regexp.MustCompile(`[\x{1F600}-\x{1F64F}\x{1F300}-\x{1F5FF}\x{1F680}-\x{1F6FF}\x{2600}-\x{26FF}\x{1F1E0}-\x{1F1FF}]`)
spaceRegex = regexp.MustCompile(`\s{2,}`)
)
func formatName(name string) string {
noEmoji := emojiRegex.ReplaceAllString(name, "")
mergedSpaces := spaceRegex.ReplaceAllString(noEmoji, " ")
return strings.TrimSpace(mergedSpaces)
}
func formatBandwidth(v float64) string {
if v <= 0 {
return "N/A"
}
if v < 1024 {
return fmt.Sprintf("%.02fB/s", v)
}
v /= 1024
if v < 1024 {
return fmt.Sprintf("%.02fKB/s", v)
}
v /= 1024
if v < 1024 {
return fmt.Sprintf("%.02fMB/s", v)
}
v /= 1024
if v < 1024 {
return fmt.Sprintf("%.02fGB/s", v)
}
v /= 1024
return fmt.Sprintf("%.02fTB/s", v)
}
func formatMillseconds(v time.Duration) string {
if v <= 0 {
return "N/A"
}
return fmt.Sprintf("%.02fms", float64(v.Milliseconds()))
}
func writeToCSV(filePath string, results []Result) error {
csvFile, err := os.Create(filePath)
if err != nil {
return err
}
defer csvFile.Close()
// 写入 UTF-8 BOM 头
csvFile.WriteString("\xEF\xBB\xBF")
csvWriter := csv.NewWriter(csvFile)
err = csvWriter.Write([]string{"节点", "带宽 (MB/s)", "延迟 (ms)"})
if err != nil {
return err
}
for _, result := range results {
line := []string{
result.Name,
fmt.Sprintf("%.2f", result.Bandwidth/1024/1024),
strconv.FormatInt(result.TTFB.Milliseconds(), 10),
}
err := csvWriter.Write(line)
if err != nil {
return err
}
}
csvWriter.Flush()
return nil
}