1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 20:54:42 -05:00
sub2clash/parser/hysteria2.go

58 lines
1.7 KiB
Go
Raw Normal View History

2023-10-31 03:14:29 -04:00
package parser
import (
"errors"
"net/url"
"strconv"
"strings"
"sub2clash/model"
)
2024-04-17 09:52:03 -04:00
// hysteria2://letmein@example.com/?insecure=1&obfs=salamander&obfs-password=gawrgura&pinSHA256=deadbeef&sni=real.example.com#name
2024-03-09 04:01:52 -05:00
2023-10-31 03:14:29 -04:00
func ParseHysteria2(proxy string) (model.Proxy, error) {
// 判断是否以 hysteria2:// 开头
2024-03-09 04:01:52 -05:00
if !strings.HasPrefix(proxy, "hysteria2://") && !strings.HasPrefix(proxy, "hy2://") {
2023-10-31 03:14:29 -04:00
return model.Proxy{}, errors.New("invalid hysteria2 Url")
}
// 分割
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 {
return model.Proxy{}, errors.New("invalid hysteria2 Url")
}
params, err := url.ParseQuery(serverInfo[1])
if err != nil {
return model.Proxy{}, errors.New("invalid hysteria2 Url")
}
// 获取端口
port, err := strconv.Atoi(serverAndPort[1])
if err != nil {
return model.Proxy{}, errors.New("invalid hysteria2 Url")
}
2024-04-17 09:52:03 -04:00
name := ""
if strings.Contains(proxy, "#") {
splitResult := strings.Split(proxy, "#")
name, _ = url.QueryUnescape(splitResult[len(splitResult)-1])
2024-04-20 12:23:29 -04:00
} else {
name = strings.Join(serverAndPort, ":")
2024-04-17 09:52:03 -04:00
}
2023-10-31 03:14:29 -04:00
// 返回结果
result := model.Proxy{
Type: "hysteria2",
2024-04-17 09:52:03 -04:00
Name: name,
Server: serverAndPort[0],
Port: port,
Password: parts[0],
Obfs: params.Get("obfs"),
ObfsParam: params.Get("obfs-password"),
Sni: params.Get("sni"),
SkipCertVerify: params.Get("insecure") == "1",
2023-10-31 03:14:29 -04:00
}
return result, nil
}