Example #1
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);
        }
Example #2
0
        public void Prepare()
        {
            if (ArgType != null)
            {
                operandType = TypeHelper.FindType(ArgType);
                OperandType ot = (OperandType)Convert.ToInt16(OpType);
                String[]    r;
                String[]    m;
                switch (ot)
                {
                case OperandType.InlineField:
                    ArgValue = new System.Text.RegularExpressions.Regex(@"\[\[(.+?)\]\]").Replace(ArgValue, "");
                    r        = ArgValue.Split(new String[] { " " }, StringSplitOptions.None);
                    m        = r[1].Split(new String[] { "::" }, StringSplitOptions.None);
                    if (m[0].StartsWith("local."))
                    {
                        operand = new TValue(m[1]);
                    }
                    else
                    {
                        operand = new TValue(TypeHelper.FindType(m[0]).GetField(m[1]));
                    }
                    break;

                case OperandType.InlineMethod:
                    ArgValue = new System.Text.RegularExpressions.Regex(@"\[\[(.+?)\]\]").Replace(ArgValue, "");
                    r        = ArgValue.Split(new String[] { " " }, StringSplitOptions.None);
                    m        = r[1].Split(new String[] { "::" }, StringSplitOptions.None);
                    operand  = new TValue(MethodFromString(r[0], m[0], m[1]));
                    break;

                case OperandType.ShortInlineBrTarget:
                case OperandType.InlineBrTarget:
                    operand = new TValue(Convert.ToInt32(ArgValue));
                    break;

                case OperandType.InlineType:
                    if (ArgValue.StartsWith("local."))
                    {
                        operand     = new TValue(ArgValue);
                        operandType = typeof(TValue);
                    }
                    else
                    {
                        operand     = new TValue(TypeHelper.FindType(ArgValue));
                        operandType = TypeHelper.FindType(ArgValue);
                    }
                    break;

                case OperandType.InlineString:
                    operand = new TValue(ArgValue);
                    break;

                case OperandType.ShortInlineVar:
                    operand = new TValue(Convert.ToInt32(ArgValue));
                    break;

                case OperandType.InlineI:
                case OperandType.ShortInlineI:
                    operand = new TValue(Convert.ToInt32(ArgValue));
                    break;

                case OperandType.InlineR:
                case OperandType.ShortInlineR:
                    System.Globalization.CultureInfo userCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
                    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ru-RU");
                    System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";
                    try
                    {
                        operand = new TValue(Convert.ToDouble(ArgValue));
                    }
                    finally
                    {
                        System.Threading.Thread.CurrentThread.CurrentCulture = userCulture;
                    }
                    break;

                case OperandType.InlineTok:
                    if (ArgValue.StartsWith("local."))
                    {
                        operand = new TValue(ArgValue);
                    }
                    else
                    {
                        operand = new TValue(TypeHelper.FindType(ArgValue));
                    }
                    break;

                case OperandType.InlineSwitch:
                    operand = new TValue(Array.ConvertAll <String, int>(ArgValue.Split(new String[] { "," }, StringSplitOptions.None), item => System.Int32.Parse(item)));
                    break;

                case OperandType.InlineI8:
                    operand = new TValue(Convert.ToUInt64(ArgValue));
                    break;
                }
            }
        }