1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-23 15:04:41 -05:00
sub2sing-box/parser/hysteria2.go

107 lines
2.6 KiB
Go
Raw Permalink 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-09-19 06:12:24 -04:00
"github.com/nitezs/sub2sing-box/constant"
"github.com/nitezs/sub2sing-box/model"
2024-10-02 13:24:54 -04:00
"github.com/sagernet/sing-box/option"
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-11-15 11:26:54 -05:00
network, obfs, obfsPassword, pinSHA256, insecure, sni, alpnStr := query.Get("network"), query.Get("obfs"), query.Get("obfs-password"), query.Get("pinSHA256"), query.Get("insecure"), query.Get("sni"), query.Get("alpn")
2024-03-22 04:10:15 -04:00
insecureBool := insecure == "1"
2024-11-15 11:26:54 -05:00
enableTLS := pinSHA256 != "" || sni != "" || alpnStr != ""
var alpn []string
alpnStr = strings.TrimSpace(alpnStr)
if alpnStr != "" {
alpn = strings.Split(alpnStr, ",")
}
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-10-02 13:24:54 -04:00
Outbound: option.Outbound{
Type: "hysteria2",
Tag: strings.TrimSpace(remarks),
Hysteria2Options: option.Hysteria2OutboundOptions{
ServerOptions: option.ServerOptions{
Server: server,
ServerPort: port,
},
Password: password,
OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: enableTLS,
Insecure: insecureBool,
ServerName: sni,
2024-11-15 11:26:54 -05:00
ALPN: alpn,
2024-10-02 13:24:54 -04:00
},
},
2024-10-02 13:24:54 -04:00
Network: option.NetworkList(network),
2024-03-10 15:13:42 -04:00
},
},
}
if pinSHA256 != "" {
result.Hysteria2Options.OutboundTLSOptionsContainer.TLS.Certificate = []string{pinSHA256}
}
if obfs != "" {
2024-10-02 13:24:54 -04:00
result.Hysteria2Options.Obfs = &option.Hysteria2Obfs{
Type: obfs,
Password: obfsPassword,
}
}
2024-03-10 15:13:42 -04:00
return result, nil
}