2022-06-02 20:47:58 +00:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2022-06-07 17:50:14 +00:00
|
|
|
"strconv"
|
2022-06-02 20:47:58 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var enc = base64.StdEncoding
|
|
|
|
|
|
|
|
func DecodeBase64(buf []byte) ([]byte, error) {
|
|
|
|
dBuf := make([]byte, enc.DecodedLen(len(buf)))
|
|
|
|
n, err := enc.Decode(dBuf, buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dBuf[:n], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBase64StringToString decode base64 string to string
|
|
|
|
func DecodeBase64StringToString(s string) (string, error) {
|
|
|
|
dBuf, err := enc.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(dBuf), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConvertsV2Ray convert V2Ray subscribe proxies data to clash proxies config
|
|
|
|
func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|
|
|
data, err := DecodeBase64(buf)
|
|
|
|
if err != nil {
|
|
|
|
data = buf
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07 17:50:14 +00: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()
|
|
|
|
hysteria["sni"] = query.Get("server_name")
|
|
|
|
hysteria["obfs"] = query.Get("obfs")
|
|
|
|
hysteria["alpn"] = query.Get("alpn")
|
|
|
|
hysteria["auth"] = query.Get("auth")
|
|
|
|
hysteria["auth_str"] = query.Get("auth_str")
|
|
|
|
hysteria["protocol"] = query.Get("protocol")
|
|
|
|
hysteria["down"] = query.Get("down_mbps")
|
|
|
|
hysteria["up"] = query.Get("up")
|
|
|
|
hysteria["down_mbps"], _ = strconv.Atoi(query.Get("down_mbps"))
|
|
|
|
hysteria["up_mbps"], _ = strconv.Atoi(query.Get("up_mbps"))
|
|
|
|
hysteria["ca"] = query.Get("ca")
|
|
|
|
hysteria["ca_str"] = query.Get("ca_str")
|
|
|
|
hysteria["recv_window_conn"], _ = strconv.Atoi(query.Get("recv_window_conn"))
|
|
|
|
hysteria["recv_window"], _ = strconv.Atoi(query.Get("recv_window"))
|
|
|
|
hysteria["disable_mtu_discovery"], _ = strconv.ParseBool(query.Get("disable_mtu_discovery"))
|
|
|
|
hysteria["skip-cert-verify"], _ = strconv.ParseBool(query.Get("insecure"))
|
|
|
|
|
|
|
|
proxies = append(proxies, hysteria)
|
|
|
|
|
2022-06-02 20:47:58 +00:00
|
|
|
case "trojan":
|
|
|
|
urlTrojan, err := url.Parse(line)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-07 17:50:14 +00:00
|
|
|
query := urlTrojan.Query()
|
2022-06-02 20:47:58 +00: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-07 17:50:14 +00:00
|
|
|
sni := query.Get("sni")
|
2022-06-02 20:47:58 +00:00
|
|
|
if sni != "" {
|
|
|
|
trojan["sni"] = sni
|
|
|
|
}
|
|
|
|
|
2022-06-07 17:50:14 +00:00
|
|
|
network := strings.ToLower(query.Get("type"))
|
2022-06-02 20:47:58 +00:00
|
|
|
if network != "" {
|
|
|
|
trojan["network"] = network
|
|
|
|
}
|
|
|
|
|
2022-06-06 19:17:33 +00:00
|
|
|
switch network {
|
|
|
|
case "ws":
|
2022-06-02 20:47:58 +00:00
|
|
|
headers := make(map[string]any)
|
|
|
|
wsOpts := make(map[string]any)
|
|
|
|
|
2022-06-06 19:17:33 +00:00
|
|
|
//headers["Host"] = RandHost()
|
2022-06-02 20:47:58 +00:00
|
|
|
headers["User-Agent"] = RandUserAgent()
|
|
|
|
|
2022-06-07 17:50:14 +00:00
|
|
|
wsOpts["path"] = query.Get("path")
|
2022-06-02 20:47:58 +00:00
|
|
|
wsOpts["headers"] = headers
|
|
|
|
|
|
|
|
trojan["ws-opts"] = wsOpts
|
2022-06-06 19:17:33 +00:00
|
|
|
|
|
|
|
case "grpc":
|
|
|
|
grpcOpts := make(map[string]any)
|
2022-06-07 17:50:14 +00:00
|
|
|
grpcOpts["grpc-service-name"] = query.Get("serviceName")
|
2022-06-06 19:17:33 +00:00
|
|
|
trojan["grpc-opts"] = grpcOpts
|
2022-06-02 20:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proxies = append(proxies, trojan)
|
2022-06-06 19:17:33 +00:00
|
|
|
|
|
|
|
case "vless":
|
|
|
|
urlVless, err := url.Parse(line)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-07 17:50:14 +00:00
|
|
|
query := urlVless.Query()
|
2022-06-06 19:17:33 +00:00
|
|
|
|
|
|
|
name := uniqueName(names, urlVless.Fragment)
|
|
|
|
vless := make(map[string]any, 20)
|
|
|
|
|
|
|
|
vless["name"] = name
|
|
|
|
vless["type"] = scheme
|
|
|
|
vless["server"] = urlVless.Hostname()
|
|
|
|
vless["port"] = urlVless.Port()
|
|
|
|
vless["uuid"] = urlVless.User.Username()
|
|
|
|
vless["udp"] = true
|
|
|
|
vless["skip-cert-verify"] = false
|
|
|
|
|
2022-06-07 17:50:14 +00:00
|
|
|
sni := query.Get("sni")
|
2022-06-06 19:17:33 +00:00
|
|
|
if sni != "" {
|
|
|
|
vless["servername"] = sni
|
|
|
|
}
|
|
|
|
|
2022-06-07 17:50:14 +00:00
|
|
|
flow := strings.ToLower(query.Get("flow"))
|
2022-06-06 19:17:33 +00:00
|
|
|
if flow != "" {
|
|
|
|
vless["flow"] = flow
|
|
|
|
}
|
|
|
|
|
2022-06-07 17:50:14 +00:00
|
|
|
network := strings.ToLower(query.Get("type"))
|
2022-06-06 19:17:33 +00:00
|
|
|
if network != "" {
|
2022-06-07 17:50:14 +00:00
|
|
|
fakeType := strings.ToLower(query.Get("headerType"))
|
|
|
|
if network == "tcp" && fakeType == "http" {
|
2022-06-06 19:17:33 +00:00
|
|
|
network = "http"
|
|
|
|
}
|
2022-06-07 17:50:14 +00:00
|
|
|
if network == "http" {
|
|
|
|
network = "h2"
|
|
|
|
}
|
2022-06-06 19:17:33 +00:00
|
|
|
vless["network"] = network
|
|
|
|
}
|
|
|
|
|
|
|
|
switch network {
|
|
|
|
case "http":
|
|
|
|
headers := make(map[string]any)
|
|
|
|
httpOpts := make(map[string]any)
|
|
|
|
|
2022-06-07 17:50:14 +00:00
|
|
|
if query.Get("method") != "" {
|
|
|
|
httpOpts["method"] = query.Get("method")
|
|
|
|
}
|
|
|
|
if query.Get("path") != "" {
|
|
|
|
httpOpts["path"] = query.Get("path")
|
|
|
|
}
|
2022-06-06 19:17:33 +00:00
|
|
|
headers["User-Agent"] = RandUserAgent()
|
|
|
|
httpOpts["headers"] = headers
|
|
|
|
|
|
|
|
vless["http-opts"] = httpOpts
|
|
|
|
|
|
|
|
case "h2":
|
|
|
|
headers := make(map[string]any)
|
|
|
|
h2Opts := make(map[string]any)
|
|
|
|
|
|
|
|
headers["User-Agent"] = RandUserAgent()
|
2022-06-07 17:50:14 +00:00
|
|
|
h2Opts["path"] = query.Get("path")
|
2022-06-06 19:17:33 +00:00
|
|
|
h2Opts["headers"] = headers
|
|
|
|
|
|
|
|
vless["h2-opts"] = h2Opts
|
|
|
|
|
|
|
|
case "ws":
|
|
|
|
headers := make(map[string]any)
|
|
|
|
wsOpts := make(map[string]any)
|
|
|
|
|
|
|
|
//headers["Host"] = RandHost()
|
|
|
|
headers["User-Agent"] = RandUserAgent()
|
2022-06-07 17:50:14 +00:00
|
|
|
wsOpts["path"] = query.Get("path")
|
2022-06-06 19:17:33 +00:00
|
|
|
wsOpts["headers"] = headers
|
|
|
|
|
|
|
|
vless["ws-opts"] = wsOpts
|
|
|
|
|
|
|
|
case "grpc":
|
|
|
|
grpcOpts := make(map[string]any)
|
2022-06-07 17:50:14 +00:00
|
|
|
grpcOpts["grpc-service-name"] = query.Get("serviceName")
|
2022-06-06 19:17:33 +00:00
|
|
|
vless["grpc-opts"] = grpcOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
proxies = append(proxies, vless)
|
|
|
|
|
2022-06-02 20:47:58 +00:00
|
|
|
case "vmess":
|
|
|
|
dcBuf, err := enc.DecodeString(body)
|
|
|
|
if err != nil {
|
|
|
|
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"]
|
|
|
|
vmess["alterId"] = values["aid"]
|
|
|
|
vmess["cipher"] = "auto"
|
|
|
|
vmess["udp"] = true
|
|
|
|
vmess["skip-cert-verify"] = false
|
|
|
|
|
2022-06-06 19:17:33 +00:00
|
|
|
sni := values["sni"]
|
|
|
|
if sni != "" {
|
|
|
|
vmess["sni"] = sni
|
|
|
|
}
|
|
|
|
|
2022-06-02 20:47:58 +00:00
|
|
|
host := values["host"]
|
|
|
|
network := strings.ToLower(values["net"].(string))
|
|
|
|
|
|
|
|
vmess["network"] = network
|
|
|
|
|
|
|
|
tls := strings.ToLower(values["tls"].(string))
|
|
|
|
if tls != "" && tls != "0" && tls != "null" {
|
|
|
|
if host != nil {
|
|
|
|
vmess["servername"] = host
|
|
|
|
}
|
|
|
|
vmess["tls"] = true
|
|
|
|
}
|
|
|
|
|
2022-06-06 19:17:33 +00:00
|
|
|
switch network {
|
|
|
|
case "http":
|
|
|
|
headers := make(map[string]any)
|
|
|
|
httpOpts := make(map[string]any)
|
|
|
|
|
|
|
|
//headers["Host"] = RandHost()
|
|
|
|
headers["User-Agent"] = RandUserAgent()
|
|
|
|
httpOpts["method"] = values["method"]
|
|
|
|
httpOpts["path"] = values["path"]
|
|
|
|
httpOpts["headers"] = headers
|
|
|
|
|
|
|
|
vmess["http-opts"] = httpOpts
|
|
|
|
|
|
|
|
case "h2":
|
|
|
|
headers := make(map[string]any)
|
|
|
|
h2Opts := make(map[string]any)
|
|
|
|
|
|
|
|
//headers["Host"] = RandHost()
|
|
|
|
headers["User-Agent"] = RandUserAgent()
|
|
|
|
h2Opts["path"] = values["path"]
|
|
|
|
h2Opts["headers"] = headers
|
|
|
|
|
|
|
|
vmess["h2-opts"] = h2Opts
|
|
|
|
|
|
|
|
case "ws":
|
2022-06-02 20:47:58 +00:00
|
|
|
headers := make(map[string]any)
|
|
|
|
wsOpts := make(map[string]any)
|
|
|
|
|
|
|
|
headers["Host"] = RandHost()
|
|
|
|
headers["User-Agent"] = RandUserAgent()
|
|
|
|
|
|
|
|
if values["path"] != nil {
|
|
|
|
wsOpts["path"] = values["path"]
|
|
|
|
}
|
|
|
|
wsOpts["headers"] = headers
|
|
|
|
|
|
|
|
vmess["ws-opts"] = wsOpts
|
2022-06-06 19:17:33 +00:00
|
|
|
|
|
|
|
case "grpc":
|
|
|
|
grpcOpts := make(map[string]any)
|
|
|
|
grpcOpts["grpc-service-name"] = values["path"]
|
|
|
|
vmess["grpc-opts"] = grpcOpts
|
2022-06-02 20:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proxies = append(proxies, vmess)
|
2022-06-06 19:17:33 +00:00
|
|
|
|
2022-06-02 20:47:58 +00:00
|
|
|
case "ss":
|
|
|
|
urlSS, err := url.Parse(line)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := uniqueName(names, urlSS.Fragment)
|
|
|
|
port := urlSS.Port()
|
|
|
|
|
|
|
|
if port == "" {
|
|
|
|
dcBuf, err := enc.DecodeString(urlSS.Host)
|
|
|
|
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 {
|
|
|
|
dcBuf, err := enc.DecodeString(cipher)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cipher, password, found = strings.Cut(string(dcBuf), ":")
|
|
|
|
if !found {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ss := make(map[string]any, 20)
|
|
|
|
|
|
|
|
ss["name"] = name
|
|
|
|
ss["type"] = scheme
|
|
|
|
ss["server"] = urlSS.Hostname()
|
|
|
|
ss["port"] = urlSS.Port()
|
|
|
|
ss["cipher"] = cipher
|
|
|
|
ss["password"] = password
|
|
|
|
ss["udp"] = true
|
|
|
|
|
|
|
|
proxies = append(proxies, ss)
|
|
|
|
case "ssr":
|
|
|
|
dcBuf, err := enc.DecodeString(body)
|
|
|
|
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 urlSafe(data string) string {
|
|
|
|
return strings.ReplaceAll(strings.ReplaceAll(data, "+", "-"), "/", "_")
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeUrlSafe(data string) string {
|
|
|
|
dcBuf, err := base64.URLEncoding.DecodeString(data)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(dcBuf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func uniqueName(names map[string]int, name string) string {
|
|
|
|
if index, ok := names[name]; ok {
|
|
|
|
index++
|
|
|
|
names[name] = index
|
2022-06-07 17:50:14 +00:00
|
|
|
name = fmt.Sprintf("%s-%02d", name, index)
|
2022-06-02 20:47:58 +00:00
|
|
|
} else {
|
|
|
|
index = 0
|
|
|
|
names[name] = index
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|