1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-24 13:24:42 -05:00
sub2sing-box/parser/hysteria.go

97 lines
2.2 KiB
Go
Raw 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"
"strconv"
"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 ParseHysteria(proxy string) (model.Outbound, error) {
2024-03-22 04:10:15 -04:00
if !strings.HasPrefix(proxy, constant.HysteriaPrefix) {
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-03-10 15:13:42 -04:00
}
2024-08-04 05:01:09 -04:00
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-03-22 04:10:15 -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-03-22 04:10:15 -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,
2024-03-22 04:10:15 -04:00
Message: err.Error(),
2024-08-04 05:01:09 -04:00
Raw: proxy,
2024-03-22 04:10:15 -04:00
}
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()
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-10 15:13:42 -04:00
insecureBool, err := strconv.ParseBool(insecure)
if err != nil {
2024-03-22 04:10:15 -04:00
insecureBool = false
}
var alpn []string
alpnStr = strings.TrimSpace(alpnStr)
if alpnStr != "" {
alpn = strings.Split(alpnStr, ",")
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
2024-08-04 05:01:09 -04:00
remarks := link.Fragment
if remarks == "" {
remarks = fmt.Sprintf("%s:%s", server, portStr)
2024-03-22 04:10:15 -04:00
}
2024-08-04 05:01:09 -04:00
remarks = strings.TrimSpace(remarks)
2024-03-22 04:10:15 -04:00
2024-10-02 13:24:54 -04:00
return model.Outbound{Outbound: option.Outbound{
2024-03-10 15:13:42 -04:00
Type: "hysteria",
2024-03-11 09:00:13 -04:00
Tag: remarks,
2024-10-02 13:24:54 -04:00
HysteriaOptions: option.HysteriaOutboundOptions{
ServerOptions: option.ServerOptions{
2024-03-22 04:10:15 -04:00
Server: server,
ServerPort: port,
},
Up: upmbps,
Down: downmbps,
Auth: []byte(auth),
Obfs: obfs,
2024-10-02 13:24:54 -04:00
Network: option.NetworkList(protocol),
OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
Insecure: insecureBool,
ALPN: alpn,
},
2024-03-10 15:13:42 -04:00
},
},
2024-10-02 13:24:54 -04:00
}}, nil
2024-03-10 15:13:42 -04:00
}