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);
        }
        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;
        }
Beispiel #3
0
        private static void WriteGroupsToFile(StreamWriter file, IPRangesGroup @group)
        {
            file.WriteLine("<group name=\"{0}\">", @group.Name);

            foreach (var region in group.Regions)
            {
                if (String.IsNullOrEmpty(region.Id))
                    throw new InvalidOperationException(String.Format("No mapping for region {0}", region.Name));

                var nameFragment = region.Name != null ? " name=\"" + region.Name + "\"" : "";
                file.WriteLine("  <region id=\"{0}\"{1} description=\"{2}\">", region.Id, nameFragment, region.Description);

                foreach (var range in region.Ranges.OrderBy(x => x.From, new IPAddressComparer()))
                {
                    file.WriteLine("    <range network=\"{0}\" from=\"{1}\" to=\"{2}\" />", range.GetNetwork(), range.From, range.To);
                }

                file.WriteLine("  </region>");
            }

            file.WriteLine("</group>");
        }
Beispiel #4
0
        private static void WriteGroupsToFile(StreamWriter file, IPRangesGroup @group)
        {
            file.WriteLine("<group name=\"{0}\">", @group.Name);

            foreach (var region in group.Regions)
            {
                if (String.IsNullOrEmpty(region.Id))
                {
                    throw new InvalidOperationException(String.Format("No mapping for region {0}", region.Name));
                }

                var nameFragment = region.Name != null ? " name=\"" + region.Name + "\"" : "";
                file.WriteLine("  <region id=\"{0}\"{1} description=\"{2}\">", region.Id, nameFragment, region.Description);

                foreach (var range in region.Ranges.OrderBy(x => x.From, new IPAddressComparer()))
                {
                    file.WriteLine("    <range network=\"{0}\" from=\"{1}\" to=\"{2}\" />", range.GetNetwork(), range.From, range.To);
                }

                file.WriteLine("  </region>");
            }

            file.WriteLine("</group>");
        }