2023-10-31 03:14:29 -04:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
2024-08-11 11:55:47 -04:00
|
|
|
"fmt"
|
2023-10-31 03:14:29 -04:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2024-09-17 01:10:13 -04:00
|
|
|
|
|
|
|
"github.com/nitezs/sub2clash/constant"
|
|
|
|
"github.com/nitezs/sub2clash/model"
|
2023-10-31 03:14:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func ParseHysteria2(proxy string) (model.Proxy, error) {
|
2024-04-23 02:39:16 -04:00
|
|
|
if !strings.HasPrefix(proxy, constant.Hysteria2Prefix1) &&
|
|
|
|
!strings.HasPrefix(proxy, constant.Hysteria2Prefix2) {
|
|
|
|
return model.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
|
|
|
|
}
|
|
|
|
|
2024-08-11 11:55:47 -04:00
|
|
|
link, err := url.Parse(proxy)
|
|
|
|
if err != nil {
|
2024-04-23 02:39:16 -04:00
|
|
|
return model.Proxy{}, &ParseError{
|
|
|
|
Type: ErrInvalidStruct,
|
2024-08-11 11:55:47 -04:00
|
|
|
Message: "url parse error",
|
2024-04-23 02:39:16 -04:00
|
|
|
Raw: proxy,
|
|
|
|
}
|
2023-10-31 03:14:29 -04:00
|
|
|
}
|
2024-04-23 02:39:16 -04:00
|
|
|
|
2024-08-11 11:55:47 -04:00
|
|
|
username := link.User.Username()
|
|
|
|
password, exist := link.User.Password()
|
|
|
|
if !exist {
|
|
|
|
password = username
|
2024-04-23 02:39:16 -04:00
|
|
|
}
|
|
|
|
|
2024-08-11 11:55:47 -04:00
|
|
|
query := link.Query()
|
|
|
|
server := link.Hostname()
|
|
|
|
if server == "" {
|
2024-04-23 02:39:16 -04:00
|
|
|
return model.Proxy{}, &ParseError{
|
|
|
|
Type: ErrInvalidStruct,
|
2024-08-11 11:55:47 -04:00
|
|
|
Message: "missing server host",
|
2024-04-23 02:39:16 -04:00
|
|
|
Raw: proxy,
|
|
|
|
}
|
2023-10-31 03:14:29 -04:00
|
|
|
}
|
2024-08-11 11:55:47 -04:00
|
|
|
portStr := link.Port()
|
|
|
|
if portStr == "" {
|
2024-04-23 02:39:16 -04:00
|
|
|
return model.Proxy{}, &ParseError{
|
2024-08-11 11:55:47 -04:00
|
|
|
Type: ErrInvalidStruct,
|
|
|
|
Message: "missing server port",
|
2024-04-23 02:39:16 -04:00
|
|
|
Raw: proxy,
|
|
|
|
}
|
2023-10-31 03:14:29 -04:00
|
|
|
}
|
2024-08-11 11:55:47 -04:00
|
|
|
port, err := ParsePort(portStr)
|
2023-10-31 03:14:29 -04:00
|
|
|
if err != nil {
|
2024-04-23 02:39:16 -04:00
|
|
|
return model.Proxy{}, &ParseError{
|
2024-08-11 11:55:47 -04:00
|
|
|
Type: ErrInvalidPort,
|
|
|
|
Raw: portStr,
|
2024-04-23 02:39:16 -04:00
|
|
|
}
|
2024-04-17 09:52:03 -04:00
|
|
|
}
|
2024-08-11 11:55:47 -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-04-23 02:39:16 -04:00
|
|
|
insecureBool := insecure == "1"
|
2024-08-11 11:55:47 -04:00
|
|
|
remarks := link.Fragment
|
2024-08-04 00:59:55 -04:00
|
|
|
if remarks == "" {
|
2024-08-11 11:55:47 -04:00
|
|
|
remarks = fmt.Sprintf("%s:%s", server, portStr)
|
2024-08-04 00:59:55 -04:00
|
|
|
}
|
2024-08-11 11:55:47 -04:00
|
|
|
remarks = strings.TrimSpace(remarks)
|
2024-08-04 00:59:55 -04:00
|
|
|
|
2023-10-31 03:14:29 -04:00
|
|
|
result := model.Proxy{
|
2023-11-02 14:35:30 -04:00
|
|
|
Type: "hysteria2",
|
2024-04-23 02:39:16 -04:00
|
|
|
Name: remarks,
|
|
|
|
Server: server,
|
2023-11-02 14:35:30 -04:00
|
|
|
Port: port,
|
2024-04-23 02:39:16 -04:00
|
|
|
Password: password,
|
|
|
|
Obfs: obfs,
|
|
|
|
ObfsParam: obfsPassword,
|
|
|
|
Sni: sni,
|
|
|
|
SkipCertVerify: insecureBool,
|
|
|
|
TLS: enableTLS,
|
|
|
|
Network: network,
|
2023-10-31 03:14:29 -04:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|