2024-03-10 15:13:42 -04:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-03-22 04:10:15 -04:00
|
|
|
"sub2sing-box/constant"
|
2024-03-20 12:02:38 -04:00
|
|
|
"sub2sing-box/model"
|
2024-03-10 15:13:42 -04:00
|
|
|
)
|
|
|
|
|
2024-03-20 12:02:38 -04:00
|
|
|
func ParseHysteria(proxy string) (model.Outbound, error) {
|
2024-03-22 04:10:15 -04:00
|
|
|
if !strings.HasPrefix(proxy, constant.HysteriaPrefix) {
|
|
|
|
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy = strings.TrimPrefix(proxy, constant.HysteriaPrefix)
|
|
|
|
urlParts := strings.SplitN(proxy, "?", 2)
|
|
|
|
if len(urlParts) != 2 {
|
|
|
|
return model.Outbound{}, &ParseError{
|
|
|
|
Type: ErrInvalidStruct,
|
|
|
|
Message: "missing character '?' in url",
|
|
|
|
Raw: proxy,
|
|
|
|
}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
|
|
|
|
serverInfo := strings.SplitN(urlParts[0], ":", 2)
|
2024-03-10 15:13:42 -04:00
|
|
|
if len(serverInfo) != 2 {
|
2024-03-22 04:10:15 -04:00
|
|
|
return model.Outbound{}, &ParseError{
|
|
|
|
Type: ErrInvalidStruct,
|
|
|
|
Message: "missing server host or port",
|
|
|
|
Raw: proxy,
|
|
|
|
}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
server, portStr := serverInfo[0], serverInfo[1]
|
|
|
|
|
|
|
|
port, err := ParsePort(portStr)
|
2024-03-10 15:13:42 -04:00
|
|
|
if err != nil {
|
2024-04-23 02:41:14 -04:00
|
|
|
return model.Outbound{}, &ParseError{
|
|
|
|
Type: ErrInvalidPort,
|
|
|
|
Message: err.Error(),
|
|
|
|
Raw: proxy,
|
|
|
|
}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
|
|
|
|
params, err := url.ParseQuery(urlParts[1])
|
2024-03-10 15:13:42 -04:00
|
|
|
if err != nil {
|
2024-03-22 04:10:15 -04:00
|
|
|
return model.Outbound{}, &ParseError{
|
|
|
|
Type: ErrCannotParseParams,
|
|
|
|
Raw: proxy,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -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")
|
2024-03-10 15:13:42 -04:00
|
|
|
insecureBool, err := strconv.ParseBool(insecure)
|
|
|
|
if err != nil {
|
2024-03-22 04:10:15 -04:00
|
|
|
insecureBool = false
|
|
|
|
}
|
|
|
|
|
|
|
|
var alpn []string
|
|
|
|
alpnStr = strings.TrimSpace(alpnStr)
|
|
|
|
if alpnStr != "" {
|
|
|
|
alpn = strings.Split(alpnStr, ",")
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
2024-03-22 04:10:15 -04:00
|
|
|
|
|
|
|
remarks := server + ":" + portStr
|
|
|
|
if params.Get("remarks") != "" {
|
|
|
|
remarks = params.Get("remarks")
|
|
|
|
}
|
|
|
|
|
|
|
|
return model.Outbound{
|
2024-03-10 15:13:42 -04:00
|
|
|
Type: "hysteria",
|
2024-03-11 09:00:13 -04:00
|
|
|
Tag: remarks,
|
2024-03-20 12:02:38 -04:00
|
|
|
HysteriaOptions: model.HysteriaOutboundOptions{
|
|
|
|
ServerOptions: model.ServerOptions{
|
2024-03-22 04:10:15 -04:00
|
|
|
Server: server,
|
|
|
|
ServerPort: port,
|
2024-03-20 12:02:38 -04:00
|
|
|
},
|
|
|
|
Up: upmbps,
|
|
|
|
Down: downmbps,
|
|
|
|
Auth: []byte(auth),
|
|
|
|
Obfs: obfs,
|
|
|
|
Network: protocol,
|
|
|
|
OutboundTLSOptionsContainer: model.OutboundTLSOptionsContainer{
|
|
|
|
TLS: &model.OutboundTLSOptions{
|
|
|
|
Enabled: true,
|
|
|
|
Insecure: insecureBool,
|
|
|
|
ALPN: alpn,
|
|
|
|
},
|
2024-03-10 15:13:42 -04:00
|
|
|
},
|
|
|
|
},
|
2024-03-22 04:10:15 -04:00
|
|
|
}, nil
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|