2022-06-03 04:47:58 +08:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2022-06-08 01:50:14 +08:00
|
|
|
"strconv"
|
2022-06-03 04:47:58 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConvertsV2Ray convert V2Ray subscribe proxies data to clash proxies config
|
|
|
|
func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
2022-06-15 03:03:26 +08:00
|
|
|
data := DecodeBase64(buf)
|
2022-06-03 04:47:58 +08:00
|
|
|
|
|
|
|
arr := strings.Split(string(data), "\n")
|
|
|
|
|
|
|
|
proxies := make([]map[string]any, 0, len(arr))
|
|
|
|
names := make(map[string]int, 200)
|
|
|
|
|
|
|
|
for _, line := range arr {
|
|
|
|
line = strings.TrimRight(line, " \r")
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
scheme, body, found := strings.Cut(line, "://")
|
|
|
|
if !found {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
scheme = strings.ToLower(scheme)
|
|
|
|
switch scheme {
|
2022-06-08 01:50:14 +08:00
|
|
|
case "hysteria":
|
|
|
|
urlHysteria, err := url.Parse(line)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
query := urlHysteria.Query()
|
|
|
|
name := uniqueName(names, urlHysteria.Fragment)
|
|
|
|
hysteria := make(map[string]any, 20)
|
|
|
|
|
|
|
|
hysteria["name"] = name
|
|
|
|
hysteria["type"] = scheme
|
|
|
|
hysteria["server"] = urlHysteria.Hostname()
|
|
|
|
hysteria["port"] = urlHysteria.Port()
|
2022-06-09 00:22:47 +08:00
|
|
|
hysteria["sni"] = query.Get("peer")
|
2022-06-08 01:50:14 +08:00
|
|
|
hysteria["obfs"] = query.Get("obfs")
|
2022-10-27 18:37:27 +08:00
|
|
|
hysteria["alpn"] = []string{query.Get("alpn")}
|
2022-06-09 00:22:47 +08:00
|
|
|
hysteria["auth_str"] = query.Get("auth")
|
2022-06-08 01:50:14 +08:00
|
|
|
hysteria["protocol"] = query.Get("protocol")
|
2022-06-15 23:18:06 +08:00
|
|
|
up := query.Get("up")
|
|
|
|
down := query.Get("down")
|
|
|
|
if up == "" {
|
|
|
|
up = query.Get("upmbps")
|
|
|
|
}
|
|
|
|
if down == "" {
|
|
|
|
down = query.Get("downmbps")
|
|
|
|
}
|
|
|
|
hysteria["down"] = down
|
|
|
|
hysteria["up"] = up
|
2022-06-08 01:50:14 +08:00
|
|
|
hysteria["skip-cert-verify"], _ = strconv.ParseBool(query.Get("insecure"))
|
|
|
|
|
|
|
|
proxies = append(proxies, hysteria)
|
|
|
|
|
2022-06-03 04:47:58 +08:00
|
|
|
case "trojan":
|
|
|
|
urlTrojan, err := url.Parse(line)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-08 01:50:14 +08:00
|
|
|
query := urlTrojan.Query()
|
2022-06-03 04:47:58 +08:00
|
|
|
|
|
|
|
name := uniqueName(names, urlTrojan.Fragment)
|
|
|
|
trojan := make(map[string]any, 20)
|
|
|
|
|
|
|
|
trojan["name"] = name
|
|
|
|
trojan["type"] = scheme
|
|
|
|
trojan["server"] = urlTrojan.Hostname()
|
|
|
|
trojan["port"] = urlTrojan.Port()
|
|
|
|
trojan["password"] = urlTrojan.User.Username()
|
|
|
|
trojan["udp"] = true
|
|
|
|
trojan["skip-cert-verify"] = false
|
|
|
|
|
2022-06-08 01:50:14 +08:00
|
|
|
sni := query.Get("sni")
|
2022-06-03 04:47:58 +08:00
|
|
|
if sni != "" {
|
|
|
|
trojan["sni"] = sni
|
|
|
|
}
|
|
|
|
|
2022-06-08 01:50:14 +08:00
|
|
|
network := strings.ToLower(query.Get("type"))
|
2022-06-03 04:47:58 +08:00
|
|
|
if network != "" {
|
|
|
|
trojan["network"] = network
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:17:33 +08:00
|
|
|
switch network {
|
|
|
|
case "ws":
|
2022-06-03 04:47:58 +08:00
|
|
|
headers := make(map[string]any)
|
|
|
|
wsOpts := make(map[string]any)
|
|
|
|
|
|
|
|
headers["User-Agent"] = RandUserAgent()
|
|
|
|
|
2022-06-08 01:50:14 +08:00
|
|
|
wsOpts["path"] = query.Get("path")
|
2022-06-03 04:47:58 +08:00
|
|
|
wsOpts["headers"] = headers
|
|
|
|
|
|
|
|
trojan["ws-opts"] = wsOpts
|
2022-06-07 03:17:33 +08:00
|
|
|
|
|
|
|
case "grpc":
|
|
|
|
grpcOpts := make(map[string]any)
|
2022-06-08 01:50:14 +08:00
|
|
|
grpcOpts["grpc-service-name"] = query.Get("serviceName")
|
2022-06-07 03:17:33 +08:00
|
|
|
trojan["grpc-opts"] = grpcOpts
|
2022-06-03 04:47:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
proxies = append(proxies, trojan)
|
2022-06-07 03:17:33 +08:00
|
|
|
|
|
|
|
case "vless":
|
2022-08-19 22:00:22 +08:00
|
|
|
urlVLess, err := url.Parse(line)
|
2022-06-07 03:17:33 +08:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-08-19 22:00:22 +08:00
|
|
|
query := urlVLess.Query()
|
2022-06-07 03:17:33 +08:00
|
|
|
vless := make(map[string]any, 20)
|
2022-08-19 22:00:22 +08:00
|
|
|
handleVShareLink(names, urlVLess, scheme, vless)
|
|
|
|
if flow := query.Get("flow"); flow != "" {
|
|
|
|
vless["flow"] = strings.ToLower(flow)
|
2022-06-07 03:17:33 +08:00
|
|
|
}
|
|
|
|
proxies = append(proxies, vless)
|
|
|
|
|
2022-06-03 04:47:58 +08:00
|
|
|
case "vmess":
|
2022-08-19 21:17:44 +08:00
|
|
|
// V2RayN-styled share link
|
|
|
|
// https://github.com/2dust/v2rayN/wiki/%E5%88%86%E4%BA%AB%E9%93%BE%E6%8E%A5%E6%A0%BC%E5%BC%8F%E8%AF%B4%E6%98%8E(ver-2)
|
|
|
|
dcBuf, err := tryDecodeBase64([]byte(body))
|
2022-06-03 04:47:58 +08:00
|
|
|
if err != nil {
|
2022-08-19 22:00:22 +08:00
|
|
|
// Xray VMessAEAD share link
|
|
|
|
urlVMess, err := url.Parse(line)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
query := urlVMess.Query()
|
|
|
|
vmess := make(map[string]any, 20)
|
|
|
|
handleVShareLink(names, urlVMess, scheme, vmess)
|
|
|
|
vmess["alterId"] = 0
|
|
|
|
vmess["cipher"] = "auto"
|
|
|
|
if encryption := query.Get("encryption"); encryption != "" {
|
|
|
|
vmess["cipher"] = encryption
|
|
|
|
}
|
2022-10-02 21:10:29 +08:00
|
|
|
if packetEncoding := query.Get("packetEncoding"); packetEncoding != "" {
|
|
|
|
switch packetEncoding {
|
|
|
|
case "packet":
|
|
|
|
vmess["packet-addr"] = true
|
|
|
|
case "xudp":
|
|
|
|
vmess["xudp"] = true
|
|
|
|
}
|
|
|
|
}
|
2022-08-19 22:00:22 +08:00
|
|
|
proxies = append(proxies, vmess)
|
2022-06-03 04:47:58 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonDc := json.NewDecoder(bytes.NewReader(dcBuf))
|
|
|
|
values := make(map[string]any, 20)
|
|
|
|
|
|
|
|
if jsonDc.Decode(&values) != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := uniqueName(names, values["ps"].(string))
|
|
|
|
vmess := make(map[string]any, 20)
|
|
|
|
|
|
|
|
vmess["name"] = name
|
|
|
|
vmess["type"] = scheme
|
|
|
|
vmess["server"] = values["add"]
|
|
|
|
vmess["port"] = values["port"]
|
|
|
|
vmess["uuid"] = values["id"]
|
2022-08-07 20:43:11 +08:00
|
|
|
if alterId, ok := values["aid"]; ok {
|
|
|
|
vmess["alterId"] = alterId
|
|
|
|
} else {
|
|
|
|
vmess["alterId"] = 0
|
|
|
|
}
|
2022-06-03 04:47:58 +08:00
|
|
|
vmess["udp"] = true
|
2022-06-21 00:18:34 +08:00
|
|
|
vmess["tls"] = false
|
2022-06-03 04:47:58 +08:00
|
|
|
vmess["skip-cert-verify"] = false
|
|
|
|
|
2022-08-19 21:17:44 +08:00
|
|
|
vmess["cipher"] = "auto"
|
|
|
|
if cipher, ok := values["scy"]; ok && cipher != "" {
|
|
|
|
vmess["cipher"] = cipher
|
2022-06-10 03:15:30 +08:00
|
|
|
}
|
|
|
|
|
2022-08-19 21:17:44 +08:00
|
|
|
if sni, ok := values["sni"]; ok && sni != "" {
|
2022-06-10 03:15:30 +08:00
|
|
|
vmess["servername"] = sni
|
2022-06-07 03:17:33 +08:00
|
|
|
}
|
|
|
|
|
2022-06-03 04:47:58 +08:00
|
|
|
network := strings.ToLower(values["net"].(string))
|
2022-06-21 00:18:34 +08:00
|
|
|
if values["type"] == "http" {
|
|
|
|
network = "http"
|
|
|
|
} else if network == "http" {
|
|
|
|
network = "h2"
|
|
|
|
}
|
2022-06-03 04:47:58 +08:00
|
|
|
vmess["network"] = network
|
|
|
|
|
|
|
|
tls := strings.ToLower(values["tls"].(string))
|
2022-08-19 21:17:44 +08:00
|
|
|
if strings.HasSuffix(tls, "tls") {
|
2022-06-03 04:47:58 +08:00
|
|
|
vmess["tls"] = true
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:17:33 +08:00
|
|
|
switch network {
|
|
|
|
case "http":
|
|
|
|
headers := make(map[string]any)
|
|
|
|
httpOpts := make(map[string]any)
|
2022-08-19 21:17:44 +08:00
|
|
|
if host, ok := values["host"]; ok && host != "" {
|
|
|
|
headers["Host"] = []string{host.(string)}
|
2022-06-21 00:18:34 +08:00
|
|
|
}
|
|
|
|
httpOpts["path"] = []string{"/"}
|
2022-08-19 21:17:44 +08:00
|
|
|
if path, ok := values["path"]; ok && path != "" {
|
|
|
|
httpOpts["path"] = []string{path.(string)}
|
2022-06-21 00:18:34 +08:00
|
|
|
}
|
2022-06-07 03:17:33 +08:00
|
|
|
httpOpts["headers"] = headers
|
|
|
|
|
|
|
|
vmess["http-opts"] = httpOpts
|
|
|
|
|
|
|
|
case "h2":
|
|
|
|
headers := make(map[string]any)
|
|
|
|
h2Opts := make(map[string]any)
|
2022-08-19 21:17:44 +08:00
|
|
|
if host, ok := values["host"]; ok && host != "" {
|
|
|
|
headers["Host"] = []string{host.(string)}
|
2022-06-21 00:18:34 +08:00
|
|
|
}
|
2022-06-07 03:17:33 +08:00
|
|
|
|
|
|
|
h2Opts["path"] = values["path"]
|
|
|
|
h2Opts["headers"] = headers
|
|
|
|
|
|
|
|
vmess["h2-opts"] = h2Opts
|
|
|
|
|
|
|
|
case "ws":
|
2022-06-03 04:47:58 +08:00
|
|
|
headers := make(map[string]any)
|
|
|
|
wsOpts := make(map[string]any)
|
2022-06-21 00:18:34 +08:00
|
|
|
wsOpts["path"] = []string{"/"}
|
2022-08-19 21:17:44 +08:00
|
|
|
if host, ok := values["host"]; ok && host != "" {
|
|
|
|
headers["Host"] = host.(string)
|
2022-06-10 03:15:30 +08:00
|
|
|
}
|
2022-08-19 21:17:44 +08:00
|
|
|
if path, ok := values["path"]; ok && path != "" {
|
|
|
|
wsOpts["path"] = path.(string)
|
2022-06-03 04:47:58 +08:00
|
|
|
}
|
|
|
|
wsOpts["headers"] = headers
|
|
|
|
vmess["ws-opts"] = wsOpts
|
2022-06-07 03:17:33 +08:00
|
|
|
|
|
|
|
case "grpc":
|
|
|
|
grpcOpts := make(map[string]any)
|
|
|
|
grpcOpts["grpc-service-name"] = values["path"]
|
|
|
|
vmess["grpc-opts"] = grpcOpts
|
2022-06-03 04:47:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
proxies = append(proxies, vmess)
|
2022-06-07 03:17:33 +08:00
|
|
|
|
2022-06-03 04:47:58 +08:00
|
|
|
case "ss":
|
|
|
|
urlSS, err := url.Parse(line)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := uniqueName(names, urlSS.Fragment)
|
|
|
|
port := urlSS.Port()
|
|
|
|
|
|
|
|
if port == "" {
|
2022-06-15 03:03:26 +08:00
|
|
|
dcBuf, err := encRaw.DecodeString(urlSS.Host)
|
2022-06-03 04:47:58 +08:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
urlSS, err = url.Parse("ss://" + string(dcBuf))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
cipher = urlSS.User.Username()
|
|
|
|
password string
|
|
|
|
)
|
|
|
|
|
|
|
|
if password, found = urlSS.User.Password(); !found {
|
2022-06-22 22:18:13 +08:00
|
|
|
dcBuf, _ := enc.DecodeString(cipher)
|
|
|
|
if !strings.Contains(string(dcBuf), "2022-blake3") {
|
|
|
|
dcBuf, _ = encRaw.DecodeString(cipher)
|
2022-06-03 04:47:58 +08:00
|
|
|
}
|
|
|
|
cipher, password, found = strings.Cut(string(dcBuf), ":")
|
|
|
|
if !found {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-05 14:41:07 +08:00
|
|
|
ss := make(map[string]any, 10)
|
2022-06-03 04:47:58 +08:00
|
|
|
|
|
|
|
ss["name"] = name
|
|
|
|
ss["type"] = scheme
|
|
|
|
ss["server"] = urlSS.Hostname()
|
|
|
|
ss["port"] = urlSS.Port()
|
|
|
|
ss["cipher"] = cipher
|
|
|
|
ss["password"] = password
|
2022-06-21 00:18:34 +08:00
|
|
|
query := urlSS.Query()
|
2022-06-03 04:47:58 +08:00
|
|
|
ss["udp"] = true
|
2022-11-05 14:41:07 +08:00
|
|
|
if query.Get("udp-over-tcp") == "true" || query.Get("uot") == "1" {
|
|
|
|
ss["udp-over-tcp"] = true
|
|
|
|
}
|
2022-06-21 00:18:34 +08:00
|
|
|
if strings.Contains(query.Get("plugin"), "obfs") {
|
|
|
|
obfsParams := strings.Split(query.Get("plugin"), ";")
|
|
|
|
ss["plugin"] = "obfs"
|
|
|
|
ss["plugin-opts"] = map[string]any{
|
|
|
|
"host": obfsParams[2][10:],
|
|
|
|
"mode": obfsParams[1][5:],
|
|
|
|
}
|
|
|
|
}
|
2022-06-03 04:47:58 +08:00
|
|
|
proxies = append(proxies, ss)
|
|
|
|
case "ssr":
|
2022-06-15 03:03:26 +08:00
|
|
|
dcBuf, err := encRaw.DecodeString(body)
|
2022-06-03 04:47:58 +08:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// ssr://host:port:protocol:method:obfs:urlsafebase64pass/?obfsparam=urlsafebase64&protoparam=&remarks=urlsafebase64&group=urlsafebase64&udpport=0&uot=1
|
|
|
|
|
|
|
|
before, after, ok := strings.Cut(string(dcBuf), "/?")
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeArr := strings.Split(before, ":")
|
|
|
|
|
|
|
|
if len(beforeArr) != 6 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
host := beforeArr[0]
|
|
|
|
port := beforeArr[1]
|
|
|
|
protocol := beforeArr[2]
|
|
|
|
method := beforeArr[3]
|
|
|
|
obfs := beforeArr[4]
|
|
|
|
password := decodeUrlSafe(urlSafe(beforeArr[5]))
|
|
|
|
|
|
|
|
query, err := url.ParseQuery(urlSafe(after))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
remarks := decodeUrlSafe(query.Get("remarks"))
|
|
|
|
name := uniqueName(names, remarks)
|
|
|
|
|
|
|
|
obfsParam := decodeUrlSafe(query.Get("obfsparam"))
|
|
|
|
protocolParam := query.Get("protoparam")
|
|
|
|
|
|
|
|
ssr := make(map[string]any, 20)
|
|
|
|
|
|
|
|
ssr["name"] = name
|
|
|
|
ssr["type"] = scheme
|
|
|
|
ssr["server"] = host
|
|
|
|
ssr["port"] = port
|
|
|
|
ssr["cipher"] = method
|
|
|
|
ssr["password"] = password
|
|
|
|
ssr["obfs"] = obfs
|
|
|
|
ssr["protocol"] = protocol
|
|
|
|
ssr["udp"] = true
|
|
|
|
|
|
|
|
if obfsParam != "" {
|
|
|
|
ssr["obfs-param"] = obfsParam
|
|
|
|
}
|
|
|
|
|
|
|
|
if protocolParam != "" {
|
|
|
|
ssr["protocol-param"] = protocolParam
|
|
|
|
}
|
|
|
|
|
|
|
|
proxies = append(proxies, ssr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(proxies) == 0 {
|
|
|
|
return nil, fmt.Errorf("convert v2ray subscribe error: format invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
return proxies, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func uniqueName(names map[string]int, name string) string {
|
|
|
|
if index, ok := names[name]; ok {
|
|
|
|
index++
|
|
|
|
names[name] = index
|
2022-06-08 01:50:14 +08:00
|
|
|
name = fmt.Sprintf("%s-%02d", name, index)
|
2022-06-03 04:47:58 +08:00
|
|
|
} else {
|
|
|
|
index = 0
|
|
|
|
names[name] = index
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|