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

95 lines
2.3 KiB
Go
Raw Normal View History

2024-03-10 15:13:42 -04:00
package parser
import (
2024-08-04 05:01:09 -04:00
"fmt"
2024-03-10 15:13:42 -04:00
"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}
}
2024-08-04 05:01:09 -04:00
link, err := url.Parse(proxy)
if err != nil {
2024-03-22 04:10:15 -04:00
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
2024-08-04 05:01:09 -04:00
Message: "url parse error",
2024-03-22 04:10:15 -04:00
Raw: proxy,
}
}
2024-08-04 05:01:09 -04:00
username := link.User.Username()
password, exist := link.User.Password()
if !exist {
password = username
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
2024-08-04 05:01:09 -04:00
query := link.Query()
server := link.Hostname()
if server == "" {
2024-03-22 04:10:15 -04:00
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
2024-08-04 05:01:09 -04:00
Message: "missing server host",
2024-03-22 04:10:15 -04:00
Raw: proxy,
}
2024-03-10 15:13:42 -04:00
}
2024-08-04 05:01:09 -04:00
portStr := link.Port()
if portStr == "" {
2024-04-23 02:41:14 -04:00
return model.Outbound{}, &ParseError{
2024-08-04 05:01:09 -04:00
Type: ErrInvalidStruct,
Message: "missing server port",
2024-04-23 02:41:14 -04:00
Raw: proxy,
}
2024-03-10 15:13:42 -04:00
}
2024-08-04 05:01:09 -04:00
port, err := ParsePort(portStr)
2024-03-10 15:13:42 -04:00
if err != nil {
2024-03-22 04:10:15 -04:00
return model.Outbound{}, &ParseError{
2024-08-04 05:01:09 -04:00
Type: ErrInvalidPort,
Raw: portStr,
2024-03-22 04:10:15 -04:00
}
2024-03-10 15:13:42 -04:00
}
2024-08-04 05:01:09 -04:00
network, obfs, obfsPassword, pinSHA256, insecure, sni := query.Get("network"), query.Get("obfs"), query.Get("obfs-password"), query.Get("pinSHA256"), query.Get("insecure"), query.Get("sni")
enableTLS := pinSHA256 != "" || sni != ""
2024-03-22 04:10:15 -04:00
insecureBool := insecure == "1"
2024-08-04 05:01:09 -04:00
remarks := link.Fragment
if remarks == "" {
remarks = fmt.Sprintf("%s:%s", server, portStr)
}
remarks = strings.TrimSpace(remarks)
2024-03-22 04:10:15 -04:00
result := model.Outbound{
2024-03-10 15:13:42 -04:00
Type: "hysteria2",
2024-08-04 05:01:09 -04:00
Tag: strings.TrimSpace(remarks),
Hysteria2Options: model.Hysteria2OutboundOptions{
ServerOptions: model.ServerOptions{
Server: server,
2024-04-23 02:41:14 -04:00
ServerPort: port,
},
Password: password,
OutboundTLSOptionsContainer: model.OutboundTLSOptionsContainer{
TLS: &model.OutboundTLSOptions{
Enabled: enableTLS,
Insecure: insecureBool,
ServerName: sni,
},
2024-03-10 15:13:42 -04:00
},
Network: network,
},
}
if pinSHA256 != "" {
result.Hysteria2Options.OutboundTLSOptionsContainer.TLS.Certificate = []string{pinSHA256}
}
if obfs != "" {
result.Hysteria2Options.Obfs = &model.Hysteria2Obfs{
Type: obfs,
Password: obfsPassword,
}
}
2024-03-10 15:13:42 -04:00
return result, nil
}