示例#1
0
        public static void Cidr_Invalid(string netmask)
        {
            // Arrange

            // Act
            Assert.Throws <ArgumentException>(() => IpNumberHelper.Cidr(IpNumberHelper.ToIpNumber(netmask)));

            // Assert
        }
示例#2
0
        public static void ToIpNumber_ArgumentException(string ip)
        {
            // Arrange

            // Act
            Assert.Throws <ArgumentException>(() => IpNumberHelper.ToIpNumber(ip));

            // Assert
        }
示例#3
0
        public static void ToIpLongFromString(long expected, string ip)
        {
            // Arrange

            // Act
            var result = IpNumberHelper.ToIpLong(IpNumberHelper.ToIpNumber(ip));

            // Assert
            Assert.Equal(expected, result);
        }
示例#4
0
        public static void Cidr(int expected, string netmask)
        {
            // Arrange

            // Act
            var result = IpNumberHelper.Cidr(IpNumberHelper.ToIpNumber(netmask));

            // Assert
            Assert.Equal(expected, result);
        }
示例#5
0
        public static void Random(string ip)
        {
            // Arrange

            // Act
            var ipnum  = IpNumberHelper.ToIpNumber(ip);
            var result = IpNumberHelper.ToIpString(ipnum);

            // Assert
            Assert.Equal(ip, result);
        }
示例#6
0
        public static void Broadcast(string expected, string ip, int cidr)
        {
            // Arrange
            var mask  = IpNumberHelper.Netmask(cidr);
            var ipnum = IpNumberHelper.Network(IpNumberHelper.ToIpNumber(ip), mask);

            // Act
            var result = IpNumberHelper.Broadcast(ipnum, mask);

            // Assert
            Assert.Equal(expected, IpNumberHelper.ToIpString(result));
        }
示例#7
0
        public static void Contains(bool expected, string net, int cidr, string ip)
        {
            // Arrange
            var network = IpNumberHelper.ToIpNumber(net);
            var mask    = IpNumberHelper.Netmask(cidr);
            var ipnum   = IpNumberHelper.ToIpNumber(ip);

            // Act
            var result = IpNumberHelper.Contains(network, mask, ipnum);

            // Assert
            Assert.Equal(expected, result);
        }
示例#8
0
        private static void OnEnter(object sender, EventArgs e)
        {
            var context = ((HttpApplication)sender).Context;

            if ((g_allowedNetworks == null && g_deniedNetworks == null) ||
                context.Error != null ||
                context.CurrentHandler == null ||
                context.CurrentHandler is DefaultHttpHandler)
            {
                return;
            }

            var request = context.Request;

            if (SecureOnly && request.Scheme() != Uri.UriSchemeHttps)
            {
                return;
            }

            foreach (var ip in request.UserHosts())
            {
                var ipnum = IpNumberHelper.ToIpNumber(ip);
                if (g_deniedNetworks != null)
                {
                    var deniedNetwork = FindMatch(ipnum, g_deniedNetworks);
                    if (deniedNetwork != null &&
                        (NoExceptionPathRegex == null ||
                         !NoExceptionPathRegex.IsMatch(request.RawUrl)))
                    {
                        throw new IpPolicyException(string.Format(CultureInfo.InvariantCulture,
                                                                  "Matched '{0}' in denied network {1}/{2}.", ip,
                                                                  IpNumberHelper.ToIpString(deniedNetwork.Network),
                                                                  IpNumberHelper.ToIpString(deniedNetwork.Mask)));
                    }
                }

                if (g_allowedNetworks != null)
                {
                    var allowedNetwork = FindMatch(ipnum, g_allowedNetworks);
                    if (allowedNetwork == null &&
                        (NoExceptionPathRegex == null ||
                         !NoExceptionPathRegex.IsMatch(request.RawUrl)))
                    {
                        throw new IpPolicyException(string.Format(CultureInfo.InvariantCulture,
                                                                  "Missmatch '{0}' in allowed networks.", ip));
                    }
                }
            }
        }
示例#9
0
        public void Add(string username, short eventid, string related, string[] arguments)
        {
            var item = new HistoryLogItem()
            {
                Username  = username,
                Timestamp = DateTime.UtcNow,
                EventId   = eventid,
                Hosts     = m_remoteLocationProvider.RemoteLocation.Hosts.Translate(host =>
                                                                                    IpNumberHelper.ToIpNumber(host)).ToArray(),
                RelatedTo = related,
                Arguments = HistoryLogItem.Join(arguments)
            };

            m_historyLogQueue.Enqueue(item);
        }
示例#10
0
        private static IpNetwork[] Parse(string[] networks)
        {
            var result = new List <IpNetwork>();

            foreach (var net in networks)
            {
                var parts = net.Split('/');
                if (parts.Length != 2)
                {
                    throw new InvalidOperationException("Invalid network");
                }

                result.Add(new IpNetwork()
                {
                    Network = IpNumberHelper.ToIpNumber(parts[0]),
                    Mask    = IpNumberHelper.Netmask(Int32.Parse(parts[1], NumberStyles.Integer, CultureInfo.InvariantCulture))
                });
            }

            return(result.ToArray());
        }
示例#11
0
        public void Add(string username, int eventid, string related, string args)
        {
            // Arrange
            Assert.True(m_historyLogQueue.IsEmpty());

            // Act
            m_historyLogAgent.Add(username, (short)eventid, related, HistoryLogItem.Split(args));

            // Assert
            Assert.False(m_historyLogQueue.IsEmpty());
            HistoryLogItem item = null;

            m_historyLogQueue.Process(x => item = x);
            Assert.NotNull(item);

            Assert.Equal(username, item.Username);
            Assert.True(DateTime.UtcNow.Subtract(item.Timestamp) < TimeSpan.FromSeconds(1));
            Assert.Equal(eventid, item.EventId);
            Assert.Equal(1, item.Hosts.Length);
            Assert.Equal(IpNumberHelper.ToIpNumber(RemoteLocation.Localhost.Hosts[0]), item.Hosts[0]);
            Assert.Equal(related, item.RelatedTo);
            Assert.Equal(args, item.Arguments);
        }