mirror of
https://github.com/bestnite/sub2sing-box.git
synced 2025-10-28 09:33:57 +00:00
整理代码
This commit is contained in:
@@ -1,62 +1,87 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sub2sing-box/constant"
|
||||
"sub2sing-box/model"
|
||||
"sub2sing-box/util"
|
||||
)
|
||||
|
||||
func ParseShadowsocks(proxy string) (model.Outbound, error) {
|
||||
if !strings.HasPrefix(proxy, "ss://") {
|
||||
return model.Outbound{}, errors.New("invalid ss Url")
|
||||
if !strings.HasPrefix(proxy, constant.ShadowsocksPrefix) {
|
||||
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||
}
|
||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "ss://"), "@", 2)
|
||||
if len(parts) != 2 {
|
||||
return model.Outbound{}, errors.New("invalid ss Url")
|
||||
}
|
||||
if !strings.Contains(parts[0], ":") {
|
||||
decoded, err := util.DecodeBase64(parts[0])
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid ss Url" + err.Error())
|
||||
|
||||
proxy = strings.TrimPrefix(proxy, constant.ShadowsocksPrefix)
|
||||
urlParts := strings.SplitN(proxy, "@", 2)
|
||||
if len(urlParts) != 2 {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing character '@' in url",
|
||||
Raw: proxy,
|
||||
}
|
||||
parts[0] = decoded
|
||||
}
|
||||
credentials := strings.SplitN(parts[0], ":", 2)
|
||||
|
||||
var serverAndPort []string
|
||||
if !strings.Contains(urlParts[0], ":") {
|
||||
decoded, err := util.DecodeBase64(urlParts[0])
|
||||
if err != nil {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "invalid base64 encoded",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
urlParts[0] = decoded
|
||||
}
|
||||
credentials := strings.SplitN(urlParts[0], ":", 2)
|
||||
if len(credentials) != 2 {
|
||||
return model.Outbound{}, errors.New("invalid ss Url")
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing server host or port",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
serverInfo := strings.SplitN(parts[1], "#", 2)
|
||||
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
|
||||
if len(serverAndPort) != 2 {
|
||||
return model.Outbound{}, errors.New("invalid ss Url")
|
||||
method, password := credentials[0], credentials[1]
|
||||
|
||||
serverInfo := strings.SplitN(urlParts[1], "#", 2)
|
||||
serverAndPort = strings.SplitN(serverInfo[0], ":", 2)
|
||||
server, portStr := serverAndPort[0], serverAndPort[1]
|
||||
if len(serverInfo) != 2 {
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing server host or port",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
|
||||
port, err := ParsePort(portStr)
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid ss Url" + err.Error())
|
||||
return model.Outbound{}, err
|
||||
}
|
||||
remarks := ""
|
||||
|
||||
var remarks string
|
||||
if len(serverInfo) == 2 {
|
||||
unescape, err := url.QueryUnescape(serverInfo[1])
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid ss Url" + err.Error())
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "cannot unescape remarks",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
remarks = strings.TrimSpace(unescape)
|
||||
} else {
|
||||
remarks = strings.TrimSpace(serverAndPort[0])
|
||||
remarks = strings.TrimSpace(server + ":" + portStr)
|
||||
}
|
||||
method := credentials[0]
|
||||
password := credentials[1]
|
||||
server := strings.TrimSpace(serverAndPort[0])
|
||||
|
||||
result := model.Outbound{
|
||||
Type: "shadowsocks",
|
||||
Tag: remarks,
|
||||
ShadowsocksOptions: model.ShadowsocksOutboundOptions{
|
||||
ServerOptions: model.ServerOptions{
|
||||
Server: server,
|
||||
ServerPort: uint16(port),
|
||||
ServerPort: port,
|
||||
},
|
||||
Method: method,
|
||||
Password: password,
|
||||
|
||||
Reference in New Issue
Block a user