1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-25 01:04:41 -05:00
sub2clash/parser/vless.go

88 lines
2.3 KiB
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package parser
import (
"fmt"
"net/url"
"strconv"
"strings"
2023-09-12 12:46:17 -04:00
"sub2clash/model"
2023-09-12 06:40:24 -04:00
)
func ParseVless(proxy string) (model.Proxy, error) {
// 判断是否以 vless:// 开头
if !strings.HasPrefix(proxy, "vless://") {
return model.Proxy{}, fmt.Errorf("invalid vless Url")
2023-09-12 06:40:24 -04:00
}
// 分割
parts := strings.SplitN(strings.TrimPrefix(proxy, "vless://"), "@", 2)
if len(parts) != 2 {
return model.Proxy{}, fmt.Errorf("invalid vless Url")
2023-09-12 06:40:24 -04:00
}
// 分割
serverInfo := strings.SplitN(parts[1], "#", 2)
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
params, err := url.ParseQuery(serverAndPortAndParams[1])
if err != nil {
return model.Proxy{}, err
}
if len(serverAndPort) != 2 {
return model.Proxy{}, fmt.Errorf("invalid vless")
2023-09-12 06:40:24 -04:00
}
// 处理端口
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
if err != nil {
return model.Proxy{}, err
}
// 返回结果
result := model.Proxy{
2024-02-18 11:31:56 -05:00
Type: "vless",
Server: strings.TrimSpace(serverAndPort[0]),
Port: port,
UUID: strings.TrimSpace(parts[0]),
UDP: true,
Sni: params.Get("sni"),
Network: params.Get("type"),
TLS: params.Get("security") == "reality",
Flow: params.Get("flow"),
ClientFingerprint: params.Get("fp"),
Servername: params.Get("sni"),
RealityOpts: model.RealityOptions{
2023-09-12 12:46:17 -04:00
PublicKey: params.Get("pbk"),
},
}
2023-09-13 01:47:22 -04:00
if params.Get("alpn") != "" {
result.Alpn = strings.Split(params.Get("alpn"), ",")
}
2023-09-12 12:46:17 -04:00
if params.Get("type") == "ws" {
result.WSOpts = model.WSOptions{
2023-09-12 06:40:24 -04:00
Path: params.Get("path"),
Headers: map[string]string{
"Host": params.Get("host"),
2023-09-12 06:40:24 -04:00
},
2023-09-12 12:46:17 -04:00
}
}
if params.Get("type") == "grpc" {
result.GrpcOpts = model.GrpcOptions{
GrpcServiceName: params.Get("serviceName"),
2023-09-12 12:46:17 -04:00
}
2023-09-12 06:40:24 -04:00
}
// 如果有节点名称
if len(serverInfo) == 2 {
if strings.Contains(serverInfo[1], "|") {
result.Name = strings.SplitN(serverInfo[1], "|", 2)[1]
} else {
result.Name, err = url.QueryUnescape(serverInfo[1])
if err != nil {
return model.Proxy{}, err
}
2023-09-12 06:40:24 -04:00
}
} else {
result.Name, err = url.QueryUnescape(serverAndPort[0])
if err != nil {
return model.Proxy{}, err
}
2023-09-12 06:40:24 -04:00
}
return result, nil
}