1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-23 15:24:42 -05:00
sub2clash/parser/hysteria.go

89 lines
1.9 KiB
Go
Raw Normal View History

2024-03-09 04:01:52 -05:00
package parser
import (
2024-08-11 11:55:47 -04:00
"fmt"
2024-03-09 04:01:52 -05:00
"net/url"
"strconv"
"strings"
"github.com/nitezs/sub2clash/constant"
"github.com/nitezs/sub2clash/model"
2024-03-09 04:01:52 -05:00
)
func ParseHysteria(proxy string) (model.Proxy, error) {
2024-04-23 02:39:16 -04:00
if !strings.HasPrefix(proxy, constant.HysteriaPrefix) {
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,
}
2024-03-09 04:01:52 -05:00
}
2024-08-11 11:55:47 -04:00
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,
}
2024-03-09 04:01:52 -05:00
}
2024-04-23 02:39:16 -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,
}
2024-03-09 04:01:52 -05:00
}
2024-04-23 02:39:16 -04:00
2024-08-11 11:55:47 -04:00
port, err := ParsePort(portStr)
2024-03-09 04:01:52 -05: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,
2024-04-23 02:39:16 -04:00
Message: err.Error(),
2024-08-11 11:55:47 -04:00
Raw: proxy,
2024-04-23 02:39:16 -04:00
}
2024-03-09 04:01:52 -05:00
}
2024-04-23 02:39:16 -04:00
2024-08-11 11:55:47 -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-04-23 02:39:16 -04:00
insecureBool, err := strconv.ParseBool(insecure)
if err != nil {
insecureBool = false
}
var alpn []string
alpnStr = strings.TrimSpace(alpnStr)
if alpnStr != "" {
alpn = strings.Split(alpnStr, ",")
2024-03-09 04:01:52 -05:00
}
2024-04-23 02:39:16 -04:00
2024-08-11 11:55:47 -04:00
remarks := link.Fragment
if remarks == "" {
remarks = fmt.Sprintf("%s:%s", server, portStr)
2024-04-23 02:39:16 -04:00
}
2024-08-11 11:55:47 -04:00
remarks = strings.TrimSpace(remarks)
2024-04-23 02:39:16 -04:00
2024-03-09 04:01:52 -05:00
result := model.Proxy{
Type: "hysteria",
Name: remarks,
2024-04-23 02:39:16 -04:00
Server: server,
2024-03-09 04:01:52 -05:00
Port: port,
Up: upmbps,
Down: downmbps,
Auth: auth,
Obfs: obfs,
2024-08-11 11:55:47 -04:00
SkipCertVerify: insecureBool,
2024-04-23 02:39:16 -04:00
Alpn: alpn,
2024-03-09 04:01:52 -05:00
Protocol: protocol,
2024-04-23 02:39:16 -04:00
AllowInsecure: insecureBool,
2024-03-09 04:01:52 -05:00
}
return result, nil
}