2024-03-10 15:13:42 -04:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2024-03-22 04:10:15 -04:00
|
|
|
"sub2sing-box/constant"
|
2024-03-20 12:02:38 -04:00
|
|
|
"sub2sing-box/model"
|
2024-03-20 08:54:23 -04:00
|
|
|
"sub2sing-box/util"
|
2024-03-10 15:13:42 -04:00
|
|
|
)
|
|
|
|
|
2024-03-20 12:02:38 -04:00
|
|
|
func ParseShadowsocks(proxy string) (model.Outbound, error) {
|
2024-03-22 04:10:15 -04:00
|
|
|
if !strings.HasPrefix(proxy, constant.ShadowsocksPrefix) {
|
|
|
|
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
|
|
|
|
var serverAndPort []string
|
|
|
|
if !strings.Contains(urlParts[0], ":") {
|
|
|
|
decoded, err := util.DecodeBase64(urlParts[0])
|
2024-03-10 15:13:42 -04:00
|
|
|
if err != nil {
|
2024-03-22 04:10:15 -04:00
|
|
|
return model.Outbound{}, &ParseError{
|
|
|
|
Type: ErrInvalidStruct,
|
|
|
|
Message: "invalid base64 encoded",
|
|
|
|
Raw: proxy,
|
|
|
|
}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
urlParts[0] = decoded
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
credentials := strings.SplitN(urlParts[0], ":", 2)
|
2024-03-10 15:13:42 -04:00
|
|
|
if len(credentials) != 2 {
|
2024-03-22 04:10:15 -04:00
|
|
|
return model.Outbound{}, &ParseError{
|
|
|
|
Type: ErrInvalidStruct,
|
|
|
|
Message: "missing server host or port",
|
|
|
|
Raw: proxy,
|
|
|
|
}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
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,
|
|
|
|
}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
port, err := ParsePort(portStr)
|
2024-03-10 15:13:42 -04:00
|
|
|
if err != nil {
|
2024-04-23 02:41:14 -04:00
|
|
|
return model.Outbound{}, &ParseError{
|
|
|
|
Type: ErrInvalidPort,
|
|
|
|
Message: err.Error(),
|
|
|
|
Raw: proxy,
|
|
|
|
}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
|
|
|
|
var remarks string
|
2024-03-10 15:13:42 -04:00
|
|
|
if len(serverInfo) == 2 {
|
|
|
|
unescape, err := url.QueryUnescape(serverInfo[1])
|
|
|
|
if err != nil {
|
2024-03-22 04:10:15 -04:00
|
|
|
return model.Outbound{}, &ParseError{
|
|
|
|
Type: ErrInvalidStruct,
|
|
|
|
Message: "cannot unescape remarks",
|
|
|
|
Raw: proxy,
|
|
|
|
}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
remarks = strings.TrimSpace(unescape)
|
|
|
|
} else {
|
2024-03-22 04:10:15 -04:00
|
|
|
remarks = strings.TrimSpace(server + ":" + portStr)
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
|
2024-03-20 12:02:38 -04:00
|
|
|
result := model.Outbound{
|
2024-03-10 15:13:42 -04:00
|
|
|
Type: "shadowsocks",
|
2024-03-11 09:00:13 -04:00
|
|
|
Tag: remarks,
|
2024-03-20 12:02:38 -04:00
|
|
|
ShadowsocksOptions: model.ShadowsocksOutboundOptions{
|
|
|
|
ServerOptions: model.ServerOptions{
|
|
|
|
Server: server,
|
2024-03-22 04:10:15 -04:00
|
|
|
ServerPort: port,
|
2024-03-20 12:02:38 -04:00
|
|
|
},
|
|
|
|
Method: method,
|
|
|
|
Password: password,
|
2024-03-10 15:13:42 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|