From eed27d720705617d38e78ad660e7d4057f49f994 Mon Sep 17 00:00:00 2001 From: SXueckShen Date: Tue, 15 Aug 2023 11:03:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E4=BD=BF=E7=94=A8=E6=9C=AC=E5=9C=B0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=8C=87=E5=AE=9A=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=20(#13)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- README.md | 4 +++- main.go | 63 ++++++++++++++++++++++++++++++++++++++++------------ main_test.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 16 deletions(-) create mode 100644 main_test.go diff --git a/.gitignore b/.gitignore index 9495459..4416ca1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ dist/ *.exe -.idea \ No newline at end of file +.idea/* \ No newline at end of file diff --git a/README.md b/README.md index 2ea9225..4f07bef 100644 --- a/README.md +++ b/README.md @@ -48,8 +48,10 @@ Premium|广港|IEPL|02 N/A N/A Premium|广港|IEPL|03 2.62MB/s 333.00ms Premium|广港|IEPL|04 1.46MB/s 272.00ms Premium|广港|IEPL|05 3.87MB/s 249.00ms +# 3. 当然你也可以混合使用 +> clash-speedtest -c "https://domain.com/link/hash?clash=1,/home/.config/clash/config.yaml" # 3. 使用自定义服务器进行测试(ip地址为示例,并无实际效果) -> clash-speedtest -c "https://ds-epimetheus.oss-cn-shenzhen.aliyuncs.com/rules" -l "http://1.1.1.1:8080/_down?bytes=%d" --size 10200 +> clash-speedtest -c "https://domain/rules" -l "http://1.1.1.1:8080/_down?bytes=%d" --size 10200 节点 带宽 延迟 FORWARD-STEAM-COM 9.27KB/s 310.00ms FORWARD-STEAM-COM-BAK 137.41KB/s 68.00ms diff --git a/main.go b/main.go index 908dd10..c25b5cd 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,32 @@ type RawConfig struct { Proxies []map[string]any `yaml:"proxies"` } +func DifferentTypesOfParsing(pv string, isUriType bool) any { + defer func() { + // 防止解析过程中出现获取文件句柄出现致命性错误 + if err := recover(); err != nil { + log.Warnln("There is an error url being ignored : %s", err) + } + }() + var err error + if isUriType { + var u *url.URL + if u, err = url.Parse(pv); err != nil { + return nil + } + + if u.Host == "" { + return nil + } + return u + } else { + if _, err = os.Stat(pv); err != nil { + return DifferentTypesOfParsing(pv, true) + } + return os.File{} + } +} + func main() { flag.Parse() @@ -51,20 +77,29 @@ func main() { var allProxies = make(map[string]C.Proxy) arURL := strings.Split(*configPathConfig, ",") for _, v := range arURL { - u, err := url.Parse(v) - if err != nil { - log.Warnln("There is an error url being ignored : %s", v) - continue - } - - resp, err := http.Get(u.String()) - 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) + var u any + var body []byte + var err error + + // 前置最基本的判断,如果错了也可以通过在方法内纠正回来 + u = DifferentTypesOfParsing(v, strings.HasPrefix(v, "http")) + + // 避免因为多层 if 产生的嵌套造成代码维护性下降 + switch u.(type) { + case os.File: + if body, err = os.ReadFile(v); err != nil { + log.Warnln("Failed to decode config: %s", err) + continue + } + case *url.URL: + resp, err := http.Get(u.(*url.URL).String()) + if err != nil { + log.Warnln("Failed to fetch config: %s", err) + } + body, err = io.ReadAll(resp.Body) + if err != nil { + log.Warnln("Failed to read config: %s", err) + } } lps, err := loadProxies(body) diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..86defd8 --- /dev/null +++ b/main_test.go @@ -0,0 +1,62 @@ +package main + +import ( + "net/url" + "os" + "testing" +) + +func TestDifferentTypesOfParsing(t *testing.T) { + // Create a temporary file + tempFile, err := os.CreateTemp("", "test20230815114.txt") + if err != nil { + t.Fatal(err) + } + defer os.Remove(tempFile.Name()) // Clean up + + testCases := []struct { + pv string + isUriType bool + expectErr bool + }{ + { + pv: "http://example.com", + isUriType: true, + expectErr: false, + }, + { + pv: "thisisnotavaliduri", + isUriType: true, + expectErr: true, + }, + { + pv: tempFile.Name(), + isUriType: false, + expectErr: false, + }, + { + pv: "non_existent_file.txt", + isUriType: false, + expectErr: true, + }, + } + + for _, tc := range testCases { + result := DifferentTypesOfParsing(tc.pv, tc.isUriType) + if tc.expectErr && result != nil { + t.Errorf("Expected error for pv %s, but got no error", tc.pv) + } + if !tc.isUriType && result != nil { + _, ok := result.(os.File) + if !ok { + t.Errorf("Expected FileInfo type for pv %s", tc.pv) + } + } + if tc.isUriType && result != nil { + _, ok := result.(*url.URL) + if !ok { + t.Errorf("Expected URL type for pv %s", tc.pv) + } + } + } +}