2023-09-12 06:40:24 -04:00
|
|
|
|
package parser
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2023-09-12 12:46:17 -04:00
|
|
|
|
"sub2clash/model"
|
2023-09-12 06:40:24 -04:00
|
|
|
|
)
|
|
|
|
|
|
2023-09-13 01:47:22 -04:00
|
|
|
|
// ParseSS 解析 SS(Shadowsocks)Url
|
2023-09-12 06:40:24 -04:00
|
|
|
|
func ParseSS(proxy string) (model.Proxy, error) {
|
|
|
|
|
// 判断是否以 ss:// 开头
|
|
|
|
|
if !strings.HasPrefix(proxy, "ss://") {
|
2023-09-20 21:08:02 -04:00
|
|
|
|
return model.Proxy{}, fmt.Errorf("invalid ss Url")
|
2023-09-12 06:40:24 -04:00
|
|
|
|
}
|
|
|
|
|
// 分割
|
|
|
|
|
parts := strings.SplitN(strings.TrimPrefix(proxy, "ss://"), "@", 2)
|
|
|
|
|
if len(parts) != 2 {
|
2023-09-20 21:08:02 -04:00
|
|
|
|
return model.Proxy{}, fmt.Errorf("invalid ss Url")
|
2023-09-12 06:40:24 -04:00
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(parts[0], ":") {
|
|
|
|
|
// 解码
|
|
|
|
|
decoded, err := DecodeBase64(parts[0])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return model.Proxy{}, err
|
|
|
|
|
}
|
|
|
|
|
parts[0] = decoded
|
|
|
|
|
}
|
|
|
|
|
credentials := strings.SplitN(parts[0], ":", 2)
|
|
|
|
|
if len(credentials) != 2 {
|
2023-09-20 21:08:02 -04:00
|
|
|
|
return model.Proxy{}, fmt.Errorf("invalid ss Url")
|
2023-09-12 06:40:24 -04:00
|
|
|
|
}
|
|
|
|
|
// 分割
|
|
|
|
|
serverInfo := strings.SplitN(parts[1], "#", 2)
|
|
|
|
|
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
|
|
|
|
|
if len(serverAndPort) != 2 {
|
2023-09-20 21:08:02 -04:00
|
|
|
|
return model.Proxy{}, fmt.Errorf("invalid ss Url")
|
2023-09-12 06:40:24 -04:00
|
|
|
|
}
|
|
|
|
|
// 转换端口字符串为数字
|
|
|
|
|
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return model.Proxy{}, err
|
|
|
|
|
}
|
|
|
|
|
// 返回结果
|
|
|
|
|
result := model.Proxy{
|
|
|
|
|
Type: "ss",
|
|
|
|
|
Cipher: strings.TrimSpace(credentials[0]),
|
|
|
|
|
Password: strings.TrimSpace(credentials[1]),
|
|
|
|
|
Server: strings.TrimSpace(serverAndPort[0]),
|
|
|
|
|
Port: port,
|
|
|
|
|
UDP: true,
|
|
|
|
|
Name: serverAndPort[0],
|
|
|
|
|
}
|
|
|
|
|
// 如果有节点名称
|
|
|
|
|
if len(serverInfo) == 2 {
|
|
|
|
|
unescape, err := url.QueryUnescape(serverInfo[1])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return model.Proxy{}, err
|
|
|
|
|
}
|
|
|
|
|
result.Name = strings.TrimSpace(unescape)
|
|
|
|
|
} else {
|
|
|
|
|
result.Name = strings.TrimSpace(serverAndPort[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|