1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-24 13:04:41 -05:00
sub2clash/parser/hysteria.go

87 lines
2.0 KiB
Go
Raw Normal View History

2024-03-09 04:01:52 -05:00
package parser
import (
"net/url"
"strconv"
"strings"
2024-04-23 02:39:16 -04:00
"sub2clash/constant"
2024-03-09 04:01:52 -05:00
"sub2clash/model"
)
func ParseHysteria(proxy string) (model.Proxy, error) {
2024-04-23 02:39:16 -04:00
if !strings.HasPrefix(proxy, constant.HysteriaPrefix) {
return model.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
}
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,
}
2024-03-09 04:01:52 -05:00
}
2024-04-23 02:39:16 -04:00
serverInfo := strings.SplitN(urlParts[0], ":", 2)
2024-03-09 04:01:52 -05:00
if len(serverInfo) != 2 {
2024-04-23 02:39:16 -04:00
return model.Proxy{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing server host or port",
Raw: proxy,
}
2024-03-09 04:01:52 -05:00
}
2024-04-23 02:39:16 -04:00
server, portStr := serverInfo[0], serverInfo[1]
port, err := ParsePort(portStr)
2024-03-09 04:01:52 -05:00
if err != nil {
2024-04-23 02:39:16 -04:00
return model.Proxy{}, &ParseError{
Type: ErrInvalidPort,
Message: err.Error(),
Raw: proxy,
}
2024-03-09 04:01:52 -05:00
}
2024-04-23 02:39:16 -04:00
params, err := url.ParseQuery(urlParts[1])
2024-03-09 04:01:52 -05:00
if err != nil {
2024-04-23 02:39:16 -04:00
return model.Proxy{}, &ParseError{
Type: ErrCannotParseParams,
Raw: proxy,
Message: err.Error(),
}
2024-03-09 04:01:52 -05:00
}
2024-04-23 02:39:16 -04:00
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, ",")
2024-03-09 04:01:52 -05:00
}
2024-04-23 02:39:16 -04:00
remarks := server + ":" + portStr
if params.Get("remarks") != "" {
remarks = params.Get("remarks")
}
2024-03-09 04:01:52 -05:00
result := model.Proxy{
Type: "hysteria",
Name: remarks,
2024-04-23 02:39:16 -04:00
Server: server,
2024-03-09 04:01:52 -05:00
Port: port,
Up: upmbps,
Down: downmbps,
Auth: auth,
Obfs: obfs,
SkipCertVerify: insecure == "1",
2024-04-23 02:39:16 -04:00
Alpn: alpn,
2024-03-09 04:01:52 -05:00
Protocol: protocol,
2024-04-23 02:39:16 -04:00
AllowInsecure: insecureBool,
2024-03-09 04:01:52 -05:00
}
return result, nil
}