mirror of
https://github.com/bestnite/sub2clash.git
synced 2025-10-26 09:11:01 +00:00
Refactor subscription handling by removing SubConfig model, updating BuildSub function to use ConvertConfig, and enhancing Base64 decoding across parsers. Update routes and frontend to support new configuration format.
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
P "github.com/bestnite/sub2clash/model/proxy"
|
||||
"github.com/bestnite/sub2clash/utils"
|
||||
)
|
||||
|
||||
func hasPrefix(proxy string, prefixes []string) bool {
|
||||
@@ -49,7 +49,7 @@ func isLikelyBase64(s string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
decoded, err := DecodeBase64(s)
|
||||
decoded, err := utils.DecodeBase64(s, true)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@@ -60,23 +60,6 @@ func isLikelyBase64(s string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func DecodeBase64(s string) (string, error) {
|
||||
s = strings.TrimSpace(s)
|
||||
|
||||
if strings.Contains(s, "-") || strings.Contains(s, "_") {
|
||||
s = strings.ReplaceAll(s, "-", "+")
|
||||
s = strings.ReplaceAll(s, "_", "/")
|
||||
}
|
||||
if len(s)%4 != 0 {
|
||||
s += strings.Repeat("=", 4-len(s)%4)
|
||||
}
|
||||
decodeStr, err := base64.StdEncoding.DecodeString(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(decodeStr), nil
|
||||
}
|
||||
|
||||
type ParseConfig struct {
|
||||
UseUDP bool
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
P "github.com/bestnite/sub2clash/model/proxy"
|
||||
"github.com/bestnite/sub2clash/utils"
|
||||
)
|
||||
|
||||
// ShadowsocksParser Shadowsocks协议解析器
|
||||
@@ -43,7 +44,7 @@ func (p *ShadowsocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, er
|
||||
break
|
||||
}
|
||||
}
|
||||
d, err := DecodeBase64(s[0])
|
||||
d, err := utils.DecodeBase64(s[0], true)
|
||||
if err != nil {
|
||||
return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
|
||||
}
|
||||
@@ -76,7 +77,7 @@ func (p *ShadowsocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, er
|
||||
password, hasPassword := link.User.Password()
|
||||
|
||||
if !hasPassword && isLikelyBase64(method) {
|
||||
decodedStr, err := DecodeBase64(method)
|
||||
decodedStr, err := utils.DecodeBase64(method, true)
|
||||
if err == nil {
|
||||
methodAndPass := strings.SplitN(decodedStr, ":", 2)
|
||||
if len(methodAndPass) == 2 {
|
||||
@@ -88,7 +89,7 @@ func (p *ShadowsocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, er
|
||||
}
|
||||
}
|
||||
if password != "" && isLikelyBase64(password) {
|
||||
password, err = DecodeBase64(password)
|
||||
password, err = utils.DecodeBase64(password, true)
|
||||
if err != nil {
|
||||
return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
|
||||
P "github.com/bestnite/sub2clash/model/proxy"
|
||||
"github.com/bestnite/sub2clash/utils"
|
||||
)
|
||||
|
||||
type ShadowsocksRParser struct{}
|
||||
@@ -39,7 +40,7 @@ func (p *ShadowsocksRParser) Parse(config ParseConfig, proxy string) (P.Proxy, e
|
||||
}
|
||||
}
|
||||
|
||||
proxy, err := DecodeBase64(proxy)
|
||||
proxy, err := utils.DecodeBase64(proxy, true)
|
||||
if err != nil {
|
||||
return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidBase64, err.Error())
|
||||
}
|
||||
@@ -55,7 +56,7 @@ func (p *ShadowsocksRParser) Parse(config ParseConfig, proxy string) (P.Proxy, e
|
||||
protocol := parts[2]
|
||||
method := parts[3]
|
||||
obfs := parts[4]
|
||||
password, err := DecodeBase64(parts[5])
|
||||
password, err := utils.DecodeBase64(parts[5], true)
|
||||
if err != nil {
|
||||
return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidStruct, err.Error())
|
||||
}
|
||||
@@ -73,13 +74,13 @@ func (p *ShadowsocksRParser) Parse(config ParseConfig, proxy string) (P.Proxy, e
|
||||
return P.Proxy{}, fmt.Errorf("%w: %s", ErrCannotParseParams, err.Error())
|
||||
}
|
||||
if params.Get("obfsparam") != "" {
|
||||
obfsParam, err = DecodeBase64(params.Get("obfsparam"))
|
||||
obfsParam, err = utils.DecodeBase64(params.Get("obfsparam"), true)
|
||||
}
|
||||
if params.Get("protoparam") != "" {
|
||||
protoParam, err = DecodeBase64(params.Get("protoparam"))
|
||||
protoParam, err = utils.DecodeBase64(params.Get("protoparam"), true)
|
||||
}
|
||||
if params.Get("remarks") != "" {
|
||||
remarks, err = DecodeBase64(params.Get("remarks"))
|
||||
remarks, err = utils.DecodeBase64(params.Get("remarks"), true)
|
||||
} else {
|
||||
remarks = server + ":" + strconv.Itoa(port)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
P "github.com/bestnite/sub2clash/model/proxy"
|
||||
"github.com/bestnite/sub2clash/utils"
|
||||
)
|
||||
|
||||
type SocksParser struct{}
|
||||
@@ -59,7 +60,7 @@ func (p *SocksParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
|
||||
password, hasPassword := link.User.Password()
|
||||
|
||||
if !hasPassword && isLikelyBase64(username) {
|
||||
decodedStr, err := DecodeBase64(username)
|
||||
decodedStr, err := utils.DecodeBase64(username, true)
|
||||
if err == nil {
|
||||
usernameAndPassword := strings.SplitN(decodedStr, ":", 2)
|
||||
if len(usernameAndPassword) == 2 {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
|
||||
P "github.com/bestnite/sub2clash/model/proxy"
|
||||
"github.com/bestnite/sub2clash/utils"
|
||||
)
|
||||
|
||||
type VmessJson struct {
|
||||
@@ -57,7 +58,7 @@ func (p *VmessParser) Parse(config ParseConfig, proxy string) (P.Proxy, error) {
|
||||
break
|
||||
}
|
||||
}
|
||||
base64, err := DecodeBase64(proxy)
|
||||
base64, err := utils.DecodeBase64(proxy, true)
|
||||
if err != nil {
|
||||
return P.Proxy{}, fmt.Errorf("%w: %s", ErrInvalidBase64, err.Error())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user