1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-24 10:54:43 -05:00
sub2clash/parser/vless.go

128 lines
2.8 KiB
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package parser
import (
2024-08-11 11:55:47 -04:00
"fmt"
2023-09-12 06:40:24 -04:00
"net/url"
"strings"
"github.com/nitezs/sub2clash/constant"
"github.com/nitezs/sub2clash/model"
2023-09-12 06:40:24 -04:00
)
func ParseVless(proxy string) (model.Proxy, error) {
2024-04-23 02:39:16 -04:00
if !strings.HasPrefix(proxy, constant.VLESSPrefix) {
return model.Proxy{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
2023-09-12 06:40:24 -04:00
}
2024-04-23 02:39:16 -04:00
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-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-08-11 11:55:47 -04:00
portStr := link.Port()
2024-04-23 02:39:16 -04:00
port, err := ParsePort(portStr)
if err != nil {
return model.Proxy{}, &ParseError{
Type: ErrInvalidPort,
Message: err.Error(),
Raw: proxy,
}
}
2024-08-11 11:55:47 -04:00
query := link.Query()
uuid := link.User.Username()
flow, security, alpnStr, sni, insecure, fp, pbk, sid, path, host, serviceName, _type := query.Get("flow"), query.Get("security"), query.Get("alpn"), query.Get("sni"), query.Get("allowInsecure"), query.Get("fp"), query.Get("pbk"), query.Get("sid"), query.Get("path"), query.Get("host"), query.Get("serviceName"), query.Get("type")
2024-04-23 02:39:16 -04:00
insecureBool := insecure == "1"
var alpn []string
if strings.Contains(alpnStr, ",") {
alpn = strings.Split(alpnStr, ",")
} else {
alpn = nil
2023-09-12 06:40:24 -04:00
}
2024-08-11 11:55:47 -04:00
remarks := link.Fragment
if remarks == "" {
remarks = fmt.Sprintf("%s:%s", server, portStr)
}
remarks = strings.TrimSpace(remarks)
2024-04-23 02:39:16 -04:00
2023-09-12 06:40:24 -04:00
result := model.Proxy{
2024-04-23 02:39:16 -04:00
Type: "vless",
Server: server,
Name: remarks,
Port: port,
UUID: uuid,
Flow: flow,
2023-09-12 12:46:17 -04:00
}
2024-04-23 02:39:16 -04:00
if security == "tls" {
result.TLS = true
result.Alpn = alpn
result.Sni = sni
result.AllowInsecure = insecureBool
2024-05-05 14:08:12 -04:00
result.ClientFingerprint = fp
2023-09-13 01:47:22 -04:00
}
2024-04-23 02:39:16 -04:00
if security == "reality" {
2024-03-12 09:46:07 -04:00
result.TLS = true
result.Servername = sni
2024-03-12 09:46:07 -04:00
result.RealityOpts = model.RealityOptions{
2024-04-23 02:39:16 -04:00
PublicKey: pbk,
ShortID: sid,
2024-03-12 09:46:07 -04:00
}
2024-05-05 14:08:12 -04:00
result.ClientFingerprint = fp
2024-03-12 09:46:07 -04:00
}
2024-04-23 02:39:16 -04:00
if _type == "ws" {
result.Network = "ws"
result.WSOpts = model.WSOptions{
2024-04-23 02:39:16 -04:00
Path: path,
2023-09-12 12:46:17 -04:00
}
2024-04-23 02:39:16 -04:00
if host != "" {
result.WSOpts.Headers = make(map[string]string)
result.WSOpts.Headers["Host"] = host
2023-09-12 12:46:17 -04:00
}
2023-09-12 06:40:24 -04:00
}
2024-04-23 02:39:16 -04:00
if _type == "grpc" {
result.Network = "grpc"
result.GrpcOpts = model.GrpcOptions{
GrpcServiceName: serviceName,
}
2024-04-23 02:39:16 -04:00
}
if _type == "http" {
2024-10-19 03:39:20 -04:00
result.HTTPOpts = model.HTTPOptions{}
result.HTTPOpts.Headers = map[string][]string{}
result.HTTPOpts.Path = strings.Split(path, ",")
2024-04-23 02:39:16 -04:00
hosts, err := url.QueryUnescape(host)
if err != nil {
return model.Proxy{}, &ParseError{
Type: ErrCannotParseParams,
Raw: proxy,
Message: err.Error(),
}
2023-09-12 06:40:24 -04:00
}
2024-04-23 02:39:16 -04:00
result.Network = "http"
2024-10-19 03:39:20 -04:00
if hosts != "" {
result.HTTPOpts.Headers["host"] = strings.Split(host, ",")
}
2023-09-12 06:40:24 -04:00
}
2024-04-23 02:39:16 -04:00
2023-09-12 06:40:24 -04:00
return result, nil
}