mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
185 lines
7.6 KiB
Go
185 lines
7.6 KiB
Go
package link
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/sagernet/sing-box/common"
|
|
C "github.com/sagernet/sing-box/constant"
|
|
"github.com/sagernet/sing-box/option"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
"github.com/sagernet/sing/common/json/badoption"
|
|
)
|
|
|
|
func parseVLESSLink(link string) (option.Outbound, error) {
|
|
linkURL, err := url.Parse(link)
|
|
if err != nil {
|
|
return option.Outbound{}, err
|
|
}
|
|
if linkURL.User == nil || linkURL.User.Username() == "" {
|
|
return option.Outbound{}, E.New("missing uuid")
|
|
}
|
|
var options option.VLESSOutboundOptions
|
|
TLSOptions := option.OutboundTLSOptions{
|
|
ECH: &option.OutboundECHOptions{},
|
|
UTLS: &option.OutboundUTLSOptions{},
|
|
Reality: &option.OutboundRealityOptions{},
|
|
}
|
|
options.UUID = linkURL.User.Username()
|
|
options.Server = linkURL.Hostname()
|
|
TLSOptions.ServerName = linkURL.Hostname()
|
|
options.ServerPort = common.StringToType[uint16](linkURL.Port())
|
|
proxy := map[string]string{}
|
|
for key, values := range linkURL.Query() {
|
|
value := values[0]
|
|
switch key {
|
|
case "key", "alpn", "seed", "path", "host":
|
|
proxy[key] = value
|
|
default:
|
|
proxy[key] = value
|
|
}
|
|
}
|
|
postQuantumSupport, exists := proxy["support-x25519mlkem768"]
|
|
if !exists {
|
|
postQuantumSupport = proxy["support_x25519mlkem768"]
|
|
}
|
|
TLSOptions.Reality.SupportX25519MLKEM768 = strings.EqualFold(postQuantumSupport, "true") || postQuantumSupport == "1"
|
|
for key, value := range proxy {
|
|
switch key {
|
|
case "type":
|
|
Transport := option.V2RayTransportOptions{
|
|
HTTPOptions: option.V2RayHTTPOptions{
|
|
Host: badoption.Listable[string]{},
|
|
Headers: badoption.HTTPHeader{},
|
|
},
|
|
GRPCOptions: option.V2RayGRPCOptions{},
|
|
}
|
|
switch value {
|
|
case "ws":
|
|
Transport.Type = C.V2RayTransportTypeWebsocket
|
|
Transport.WebsocketOptions = v2rayTransportWs(proxy["host"], proxy["path"])
|
|
case "http":
|
|
Transport.Type = C.V2RayTransportTypeHTTP
|
|
if host, exists := proxy["host"]; exists && host != "" {
|
|
Transport.HTTPOptions.Host = strings.Split(host, ",")
|
|
}
|
|
if path, exists := proxy["path"]; exists && path != "" {
|
|
Transport.HTTPOptions.Path = path
|
|
}
|
|
case "grpc":
|
|
Transport.Type = C.V2RayTransportTypeGRPC
|
|
if serviceName, exists := proxy["serviceName"]; exists && serviceName != "" {
|
|
Transport.GRPCOptions.ServiceName = serviceName
|
|
}
|
|
case "xhttp":
|
|
Transport.Type = C.V2RayTransportTypeXHTTP
|
|
if alpn, exists := proxy["alpn"]; exists && alpn != "" {
|
|
TLSOptions.ALPN = strings.Split(alpn, ",")
|
|
} else {
|
|
TLSOptions.ALPN = []string{"h2", "http/1.1"}
|
|
}
|
|
if host, exists := proxy["host"]; exists && host != "" {
|
|
Transport.XHTTPOptions.Host = host
|
|
}
|
|
if path, exists := proxy["path"]; exists && path != "" {
|
|
Transport.XHTTPOptions.Path = path
|
|
}
|
|
if mode, exists := proxy["mode"]; exists && mode != "" {
|
|
Transport.XHTTPOptions.Mode = mode
|
|
}
|
|
if padding := proxy["x_padding_bytes"]; padding != "" {
|
|
if paddingRange, err := common.ParseXHTTPRange(padding); err == nil {
|
|
Transport.XHTTPOptions.XPaddingBytes = paddingRange
|
|
}
|
|
}
|
|
if extra := proxy["extra"]; extra != "" {
|
|
decodedExtra, err := common.DecodeBase64URLSafe(extra)
|
|
var fields map[string]json.RawMessage
|
|
if err == nil && json.Unmarshal([]byte(decodedExtra), &fields) == nil && fields != nil {
|
|
xhttpOptions := &Transport.XHTTPOptions
|
|
decodeOption(fields, "headers", "headers", &xhttpOptions.Headers)
|
|
decodeOption(fields, "x_padding_bytes", "xPaddingBytes", &xhttpOptions.XPaddingBytes)
|
|
decodeOption(fields, "x_padding_obfs_mode", "xPaddingObfsMode", &xhttpOptions.XPaddingObfsMode)
|
|
decodeOption(fields, "x_padding_key", "xPaddingKey", &xhttpOptions.XPaddingKey)
|
|
decodeOption(fields, "x_padding_header", "xPaddingHeader", &xhttpOptions.XPaddingHeader)
|
|
decodeOption(fields, "x_padding_placement", "xPaddingPlacement", &xhttpOptions.XPaddingPlacement)
|
|
decodeOption(fields, "x_padding_method", "xPaddingMethod", &xhttpOptions.XPaddingMethod)
|
|
decodeOption(fields, "no_grpc_header", "noGRPCHeader", &xhttpOptions.NoGRPCHeader)
|
|
decodeOption(fields, "no_sse_header", "noSSEHeader", &xhttpOptions.NoSSEHeader)
|
|
decodeOption(fields, "sc_max_each_post_bytes", "scMaxEachPostBytes", &xhttpOptions.ScMaxEachPostBytes)
|
|
decodeOption(fields, "sc_min_posts_interval_ms", "scMinPostsIntervalMs", &xhttpOptions.ScMinPostsIntervalMs)
|
|
decodeOption(fields, "sc_max_buffered_posts", "scMaxBufferedPosts", &xhttpOptions.ScMaxBufferedPosts)
|
|
decodeOption(fields, "sc_stream_up_server_secs", "scStreamUpServerSecs", &xhttpOptions.ScStreamUpServerSecs)
|
|
decodeOption(fields, "server_max_header_bytes", "serverMaxHeaderBytes", &xhttpOptions.ServerMaxHeaderBytes)
|
|
decodeOption(fields, "uplink_http_method", "uplinkHTTPMethod", &xhttpOptions.UplinkHTTPMethod)
|
|
decodeOption(fields, "uplink_data_placement", "uplinkDataPlacement", &xhttpOptions.UplinkDataPlacement)
|
|
decodeOption(fields, "uplink_data_key", "uplinkDataKey", &xhttpOptions.UplinkDataKey)
|
|
decodeOption(fields, "uplink_chunk_size", "uplinkChunkSize", &xhttpOptions.UplinkChunkSize)
|
|
decodeOption(fields, "session_placement", "sessionPlacement", &xhttpOptions.SessionPlacement)
|
|
decodeOption(fields, "session_key", "sessionKey", &xhttpOptions.SessionKey)
|
|
decodeOption(fields, "session_id_table", "sessionIDTable", &xhttpOptions.SessionIDTable)
|
|
decodeOption(fields, "session_id_length", "sessionIDLength", &xhttpOptions.SessionIDLength)
|
|
decodeOption(fields, "seq_placement", "seqPlacement", &xhttpOptions.SeqPlacement)
|
|
decodeOption(fields, "seq_key", "seqKey", &xhttpOptions.SeqKey)
|
|
var multiplexFields map[string]json.RawMessage
|
|
decodeOption(fields, "xmux", "xmux", &multiplexFields)
|
|
if multiplexFields != nil {
|
|
multiplexOptions := &option.V2RayXHTTPXmuxOptions{}
|
|
decodeOption(multiplexFields, "max_concurrency", "maxConcurrency", &multiplexOptions.MaxConcurrency)
|
|
decodeOption(multiplexFields, "max_connections", "maxConnections", &multiplexOptions.MaxConnections)
|
|
decodeOption(multiplexFields, "c_max_reuse_times", "cMaxReuseTimes", &multiplexOptions.CMaxReuseTimes)
|
|
decodeOption(multiplexFields, "h_max_request_times", "hMaxRequestTimes", &multiplexOptions.HMaxRequestTimes)
|
|
decodeOption(multiplexFields, "h_max_reusable_secs", "hMaxReusableSecs", &multiplexOptions.HMaxReusableSecs)
|
|
decodeOption(multiplexFields, "h_keep_alive_period", "hKeepAlivePeriod", &multiplexOptions.HKeepAlivePeriod)
|
|
xhttpOptions.Xmux = multiplexOptions
|
|
}
|
|
}
|
|
}
|
|
default:
|
|
continue
|
|
}
|
|
options.Transport = &Transport
|
|
case "security":
|
|
if value == "tls" {
|
|
TLSOptions.Enabled = true
|
|
} else if value == "reality" {
|
|
TLSOptions.Enabled = true
|
|
TLSOptions.Reality.Enabled = true
|
|
}
|
|
case "insecure", "skip-cert-verify":
|
|
if value == "1" || value == "true" {
|
|
TLSOptions.Insecure = true
|
|
}
|
|
case "serviceName", "sni", "peer":
|
|
TLSOptions.ServerName = value
|
|
case "alpn":
|
|
TLSOptions.ALPN = strings.Split(value, ",")
|
|
case "fp":
|
|
TLSOptions.UTLS.Enabled = true
|
|
TLSOptions.UTLS.Fingerprint = value
|
|
case "flow":
|
|
if value == "xtls-rprx-vision" {
|
|
options.Flow = "xtls-rprx-vision"
|
|
}
|
|
case "pbk":
|
|
TLSOptions.Reality.PublicKey = value
|
|
case "sid":
|
|
TLSOptions.Reality.ShortID = value
|
|
case "tfo", "tcp-fast-open", "tcp_fast_open":
|
|
if value == "1" || value == "true" {
|
|
options.TCPFastOpen = true
|
|
}
|
|
}
|
|
}
|
|
outbound := option.Outbound{
|
|
Type: C.TypeVLESS,
|
|
Tag: linkURL.Fragment,
|
|
}
|
|
if TLSOptions.Enabled {
|
|
options.TLS = &TLSOptions
|
|
}
|
|
outbound.Options = &options
|
|
return outbound, nil
|
|
}
|