sub2sing-box/parser/hysteria.go

97 lines
2.2 KiB
Go
Raw Normal View History

2024-03-11 03:13:42 +08:00
package parser
import (
2024-08-04 17:01:09 +08:00
"fmt"
2024-03-11 03:13:42 +08:00
"net/url"
"strconv"
"strings"
2024-09-19 18:12:24 +08:00
"github.com/nitezs/sub2sing-box/constant"
"github.com/nitezs/sub2sing-box/model"
2024-10-03 01:24:54 +08:00
"github.com/sagernet/sing-box/option"
2024-03-11 03:13:42 +08:00
)
func ParseHysteria(proxy string) (model.Outbound, error) {
2024-03-22 16:10:15 +08:00
if !strings.HasPrefix(proxy, constant.HysteriaPrefix) {
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
}
2024-08-04 17:01:09 +08:00
link, err := url.Parse(proxy)
if err != nil {
2024-03-22 16:10:15 +08:00
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
2024-08-04 17:01:09 +08:00
Message: "url parse error",
2024-03-22 16:10:15 +08:00
Raw: proxy,
}
2024-03-11 03:13:42 +08:00
}
2024-08-04 17:01:09 +08:00
server := link.Hostname()
if server == "" {
2024-03-22 16:10:15 +08:00
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
2024-08-04 17:01:09 +08:00
Message: "missing server host",
2024-03-22 16:10:15 +08:00
Raw: proxy,
}
2024-03-11 03:13:42 +08:00
}
2024-03-22 16:10:15 +08:00
2024-08-04 17:01:09 +08:00
portStr := link.Port()
if portStr == "" {
2024-04-23 14:41:14 +08:00
return model.Outbound{}, &ParseError{
2024-08-04 17:01:09 +08:00
Type: ErrInvalidStruct,
Message: "missing server port",
2024-04-23 14:41:14 +08:00
Raw: proxy,
}
2024-03-11 03:13:42 +08:00
}
2024-03-22 16:10:15 +08:00
2024-08-04 17:01:09 +08:00
port, err := ParsePort(portStr)
2024-03-11 03:13:42 +08:00
if err != nil {
2024-03-22 16:10:15 +08:00
return model.Outbound{}, &ParseError{
2024-08-04 17:01:09 +08:00
Type: ErrInvalidPort,
2024-03-22 16:10:15 +08:00
Message: err.Error(),
2024-08-04 17:01:09 +08:00
Raw: proxy,
2024-03-22 16:10:15 +08:00
}
2024-03-11 03:13:42 +08:00
}
2024-03-22 16:10:15 +08:00
2024-08-04 17:01:09 +08:00
query := link.Query()
protocol, auth, insecure, upmbps, downmbps, obfs, alpnStr := query.Get("protocol"), query.Get("auth"), query.Get("insecure"), query.Get("upmbps"), query.Get("downmbps"), query.Get("obfs"), query.Get("alpn")
2024-03-11 03:13:42 +08:00
insecureBool, err := strconv.ParseBool(insecure)
if err != nil {
2024-03-22 16:10:15 +08:00
insecureBool = false
}
var alpn []string
alpnStr = strings.TrimSpace(alpnStr)
if alpnStr != "" {
alpn = strings.Split(alpnStr, ",")
2024-03-11 03:13:42 +08:00
}
2024-03-22 16:10:15 +08:00
2024-08-04 17:01:09 +08:00
remarks := link.Fragment
if remarks == "" {
remarks = fmt.Sprintf("%s:%s", server, portStr)
2024-03-22 16:10:15 +08:00
}
2024-08-04 17:01:09 +08:00
remarks = strings.TrimSpace(remarks)
2024-03-22 16:10:15 +08:00
2024-10-03 01:24:54 +08:00
return model.Outbound{Outbound: option.Outbound{
2024-03-11 03:13:42 +08:00
Type: "hysteria",
2024-03-11 21:00:13 +08:00
Tag: remarks,
2024-10-03 01:24:54 +08:00
HysteriaOptions: option.HysteriaOutboundOptions{
ServerOptions: option.ServerOptions{
2024-03-22 16:10:15 +08:00
Server: server,
ServerPort: port,
},
Up: upmbps,
Down: downmbps,
Auth: []byte(auth),
Obfs: obfs,
2024-10-03 01:24:54 +08:00
Network: option.NetworkList(protocol),
OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
Insecure: insecureBool,
ALPN: alpn,
},
2024-03-11 03:13:42 +08:00
},
},
2024-10-03 01:24:54 +08:00
}}, nil
2024-03-11 03:13:42 +08:00
}