/// <summary>
 /// 添加代理转发规则
 /// </summary>
 /// <param name="rule"></param>
 public static void AddProxyRule(ProxyOptions rule)
 {
     if (options == null)
     {
         options = new ProxyOptionGroup {
             Excludes = new List <string>(), Options = new List <ProxyOptions>()
         };
     }
     options.Options.Add(rule);
 }
Example #2
0
        /// <summary>
        /// 找到转发匹配规则
        /// </summary>
        /// <param name="options">规则组</param>
        /// <param name="path">请求路径</param>
        /// <returns>规则项</returns>
        public static ProxyOptions MatchProxyRule(this string path, ProxyOptionGroup options)
        {
            ProxyOptions op = null;

            if (options.Excludes != null && options.Excludes.Exists(e => path.StartsWith(e, StringComparison.CurrentCultureIgnoreCase)))
            {
                return(op);
            }
            foreach (var item in options.Options)
            {
                if (item.Excludes != null && item.Excludes.Exists(e => path.StartsWith(e, StringComparison.CurrentCultureIgnoreCase)))
                {
                    continue;
                }
                if (item.MatchReg.IsMatch(path))
                {
                    op = item;
                    break;
                }
            }
            return(op);
        }
        /// <summary>
        /// 找到转发匹配规则
        /// </summary>
        /// <param name="options">规则组</param>
        /// <param name="path">请求路径</param>
        /// <returns>规则组</returns>
        public async static Task <List <ProxyOptions> > MatchProxyRule(this string path, ProxyOptionGroup options)
        {
            //静态代理规则
            if (options == null || (options.Excludes != null && options.Excludes.Exists(e => path.StartsWith(e, StringComparison.CurrentCultureIgnoreCase))))
            {
                return(null);
            }
            List <ProxyOptions> opList = new List <ProxyOptions>();

            foreach (var item in options.Options)
            {
                if (item.Excludes != null && item.Excludes.Exists(e => path.StartsWith(e, StringComparison.CurrentCultureIgnoreCase)))
                {
                    continue;
                }
                if (item.MatchReg.IsMatch(path))
                {
                    opList.Add(item);
                }
            }
            //静态代理规则没有匹配中
            if (opList.Count == 0)
            {
                //动态代理规则
                var dynamicProxyList = IocManager.Instance.Resolve <IEnumerable <IDynamicProxyRule> >();
                if (dynamicProxyList != null && dynamicProxyList.Count() > 0)
                {
                    foreach (var pathParse in dynamicProxyList)
                    {
                        string uri = await pathParse.ParsePath(path);

                        if (!string.IsNullOrWhiteSpace(uri) && !uri.Equals(path))
                        {
                            opList.Add(new ProxyOptions
                            {
                                MatchReg = new Regex(path, RegexOptions.IgnorePatternWhitespace),
                                Uri      = new Uri(uri)
                            });
                        }
                    }
                }
            }
            return(opList);
        }
 /// <summary>
 /// 初始化代理转发规则
 /// </summary>
 /// <param name="_options"></param>
 public static void InitProxyRule(ProxyOptionGroup _options)
 {
     options = _options;
 }