Skip to content

Commit

Permalink
fix: 修复了无法使用本地文件进行指定的情况 (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxueck authored Aug 15, 2023
1 parent 1be74d0 commit eed27d7
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
dist/
*.exe

.idea
.idea/*
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 49 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand Down
62 changes: 62 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}

0 comments on commit eed27d7

Please sign in to comment.