2024-03-10 15:13:42 -04:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-03-20 08:54:23 -04:00
|
|
|
model2 "sub2sing-box/model"
|
2024-03-10 15:13:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// hysteria2://letmein@example.com/?insecure=1&obfs=salamander&obfs-password=gawrgura&pinSHA256=deadbeef&sni=real.example.com
|
|
|
|
|
2024-03-20 08:54:23 -04:00
|
|
|
func ParseHysteria2(proxy string) (model2.Proxy, error) {
|
2024-03-10 15:13:42 -04:00
|
|
|
if !strings.HasPrefix(proxy, "hysteria2://") && !strings.HasPrefix(proxy, "hy2://") {
|
2024-03-20 08:54:23 -04:00
|
|
|
return model2.Proxy{}, errors.New("invalid hysteria2 Url")
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
parts := strings.SplitN(strings.TrimPrefix(proxy, "hysteria2://"), "@", 2)
|
|
|
|
serverInfo := strings.SplitN(parts[1], "/?", 2)
|
|
|
|
serverAndPort := strings.SplitN(serverInfo[0], ":", 2)
|
|
|
|
if len(serverAndPort) == 1 {
|
|
|
|
serverAndPort = append(serverAndPort, "443")
|
|
|
|
} else if len(serverAndPort) != 2 {
|
2024-03-20 08:54:23 -04:00
|
|
|
return model2.Proxy{}, errors.New("invalid hysteria2 Url")
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
params, err := url.ParseQuery(serverInfo[1])
|
|
|
|
if err != nil {
|
2024-03-20 08:54:23 -04:00
|
|
|
return model2.Proxy{}, errors.New("invalid hysteria2 Url")
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
port, err := strconv.Atoi(serverAndPort[1])
|
|
|
|
if err != nil {
|
2024-03-20 08:54:23 -04:00
|
|
|
return model2.Proxy{}, errors.New("invalid hysteria2 Url")
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
remarks := params.Get("name")
|
|
|
|
server := serverAndPort[0]
|
|
|
|
password := parts[0]
|
|
|
|
network := params.Get("network")
|
2024-03-20 08:54:23 -04:00
|
|
|
result := model2.Proxy{
|
2024-03-10 15:13:42 -04:00
|
|
|
Type: "hysteria2",
|
2024-03-11 09:00:13 -04:00
|
|
|
Tag: remarks,
|
2024-03-20 08:54:23 -04:00
|
|
|
Hysteria2: model2.Hysteria2{
|
2024-03-10 15:13:42 -04:00
|
|
|
Server: server,
|
|
|
|
ServerPort: uint16(port),
|
|
|
|
Password: password,
|
2024-03-20 08:54:23 -04:00
|
|
|
Obfs: &model2.Hysteria2Obfs{
|
2024-03-10 15:13:42 -04:00
|
|
|
Type: params.Get("obfs"),
|
|
|
|
Password: params.Get("obfs-password"),
|
|
|
|
},
|
2024-03-20 08:54:23 -04:00
|
|
|
TLS: &model2.OutboundTLSOptions{
|
2024-03-10 15:13:42 -04:00
|
|
|
Enabled: params.Get("pinSHA256") != "",
|
|
|
|
Insecure: params.Get("insecure") == "1",
|
|
|
|
ServerName: params.Get("sni"),
|
2024-03-11 00:53:58 -04:00
|
|
|
Certificate: []string{params.Get("pinSHA256")},
|
2024-03-10 15:13:42 -04:00
|
|
|
},
|
|
|
|
Network: network,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|