2024-03-10 15:13:42 -04:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
2024-03-11 12:13:59 -04:00
|
|
|
"errors"
|
2024-03-10 15:13:42 -04:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-03-11 11:39:58 -04:00
|
|
|
"sub2sing-box/internal/model"
|
2024-03-10 15:13:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func ParseVless(proxy string) (model.Proxy, error) {
|
|
|
|
if !strings.HasPrefix(proxy, "vless://") {
|
2024-03-11 12:13:59 -04:00
|
|
|
return model.Proxy{}, errors.New("invalid vless Url")
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
parts := strings.SplitN(strings.TrimPrefix(proxy, "vless://"), "@", 2)
|
|
|
|
if len(parts) != 2 {
|
2024-03-11 12:13:59 -04:00
|
|
|
return model.Proxy{}, errors.New("invalid vless Url")
|
2024-03-10 15:13:42 -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 {
|
2024-03-11 12:13:59 -04:00
|
|
|
return model.Proxy{}, errors.New("invalid vless Url")
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
port, err := strconv.Atoi(strings.TrimSpace(serverAndPort[1]))
|
|
|
|
if err != nil {
|
|
|
|
return model.Proxy{}, err
|
|
|
|
}
|
|
|
|
remarks := ""
|
|
|
|
if len(serverInfo) == 2 {
|
|
|
|
if strings.Contains(serverInfo[1], "|") {
|
|
|
|
remarks = strings.SplitN(serverInfo[1], "|", 2)[1]
|
|
|
|
} else {
|
|
|
|
remarks, err = url.QueryUnescape(serverInfo[1])
|
|
|
|
if err != nil {
|
|
|
|
return model.Proxy{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
remarks, err = url.QueryUnescape(serverAndPort[0])
|
|
|
|
if err != nil {
|
|
|
|
return model.Proxy{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
server := strings.TrimSpace(serverAndPort[0])
|
|
|
|
uuid := strings.TrimSpace(parts[0])
|
|
|
|
result := model.Proxy{
|
|
|
|
Type: "vless",
|
2024-03-11 09:00:13 -04:00
|
|
|
Tag: remarks,
|
2024-03-10 15:13:42 -04:00
|
|
|
VLESS: model.VLESS{
|
|
|
|
Server: server,
|
|
|
|
ServerPort: uint16(port),
|
|
|
|
UUID: uuid,
|
2024-03-11 00:53:58 -04:00
|
|
|
Flow: params.Get("flow"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if params.Get("security") == "tls" {
|
2024-03-11 12:13:59 -04:00
|
|
|
var alpn []string
|
|
|
|
if strings.Contains(params.Get("alpn"), ",") {
|
|
|
|
alpn = strings.Split(params.Get("alpn"), ",")
|
|
|
|
} else {
|
|
|
|
alpn = nil
|
|
|
|
}
|
2024-03-11 00:53:58 -04:00
|
|
|
result.VLESS.TLS = &model.OutboundTLSOptions{
|
|
|
|
Enabled: true,
|
2024-03-11 12:13:59 -04:00
|
|
|
ALPN: alpn,
|
2024-03-11 00:53:58 -04:00
|
|
|
Insecure: params.Get("allowInsecure") == "1",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if params.Get("security") == "reality" {
|
2024-03-11 12:13:59 -04:00
|
|
|
var alpn []string
|
|
|
|
if strings.Contains(params.Get("alpn"), ",") {
|
|
|
|
alpn = strings.Split(params.Get("alpn"), ",")
|
|
|
|
} else {
|
|
|
|
alpn = nil
|
|
|
|
}
|
2024-03-11 00:53:58 -04:00
|
|
|
result.VLESS.TLS = &model.OutboundTLSOptions{
|
|
|
|
Enabled: true,
|
|
|
|
ServerName: params.Get("sni"),
|
|
|
|
UTLS: &model.OutboundUTLSOptions{
|
|
|
|
Enabled: params.Get("fp") != "",
|
|
|
|
Fingerprint: params.Get("fp"),
|
2024-03-10 15:13:42 -04:00
|
|
|
},
|
2024-03-11 00:53:58 -04:00
|
|
|
Reality: &model.OutboundRealityOptions{
|
|
|
|
Enabled: true,
|
|
|
|
PublicKey: params.Get("pbk"),
|
|
|
|
ShortID: params.Get("sid"),
|
|
|
|
},
|
2024-03-11 12:13:59 -04:00
|
|
|
ALPN: alpn,
|
2024-03-11 00:53:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if params.Get("type") == "ws" {
|
|
|
|
result.VLESS.Transport = &model.V2RayTransportOptions{
|
|
|
|
Type: "ws",
|
|
|
|
WebsocketOptions: model.V2RayWebsocketOptions{
|
|
|
|
Path: params.Get("path"),
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Host": params.Get("host"),
|
2024-03-10 15:13:42 -04:00
|
|
|
},
|
|
|
|
},
|
2024-03-11 00:53:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if params.Get("type") == "quic" {
|
|
|
|
result.VLESS.Transport = &model.V2RayTransportOptions{
|
|
|
|
Type: "quic",
|
|
|
|
QUICOptions: model.V2RayQUICOptions{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if params.Get("type") == "grpc" {
|
|
|
|
result.VLESS.Transport = &model.V2RayTransportOptions{
|
|
|
|
Type: "grpc",
|
|
|
|
GRPCOptions: model.V2RayGRPCOptions{
|
|
|
|
ServiceName: params.Get("serviceName"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if params.Get("type") == "http" {
|
|
|
|
host, err := url.QueryUnescape(params.Get("host"))
|
|
|
|
if err != nil {
|
|
|
|
return model.Proxy{}, err
|
|
|
|
}
|
|
|
|
result.VLESS.Transport = &model.V2RayTransportOptions{
|
|
|
|
Type: "http",
|
|
|
|
HTTPOptions: model.V2RayHTTPOptions{
|
|
|
|
Host: strings.Split(host, ","),
|
|
|
|
},
|
|
|
|
}
|
2024-03-10 15:13:42 -04:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|