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

99 lines
2.5 KiB
Go
Raw Normal View History

2024-03-10 15:13:42 -04:00
package parser
import (
"net/url"
"strings"
2024-03-22 04:10:15 -04:00
"sub2sing-box/constant"
"sub2sing-box/model"
2024-03-10 15:13:42 -04:00
)
func ParseHysteria2(proxy string) (model.Outbound, error) {
2024-03-22 04:10:15 -04:00
if !strings.HasPrefix(proxy, constant.Hysteria2Prefix1) &&
!strings.HasPrefix(proxy, constant.Hysteria2Prefix2) {
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
}
proxy = strings.TrimPrefix(proxy, constant.Hysteria2Prefix1)
proxy = strings.TrimPrefix(proxy, constant.Hysteria2Prefix2)
urlParts := strings.SplitN(proxy, "@", 2)
if len(urlParts) != 2 {
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing character '@' in url",
Raw: proxy,
}
}
password := urlParts[0]
serverInfo := strings.SplitN(urlParts[1], "/?", 2)
if len(serverInfo) != 2 {
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing params in url",
Raw: proxy,
}
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
paramStr := serverInfo[1]
2024-03-10 15:13:42 -04:00
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
2024-03-22 04:10:15 -04:00
var server string
var portStr string
2024-03-10 15:13:42 -04:00
if len(serverAndPort) == 1 {
2024-03-22 04:10:15 -04:00
portStr = "443"
} else if len(serverAndPort) == 2 {
server, portStr = serverAndPort[0], serverAndPort[1]
} else {
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
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(paramStr)
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
remarks, network, obfs, obfsPassword, pinSHA256, insecure, sni := params.Get("name"), params.Get("network"), params.Get("obfs"), params.Get("obfs-password"), params.Get("pinSHA256"), params.Get("insecure"), params.Get("sni")
enableTLS := pinSHA256 != ""
insecureBool := insecure == "1"
result := model.Outbound{
2024-03-10 15:13:42 -04:00
Type: "hysteria2",
2024-03-11 09:00:13 -04:00
Tag: remarks,
Hysteria2Options: model.Hysteria2OutboundOptions{
ServerOptions: model.ServerOptions{
Server: server,
2024-04-23 02:41:14 -04:00
ServerPort: port,
},
Password: password,
Obfs: &model.Hysteria2Obfs{
2024-03-22 04:10:15 -04:00
Type: obfs,
Password: obfsPassword,
2024-03-10 15:13:42 -04:00
},
OutboundTLSOptionsContainer: model.OutboundTLSOptionsContainer{
2024-03-22 04:10:15 -04:00
TLS: &model.OutboundTLSOptions{Enabled: enableTLS,
Insecure: insecureBool,
ServerName: sni,
Certificate: []string{pinSHA256}},
2024-03-10 15:13:42 -04:00
},
Network: network,
},
}
return result, nil
}