Exemplo n.º 1
0
        public void AddTest()
        {
            var list = new IPList(IPListLookupPriority.IndividualIP);
            list.Add("192.168.1.1", "rule1");
            list.Add("192.168.1.2", null);
            list.Add(IPHelper.ParseIP("192.168.1.3"), "rule3");
            list.Add(IPHelper.ParseIP("192.168.1.4"), null);
            list.Prepare();

            Assert.IsTrue(list.Check("192.168.1.1"));
            Assert.IsTrue(list.Check("192.168.1.2"));
            Assert.IsTrue(list.Check("192.168.1.3"));
            Assert.IsTrue(list.Check("192.168.1.4"));
            Assert.IsFalse(list.Check("192.168.1.100"));

            object rule;
            Assert.IsTrue(list.TryGetRule("192.168.1.1", out rule));
            Assert.AreEqual("rule1", (string)rule);

            Assert.IsTrue(list.TryGetRule("192.168.1.2", out rule));
            Assert.IsNull(rule);

            Assert.IsTrue(list.TryGetRule("192.168.1.3", out rule));
            Assert.AreEqual("rule3", (string)rule);

            Assert.IsTrue(list.TryGetRule("192.168.1.4", out rule));
            Assert.IsNull(rule);

            Assert.IsFalse(list.TryGetRule("192.168.1.100", out rule));
            Assert.IsNull(rule);
        }
Exemplo n.º 2
0
        public void LookupPriorityTest()
        {
            var list1 = new IPList(IPListLookupPriority.IndividualIP);
            list1.Add("192.168.1.0", "rule1");
            list1.Add("192.168.1.0", 24, "rule2");
            list1.Prepare();

            var list2 = new IPList(IPListLookupPriority.IPRange);
            list2.Add("192.168.1.0", "rule1");
            list2.Add("192.168.1.0", 24, "rule2");
            list2.Prepare();

            object rule;
            Assert.IsTrue(list1.TryGetRule("192.168.1.0", out rule));
            Assert.AreEqual("rule1", (string)rule);

            Assert.IsTrue(list2.TryGetRule("192.168.1.0", out rule));
            Assert.AreEqual("rule2", (string)rule);
        }
Exemplo n.º 3
0
        public void AddRangeTest()
        {
            var list = new IPList(IPListLookupPriority.IndividualIP);
            list.Add("192.168.1.0", 24, "rule1");
            list.Prepare();

            Assert.IsTrue(list.Check("192.168.1.0"));
            Assert.IsTrue(list.Check("192.168.1.100"));
            Assert.IsTrue(list.Check("192.168.1.255"));
            Assert.IsFalse(list.Check("192.168.2.0"));

            object rule;
            Assert.IsTrue(list.TryGetRule("192.168.1.0", out rule));
            Assert.AreEqual("rule1", (string)rule);

            Assert.IsTrue(list.TryGetRule("192.168.1.100", out rule));
            Assert.AreEqual("rule1", (string)rule);

            Assert.IsTrue(list.TryGetRule("192.168.1.255", out rule));
            Assert.AreEqual("rule1", (string)rule);

            Assert.IsFalse(list.TryGetRule("192.168.2.0", out rule));
            Assert.IsNull(rule);
        }
Exemplo n.º 4
0
        private IPList ParseCentralIPList(string name)
        {
            IPList list = new IPList(IPListLookupPriority.IPRange);

            var value = CentralConfig.GetValue(name);
            if (!string.IsNullOrWhiteSpace(value))
            {
                try
                {
                    foreach (var item in value.Split(';'))
                    {
                        if (!string.IsNullOrWhiteSpace(item))
                        {
                            var segs = item.Split(',');
                            if (segs.Length == 1)
                                list.Add(segs[0], null);
                            else if (segs.Length == 2)
                                list.Add(segs[0], int.Parse(segs[1]), null);
                        }
                    }

                    list.Prepare();
                }
                catch(Exception ex)
                {
                    list = null;
                    logger.Info("ParseCentralIPList", ex);
                }
            }

            return list;
        }
Exemplo n.º 5
0
        private void LoadCentralIPList()
        {
            IPList list = ParseCentralIPList(AppConst.CFX_ViewPermissionIPList);
            if (list != null)
                centralIPViewList = list;

            list = ParseCentralIPList(AppConst.CFX_ChangePermissionIPList);
            if (list != null)
                centralIPChangeList = list;

            list = ParseCentralIPList(AppConst.CFX_PublicIPList);
            if (list != null)
                publicIPList = list;
        }
Exemplo n.º 6
0
        private void LoadInnerIPList()
        {
            var ipList = new IPList(IPListLookupPriority.IPRange);
            ipList.Add("10.0.0.0", 8, null);
            ipList.Add("192.168.0.0", 16, null);
            ipList.Add("172.16.0.0", 12, null);
            ipList.Prepare();

            innerIPList = ipList;
        }
Exemplo n.º 7
0
        private void GetIPList(object state)
        {
            timer.Change(Timeout.Infinite, Timeout.Infinite);
            var beginTime = DateTime.Now;

            try
            {
                var newList = new IPList(IPListLookupPriority.IndividualIP);
                var json = DynamicJson.Parse(SendRequest());
                foreach (string r in Convert.ToString(json.Result).Split('|'))
                {
                    if (!string.IsNullOrWhiteSpace(r))
                    {
                        var rule = IPRule.Parse(r);
                        newList.Add(rule.IP, rule.MaskLevel, rule);
                    }
                }

                newList.Prepare();

                var oldContent = content;
                content = new Tuple<IPList, ConcurrentDictionary<string, IPInfo>>(newList, new ConcurrentDictionary<string, IPInfo>());
                Metrics(oldContent.Item2); //记录Metrics信息
            }
            catch (Exception ex)
            {
                content = EmptyContent();
                logger.Error("Get ip blocklist", ex);
            }

            if (ConfigSettings.BlockEnabled)
            {
                var delay = (long)DateTime.Now.Subtract(beginTime).TotalMilliseconds;
                var interval = ConfigSettings.ServiceRequestInterval * 1000;

                if (delay < interval)
                    timer.Change(interval - delay, Timeout.Infinite);
                else
                    timer.Change(0, Timeout.Infinite);
            }
            else
            {
                content = EmptyContent();
                opened = false;
            }
        }