mirror of
https://github.com/bestnite/sub2sing-box.git
synced 2025-10-28 01:31:19 +00:00
整理代码
This commit is contained in:
@@ -1,39 +1,73 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sub2sing-box/constant"
|
||||
"sub2sing-box/model"
|
||||
)
|
||||
|
||||
// hysteria2://letmein@example.com/?insecure=1&obfs=salamander&obfs-password=gawrgura&pinSHA256=deadbeef&sni=real.example.com
|
||||
|
||||
func ParseHysteria2(proxy string) (model.Outbound, error) {
|
||||
if !strings.HasPrefix(proxy, "hysteria2://") && !strings.HasPrefix(proxy, "hy2://") {
|
||||
return model.Outbound{}, errors.New("invalid hysteria2 Url")
|
||||
if !strings.HasPrefix(proxy, constant.Hysteria2Prefix1) &&
|
||||
!strings.HasPrefix(proxy, constant.Hysteria2Prefix2) {
|
||||
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
||||
}
|
||||
parts := strings.SplitN(strings.TrimPrefix(proxy, "hysteria2://"), "@", 2)
|
||||
serverInfo := strings.SplitN(parts[1], "/?", 2)
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
paramStr := serverInfo[1]
|
||||
|
||||
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
|
||||
var server string
|
||||
var portStr string
|
||||
if len(serverAndPort) == 1 {
|
||||
serverAndPort = append(serverAndPort, "443")
|
||||
} else if len(serverAndPort) != 2 {
|
||||
return model.Outbound{}, errors.New("invalid hysteria2 Url")
|
||||
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,
|
||||
}
|
||||
}
|
||||
params, err := url.ParseQuery(serverInfo[1])
|
||||
|
||||
port, err := ParsePort(portStr)
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid hysteria2 Url")
|
||||
return model.Outbound{}, err
|
||||
}
|
||||
port, err := strconv.Atoi(serverAndPort[1])
|
||||
|
||||
params, err := url.ParseQuery(paramStr)
|
||||
if err != nil {
|
||||
return model.Outbound{}, errors.New("invalid hysteria2 Url")
|
||||
return model.Outbound{}, &ParseError{
|
||||
Type: ErrCannotParseParams,
|
||||
Raw: proxy,
|
||||
Message: err.Error(),
|
||||
}
|
||||
}
|
||||
remarks := params.Get("name")
|
||||
server := serverAndPort[0]
|
||||
password := parts[0]
|
||||
network := params.Get("network")
|
||||
|
||||
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{
|
||||
Type: "hysteria2",
|
||||
Tag: remarks,
|
||||
@@ -44,14 +78,14 @@ func ParseHysteria2(proxy string) (model.Outbound, error) {
|
||||
},
|
||||
Password: password,
|
||||
Obfs: &model.Hysteria2Obfs{
|
||||
Type: params.Get("obfs"),
|
||||
Password: params.Get("obfs-password"),
|
||||
Type: obfs,
|
||||
Password: obfsPassword,
|
||||
},
|
||||
OutboundTLSOptionsContainer: model.OutboundTLSOptionsContainer{
|
||||
TLS: &model.OutboundTLSOptions{Enabled: params.Get("pinSHA256") != "",
|
||||
Insecure: params.Get("insecure") == "1",
|
||||
ServerName: params.Get("sni"),
|
||||
Certificate: []string{params.Get("pinSHA256")}},
|
||||
TLS: &model.OutboundTLSOptions{Enabled: enableTLS,
|
||||
Insecure: insecureBool,
|
||||
ServerName: sni,
|
||||
Certificate: []string{pinSHA256}},
|
||||
},
|
||||
Network: network,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user