1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-25 01:14:42 -05:00
sub2clash/parser/ssr.go

68 lines
1.5 KiB
Go
Raw Normal View History

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
)
func ParseShadowsocksR(proxy string) (model.Proxy, error) {
// 判断是否以 ssr:// 开头
if !strings.HasPrefix(proxy, "ssr://") {
return model.Proxy{}, fmt.Errorf("invalid ssr Url")
2023-09-12 06:40:24 -04:00
}
var err error
proxy = strings.TrimPrefix(proxy, "ssr://")
2023-09-12 06:40:24 -04:00
if !strings.Contains(proxy, ":") {
proxy, err = DecodeBase64(strings.TrimPrefix(proxy, "ssr://"))
if err != nil {
return model.Proxy{}, err
}
}
// 分割
detailsAndParams := strings.SplitN(proxy, "/?", 2)
2023-09-12 06:40:24 -04:00
parts := strings.Split(detailsAndParams[0], ":")
params, err := url.ParseQuery(detailsAndParams[1])
if err != nil {
return model.Proxy{}, err
}
// 处理端口
port, err := strconv.Atoi(parts[1])
if err != nil {
return model.Proxy{}, err
}
var obfsParam string
var protoParam string
var remarks string
if params.Get("obfsparam") != "" {
obfsParam, err = DecodeBase64(params.Get("obfsparam"))
}
if params.Get("protoparam") != "" {
protoParam, err = DecodeBase64(params.Get("protoparam"))
}
if params.Get("remarks") != "" {
remarks, err = DecodeBase64(params.Get("remarks"))
}
2023-09-12 06:40:24 -04:00
result := model.Proxy{
Name: remarks,
2023-09-12 06:40:24 -04:00
Type: "ssr",
Server: parts[0],
Port: port,
Protocol: parts[2],
Cipher: parts[3],
Obfs: parts[4],
Password: parts[5],
ObfsParam: obfsParam,
ProtocolParam: protoParam,
2023-09-12 06:40:24 -04:00
}
if result.Name == "" {
result.Name = result.Server
}
2023-09-12 06:40:24 -04:00
return result, nil
}