예제 #1
0
        Tuple <JObject, JToken> Vmess2Config(Models.Datas.Vmess vmess)
        {
            if (vmess == null)
            {
                return(null);
            }

            var outVmess = LoadVmessDecodeTemplate();

            var streamToken = JObject.Parse(@"{streamSettings:{}}");

            streamToken["streamSettings"] = GenStreamSetting(vmess);
            var o = outVmess as JObject;

            Misc.Utils.MergeJson(ref o, streamToken);

            var node = outVmess["settings"]["vnext"][0];

            node["address"]             = vmess.add;
            node["port"]                = VgcApis.Misc.Utils.Str2Int(vmess.port);
            node["users"][0]["id"]      = vmess.id;
            node["users"][0]["alterId"] = VgcApis.Misc.Utils.Str2Int(vmess.aid);

            var tpl = cache.tpl.LoadTemplate("tplImportVmess") as JObject;

            tpl["v2raygcon"]["alias"] = vmess.ps;
            return(new Tuple <JObject, JToken>(tpl, outVmess));
        }
예제 #2
0
        JToken GenStreamSetting(Models.Datas.Vmess vmess)
        {
            // insert stream type
            string[] streamTypes = { "ws", "tcp", "kcp", "h2", "quic" };
            string   streamType  = vmess?.net?.ToLower();

            if (!streamTypes.Contains(streamType))
            {
                return(JToken.Parse(@"{}"));
            }

            if (streamType == "tcp" && vmess.type == "http")
            {
                streamType = "tcp_http";
            }

            var streamToken = cache.tpl.LoadTemplate(streamType);

            try
            {
                FillStreamSettingsDetail(vmess, streamType, streamToken);
            }
            catch { }

            var isUseTls = vmess.tls?.ToLower() == "tls";

            try
            {
                streamToken["security"] = isUseTls ? "tls" : "none";
            }
            catch { }
            return(streamToken);
        }
예제 #3
0
        List <Tuple <Models.Datas.Vmess, string> > GenVmessTestData()
        {
            var data = new List <Tuple <Models.Datas.Vmess, string> >();

            var vmessStandard = new Models.Datas.Vmess
            {
                v    = "2",
                add  = "1.2.3.4",
                port = "1234",
                id   = Guid.NewGuid().ToString(),
                ps   = "standard",
            };

            var linkStandard = vmessStandard.ToVmessLink();

            var vmessNonStandard = new Models.Datas.Vmess
            {
                v    = "2",
                add  = "1.2.3.4",
                port = "1234",
                id   = Guid.NewGuid().ToString(),
                ps   = "non-standard",
            };

            var linkNonStandard = vmessNonStandard.ToVmessLink().Replace(@"=", @"");

            data.Add(new Tuple <Models.Datas.Vmess, string>(vmessStandard, linkStandard));
            data.Add(new Tuple <Models.Datas.Vmess, string>(vmessNonStandard, linkNonStandard));
            return(data);
        }
예제 #4
0
 Models.Datas.Vmess ExtractBasicInfo(
     Func <string, string, string> GetStr, string prefix)
 {
     Models.Datas.Vmess vmess = new Models.Datas.Vmess
     {
         v  = "2",
         ps = GetStr("v2raygcon", "alias")
     };
     vmess.add  = GetStr(prefix, "address");
     vmess.port = GetStr(prefix, "port");
     vmess.id   = GetStr(prefix, "users.0.id");
     vmess.aid  = GetStr(prefix, "users.0.alterId");
     return(vmess);
 }
예제 #5
0
        private static void FillStreamSettingsDetail(Models.Datas.Vmess vmess, string streamType, JToken streamToken)
        {
            switch (streamType)
            {
            case "grpc_multi":
                streamToken["grpcSettings"]["serviceName"] = vmess.path;     // quic.type
                break;

            case "quic":
                streamToken["quicSettings"]["header"]["type"] = vmess.type; // quic.type
                streamToken["quicSettings"]["security"]       = vmess.host; // quic.security
                streamToken["quicSettings"]["key"]            = vmess.path; // quic.key
                break;

            case "tcp":
                // issue #7 should keep header.type = none
                // streamToken["tcpSettings"]["header"]["type"] = vmess.type;
                break;

            case "tcp_http":
                streamToken["tcpSettings"]["header"]["type"]            = vmess.type;
                streamToken["tcpSettings"]["header"]["request"]["path"] =
                    Misc.Utils.Str2JArray(string.IsNullOrEmpty(vmess.path) ? "/" : vmess.path);
                streamToken["tcpSettings"]["header"]["request"]["headers"]["Host"] =
                    Misc.Utils.Str2JArray(vmess.host);
                break;

            case "kcp":
                streamToken["kcpSettings"]["header"]["type"] = vmess.type;
                if (!string.IsNullOrEmpty(vmess.path))
                {
                    streamToken["kcpSettings"]["seed"] = vmess.path;
                }
                break;

            case "ws":
                streamToken["wsSettings"]["path"] =
                    string.IsNullOrEmpty(vmess.v) ? vmess.host : vmess.path;
                if (vmess.v == "2" && !string.IsNullOrEmpty(vmess.host))
                {
                    streamToken["wsSettings"]["headers"]["Host"] = vmess.host;
                }
                break;

            case "h2":
                streamToken["httpSettings"]["path"] = vmess.path;
                streamToken["httpSettings"]["host"] = Misc.Utils.Str2JArray(vmess.host);
                break;
            }
        }
예제 #6
0
        JToken GenStreamSetting(Models.Datas.Vmess vmess)
        {
            // insert stream type
            string[] streamTypes = { "ws", "tcp", "kcp", "h2", "quic", "grpc" };
            string   streamType  = vmess?.net?.ToLower();

            if (!streamTypes.Contains(streamType))
            {
                return(JToken.Parse(@"{}"));
            }

            if (streamType == "tcp" && vmess.type == "http")
            {
                streamType = "tcp_http";
            }

            if (streamType == "grpc")
            {
                streamType = "grpc_multi";
            }

            var streamToken = cache.tpl.LoadTemplate(streamType);

            try
            {
                FillStreamSettingsDetail(vmess, streamType, streamToken);
            }
            catch { }

            var isUseTls = vmess.tls?.ToLower() == "tls";

            try
            {
                streamToken["security"] = isUseTls ? "tls" : "none";
            }
            catch { }

            if (isUseTls && !string.IsNullOrWhiteSpace(vmess.sni))
            {
                try
                {
                    streamToken["tlsSettings"] = JObject.Parse(@"{}");
                    streamToken["tlsSettings"]["allowInsecure"] = false;
                    streamToken["tlsSettings"]["serverName"]    = vmess.sni;
                }
                catch { }
            }
            return(streamToken);
        }
예제 #7
0
 void ExtractTcpHttpSettings(JObject json, bool isUseV4, Models.Datas.Vmess vmess)
 {
     try
     {
         var path = isUseV4 ?
                    json["outbounds"][0]["streamSettings"]["tcpSettings"]["header"]["request"]["path"] :
                    json["outbound"]["streamSettings"]["tcpSettings"]["header"]["request"]["path"];
         vmess.path = Misc.Utils.JArray2Str(path as JArray);
     }
     catch { }
     try
     {
         var hosts = isUseV4 ?
                     json["outbounds"][0]["streamSettings"]["tcpSettings"]["header"]["request"]["headers"]["Host"] :
                     json["outbound"]["streamSettings"]["tcpSettings"]["header"]["request"]["headers"]["Host"];
         vmess.host = Misc.Utils.JArray2Str(hosts as JArray);
     }
     catch { }
 }
예제 #8
0
        Tuple <JObject, JToken> Vmess2Config(Models.Datas.Vmess vmess)
        {
            if (vmess == null)
            {
                return(null);
            }

            var outVmess = cache.tpl.LoadTemplate("outbVmess");

            outVmess["streamSettings"] = GenStreamSetting(vmess);
            var node = outVmess["settings"]["vnext"][0];

            node["address"]             = vmess.add;
            node["port"]                = VgcApis.Misc.Utils.Str2Int(vmess.port);
            node["users"][0]["id"]      = vmess.id;
            node["users"][0]["alterId"] = VgcApis.Misc.Utils.Str2Int(vmess.aid);

            var tpl = cache.tpl.LoadTemplate("tplImportVmess") as JObject;

            tpl["v2raygcon"]["alias"] = vmess.ps;
            return(new Tuple <JObject, JToken>(tpl, outVmess));
        }
예제 #9
0
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            // using vmess:// v1  do not need fix
            var vmess = new Models.Datas.Vmess();

            vmess.add  = tboxHost.Text;
            vmess.port = tboxPort.Text;
            vmess.aid  = tboxAID.Text;
            vmess.id   = tboxUID.Text;
            vmess.ps   = tboxAlias.Text;

            if (rbtnWS.Checked)
            {
                vmess.net  = "ws";
                vmess.host = tboxWSPath.Text;
            }

            if (rbtnKCP.Checked)
            {
                vmess.net = "kcp";
                var index = Math.Max(0, cboxKCP.SelectedIndex);
                vmess.type = Models.Datas.Table.kcpTypes[index];
            }

            if (rbtnTCP.Checked)
            {
                vmess.net = "tcp";
            }

            if (chkboxTLS.Checked)
            {
                vmess.tls = "tls";
            }

            slinkMgr.ImportLinkWithOutV2cfgLinks(vmess.ToVmessLink());
            this.Close();
        }
예제 #10
0
        Models.Datas.Vmess ConfigString2Vmess(string config)
        {
            if (!TryParseConfig(config, out JObject json))
            {
                return(null);
            }

            var GetStr = Misc.Utils.GetStringByPrefixAndKeyHelper(json);

            if (!TryDetectConfigVersion(GetStr, out bool isUseV4, out string root))
            {
                return(null);
            }

            var basicPrefix = root + "." + "settings.vnext.0";

            Models.Datas.Vmess vmess = ExtractBasicInfo(GetStr, basicPrefix);

            var streamPrefix = root + "." + "streamSettings";

            vmess.net = GetStr(streamPrefix, "network");
            vmess.tls = GetStr(streamPrefix, "security");

            switch (vmess.net)
            {
            case "quic":
                vmess.type = GetStr(streamPrefix, "quicSettings.header.type");
                vmess.host = GetStr(streamPrefix, "quicSettings.security");
                vmess.path = GetStr(streamPrefix, "quicSettings.key");
                break;

            case "tcp":
                vmess.type = GetStr(streamPrefix, "tcpSettings.header.type");
                if (vmess.type?.ToLower() == "http")
                {
                    ExtractTcpHttpSettings(json, isUseV4, vmess);
                }
                break;

            case "kcp":
                vmess.type = GetStr(streamPrefix, "kcpSettings.header.type");
                break;

            case "ws":
                vmess.path = GetStr(streamPrefix, "wsSettings.path");
                vmess.host = GetStr(streamPrefix, "wsSettings.headers.Host");
                break;

            case "h2":
                try
                {
                    vmess.path = GetStr(streamPrefix, "httpSettings.path");
                    var hosts = isUseV4 ?
                                json["outbounds"][0]["streamSettings"]["httpSettings"]["host"] :
                                json["outbound"]["streamSettings"]["httpSettings"]["host"];
                    vmess.host = Misc.Utils.JArray2Str(hosts as JArray);
                }
                catch { }
                break;

            default:
                break;
            }
            return(vmess);
        }