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

164 lines
3.9 KiB
Go
Raw Normal View History

2024-03-10 15:13:42 -04:00
package parser
import (
2024-08-04 05:01:09 -04:00
"fmt"
2024-03-10 15:13:42 -04:00
"net/url"
"strings"
2024-09-19 06:12:24 -04:00
"github.com/nitezs/sub2sing-box/constant"
"github.com/nitezs/sub2sing-box/model"
2024-10-02 13:24:54 -04:00
"github.com/sagernet/sing-box/option"
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
2024-08-04 05:01:09 -04:00
link, err := url.Parse(proxy)
if err != nil {
2024-03-22 04:10:15 -04:00
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
2024-08-04 05:01:09 -04:00
Message: "url parse error",
2024-03-22 04:10:15 -04:00
Raw: proxy,
}
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
2024-08-04 05:01:09 -04:00
server := link.Hostname()
if server == "" {
2024-03-22 04:10:15 -04:00
return model.Outbound{}, &ParseError{
Type: ErrInvalidStruct,
2024-08-04 05:01:09 -04:00
Message: "missing server host",
2024-03-22 04:10:15 -04:00
Raw: proxy,
}
2024-03-10 15:13:42 -04:00
}
2024-08-04 05:01:09 -04:00
portStr := link.Port()
2024-03-22 04:10:15 -04:00
port, err := ParsePort(portStr)
2024-03-10 15:13:42 -04:00
if err != nil {
2024-04-23 02:41:14 -04:00
return model.Outbound{}, &ParseError{
Type: ErrInvalidPort,
Message: err.Error(),
Raw: proxy,
}
2024-03-10 15:13:42 -04:00
}
2024-03-22 04:10:15 -04:00
2024-08-04 05:01:09 -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-03-22 04:10:15 -04:00
enableUTLS := fp != ""
insecureBool := insecure == "1"
var alpn []string
if strings.Contains(alpnStr, ",") {
alpn = strings.Split(alpnStr, ",")
} else {
alpn = nil
}
2024-08-04 05:01:09 -04:00
remarks := link.Fragment
if remarks == "" {
remarks = fmt.Sprintf("%s:%s", server, portStr)
}
remarks = strings.TrimSpace(remarks)
2024-03-22 04:10:15 -04:00
2024-10-02 13:24:54 -04:00
result := model.Outbound{Outbound: option.Outbound{
2024-03-10 15:13:42 -04:00
Type: "vless",
2024-03-11 09:00:13 -04:00
Tag: remarks,
2024-10-02 13:24:54 -04:00
VLESSOptions: option.VLESSOutboundOptions{
ServerOptions: option.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-10-02 13:24:54 -04:00
}}
2024-03-22 04:10:15 -04:00
if security == "tls" {
2024-10-02 13:24:54 -04:00
result.VLESSOptions.OutboundTLSOptionsContainer = option.OutboundTLSOptionsContainer{
TLS: &option.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
if security == "reality" {
2024-10-02 13:24:54 -04:00
result.VLESSOptions.OutboundTLSOptionsContainer = option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
ALPN: alpn,
ServerName: sni,
Insecure: insecureBool,
2024-10-02 13:24:54 -04:00
Reality: &option.OutboundRealityOptions{
2024-05-05 14:07:30 -04:00
Enabled: true,
PublicKey: pbk,
ShortID: sid,
},
2024-10-22 08:29:45 -04:00
UTLS: &option.OutboundUTLSOptions{
Enabled: true,
Fingerprint: fp,
},
},
}
2024-03-11 00:53:58 -04:00
}
2024-03-22 04:10:15 -04:00
2024-04-23 02:41:14 -04:00
if _type == "ws" {
2024-10-02 13:24:54 -04:00
result.VLESSOptions.Transport = &option.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "ws",
2024-10-02 13:24:54 -04:00
WebsocketOptions: option.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
}
if host != "" {
if result.VLESSOptions.Transport.WebsocketOptions.Headers == nil {
2024-10-02 13:24:54 -04:00
result.VLESSOptions.Transport.WebsocketOptions.Headers = make(map[string]option.Listable[string])
}
2024-10-02 13:24:54 -04:00
result.VLESSOptions.Transport.WebsocketOptions.Headers["Host"] = option.Listable[string]{host}
}
2024-03-11 00:53:58 -04:00
}
2024-03-22 04:10:15 -04:00
2024-04-23 02:41:14 -04:00
if _type == "quic" {
2024-10-02 13:24:54 -04:00
result.VLESSOptions.Transport = &option.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "quic",
2024-10-02 13:24:54 -04:00
QUICOptions: option.V2RayQUICOptions{},
2024-03-11 00:53:58 -04:00
}
}
2024-03-22 04:10:15 -04:00
2024-04-23 02:41:14 -04:00
if _type == "grpc" {
2024-10-02 13:24:54 -04:00
result.VLESSOptions.Transport = &option.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "grpc",
2024-10-02 13:24:54 -04:00
GRPCOptions: option.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-04-23 02:41:14 -04:00
if _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{
2024-08-05 03:01:05 -04:00
Type: ErrInvalidStruct,
2024-03-22 04:10:15 -04:00
Raw: proxy,
Message: err.Error(),
}
2024-03-11 00:53:58 -04:00
}
2024-10-02 13:24:54 -04:00
result.VLESSOptions.Transport = &option.V2RayTransportOptions{
2024-03-11 00:53:58 -04:00
Type: "http",
2024-10-02 13:24:54 -04:00
HTTPOptions: option.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
}
2024-08-05 03:01:05 -04:00
if enableUTLS {
2024-10-02 13:24:54 -04:00
result.VLESSOptions.OutboundTLSOptionsContainer.TLS.UTLS = &option.OutboundUTLSOptions{
2024-08-05 03:01:05 -04:00
Enabled: enableUTLS,
Fingerprint: fp,
}
}
2024-03-10 15:13:42 -04:00
return result, nil
}