1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-24 11:44:42 -05:00
sub2sing-box/parser/shadowsocks.go

96 lines
2.3 KiB
Go
Raw Normal View History

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"
"sub2sing-box/model"
2024-03-20 08:54:23 -04:00
"sub2sing-box/util"
2024-03-10 15:13:42 -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
result := model.Outbound{
2024-03-10 15:13:42 -04:00
Type: "shadowsocks",
2024-03-11 09:00:13 -04:00
Tag: remarks,
ShadowsocksOptions: model.ShadowsocksOutboundOptions{
ServerOptions: model.ServerOptions{
Server: server,
2024-03-22 04:10:15 -04:00
ServerPort: port,
},
Method: method,
Password: password,
2024-03-10 15:13:42 -04:00
},
}
return result, nil
}