Ejemplo n.º 1
0
        private static string ShareVmess(VmessItem item)
        {
            string url = string.Empty;

            VmessQRCode vmessQRCode = new VmessQRCode
            {
                v    = item.configVersion.ToString(),
                ps   = item.remarks.TrimEx(), //备注也许很长 ;
                add  = item.address,
                port = item.port.ToString(),
                id   = item.id,
                aid  = item.alterId.ToString(),
                scy  = item.security,
                net  = item.network,
                type = item.headerType,
                host = item.requestHost,
                path = item.path,
                tls  = item.streamSecurity,
                sni  = item.sni
            };

            url = Utils.ToJson(vmessQRCode);
            url = Utils.Base64Encode(url);
            url = string.Format("{0}{1}", Global.vmessProtocol, url);

            return(url);
        }
Ejemplo n.º 2
0
        private static VmessItem ResolveVmess(string result, out string msg)
        {
            msg = string.Empty;
            VmessItem vmessItem = new VmessItem();

            vmessItem.configType = (int)EConfigType.Vmess;
            result = result.Substring(Global.vmessProtocol.Length);
            result = Utils.Base64Decode(result);

            //转成Json
            VmessQRCode vmessQRCode = Utils.FromJson <VmessQRCode>(result);

            if (vmessQRCode == null)
            {
                msg = UIRes.I18N("FailedConversionConfiguration");
                return(null);
            }

            vmessItem.network    = Global.DefaultNetwork;
            vmessItem.headerType = Global.None;

            vmessItem.configVersion = Utils.ToInt(vmessQRCode.v);
            vmessItem.remarks       = Utils.ToString(vmessQRCode.ps);
            vmessItem.address       = Utils.ToString(vmessQRCode.add);
            vmessItem.port          = Utils.ToInt(vmessQRCode.port);
            vmessItem.id            = Utils.ToString(vmessQRCode.id);
            vmessItem.alterId       = Utils.ToInt(vmessQRCode.aid);
            vmessItem.security      = Utils.ToString(vmessQRCode.scy);

            if (!Utils.IsNullOrEmpty(vmessQRCode.scy))
            {
                vmessItem.security = vmessQRCode.scy;
            }
            else
            {
                vmessItem.security = Global.DefaultSecurity;
            }
            if (!Utils.IsNullOrEmpty(vmessQRCode.net))
            {
                vmessItem.network = vmessQRCode.net;
            }
            if (!Utils.IsNullOrEmpty(vmessQRCode.type))
            {
                vmessItem.headerType = vmessQRCode.type;
            }

            vmessItem.requestHost    = Utils.ToString(vmessQRCode.host);
            vmessItem.path           = Utils.ToString(vmessQRCode.path);
            vmessItem.streamSecurity = Utils.ToString(vmessQRCode.tls);
            vmessItem.sni            = Utils.ToString(vmessQRCode.sni);
            vmessItem.alpn           = Utils.String2List(vmessQRCode.alpn);

            return(vmessItem);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 从剪贴板导入URL
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static VmessItem ImportFromClipboardConfig(out string msg)
        {
            msg = string.Empty;
            VmessItem vmessItem = new VmessItem();

            try
            {
                //载入配置文件
                string result = Utils.GetClipboardData();
                if (Utils.IsNullOrEmpty(result))
                {
                    msg = "读取配置文件失败";
                    return(null);
                }
                if (!result.StartsWith(Global.vmessProtocol))
                {
                    msg = "非vmess协议";
                    return(null);
                }
                result = result.Substring(Global.vmessProtocol.Length);
                //解码
                result = Utils.Base64Decode(result);

                //转成Json
                VmessQRCode vmessQRCode = Utils.FromJson <VmessQRCode>(result);
                if (vmessQRCode == null)
                {
                    msg = "转换配置文件失败";
                    return(null);
                }

                vmessItem.security   = Global.DefaultSecurity;
                vmessItem.network    = Global.DefaultNetwork;
                vmessItem.headerType = Global.None;

                vmessItem.remarks        = vmessQRCode.ps;
                vmessItem.address        = vmessQRCode.add;
                vmessItem.port           = Convert.ToInt32(vmessQRCode.port);
                vmessItem.id             = vmessQRCode.id;
                vmessItem.alterId        = Convert.ToInt32(vmessQRCode.aid);
                vmessItem.network        = vmessQRCode.net;
                vmessItem.headerType     = vmessQRCode.type;
                vmessItem.requestHost    = vmessQRCode.host;
                vmessItem.streamSecurity = vmessQRCode.tls;
            }
            catch
            {
                msg = "异常,不是正确的客户端配置文件,请检查";
                return(null);
            }

            return(vmessItem);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 取得服务器QRCode配置
        /// </summary>
        /// <param name="config"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string GetVmessQRCode(Config config, int index)
        {
            try
            {
                string url = string.Empty;

                VmessItem vmessItem = config.vmess[index];
                if (vmessItem.configType == (int)EConfigType.Vmess)
                {
                    VmessQRCode vmessQRCode = new VmessQRCode();
                    vmessQRCode.v    = vmessItem.configVersion.ToString();
                    vmessQRCode.ps   = vmessItem.remarks.Trim(); //备注也许很长 ;
                    vmessQRCode.add  = vmessItem.address;
                    vmessQRCode.port = vmessItem.port.ToString();
                    vmessQRCode.id   = vmessItem.id;
                    vmessQRCode.aid  = vmessItem.alterId.ToString();
                    vmessQRCode.net  = vmessItem.network;
                    vmessQRCode.type = vmessItem.headerType;
                    vmessQRCode.host = vmessItem.requestHost;
                    vmessQRCode.path = vmessItem.path;
                    vmessQRCode.tls  = vmessItem.streamSecurity;

                    url = Utils.ToJson(vmessQRCode);
                    url = Utils.Base64Encode(url);
                    url = string.Format("{0}{1}", Global.vmessProtocol, url);
                }
                else if (vmessItem.configType == (int)EConfigType.Shadowsocks)
                {
                    var remark = string.Empty;
                    if (!Utils.IsNullOrEmpty(vmessItem.remarks))
                    {
                        remark = "#" + WebUtility.UrlEncode(vmessItem.remarks);
                    }
                    url = string.Format("{0}:{1}@{2}:{3}",
                                        vmessItem.security,
                                        vmessItem.id,
                                        vmessItem.address,
                                        vmessItem.port);
                    url = Utils.Base64Encode(url);
                    url = string.Format("{0}{1}{2}", Global.ssProtocol, url, remark);
                }
                else
                {
                }
                return(url);
            }
            catch
            {
                return("");
            }
        }
 private void QRCodeForm_Shown(object sender, EventArgs e)
 {
     if (Index >= 0)
     {
         VmessQRCode vmessQRCode = null;
         if (ConfigHandler.GetVmessQRCode(config, Index, ref vmessQRCode) != 0)
         {
             return;
         }
         string url = Utils.ToJson(vmessQRCode);
         url             = Utils.Base64Encode(url);
         url             = string.Format("{0}{1}", Global.vmessProtocol, url);
         picQRCode.Image = QRCodeHelper.GetQRCode(url);
         txtUrl.Text     = url;
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 取得服务器QRCode配置
        /// </summary>
        /// <param name="config"></param>
        /// <param name="index"></param>
        /// <param name="vmessQRCode"></param>
        /// <returns></returns>
        public static int GetVmessQRCode(Config config, int index, ref VmessQRCode vmessQRCode)
        {
            try
            {
                VmessItem vmessItem = config.vmess[index];

                vmessQRCode      = new VmessQRCode();
                vmessQRCode.ps   = vmessItem.remarks.Trim(); //备注也许很长 ;
                vmessQRCode.add  = vmessItem.address;
                vmessQRCode.port = vmessItem.port.ToString();
                vmessQRCode.id   = vmessItem.id;
                vmessQRCode.aid  = vmessItem.alterId.ToString();
                vmessQRCode.net  = vmessItem.network;
                vmessQRCode.type = vmessItem.headerType;
                vmessQRCode.host = vmessItem.requestHost;
                vmessQRCode.tls  = vmessItem.streamSecurity;

                return(0);
            }
            catch
            {
                return(-1);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 从剪贴板导入URL
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static VmessItem ImportFromClipboardConfig(string clipboardData, out string msg)
        {
            msg = string.Empty;
            VmessItem vmessItem = new VmessItem();

            try
            {
                //载入配置文件
                string result = clipboardData.Trim();// Utils.GetClipboardData();
                if (Utils.IsNullOrEmpty(result))
                {
                    msg = "读取配置文件失败";
                    return(null);
                }

                if (result.StartsWith(Global.vmessProtocol))
                {
                    int indexSplit = result.IndexOf("?");
                    if (indexSplit > 0)
                    {
                        vmessItem = ResolveVmess4Kitsunebi(result);
                    }
                    else
                    {
                        vmessItem.configType = (int)EConfigType.Vmess;
                        result = result.Substring(Global.vmessProtocol.Length);
                        result = Utils.Base64Decode(result);

                        //转成Json
                        VmessQRCode vmessQRCode = Utils.FromJson <VmessQRCode>(result);
                        if (vmessQRCode == null)
                        {
                            msg = "转换配置文件失败";
                            return(null);
                        }
                        vmessItem.security   = Global.DefaultSecurity;
                        vmessItem.network    = Global.DefaultNetwork;
                        vmessItem.headerType = Global.None;

                        vmessItem.configVersion  = Utils.ToInt(vmessQRCode.v);
                        vmessItem.remarks        = vmessQRCode.ps;
                        vmessItem.address        = vmessQRCode.add;
                        vmessItem.port           = Utils.ToInt(vmessQRCode.port);
                        vmessItem.id             = vmessQRCode.id;
                        vmessItem.alterId        = Utils.ToInt(vmessQRCode.aid);
                        vmessItem.network        = vmessQRCode.net;
                        vmessItem.headerType     = vmessQRCode.type;
                        vmessItem.requestHost    = vmessQRCode.host;
                        vmessItem.path           = vmessQRCode.path;
                        vmessItem.streamSecurity = vmessQRCode.tls;
                    }

                    ConfigHandler.UpgradeServerVersion(ref vmessItem);
                }
                else if (result.StartsWith(Global.ssProtocol))
                {
                    msg = "配置格式不正确";

                    vmessItem.configType = (int)EConfigType.Shadowsocks;
                    result = result.Substring(Global.ssProtocol.Length);
                    //remark
                    int indexRemark = result.IndexOf("#");
                    if (indexRemark > 0)
                    {
                        try
                        {
                            vmessItem.remarks = WebUtility.UrlDecode(result.Substring(indexRemark + 1, result.Length - indexRemark - 1));
                        }
                        catch { }
                        result = result.Substring(0, indexRemark);
                    }
                    //part decode
                    int indexS = result.IndexOf("@");
                    if (indexS > 0)
                    {
                        result = Utils.Base64Decode(result.Substring(0, indexS)) + result.Substring(indexS, result.Length - indexS);
                    }
                    else
                    {
                        result = Utils.Base64Decode(result);
                    }

                    string[] arr1 = result.Split('@');
                    if (arr1.Length != 2)
                    {
                        return(null);
                    }
                    string[] arr21 = arr1[0].Split(':');
                    string[] arr22 = arr1[1].Split(':');
                    if (arr21.Length != 2 || arr21.Length != 2)
                    {
                        return(null);
                    }
                    vmessItem.address  = arr22[0];
                    vmessItem.port     = Utils.ToInt(arr22[1]);
                    vmessItem.security = arr21[0];
                    vmessItem.id       = arr21[1];
                }
                else
                {
                    msg = "非vmess或ss协议";
                    return(null);
                }
            }
            catch
            {
                msg = "异常,不是正确的配置,请检查";
                return(null);
            }

            return(vmessItem);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 从剪贴板导入URL
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static VmessItem ImportFromClipboardConfig(string clipboardData, out string msg)
        {
            msg = string.Empty;
            VmessItem vmessItem = new VmessItem();

            try
            {
                //载入配置文件
                string result = clipboardData.TrimEx();// Utils.GetClipboardData();
                if (Utils.IsNullOrEmpty(result))
                {
                    msg = UIRes.I18N("FailedReadConfiguration");
                    return(null);
                }

                if (result.StartsWith(Global.vmessProtocol))
                {
                    int indexSplit = result.IndexOf("?");
                    if (indexSplit > 0)
                    {
                        vmessItem = ResolveStdVmess(result) ?? ResolveVmess4Kitsunebi(result);
                    }
                    else
                    {
                        vmessItem.configType = (int)EConfigType.Vmess;
                        result = result.Substring(Global.vmessProtocol.Length);
                        result = Utils.Base64Decode(result);

                        //转成Json
                        VmessQRCode vmessQRCode = Utils.FromJson <VmessQRCode>(result);
                        if (vmessQRCode == null)
                        {
                            msg = UIRes.I18N("FailedConversionConfiguration");
                            return(null);
                        }
                        vmessItem.security   = Global.DefaultSecurity;
                        vmessItem.network    = Global.DefaultNetwork;
                        vmessItem.headerType = Global.None;


                        vmessItem.configVersion = Utils.ToInt(vmessQRCode.v);
                        vmessItem.remarks       = Utils.ToString(vmessQRCode.ps);
                        vmessItem.address       = Utils.ToString(vmessQRCode.add);
                        vmessItem.port          = Utils.ToInt(vmessQRCode.port);
                        vmessItem.id            = Utils.ToString(vmessQRCode.id);
                        vmessItem.alterId       = Utils.ToInt(vmessQRCode.aid);

                        if (!Utils.IsNullOrEmpty(vmessQRCode.net))
                        {
                            vmessItem.network = vmessQRCode.net;
                        }
                        if (!Utils.IsNullOrEmpty(vmessQRCode.type))
                        {
                            vmessItem.headerType = vmessQRCode.type;
                        }

                        vmessItem.requestHost    = Utils.ToString(vmessQRCode.host);
                        vmessItem.path           = Utils.ToString(vmessQRCode.path);
                        vmessItem.streamSecurity = Utils.ToString(vmessQRCode.tls);
                        vmessItem.sni            = Utils.ToString(vmessQRCode.sni);
                    }

                    ConfigHandler.UpgradeServerVersion(ref vmessItem);
                }
                else if (result.StartsWith(Global.ssProtocol))
                {
                    msg = UIRes.I18N("ConfigurationFormatIncorrect");

                    vmessItem = ResolveSSLegacy(result);
                    if (vmessItem == null)
                    {
                        vmessItem = ResolveSip002(result);
                    }
                    if (vmessItem == null)
                    {
                        return(null);
                    }
                    if (vmessItem.address.Length == 0 || vmessItem.port == 0 || vmessItem.security.Length == 0 || vmessItem.id.Length == 0)
                    {
                        return(null);
                    }

                    vmessItem.configType = (int)EConfigType.Shadowsocks;
                }
                else if (result.StartsWith(Global.socksProtocol))
                {
                    msg = UIRes.I18N("ConfigurationFormatIncorrect");

                    vmessItem.configType = (int)EConfigType.Socks;
                    result = result.Substring(Global.socksProtocol.Length);
                    //remark
                    int indexRemark = result.IndexOf("#");
                    if (indexRemark > 0)
                    {
                        try
                        {
                            vmessItem.remarks = Utils.UrlDecode(result.Substring(indexRemark + 1, result.Length - indexRemark - 1));
                        }
                        catch { }
                        result = result.Substring(0, indexRemark);
                    }
                    //part decode
                    int indexS = result.IndexOf("@");
                    if (indexS > 0)
                    {
                    }
                    else
                    {
                        result = Utils.Base64Decode(result);
                    }

                    string[] arr1 = result.Split('@');
                    if (arr1.Length != 2)
                    {
                        return(null);
                    }
                    string[] arr21 = arr1[0].Split(':');
                    //string[] arr22 = arr1[1].Split(':');
                    int indexPort = arr1[1].LastIndexOf(":");
                    if (arr21.Length != 2 || indexPort < 0)
                    {
                        return(null);
                    }
                    vmessItem.address  = arr1[1].Substring(0, indexPort);
                    vmessItem.port     = Utils.ToInt(arr1[1].Substring(indexPort + 1, arr1[1].Length - (indexPort + 1)));
                    vmessItem.security = arr21[0];
                    vmessItem.id       = arr21[1];
                }
                else if (result.StartsWith(Global.trojanProtocol))
                {
                    msg = UIRes.I18N("ConfigurationFormatIncorrect");

                    vmessItem.configType = (int)EConfigType.Trojan;

                    Uri uri = new Uri(result);
                    vmessItem.address = uri.IdnHost;
                    vmessItem.port    = uri.Port;
                    vmessItem.id      = uri.UserInfo;

                    var qurery = HttpUtility.ParseQueryString(uri.Query);
                    vmessItem.sni = qurery["sni"] ?? "";

                    var remarks = uri.Fragment.Replace("#", "");
                    if (Utils.IsNullOrEmpty(remarks))
                    {
                        vmessItem.remarks = "NONE";
                    }
                    else
                    {
                        vmessItem.remarks = Utils.UrlDecode(remarks);
                    }
                }
                else if (result.StartsWith(Global.vlessProtocol))
                {
                    vmessItem = ResolveStdVLESS(result);

                    ConfigHandler.UpgradeServerVersion(ref vmessItem);
                }
                else
                {
                    msg = UIRes.I18N("NonvmessOrssProtocol");
                    return(null);
                }
            }
            catch
            {
                msg = UIRes.I18N("Incorrectconfiguration");
                return(null);
            }

            return(vmessItem);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// GetShareUrl
        /// </summary>
        /// <param name="config"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string GetShareUrl(Config config, int index)
        {
            try
            {
                string url = string.Empty;

                VmessItem item = config.vmess[index];
                if (item.configType == (int)EConfigType.Vmess)
                {
                    VmessQRCode vmessQRCode = new VmessQRCode
                    {
                        v    = item.configVersion.ToString(),
                        ps   = item.remarks.TrimEx(), //备注也许很长 ;
                        add  = item.address,
                        port = item.port.ToString(),
                        id   = item.id,
                        aid  = item.alterId.ToString(),
                        net  = item.network,
                        type = item.headerType,
                        host = item.requestHost,
                        path = item.path,
                        tls  = item.streamSecurity,
                        sni  = item.sni
                    };

                    url = Utils.ToJson(vmessQRCode);
                    url = Utils.Base64Encode(url);
                    url = string.Format("{0}{1}", Global.vmessProtocol, url);
                }
                else if (item.configType == (int)EConfigType.Shadowsocks)
                {
                    string remark = string.Empty;
                    if (!Utils.IsNullOrEmpty(item.remarks))
                    {
                        remark = "#" + Utils.UrlEncode(item.remarks);
                    }
                    url = string.Format("{0}:{1}@{2}:{3}",
                                        item.security,
                                        item.id,
                                        item.address,
                                        item.port);
                    url = Utils.Base64Encode(url);
                    url = string.Format("{0}{1}{2}", Global.ssProtocol, url, remark);
                }
                else if (item.configType == (int)EConfigType.Socks)
                {
                    string remark = string.Empty;
                    if (!Utils.IsNullOrEmpty(item.remarks))
                    {
                        remark = "#" + Utils.UrlEncode(item.remarks);
                    }
                    url = string.Format("{0}:{1}@{2}:{3}",
                                        item.security,
                                        item.id,
                                        item.address,
                                        item.port);
                    url = Utils.Base64Encode(url);
                    url = string.Format("{0}{1}{2}", Global.socksProtocol, url, remark);
                }
                else if (item.configType == (int)EConfigType.Trojan)
                {
                    string remark = string.Empty;
                    if (!Utils.IsNullOrEmpty(item.remarks))
                    {
                        remark = "#" + Utils.UrlEncode(item.remarks);
                    }
                    string query = string.Empty;
                    if (!Utils.IsNullOrEmpty(item.sni))
                    {
                        query = string.Format("?sni={0}", Utils.UrlEncode(item.sni));
                    }
                    url = string.Format("{0}@{1}:{2}",
                                        item.id,
                                        GetIpv6(item.address),
                                        item.port);
                    url = string.Format("{0}{1}{2}{3}", Global.trojanProtocol, url, query, remark);
                }
                else if (item.configType == (int)EConfigType.VLESS)
                {
                    string remark = string.Empty;
                    if (!Utils.IsNullOrEmpty(item.remarks))
                    {
                        remark = "#" + Utils.UrlEncode(item.remarks);
                    }
                    var dicQuery = new Dictionary <string, string>();
                    if (!Utils.IsNullOrEmpty(item.flow))
                    {
                        dicQuery.Add("flow", item.flow);
                    }
                    if (!Utils.IsNullOrEmpty(item.security))
                    {
                        dicQuery.Add("encryption", item.security);
                    }
                    else
                    {
                        dicQuery.Add("encryption", "none");
                    }
                    if (!Utils.IsNullOrEmpty(item.streamSecurity))
                    {
                        dicQuery.Add("security", item.streamSecurity);
                    }
                    else
                    {
                        dicQuery.Add("security", "none");
                    }
                    if (!Utils.IsNullOrEmpty(item.sni))
                    {
                        dicQuery.Add("sni", item.sni);
                    }
                    if (!Utils.IsNullOrEmpty(item.network))
                    {
                        dicQuery.Add("type", item.network);
                    }
                    else
                    {
                        dicQuery.Add("type", "tcp");
                    }

                    switch (item.network)
                    {
                    case "tcp":
                        if (!Utils.IsNullOrEmpty(item.headerType))
                        {
                            dicQuery.Add("headerType", item.headerType);
                        }
                        else
                        {
                            dicQuery.Add("headerType", "none");
                        }
                        if (!Utils.IsNullOrEmpty(item.requestHost))
                        {
                            dicQuery.Add("host", Utils.UrlEncode(item.requestHost));
                        }
                        break;

                    case "kcp":
                        if (!Utils.IsNullOrEmpty(item.headerType))
                        {
                            dicQuery.Add("headerType", item.headerType);
                        }
                        else
                        {
                            dicQuery.Add("headerType", "none");
                        }
                        if (!Utils.IsNullOrEmpty(item.path))
                        {
                            dicQuery.Add("seed", Utils.UrlEncode(item.path));
                        }
                        break;

                    case "ws":
                        if (!Utils.IsNullOrEmpty(item.requestHost))
                        {
                            dicQuery.Add("host", Utils.UrlEncode(item.requestHost));
                        }
                        if (!Utils.IsNullOrEmpty(item.path))
                        {
                            dicQuery.Add("path", Utils.UrlEncode(item.path));
                        }
                        break;

                    case "http":
                    case "h2":
                        dicQuery["type"] = "http";
                        if (!Utils.IsNullOrEmpty(item.requestHost))
                        {
                            dicQuery.Add("host", Utils.UrlEncode(item.requestHost));
                        }
                        if (!Utils.IsNullOrEmpty(item.path))
                        {
                            dicQuery.Add("path", Utils.UrlEncode(item.path));
                        }
                        break;

                    case "quic":
                        if (!Utils.IsNullOrEmpty(item.headerType))
                        {
                            dicQuery.Add("headerType", item.headerType);
                        }
                        else
                        {
                            dicQuery.Add("headerType", "none");
                        }
                        dicQuery.Add("quicSecurity", Utils.UrlEncode(item.requestHost));
                        dicQuery.Add("key", Utils.UrlEncode(item.path));
                        break;
                    }
                    string query = "?" + string.Join("&", dicQuery.Select(x => x.Key + "=" + x.Value).ToArray());

                    url = string.Format("{0}@{1}:{2}",
                                        item.id,
                                        GetIpv6(item.address),
                                        item.port);
                    url = string.Format("{0}{1}{2}{3}", Global.vlessProtocol, url, query, remark);
                }
                else
                {
                }
                return(url);
            }
            catch
            {
                return("");
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 取得服务器QRCode配置
        /// </summary>
        /// <param name="config"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string GetVmessQRCode(Config config, int index)
        {
            try
            {
                string url = string.Empty;

                VmessItem vmessItem = config.vmess[index];
                if (vmessItem.configType == (int)EConfigType.Vmess)
                {
                    VmessQRCode vmessQRCode = new VmessQRCode
                    {
                        v    = vmessItem.configVersion.ToString(),
                        ps   = vmessItem.remarks.TrimEx(), //备注也许很长 ;
                        add  = vmessItem.address,
                        port = vmessItem.port.ToString(),
                        id   = vmessItem.id,
                        aid  = vmessItem.alterId.ToString(),
                        net  = vmessItem.network,
                        type = vmessItem.headerType,
                        host = vmessItem.requestHost,
                        path = vmessItem.path,
                        tls  = vmessItem.streamSecurity
                    };

                    url = Utils.ToJson(vmessQRCode);
                    url = Utils.Base64Encode(url);
                    url = string.Format("{0}{1}", Global.vmessProtocol, url);
                }
                else if (vmessItem.configType == (int)EConfigType.Shadowsocks)
                {
                    string remark = string.Empty;
                    if (!Utils.IsNullOrEmpty(vmessItem.remarks))
                    {
                        remark = "#" + WebUtility.UrlEncode(vmessItem.remarks);
                    }
                    url = string.Format("{0}:{1}@{2}:{3}",
                                        vmessItem.security,
                                        vmessItem.id,
                                        vmessItem.address,
                                        vmessItem.port);
                    url = Utils.Base64Encode(url);
                    url = string.Format("{0}{1}{2}", Global.ssProtocol, url, remark);
                }
                else if (vmessItem.configType == (int)EConfigType.Socks)
                {
                    string remark = string.Empty;
                    if (!Utils.IsNullOrEmpty(vmessItem.remarks))
                    {
                        remark = "#" + WebUtility.UrlEncode(vmessItem.remarks);
                    }
                    url = string.Format("{0}:{1}@{2}:{3}",
                                        vmessItem.security,
                                        vmessItem.id,
                                        vmessItem.address,
                                        vmessItem.port);
                    url = Utils.Base64Encode(url);
                    url = string.Format("{0}{1}{2}", Global.socksProtocol, url, remark);
                }
                else if (vmessItem.configType == (int)EConfigType.Trojan)
                {
                    string remark = string.Empty;
                    if (!Utils.IsNullOrEmpty(vmessItem.remarks))
                    {
                        remark = "#" + WebUtility.UrlEncode(vmessItem.remarks);
                    }
                    string query = string.Empty;
                    if (!Utils.IsNullOrEmpty(vmessItem.requestHost))
                    {
                        query = string.Format("?sni={0}", vmessItem.requestHost);
                    }
                    url = string.Format("{0}@{1}:{2}",
                                        vmessItem.id,
                                        vmessItem.address,
                                        vmessItem.port);
                    url = string.Format("{0}{1}{2}{3}", Global.trojanProtocol, url, query, remark);
                }
                else
                {
                }
                return(url);
            }
            catch
            {
                return("");
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 传入字符串来生成vmess配置
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="msg"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List <VmessItem> ImportFromStrConfig(out string msg, string str)
        {
            msg = string.Empty;
            List <VmessItem> vmessItems = new List <VmessItem>();

            try
            {
                if (Utils.IsNullOrEmpty(str))
                {
                    msg = "读取配置文件失败";
                    return(null);
                }

                //换行符换成空格
                str = str.Replace("\r\n", " ");
                //多个空格合并为一个
                str = new System.Text.RegularExpressions.Regex("[\\s]+").Replace(str, " ");
                //空格分割字符串
                string[] vmessStrs = str.Trim().Split(' ');

                if (str.StartsWith(Global.vmessProtocol))
                {
                    foreach (string s in vmessStrs)
                    {
                        VmessItem vmessItem = new VmessItem();

                        vmessItem.configType = (int)EConfigType.Vmess;
                        string temp = s.Substring(Global.vmessProtocol.Length);
                        temp = Utils.Base64Decode(temp);

                        //转成Json
                        VmessQRCode vmessQRCode = Utils.FromJson <VmessQRCode>(temp);
                        if (vmessQRCode == null)
                        {
                            msg = "转换配置文件失败";
                            return(null);
                        }
                        vmessItem.security   = Global.DefaultSecurity;
                        vmessItem.network    = Global.DefaultNetwork;
                        vmessItem.headerType = Global.None;

                        vmessItem.remarks        = vmessQRCode.ps;
                        vmessItem.address        = vmessQRCode.add;
                        vmessItem.port           = Convert.ToInt32(vmessQRCode.port);
                        vmessItem.id             = vmessQRCode.id;
                        vmessItem.alterId        = Convert.ToInt32(vmessQRCode.aid);
                        vmessItem.network        = vmessQRCode.net;
                        vmessItem.headerType     = vmessQRCode.type;
                        vmessItem.requestHost    = vmessQRCode.host;
                        vmessItem.streamSecurity = vmessQRCode.tls;

                        vmessItems.Add(vmessItem);
                    }
                }
                else if (str.StartsWith(Global.ssProtocol))
                {
                    msg = "配置格式不正确";

                    foreach (string s in vmessStrs)
                    {
                        VmessItem vmessItem = new VmessItem();

                        vmessItem.configType = (int)EConfigType.Shadowsocks;
                        string temp        = s.Substring(Global.ssProtocol.Length);
                        int    indexRemark = temp.IndexOf("#");
                        if (indexRemark > 0)
                        {
                            temp = temp.Substring(0, indexRemark);
                        }
                        temp = Utils.Base64Decode(temp);

                        string[] arr1 = temp.Split('@');
                        if (arr1.Length != 2)
                        {
                            return(null);
                        }
                        string[] arr21 = arr1[0].Split(':');
                        string[] arr22 = arr1[1].Split(':');
                        if (arr21.Length != 2 || arr21.Length != 2)
                        {
                            return(null);
                        }
                        vmessItem.address  = arr22[0];
                        vmessItem.port     = Convert.ToInt32(arr22[1]);
                        vmessItem.security = arr21[0];
                        vmessItem.id       = arr21[1];

                        vmessItems.Add(vmessItem);
                    }
                }
                else
                {
                    msg = "非vmess或ss协议";
                    return(null);
                }
            }
            catch
            {
                msg = "异常,不是正确的配置,请检查";
                return(null);
            }

            return(vmessItems);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 从剪贴板导入URL
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static VmessItem ImportFromClipboardConfig(out string msg)
        {
            msg = string.Empty;
            VmessItem vmessItem = new VmessItem();

            try
            {
                //载入配置文件
                string result = Utils.GetClipboardData();
                if (Utils.IsNullOrEmpty(result))
                {
                    msg = "读取配置文件失败";
                    return(null);
                }

                if (result.StartsWith(Global.vmessProtocol))
                {
                    vmessItem.configType = (int)EConfigType.Vmess;
                    result = result.Substring(Global.vmessProtocol.Length);
                    result = Utils.Base64Decode(result);

                    //转成Json
                    VmessQRCode vmessQRCode = Utils.FromJson <VmessQRCode>(result);
                    if (vmessQRCode == null)
                    {
                        msg = "转换配置文件失败";
                        return(null);
                    }
                    vmessItem.security   = Global.DefaultSecurity;
                    vmessItem.network    = Global.DefaultNetwork;
                    vmessItem.headerType = Global.None;

                    vmessItem.remarks        = vmessQRCode.ps;
                    vmessItem.address        = vmessQRCode.add;
                    vmessItem.port           = Convert.ToInt32(vmessQRCode.port);
                    vmessItem.id             = vmessQRCode.id;
                    vmessItem.alterId        = Convert.ToInt32(vmessQRCode.aid);
                    vmessItem.network        = vmessQRCode.net;
                    vmessItem.headerType     = vmessQRCode.type;
                    vmessItem.requestHost    = vmessQRCode.host;
                    vmessItem.streamSecurity = vmessQRCode.tls;
                }
                else if (result.StartsWith(Global.ssProtocol))
                {
                    msg = "配置格式不正确";

                    vmessItem.configType = (int)EConfigType.Shadowsocks;
                    result = result.Substring(Global.ssProtocol.Length);
                    int indexRemark = result.IndexOf("#");
                    if (indexRemark > 0)
                    {
                        result = result.Substring(0, indexRemark);
                    }
                    result = Utils.Base64Decode(result);

                    string[] arr1 = result.Split('@');
                    if (arr1.Length != 2)
                    {
                        return(null);
                    }
                    string[] arr21 = arr1[0].Split(':');
                    string[] arr22 = arr1[1].Split(':');
                    if (arr21.Length != 2 || arr21.Length != 2)
                    {
                        return(null);
                    }
                    vmessItem.address  = arr22[0];
                    vmessItem.port     = Convert.ToInt32(arr22[1]);
                    vmessItem.security = arr21[0];
                    vmessItem.id       = arr21[1];
                }
                else
                {
                    msg = "非vmess或ss协议";
                    return(null);
                }
            }
            catch
            {
                msg = "异常,不是正确的配置,请检查";
                return(null);
            }

            return(vmessItem);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 传入字符串来生成vmess配置
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List <VmessItem> ImportFromStrConfig(out string msg, string str)
        {
            msg = string.Empty;
            List <VmessItem> vmessItems = new List <VmessItem>();

            try
            {
                if (Utils.IsNullOrEmpty(str))
                {
                    msg = "读取配置文件失败";
                    return(null);
                }

                if (!str.Contains(Global.vmessProtocol) && !str.Contains(Global.ssProtocol))
                {
                    msg = "非vmess或ss协议";
                    return(null);
                }

                //去除vmess://或者ss://前面开头的多余字符
                int vIndex = str.IndexOf(Global.vmessProtocol, StringComparison.Ordinal);
                int sIndex = str.IndexOf(Global.ssProtocol, StringComparison.Ordinal);
                int index  = vIndex > sIndex ? (sIndex == -1?vIndex:sIndex) : (vIndex == -1 ? sIndex : vIndex);
                str = str.Substring(index);

                //以vmess://和ss://为分割符,应对混合批量字符串
                string[] strArray = str.Trim().Split(new string[] { Global.vmessProtocol, Global.ssProtocol }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string s in strArray)
                {
                    string temp      = s.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "");
                    Match  tempMatch = Regex.Match(temp, "([A-Za-z0-9_=-]+)", RegexOptions.IgnoreCase);

                    temp = Utils.Base64Decode(tempMatch.Groups[0].Value);

                    //base64转为失败则忽略这条
                    if (Utils.IsNullOrEmpty(temp))
                    {
                        continue;
                    }

                    VmessItem vmessItem = new VmessItem();

                    //vmess://字符串处理
                    if (temp.Contains("aid") && temp.Contains("ps"))
                    {
                        vmessItem.configType = (int)EConfigType.Vmess;

                        //转成Json
                        VmessQRCode vmessQRCode = Utils.FromJson <VmessQRCode>(temp);
                        if (vmessQRCode == null)
                        {
                            msg = "转换配置文件失败";
                            return(null);
                        }
                        vmessItem.security   = Global.DefaultSecurity;
                        vmessItem.network    = Global.DefaultNetwork;
                        vmessItem.headerType = Global.None;

                        vmessItem.configVersion  = Utils.ToInt(vmessQRCode.v);
                        vmessItem.remarks        = vmessQRCode.ps;
                        vmessItem.address        = vmessQRCode.add;
                        vmessItem.port           = Convert.ToInt32(vmessQRCode.port);
                        vmessItem.id             = vmessQRCode.id;
                        vmessItem.alterId        = Convert.ToInt32(vmessQRCode.aid);
                        vmessItem.network        = vmessQRCode.net;
                        vmessItem.headerType     = vmessQRCode.type;
                        vmessItem.requestHost    = vmessQRCode.host;
                        vmessItem.path           = vmessQRCode.path;
                        vmessItem.streamSecurity = vmessQRCode.tls;

                        ConfigHandler.UpgradeServerVersion(ref vmessItem);

                        vmessItems.Add(vmessItem);
                    }
                    //ss://字符串处理
                    else
                    {
                        vmessItem.configType = (int)EConfigType.Shadowsocks;
                        int indexRemark = temp.IndexOf("#", StringComparison.Ordinal);
                        if (indexRemark > 0)
                        {
                            temp = temp.Substring(0, indexRemark);
                        }

                        string[] arr1 = temp.Split('@');
                        if (arr1.Length != 2)
                        {
                            continue;
                        }

                        string[] arr21 = arr1[0].Split(':');
                        string[] arr22 = arr1[1].Split(':');
                        if (arr21.Length != 2 || arr21.Length != 2)
                        {
                            continue;
                        }

                        vmessItem.address  = arr22[0];
                        vmessItem.port     = Convert.ToInt32(arr22[1]);
                        vmessItem.security = arr21[0];
                        vmessItem.id       = arr21[1];

                        vmessItems.Add(vmessItem);
                    }
                }
            }
            catch
            {
                msg = "异常,不是正确的配置,请检查";
                return(null);
            }

            return(vmessItems);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 从剪贴板导入URL
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static VmessItem ImportFromClipboardConfig(string clipboardData)
        {
            VmessItem vmessItem = new VmessItem();

            try
            {
                //载入配置文件
                string result = clipboardData.TrimEx();// Utils.GetClipboardData();
                if (Utils.IsNullOrEmpty(result))
                {
                    return(null);
                }

                if (result.StartsWith(Global.vmessProtocol))
                {
                    int indexSplit = result.IndexOf("?");
                    if (indexSplit > 0)
                    {
                        vmessItem = ResolveVmess4Kitsunebi(result);
                    }
                    else
                    {
                        vmessItem.configType = (int)EConfigType.Vmess;
                        result = result.Substring(Global.vmessProtocol.Length);
                        result = Utils.Base64Decode(result);

                        //转成Json
                        VmessQRCode vmessQRCode = Utils.FromJson <VmessQRCode>(result);
                        if (vmessQRCode == null)
                        {
                            return(null);
                        }
                        vmessItem.security   = Global.DefaultSecurity;
                        vmessItem.network    = Global.DefaultNetwork;
                        vmessItem.headerType = Global.None;


                        vmessItem.configVersion = Utils.ToInt(vmessQRCode.v);
                        vmessItem.remarks       = Utils.ToString(vmessQRCode.ps);
                        vmessItem.address       = Utils.ToString(vmessQRCode.add);
                        vmessItem.port          = Utils.ToInt(vmessQRCode.port);
                        vmessItem.id            = Utils.ToString(vmessQRCode.id);
                        vmessItem.alterId       = Utils.ToInt(vmessQRCode.aid);

                        if (!Utils.IsNullOrEmpty(vmessQRCode.net))
                        {
                            vmessItem.network = vmessQRCode.net;
                        }
                        if (!Utils.IsNullOrEmpty(vmessQRCode.type))
                        {
                            vmessItem.headerType = vmessQRCode.type;
                        }

                        vmessItem.requestHost    = Utils.ToString(vmessQRCode.host);
                        vmessItem.path           = Utils.ToString(vmessQRCode.path);
                        vmessItem.streamSecurity = Utils.ToString(vmessQRCode.tls);
                    }
                }
                else if (result.StartsWith(Global.ssProtocol))
                {
                    vmessItem.configType = (int)EConfigType.Shadowsocks;
                    result = result.Substring(Global.ssProtocol.Length);
                    //remark
                    int indexRemark = result.IndexOf("#");
                    if (indexRemark > 0)
                    {
                        try
                        {
                            vmessItem.remarks = WebUtility.UrlDecode(result.Substring(indexRemark + 1, result.Length - indexRemark - 1));
                        }
                        catch { }
                        result = result.Substring(0, indexRemark);
                    }
                    //part decode
                    int indexS = result.IndexOf("@");
                    if (indexS > 0)
                    {
                        result = Utils.Base64Decode(result.Substring(0, indexS)) + result.Substring(indexS, result.Length - indexS);
                    }
                    else
                    {
                        result = Utils.Base64Decode(result);
                    }

                    string[] arr1 = result.Split('@');
                    if (arr1.Length != 2)
                    {
                        return(null);
                    }
                    string[] arr21 = arr1[0].Split(':');
                    //string[] arr22 = arr1[1].Split(':');
                    int indexPort = arr1[1].LastIndexOf(":");
                    if (arr21.Length != 2 || indexPort < 0)
                    {
                        return(null);
                    }
                    vmessItem.address  = arr1[1].Substring(0, indexPort);
                    vmessItem.port     = Utils.ToInt(arr1[1].Substring(indexPort + 1, arr1[1].Length - (indexPort + 1)));
                    vmessItem.security = arr21[0];
                    vmessItem.id       = arr21[1];
                }
                else if (result.StartsWith(Global.socksProtocol))
                {
                    vmessItem.configType = (int)EConfigType.Socks;
                    result = result.Substring(Global.socksProtocol.Length);
                    //remark
                    int indexRemark = result.IndexOf("#");
                    if (indexRemark > 0)
                    {
                        try
                        {
                            vmessItem.remarks = WebUtility.UrlDecode(result.Substring(indexRemark + 1, result.Length - indexRemark - 1));
                        }
                        catch { }
                        result = result.Substring(0, indexRemark);
                    }
                    //part decode
                    int indexS = result.IndexOf("@");
                    if (indexS > 0)
                    {
                    }
                    else
                    {
                        result = Utils.Base64Decode(result);
                    }

                    string[] arr1 = result.Split('@');
                    if (arr1.Length != 2)
                    {
                        return(null);
                    }
                    string[] arr21 = arr1[0].Split(':');
                    //string[] arr22 = arr1[1].Split(':');
                    int indexPort = arr1[1].LastIndexOf(":");
                    if (arr21.Length != 2 || indexPort < 0)
                    {
                        return(null);
                    }
                    vmessItem.address  = arr1[1].Substring(0, indexPort);
                    vmessItem.port     = Utils.ToInt(arr1[1].Substring(indexPort + 1, arr1[1].Length - (indexPort + 1)));
                    vmessItem.security = arr21[0];
                    vmessItem.id       = arr21[1];
                }
                else
                {
                    return(null);
                }
            }
            catch
            {
                return(null);
            }

            return(vmessItem);
        }