コード例 #1
0
ファイル: Program.cs プロジェクト: salihy/NetIpTools
        static void Main(string[] args)
        {
            IPRange ipRange = new IPRange();

            ipRange.Parse("192.168.0.12/24");
            var ipList        = ipRange.GetAllIpAddresses();
            var ipRangeList25 = ipRange.GetSmallerRanges(25);
            var ipRangeList26 = ipRange.GetSmallerRanges(26);
            var ipRangeList27 = ipRange.GetSmallerRanges(27);

            foreach (var item in ipRangeList25)
            {
                var v1 = item.GetAllIpAddresses();
                var v2 = v1;
            }

            foreach (var item in ipRangeList26)
            {
                var v1 = item.GetAllIpAddresses();
                var v2 = v1;
            }

            foreach (var item in ipRangeList27)
            {
                var v1 = item.GetAllIpAddresses();
                var v2 = v1;
            }
            var x = ipList;

            //var ipList2 = IPAddressRange.Parse("192.168.0.12/25");
            //var y = ipList2;
        }
コード例 #2
0
        public void FileUriBuilder_SasTest()
        {
            // Arrange
            var uriString   = "https://account.file.core.windows.net/share?comp=list&sv=2015-04-05&spr=https&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sip=168.1.5.60-168.1.5.70&sr=b&sp=rw&sig=Z%2FRHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk%3D";
            var originalUri = new UriBuilder(uriString);

            // Act
            var fileUriBuilder = new FileUriBuilder(originalUri.Uri);
            var newUri         = fileUriBuilder.ToUri();

            // Assert
            Assert.AreEqual("https", fileUriBuilder.Scheme);
            Assert.AreEqual("account.file.core.windows.net", fileUriBuilder.Host);
            Assert.AreEqual(443, fileUriBuilder.Port);
            Assert.AreEqual("share", fileUriBuilder.ShareName);
            Assert.AreEqual("", fileUriBuilder.DirectoryOrFilePath);
            Assert.AreEqual("", fileUriBuilder.Snapshot);

            Assert.AreEqual(new DateTimeOffset(2015, 4, 30, 2, 23, 26, TimeSpan.Zero), fileUriBuilder.Sas.ExpiryTime);
            Assert.AreEqual("", fileUriBuilder.Sas.Identifier);
            Assert.AreEqual(IPRange.Parse("168.1.5.60-168.1.5.70"), fileUriBuilder.Sas.IPRange);
            Assert.AreEqual("rw", fileUriBuilder.Sas.Permissions);
            Assert.AreEqual(SasProtocol.Https, fileUriBuilder.Sas.Protocol);
            Assert.AreEqual("b", fileUriBuilder.Sas.Resource);
            Assert.AreEqual("", fileUriBuilder.Sas.ResourceTypes);
            Assert.AreEqual("", fileUriBuilder.Sas.Services);
            Assert.AreEqual("Z/RHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk=", fileUriBuilder.Sas.Signature);
            Assert.AreEqual(new DateTimeOffset(2015, 4, 29, 22, 18, 26, TimeSpan.Zero), fileUriBuilder.Sas.StartTime);
            Assert.AreEqual("2015-04-05", fileUriBuilder.Sas.Version);

            Assert.AreEqual("comp=list", fileUriBuilder.UnparsedParams);

            Assert.AreEqual(originalUri, newUri);
        }
コード例 #3
0
        public void BlobUriBuilder_RegularUrl_SasTest()
        {
            // Arrange
            var uriString   = "https://account.core.blob.windows.net/container/blob?sv=2015-04-05&spr=https&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sip=168.1.5.60-168.1.5.70&sr=b&sp=rw&sig=Z%2FRHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk%3D";
            var originalUri = new UriBuilder(uriString);

            // Act
            var blobUriBuilder = new BlobUriBuilder(originalUri.Uri);
            Uri newUri         = blobUriBuilder.ToUri();

            // Assert
            Assert.AreEqual("https", blobUriBuilder.Scheme);
            Assert.AreEqual("account.core.blob.windows.net", blobUriBuilder.Host);
            Assert.AreEqual("", blobUriBuilder.AccountName);
            Assert.AreEqual("container", blobUriBuilder.BlobContainerName);
            Assert.AreEqual("blob", blobUriBuilder.BlobName);
            Assert.AreEqual("", blobUriBuilder.Snapshot);

            Assert.AreEqual(new DateTimeOffset(2015, 4, 30, 2, 23, 26, TimeSpan.Zero), blobUriBuilder.Sas.ExpiryTime);
            Assert.AreEqual("", blobUriBuilder.Sas.Identifier);
            Assert.AreEqual(IPRange.Parse("168.1.5.60-168.1.5.70"), blobUriBuilder.Sas.IPRange);
            Assert.AreEqual("rw", blobUriBuilder.Sas.Permissions);
            Assert.AreEqual(SasProtocol.Https, blobUriBuilder.Sas.Protocol);
            Assert.AreEqual("b", blobUriBuilder.Sas.Resource);
            Assert.AreEqual("", blobUriBuilder.Sas.ResourceTypes);
            Assert.AreEqual("", blobUriBuilder.Sas.Services);
            Assert.AreEqual("Z/RHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk=", blobUriBuilder.Sas.Signature);
            Assert.AreEqual(new DateTimeOffset(2015, 4, 29, 22, 18, 26, TimeSpan.Zero), blobUriBuilder.Sas.StartTime);
            Assert.AreEqual("2015-04-05", blobUriBuilder.Sas.Version);

            Assert.AreEqual("", blobUriBuilder.Query);
            Assert.AreEqual(443, blobUriBuilder.Port);
            Assert.AreEqual(originalUri, newUri);
        }
コード例 #4
0
        public void QueueUriBuilder_IPStyleUrl_SasTest()
        {
            // Arrange
            var uriString   = "https://127.0.0.1/account/queue?sv=2015-04-05&spr=https&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sip=168.1.5.60-168.1.5.70&sr=b&sp=rw&sig=Z%2FRHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk%3D";
            var originalUri = new UriBuilder(uriString);

            // Act
            var queueUriBuilder = new QueueUriBuilder(originalUri.Uri);
            var newUri          = queueUriBuilder.ToUri();

            // Assert
            Assert.AreEqual("https", queueUriBuilder.Scheme);
            Assert.AreEqual("127.0.0.1", queueUriBuilder.Host);
            Assert.AreEqual(443, queueUriBuilder.Port);
            Assert.AreEqual("account", queueUriBuilder.AccountName);
            Assert.AreEqual("queue", queueUriBuilder.QueueName);
            Assert.IsFalse(queueUriBuilder.Messages);
            Assert.AreEqual("", queueUriBuilder.MessageId);

            Assert.AreEqual(new DateTimeOffset(2015, 4, 30, 2, 23, 26, TimeSpan.Zero), queueUriBuilder.Sas.ExpiryTime);
            Assert.AreEqual("", queueUriBuilder.Sas.Identifier);
            Assert.AreEqual(IPRange.Parse("168.1.5.60-168.1.5.70"), queueUriBuilder.Sas.IPRange);
            Assert.AreEqual("rw", queueUriBuilder.Sas.Permissions);
            Assert.AreEqual(SasProtocol.Https, queueUriBuilder.Sas.Protocol);
            Assert.AreEqual("b", queueUriBuilder.Sas.Resource);
            Assert.AreEqual("", queueUriBuilder.Sas.ResourceTypes);
            Assert.AreEqual("", queueUriBuilder.Sas.Services);
            Assert.AreEqual("Z/RHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk=", queueUriBuilder.Sas.Signature);
            Assert.AreEqual(new DateTimeOffset(2015, 4, 29, 22, 18, 26, TimeSpan.Zero), queueUriBuilder.Sas.StartTime);
            Assert.AreEqual("2015-04-05", queueUriBuilder.Sas.Version);

            Assert.AreEqual("", queueUriBuilder.UnparsedParams);

            Assert.AreEqual(originalUri, newUri);
        }
コード例 #5
0
        private static IPRange[] GetAllLocalIPAddresses()
        {
            List <IPRange> list = new List <IPRange>();

            try
            {
                NetworkInterface[] allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface networkInterface in from a in allNetworkInterfaces
                         where a.OperationalStatus == OperationalStatus.Up
                         select a)
                {
                    IPInterfaceProperties ipproperties = networkInterface.GetIPProperties();
                    UnicastIPAddressInformationCollection unicastAddresses = ipproperties.UnicastAddresses;
                    foreach (IPAddressInformation ipaddressInformation in unicastAddresses.OrderBy((UnicastIPAddressInformation ua) => ua.Address.AddressFamily))
                    {
                        if (!ipaddressInformation.IsTransient)
                        {
                            if (ipaddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                            {
                                list.Add(IPRange.Parse(ipaddressInformation.Address.ToString()));
                            }
                            else if (ipaddressInformation.Address.AddressFamily == AddressFamily.InterNetworkV6)
                            {
                                list.Add(IPRange.Parse(ipaddressInformation.Address.ToString()));
                            }
                        }
                    }
                }
            }
            catch (NetworkInformationException ex)
            {
                ExTraceGlobals.ClientAccessRulesTracer.TraceDebug(0L, string.Format("[Client Access Rules] GetAllLocalIPAddresses threw an unexpected exception ({0})", ex.ToString()));
            }
            return(list.Distinct <IPRange>().ToArray <IPRange>());
        }
コード例 #6
0
 /// <summary>
 /// Default constructor.
 /// </summary>
 /// <param name="pattern"></param>
 public AddressCondition(string pattern)
 {
     if (pattern == null)
     {
         throw new ArgumentNullException("pattern");
     }
     _range = IPRange.Parse(pattern);
 }
コード例 #7
0
        public void Parse_SingleIP_FromToSame()
        {
            var ip    = IPAddress.Parse("1.2.3.4");
            var range = IPRange.Parse(ip.ToString());

            Assert.Equal(ip, range.From);
            Assert.Equal(ip, range.To);
        }
コード例 #8
0
        public void Parse(string network, string from, string to)
        {
            var fromIp = IPAddress.Parse(from);
            var toIp   = IPAddress.Parse(to);
            var range  = IPRange.Parse(network);

            Assert.Equal(fromIp, range.From);
            Assert.Equal(toIp, range.To);
        }
コード例 #9
0
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="pattern"></param>
        public AddressCondition(string pattern)
        {
            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }

            this._range = IPRange.Parse(pattern);
        }
コード例 #10
0
        public IPRangesGroup GetRangesGroup()
        {
            var amazonRegionMapper = new AmazonRegionMapper();

            var prefixes       = new AmazonIpRangeSource().LoadPrefixes();
            var rangesByRegion =
                prefixes.Select(
                    prefix =>
            {
                var regionInformation = amazonRegionMapper.GetIPRangesRegion(prefix.Region);
                return(new
                {
                    regionInformation.Id,
                    regionInformation.Name,
                    regionInformation.Description,
                    prefix.IpPrefix
                });
            })
                .ToLookup(x => x.Id);

            var regionIds = rangesByRegion.Select(x => x.Key).Distinct().OrderBy(x => x);

            var ipRangesGroup = new IPRangesGroup {
                Name = "AmazonAWS"
            };

            foreach (var regionId in regionIds)
            {
                IPRangesRegion ipRangesRegion = null;

                foreach (var range in rangesByRegion[regionId].Distinct())
                {
                    if (ipRangesRegion == null)
                    {
                        ipRangesRegion = new IPRangesRegion
                        {
                            Id          = range.Id,
                            Name        = range.Name,
                            Description = range.Description
                        };
                        ipRangesGroup.Regions.Add(ipRangesRegion);
                    }

                    var ipRange = IPRange.Parse(range.IpPrefix);
                    ipRangesRegion.Ranges.Add(ipRange);
                }
            }

            return(ipRangesGroup);
        }
コード例 #11
0
ファイル: OptionsForm.cs プロジェクト: DmitriiKostrov/Nemo
        private void setNewLocals()
        {
            _options.Locals.Clear();
            IPRange range = new IPRange();

            foreach (DataGridViewRow row in LocalsDataGrid.Rows)
            {
                if (range.Parse(row.AccessibilityObject.GetChild(1).Value))
                {
                    _options.Locals.Add(range);
                    range = new IPRange();
                }
            }
        }
コード例 #12
0
ファイル: OptionsForm.cs プロジェクト: DmitriiKostrov/Nemo
        private void button1_Click(object sender, EventArgs e)
        {
            IPRange range  = new IPRange();
            bool    errors = false;

            foreach (DataGridViewRow row in LocalsDataGrid.SelectedRows)
            {
                if (!range.Parse(row.AccessibilityObject.GetChild(1).Value))
                {
                    errors = true;
                    row.AccessibilityObject.GetChild(1).Value = "!!!" + row.AccessibilityObject.GetChild(1).Value;
                }
            }
            MessageBox.Show((errors) ? "Invalid values marked with !!!" : "No errors have been detected", "Verified");
        }
コード例 #13
0
 public void Parse_Null_ThrowsArgumentNullException()
 {
     Assert.Throws <ArgumentNullException>(() => IPRange.Parse(null));
 }
コード例 #14
0
 public void Count(string network, long count)
 {
     Assert.Equal((ulong)count, IPRange.Parse(network).Count);
 }
コード例 #15
0
 public void BigCount(string network, long count)
 {
     Assert.Equal(new BigInteger(count), IPRange.Parse(network).BigCount);
 }
コード例 #16
0
        public Options()
        {
            CustomFilters = new DynamicFilter();
            //prepare standard filters
            CheckedFilters.Add(FilterName.Address);
            CheckedFilters.Add(FilterName.Port);
            CheckedFilters.Add(FilterName.Service);
            CheckedFilters.Add(FilterName.Size);
            CheckedFilters.Add(FilterName.Time);
            CheckedFilters.Add(FilterName.Custom);
            //service map with proto names and ports
            ServiceMap = Constants.GetDefaultServiceMap();
            XElement rootXml = null;

            try
            {
                rootXml = XElement.Load(DynamicFilter.WorkingDir + cFileName);
            }
            catch
            {
                return;
            }
            //load filters
            try
            {
                List <XElement> filters = rootXml.Element("filters").Elements("filter").ToList();
                foreach (XElement fil in filters)
                {
                    if (CustomFilters.BuildFilter(fil.Attribute("text").Value))
                    {
                        CustomFilters.AddFilter(fil.Attribute("name").Value);
                    }
                }
            }
            catch {}
            //load aliases
            try
            {
                List <XElement> aliases = rootXml.Element("aliases").Elements("alias").ToList();
                foreach (XElement al in aliases)
                {
                    try
                    {
                        string ip = al.Attribute("ip").Value;
                        IPAddress.Parse(ip);
                        Aliases[ip] = al.Attribute("name").Value;
                    }
                    catch { }
                }
            }
            catch { }
            //load locals
            try
            {
                List <XElement> locals = rootXml.Element("locals").Elements("local").ToList();
                foreach (XElement loc in locals)
                {
                    IPRange range = new IPRange();
                    if (range.Parse(loc.Attribute("range").Value))
                    {
                        Locals.Add(range);
                    }
                }
            }
            catch { }
        }
コード例 #17
0
        public void InvalidParses()
        {
            // Can't parse empty strings
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("");
            }
                                            );

            // Can't parse combined ip address families
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("127.0.0.1-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
            }
                                            );
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("::1-255.255.255.255");
            }
                                            );

            // Can't parse invalid ip addresses
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("-1.0.0.0");
            }
                                            );
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("-1::");
            }
                                            );
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("256.0.0.0");
            }
                                            );
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("10000::");
            }
                                            );

            // Can't parse ip ranges with `any` addresses inside
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("0.0.0.0-192.168.1.1");
            }
                                            );
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("::-2001::");
            }
                                            );
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("192.168.1.1-0.0.0.0");
            }
                                            );
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("2001::-::");
            }
                                            );

            // Can't parse network addresses
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("127.0.0.1/28");
            }
                                            );
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("::1/112");
            }
                                            );
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("192.168.1.1/255.255.255.0");
            }
                                            );
            Assert.Throws <FormatException>(() =>
            {
                IPRange.Parse("2001:1::/ffff:ffff:ffff:ffff:ffff:ffff:ffff:0");
            }
                                            );
        }
コード例 #18
0
 public void Parse_InvalidIP_ThrowsException()
 {
     Assert.Throws <ArgumentException>(() => IPRange.Parse("257.2.3.4"));
 }
コード例 #19
0
 public void Parse_Empty_ThrowsArgumentException()
 {
     Assert.Throws <ArgumentException>(() => IPRange.Parse(string.Empty));
 }