mirror of
https://github.com/bestnite/sub2clash.git
synced 2025-06-17 12:43:18 +08:00
♻️ Refactor parsers
This commit is contained in:
@ -1,79 +1,86 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sub2clash/constant"
|
||||
"sub2clash/model"
|
||||
)
|
||||
|
||||
//hysteria://host:port?protocol=udp&auth=123456&peer=sni.domain&insecure=1&upmbps=100&downmbps=100&alpn=hysteria&obfs=xplus&obfsParam=123456#remarks
|
||||
//
|
||||
//- host: hostname or IP address of the server to connect to (required)
|
||||
//- port: port of the server to connect to (required)
|
||||
//- protocol: protocol to use ("udp", "wechat-video", "faketcp") (optional, default: "udp")
|
||||
//- auth: authentication payload (string) (optional)
|
||||
//- peer: SNI for TLS (optional)
|
||||
//- insecure: ignore certificate errors (optional)
|
||||
//- upmbps: upstream bandwidth in Mbps (required)
|
||||
//- downmbps: downstream bandwidth in Mbps (required)
|
||||
//- alpn: QUIC ALPN (optional)
|
||||
//- obfs: Obfuscation mode (optional, empty or "xplus")
|
||||
//- obfsParam: Obfuscation password (optional)
|
||||
//- remarks: remarks (optional)
|
||||
|
||||
func ParseHysteria(proxy string) (model.Proxy, error) {
|
||||
// 判断是否以 hysteria:// 开头
|
||||
if !strings.HasPrefix(proxy, "hysteria://") {
|
||||
return model.Proxy{}, errors.New("invalid hysteria Url")
|
||||
if !strings.HasPrefix(proxy, constant.HysteriaPrefix) {
|
||||
return model.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||
}
|
||||
// 分割
|
||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "hysteria://"), "?", 2)
|
||||
serverInfo := strings.SplitN(parts[0], ":", 2)
|
||||
|
||||
proxy = strings.TrimPrefix(proxy, constant.HysteriaPrefix)
|
||||
urlParts := strings.SplitN(proxy, "?", 2)
|
||||
if len(urlParts) != 2 {
|
||||
return model.Proxy{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing character '?' in url",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
|
||||
serverInfo := strings.SplitN(urlParts[0], ":", 2)
|
||||
if len(serverInfo) != 2 {
|
||||
return model.Proxy{}, errors.New("invalid hysteria Url")
|
||||
return model.Proxy{}, &ParseError{
|
||||
Type: ErrInvalidStruct,
|
||||
Message: "missing server host or port",
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
params, err := url.ParseQuery(parts[1])
|
||||
server, portStr := serverInfo[0], serverInfo[1]
|
||||
|
||||
port, err := ParsePort(portStr)
|
||||
if err != nil {
|
||||
return model.Proxy{}, errors.New("invalid hysteria Url")
|
||||
return model.Proxy{}, &ParseError{
|
||||
Type: ErrInvalidPort,
|
||||
Message: err.Error(),
|
||||
Raw: proxy,
|
||||
}
|
||||
}
|
||||
host := serverInfo[0]
|
||||
port, err := strconv.Atoi(serverInfo[1])
|
||||
|
||||
params, err := url.ParseQuery(urlParts[1])
|
||||
if err != nil {
|
||||
return model.Proxy{}, errors.New("invalid hysteria Url")
|
||||
return model.Proxy{}, &ParseError{
|
||||
Type: ErrCannotParseParams,
|
||||
Raw: proxy,
|
||||
Message: err.Error(),
|
||||
}
|
||||
}
|
||||
protocol := params.Get("protocol")
|
||||
auth := params.Get("auth")
|
||||
peer := params.Get("peer")
|
||||
insecure := params.Get("insecure")
|
||||
upmbps := params.Get("upmbps")
|
||||
downmbps := params.Get("downmbps")
|
||||
alpn := params.Get("alpn")
|
||||
obfs := params.Get("obfs")
|
||||
obfsParam := params.Get("obfsParam")
|
||||
remarks := ""
|
||||
if strings.Contains(parts[1], "#") {
|
||||
r := strings.Split(parts[1], "#")
|
||||
remarks = r[len(r)-1]
|
||||
} else {
|
||||
remarks = serverInfo[0] + ":" + serverInfo[1]
|
||||
|
||||
protocol, auth, insecure, upmbps, downmbps, obfs, alpnStr := params.Get("protocol"), params.Get("auth"), params.Get("insecure"), params.Get("upmbps"), params.Get("downmbps"), params.Get("obfs"), params.Get("alpn")
|
||||
insecureBool, err := strconv.ParseBool(insecure)
|
||||
if err != nil {
|
||||
insecureBool = false
|
||||
}
|
||||
// 返回结果
|
||||
|
||||
var alpn []string
|
||||
alpnStr = strings.TrimSpace(alpnStr)
|
||||
if alpnStr != "" {
|
||||
alpn = strings.Split(alpnStr, ",")
|
||||
}
|
||||
|
||||
remarks := server + ":" + portStr
|
||||
if params.Get("remarks") != "" {
|
||||
remarks = params.Get("remarks")
|
||||
}
|
||||
|
||||
result := model.Proxy{
|
||||
Type: "hysteria",
|
||||
Name: remarks,
|
||||
Server: host,
|
||||
Server: server,
|
||||
Port: port,
|
||||
Up: upmbps,
|
||||
Down: downmbps,
|
||||
Auth: auth,
|
||||
Obfs: obfs,
|
||||
Sni: peer,
|
||||
SkipCertVerify: insecure == "1",
|
||||
Alpn: strings.Split(alpn, ","),
|
||||
ObfsParam: obfsParam,
|
||||
Alpn: alpn,
|
||||
Protocol: protocol,
|
||||
AllowInsecure: insecureBool,
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user