Skip to content

Commit 5344e86

Browse files
fix: ssr uri decode (#2116)
1 parent 6cfaf15 commit 5344e86

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

common/convert/base64.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package convert
22

33
import (
44
"encoding/base64"
5+
"fmt"
56
"strings"
67
)
78

@@ -43,3 +44,22 @@ func decodeUrlSafe(data string) string {
4344
}
4445
return string(dcBuf)
4546
}
47+
48+
func TryDecodeBase64(s string) (decoded []byte, err error) {
49+
if len(s)%4 == 0 {
50+
if decoded, err = base64.StdEncoding.DecodeString(s); err == nil {
51+
return
52+
}
53+
if decoded, err = base64.URLEncoding.DecodeString(s); err == nil {
54+
return
55+
}
56+
} else {
57+
if decoded, err = base64.RawStdEncoding.DecodeString(s); err == nil {
58+
return
59+
}
60+
if decoded, err = base64.RawURLEncoding.DecodeString(s); err == nil {
61+
return
62+
}
63+
}
64+
return nil, fmt.Errorf("invalid base64-encoded string")
65+
}

common/convert/converter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,12 +456,12 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
456456
proxies = append(proxies, ss)
457457

458458
case "ssr":
459-
dcBuf, err := encRaw.DecodeString(body)
459+
dcBuf, err := TryDecodeBase64(body)
460460
if err != nil {
461461
continue
462462
}
463463

464-
// ssr://host:port:protocol:method:obfs:urlsafebase64pass/?obfsparam=urlsafebase64&protoparam=&remarks=urlsafebase64&group=urlsafebase64&udpport=0&uot=1
464+
// ssr://host:port:protocol:method:obfs:urlsafebase64pass/?obfsparam=urlsafebase64param&protoparam=urlsafebase64param&remarks=urlsafebase64remarks&group=urlsafebase64group&udpport=0&uot=1
465465

466466
before, after, ok := strings.Cut(string(dcBuf), "/?")
467467
if !ok {
@@ -490,7 +490,7 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
490490
name := uniqueName(names, remarks)
491491

492492
obfsParam := decodeUrlSafe(query.Get("obfsparam"))
493-
protocolParam := query.Get("protoparam")
493+
protocolParam := decodeUrlSafe(query.Get("protoparam"))
494494

495495
ssr := make(map[string]any, 20)
496496

0 commit comments

Comments
 (0)