public void Foo()
        {
            string         ip    = "127.0.0.1/28";
            IPAddressRange range = IPAddressRange.FromCidrAddress(ip);

            Assert.IsFalse(IPAddressRange.IsNullOrEmpty(range));
            Console.Error.WriteLine(range.ToString());
        }
        public void IPAddressNotInRangeTest()
        {
            string         ip    = "127.10.0.0/28";
            IPAddressRange range = IPAddressRange.FromCidrAddress(ip);

            IPAddress.TryParse("192.168.0.1", out IPAddress candidate);

            Assert.IsFalse(range.IPInRange(candidate));
        }
        public void ParseCidrBlockTest()
        {
            string         ip    = "127.10.0.0/28";
            IPAddressRange range = IPAddressRange.FromCidrAddress(ip);

            IPAddress.TryParse("127.10.0.0", out IPAddress lower);
            IPAddress.TryParse("127.10.0.15", out IPAddress higher);

            Assert.IsFalse(IPAddressRange.IsNullOrEmpty(range));
            Assert.AreEqual(lower, range.Lower);
            Assert.AreEqual(higher, range.Upper);
            Console.Error.WriteLine(range.ToString());
        }
Ejemplo n.º 4
0
        public Toggle GetFlag(string name, ToggleData userData)
        {
            if (!toggles.ContainsKey(name))
            {
                return(Toggle.Empty);
            }

            ToggleElement element = toggles[name];

            if (!string.IsNullOrWhiteSpace(userData.UserRoles))
            {
                List <string> roles = element.roles.Select(x => x.role.name).ToList();
                bool          found = false;
                foreach (string role in userData.UserRoles.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (roles.Contains(role))
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    return(new Toggle(name, false));
                }
            }

            if (!string.IsNullOrWhiteSpace(userData.UserId))
            {
                List <string> users = element.users.Select(x => x.user.name).ToList();

                if (!users.Contains(userData.UserId))
                {
                    return(new Toggle(name, false));
                }
            }

            if (!string.IsNullOrWhiteSpace(userData.IpAddress))
            {
                if (!IPAddress.TryParse(userData.IpAddress, out IPAddress candidate))
                {
                    return(new Toggle(name, false));
                }

                List <string> addresses = element.ipaddresses.Select(x => x.ipaddress.value).ToList();
                bool          found     = false;
                foreach (string address in addresses)
                {
                    if (string.IsNullOrWhiteSpace(address))
                    {
                        continue;
                    }

                    IPAddressRange range = IPAddressRange.FromCidrAddress(address);
                    if (range == IPAddressRange.Empty)
                    {
                        continue;
                    }

                    if (range.IPInRange(candidate))
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    return(new Toggle(name, false));
                }
            }

            return(new Toggle(name, true));
        }