1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-24 11:14:41 -05:00
sub2sing-box/parser/hysteria.go

94 lines
2.2 KiB
Go
Raw Normal View History

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"
"sub2sing-box/model"
2024-03-10 15:13:42 -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,
HysteriaOptions: model.HysteriaOutboundOptions{
ServerOptions: model.ServerOptions{
2024-03-22 04:10:15 -04:00
Server: server,
ServerPort: port,
},
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
}