2022-08-19 22:00:22 +08:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2023-01-24 16:34:52 +08:00
|
|
|
"errors"
|
2023-01-27 11:31:58 +08:00
|
|
|
"fmt"
|
2022-08-19 22:00:22 +08:00
|
|
|
"net/url"
|
2023-01-27 11:31:58 +08:00
|
|
|
"strconv"
|
2022-08-19 22:00:22 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-01-24 16:34:52 +08:00
|
|
|
func handleVShareLink(names map[string]int, url *url.URL, scheme string, proxy map[string]any) error {
|
2022-08-19 22:00:22 +08:00
|
|
|
// Xray VMessAEAD / VLESS share link standard
|
|
|
|
// https://github.com/XTLS/Xray-core/discussions/716
|
|
|
|
query := url.Query()
|
|
|
|
proxy["name"] = uniqueName(names, url.Fragment)
|
2023-01-24 16:34:52 +08:00
|
|
|
if url.Hostname() == "" {
|
|
|
|
return errors.New("url.Hostname() is empty")
|
|
|
|
}
|
|
|
|
if url.Port() == "" {
|
|
|
|
return errors.New("url.Port() is empty")
|
|
|
|
}
|
2022-08-19 22:00:22 +08:00
|
|
|
proxy["type"] = scheme
|
|
|
|
proxy["server"] = url.Hostname()
|
|
|
|
proxy["port"] = url.Port()
|
|
|
|
proxy["uuid"] = url.User.Username()
|
|
|
|
proxy["udp"] = true
|
|
|
|
proxy["skip-cert-verify"] = false
|
|
|
|
proxy["tls"] = false
|
|
|
|
tls := strings.ToLower(query.Get("security"))
|
|
|
|
if strings.HasSuffix(tls, "tls") {
|
|
|
|
proxy["tls"] = true
|
2023-02-11 16:40:01 +08:00
|
|
|
if fingerprint := query.Get("fp"); fingerprint == "" {
|
|
|
|
proxy["client-fingerprint"] = "chrome"
|
|
|
|
} else {
|
|
|
|
proxy["client-fingerprint"] = fingerprint
|
|
|
|
}
|
2022-08-19 22:00:22 +08:00
|
|
|
}
|
|
|
|
if sni := query.Get("sni"); sni != "" {
|
|
|
|
proxy["servername"] = sni
|
|
|
|
}
|
2023-03-19 17:31:52 +08:00
|
|
|
if realityPublicKey := query.Get("pbk"); realityPublicKey != "" {
|
|
|
|
proxy["reality-opts"] = map[string]any{
|
|
|
|
"public-key": realityPublicKey,
|
|
|
|
"short-id": query.Get("sid"),
|
|
|
|
}
|
|
|
|
}
|
2022-08-19 22:00:22 +08:00
|
|
|
|
2023-01-10 18:25:05 +08:00
|
|
|
switch query.Get("packetEncoding") {
|
|
|
|
case "none":
|
|
|
|
case "packet":
|
|
|
|
proxy["packet-addr"] = true
|
|
|
|
default:
|
|
|
|
proxy["xudp"] = true
|
|
|
|
}
|
|
|
|
|
2022-08-19 22:00:22 +08:00
|
|
|
network := strings.ToLower(query.Get("type"))
|
|
|
|
if network == "" {
|
|
|
|
network = "tcp"
|
|
|
|
}
|
|
|
|
fakeType := strings.ToLower(query.Get("headerType"))
|
|
|
|
if fakeType == "http" {
|
|
|
|
network = "http"
|
|
|
|
} else if network == "http" {
|
|
|
|
network = "h2"
|
|
|
|
}
|
|
|
|
proxy["network"] = network
|
|
|
|
switch network {
|
|
|
|
case "tcp":
|
|
|
|
if fakeType != "none" {
|
|
|
|
headers := make(map[string]any)
|
|
|
|
httpOpts := make(map[string]any)
|
|
|
|
httpOpts["path"] = []string{"/"}
|
|
|
|
|
|
|
|
if host := query.Get("host"); host != "" {
|
|
|
|
headers["Host"] = []string{host}
|
|
|
|
}
|
|
|
|
|
|
|
|
if method := query.Get("method"); method != "" {
|
|
|
|
httpOpts["method"] = method
|
|
|
|
}
|
|
|
|
|
|
|
|
if path := query.Get("path"); path != "" {
|
|
|
|
httpOpts["path"] = []string{path}
|
|
|
|
}
|
|
|
|
httpOpts["headers"] = headers
|
|
|
|
proxy["http-opts"] = httpOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
case "http":
|
|
|
|
headers := make(map[string]any)
|
|
|
|
h2Opts := make(map[string]any)
|
|
|
|
h2Opts["path"] = []string{"/"}
|
|
|
|
if path := query.Get("path"); path != "" {
|
|
|
|
h2Opts["path"] = []string{path}
|
|
|
|
}
|
|
|
|
if host := query.Get("host"); host != "" {
|
|
|
|
h2Opts["host"] = []string{host}
|
|
|
|
}
|
|
|
|
h2Opts["headers"] = headers
|
|
|
|
proxy["h2-opts"] = h2Opts
|
|
|
|
|
|
|
|
case "ws":
|
|
|
|
headers := make(map[string]any)
|
|
|
|
wsOpts := make(map[string]any)
|
|
|
|
headers["User-Agent"] = RandUserAgent()
|
|
|
|
headers["Host"] = query.Get("host")
|
|
|
|
wsOpts["path"] = query.Get("path")
|
|
|
|
wsOpts["headers"] = headers
|
|
|
|
|
2023-01-27 11:31:58 +08:00
|
|
|
if earlyData := query.Get("ed"); earlyData != "" {
|
|
|
|
med, err := strconv.Atoi(earlyData)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("bad WebSocket max early data size: %v", err)
|
|
|
|
}
|
|
|
|
wsOpts["max-early-data"] = med
|
|
|
|
}
|
|
|
|
if earlyDataHeader := query.Get("eh"); earlyDataHeader != "" {
|
|
|
|
wsOpts["early-data-header-name"] = earlyDataHeader
|
|
|
|
}
|
|
|
|
|
2022-08-19 22:00:22 +08:00
|
|
|
proxy["ws-opts"] = wsOpts
|
|
|
|
|
|
|
|
case "grpc":
|
|
|
|
grpcOpts := make(map[string]any)
|
|
|
|
grpcOpts["grpc-service-name"] = query.Get("serviceName")
|
|
|
|
proxy["grpc-opts"] = grpcOpts
|
|
|
|
}
|
2023-01-24 16:34:52 +08:00
|
|
|
return nil
|
2022-08-19 22:00:22 +08:00
|
|
|
}
|