2024-03-10 15:13:42 -04:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
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-10 15:13:42 -04:00
|
|
|
if !strings.HasPrefix(proxy, "ss://") {
|
2024-03-20 12:02:38 -04:00
|
|
|
return model.Outbound{}, errors.New("invalid ss Url")
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
parts := strings.SplitN(strings.TrimPrefix(proxy, "ss://"), "@", 2)
|
|
|
|
if len(parts) != 2 {
|
2024-03-20 12:02:38 -04:00
|
|
|
return model.Outbound{}, errors.New("invalid ss Url")
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
if !strings.Contains(parts[0], ":") {
|
2024-03-19 05:01:53 -04:00
|
|
|
decoded, err := util.DecodeBase64(parts[0])
|
2024-03-10 15:13:42 -04:00
|
|
|
if err != nil {
|
2024-03-20 12:02:38 -04:00
|
|
|
return model.Outbound{}, errors.New("invalid ss Url" + err.Error())
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
parts[0] = decoded
|
|
|
|
}
|
|
|
|
credentials := strings.SplitN(parts[0], ":", 2)
|
|
|
|
if len(credentials) != 2 {
|
2024-03-20 12:02:38 -04:00
|
|
|
return model.Outbound{}, errors.New("invalid ss Url")
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
serverInfo := strings.SplitN(parts[1], "#", 2)
|
|
|
|
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
|
|
|
|
if len(serverAndPort) != 2 {
|
2024-03-20 12:02:38 -04:00
|
|
|
return model.Outbound{}, errors.New("invalid ss Url")
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
|
|
|
|
if err != nil {
|
2024-03-20 12:02:38 -04:00
|
|
|
return model.Outbound{}, errors.New("invalid ss Url" + err.Error())
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
remarks := ""
|
|
|
|
if len(serverInfo) == 2 {
|
|
|
|
unescape, err := url.QueryUnescape(serverInfo[1])
|
|
|
|
if err != nil {
|
2024-03-20 12:02:38 -04:00
|
|
|
return model.Outbound{}, errors.New("invalid ss Url" + err.Error())
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
remarks = strings.TrimSpace(unescape)
|
|
|
|
} else {
|
|
|
|
remarks = strings.TrimSpace(serverAndPort[0])
|
|
|
|
}
|
|
|
|
method := credentials[0]
|
|
|
|
password := credentials[1]
|
|
|
|
server := strings.TrimSpace(serverAndPort[0])
|
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,
|
|
|
|
ServerPort: uint16(port),
|
|
|
|
},
|
|
|
|
Method: method,
|
|
|
|
Password: password,
|
2024-03-10 15:13:42 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|