1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 14:04:41 -05:00

try to fix ss parser

This commit is contained in:
Nite07 2024-11-06 18:43:48 +08:00
parent f7dc78aabc
commit 66f214ae10
2 changed files with 31 additions and 27 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
* text=auto eol=lf

View File

@ -13,7 +13,6 @@ func ParseShadowsocks(proxy string) (model.Proxy, error) {
if !strings.HasPrefix(proxy, constant.ShadowsocksPrefix) { if !strings.HasPrefix(proxy, constant.ShadowsocksPrefix) {
return model.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy} return model.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
} }
needDecode := true
if !strings.Contains(proxy, "@") { if !strings.Contains(proxy, "@") {
s := strings.SplitN(proxy, "#", 2) s := strings.SplitN(proxy, "#", 2)
d, err := DecodeBase64(strings.TrimPrefix(s[0], "ss://")) d, err := DecodeBase64(strings.TrimPrefix(s[0], "ss://"))
@ -29,7 +28,6 @@ func ParseShadowsocks(proxy string) (model.Proxy, error) {
} else { } else {
proxy = "ss://" + d proxy = "ss://" + d
} }
needDecode = false
} }
link, err := url.Parse(proxy) link, err := url.Parse(proxy)
if err != nil { if err != nil {
@ -65,37 +63,28 @@ func ParseShadowsocks(proxy string) (model.Proxy, error) {
} }
} }
method := "" method := link.User.Username()
password := "" password, _ := link.User.Password()
if needDecode {
user, err := DecodeBase64(link.User.Username()) if password == "" {
user, err := DecodeBase64(method)
if err == nil {
methodAndPass := strings.SplitN(user, ":", 2)
if len(methodAndPass) == 2 {
method = methodAndPass[0]
password = methodAndPass[1]
}
}
}
if isLikelyBase64(password) {
password, err = DecodeBase64(password)
if err != nil { if err != nil {
return model.Proxy{}, &ParseError{ return model.Proxy{}, &ParseError{
Type: ErrInvalidStruct, Type: ErrInvalidStruct,
Message: "missing method and password", Message: "password decode error",
Raw: proxy, Raw: proxy,
} }
} }
if user == "" {
return model.Proxy{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing method and password",
Raw: proxy,
}
}
methodAndPass := strings.SplitN(user, ":", 2)
if len(methodAndPass) != 2 {
return model.Proxy{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing method and password",
Raw: proxy,
}
}
method = methodAndPass[0]
password = methodAndPass[1]
} else {
method = link.User.Username()
password, _ = link.User.Password()
} }
remarks := link.Fragment remarks := link.Fragment
@ -115,3 +104,17 @@ func ParseShadowsocks(proxy string) (model.Proxy, error) {
return result, nil return result, nil
} }
func isLikelyBase64(s string) bool {
if len(s)%4 == 0 && strings.HasSuffix(s, "=") && !strings.Contains(strings.TrimSuffix(s, "="), "=") {
s = strings.TrimSuffix(s, "=")
chars := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
for _, c := range s {
if !strings.ContainsRune(chars, c) {
return false
}
}
return true
}
return false
}