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

100 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
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]
serverInfoAndTag := strings.SplitN(urlParts[1], "#", 2)
serverAndPort := serverInfoAndTag[0]
lastColonIndex := strings.LastIndex(serverAndPort, ":")
if lastColonIndex == -1 {
2024-03-22 04:10:15 -04:00
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing port in address",
2024-03-22 04:10:15 -04:00
Raw: proxy,
}
2024-03-10 15:13:42 -04:00
}
server := serverAndPort[:lastColonIndex]
portStr := serverAndPort[lastColonIndex+1:]
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
if len(serverInfoAndTag) == 2 {
unescape, err := url.QueryUnescape(serverInfoAndTag[1])
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: "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
}