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

170 lines
4.2 KiB
Go
Raw Normal View History

2024-03-10 15:13:42 -04:00
package parser
import (
"net/url"
"strings"
2024-03-22 04:10:15 -04:00
"sub2sing-box/constant"
"sub2sing-box/model"
2024-03-10 15:13:42 -04:00
)
func ParseVless(proxy string) (model.Outbound, error) {
2024-03-22 04:10:15 -04:00
if !strings.HasPrefix(proxy, constant.VLESSPrefix) {
return model.Outbound{}, &ParseError{Type: ErrInvalidPrefix, Raw: proxy}
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
urlParts := strings.SplitN(strings.TrimPrefix(proxy, constant.VLESSPrefix), "@", 2)
if len(urlParts) != 2 {
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing character '@' in url",
Raw: proxy,
}
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
serverInfo := strings.SplitN(urlParts[1], "#", 2)
2024-03-10 15:13:42 -04:00
serverAndPortAndParams := strings.SplitN(serverInfo[0], "?", 2)
2024-03-22 04:10:15 -04:00
if len(serverAndPortAndParams) != 2 {
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing character '?' in url",
Raw: proxy,
}
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
serverAndPort := strings.SplitN(serverAndPortAndParams[0], ":", 2)
2024-03-10 15:13:42 -04:00
if len(serverAndPort) != 2 {
2024-03-22 04:10:15 -04:00
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
Message: "missing server host or port",
Raw: proxy,
}
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
server, portStr := serverAndPort[0], serverAndPort[1]
port, err := ParsePort(portStr)
2024-03-10 15:13:42 -04:00
if err != nil {
return model.Outbound{}, err
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
params, err := url.ParseQuery(serverAndPortAndParams[1])
if err != nil {
return model.Outbound{}, &ParseError{
Type: ErrCannotParseParams,
Raw: proxy,
Message: err.Error(),
}
}
2024-03-10 15:13:42 -04:00
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 {
2024-03-22 04:10:15 -04:00
return model.Outbound{}, &ParseError{
Type: ErrCannotParseParams,
Raw: proxy,
Message: err.Error(),
}
2024-03-10 15:13:42 -04:00
}
}
} else {
2024-03-22 04:10:15 -04:00
remarks, err = url.QueryUnescape(server)
2024-03-10 15:13:42 -04:00
if err != nil {
return model.Outbound{}, err
2024-03-10 15:13:42 -04:00
}
}
2024-03-22 04:10:15 -04:00
uuid := strings.TrimSpace(urlParts[0])
flow, security, alpnStr, sni, insecure, fp, pbk, sid, path, host, serviceName := params.Get("flow"), params.Get("security"), params.Get("alpn"), params.Get("sni"), params.Get("allowInsecure"), params.Get("fp"), params.Get("pbk"), params.Get("sid"), params.Get("path"), params.Get("host"), params.Get("serviceName")
enableUTLS := fp != ""
insecureBool := insecure == "1"
var alpn []string
if strings.Contains(alpnStr, ",") {
alpn = strings.Split(alpnStr, ",")
} else {
alpn = nil
}
result := model.Outbound{
2024-03-10 15:13:42 -04:00
Type: "vless",
2024-03-11 09:00:13 -04:00
Tag: remarks,
VLESSOptions: model.VLESSOutboundOptions{
ServerOptions: model.ServerOptions{
Server: server,
2024-03-22 04:10:15 -04:00
ServerPort: port,
},
UUID: uuid,
2024-03-22 04:10:15 -04:00
Flow: flow,
2024-03-11 00:53:58 -04:00
},
}
2024-03-22 04:10:15 -04:00
if security == "tls" {
result.VLESSOptions.OutboundTLSOptionsContainer = model.OutboundTLSOptionsContainer{
TLS: &model.OutboundTLSOptions{
2024-03-20 12:59:47 -04:00
Enabled: true,
ALPN: alpn,
2024-03-22 04:10:15 -04:00
ServerName: sni,
Insecure: insecureBool,
},
2024-03-11 00:53:58 -04:00
}
2024-03-22 04:10:15 -04:00
result.VLESSOptions.OutboundTLSOptionsContainer.TLS.UTLS = &model.OutboundUTLSOptions{
Enabled: enableUTLS,
Fingerprint: fp,
2024-03-20 12:59:47 -04:00
}
2024-03-11 00:53:58 -04:00
}
2024-03-22 04:10:15 -04:00
if security == "reality" {
2024-03-20 12:59:47 -04:00
result.VLESSOptions.OutboundTLSOptionsContainer.TLS.Reality = &model.OutboundRealityOptions{
Enabled: true,
2024-03-22 04:10:15 -04:00
PublicKey: pbk,
ShortID: sid,
2024-03-11 00:53:58 -04:00
}
}
2024-03-22 04:10:15 -04:00
2024-03-11 00:53:58 -04:00
if params.Get("type") == "ws" {
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "ws",
WebsocketOptions: model.V2RayWebsocketOptions{
2024-03-22 04:10:15 -04:00
Path: path,
2024-03-10 15:13:42 -04:00
},
2024-03-11 00:53:58 -04:00
}
2024-03-22 04:10:15 -04:00
result.VLESSOptions.Transport.WebsocketOptions.Headers["Host"] = host
2024-03-11 00:53:58 -04:00
}
2024-03-22 04:10:15 -04:00
2024-03-11 00:53:58 -04:00
if params.Get("type") == "quic" {
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "quic",
QUICOptions: model.V2RayQUICOptions{},
2024-03-11 00:53:58 -04:00
}
}
2024-03-22 04:10:15 -04:00
2024-03-11 00:53:58 -04:00
if params.Get("type") == "grpc" {
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "grpc",
GRPCOptions: model.V2RayGRPCOptions{
2024-03-22 04:10:15 -04:00
ServiceName: serviceName,
2024-03-11 00:53:58 -04:00
},
}
}
2024-03-22 04:10:15 -04:00
2024-03-11 00:53:58 -04:00
if params.Get("type") == "http" {
2024-03-22 04:10:15 -04:00
hosts, err := url.QueryUnescape(host)
2024-03-11 00:53:58 -04:00
if err != nil {
2024-03-22 04:10:15 -04:00
return model.Outbound{}, &ParseError{
Type: ErrCannotParseParams,
Raw: proxy,
Message: err.Error(),
}
2024-03-11 00:53:58 -04:00
}
result.VLESSOptions.Transport = &model.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "http",
HTTPOptions: model.V2RayHTTPOptions{
2024-03-22 04:10:15 -04:00
Host: strings.Split(hosts, ","),
2024-03-11 00:53:58 -04:00
},
}
2024-03-10 15:13:42 -04:00
}
return result, nil
}