public List <IpRangeViewModel> GetSubnetList(string ipFilePath = null) { if (string.IsNullOrEmpty(ipFilePath)) { ipFilePath = HostingEnvironment.MapPath("~/App_Data/"); } var result = new List <IpRangeViewModel>(); // Load Azure ip range data string ipFileList = ConfigurationManager.AppSettings["AzureIpRangeFileList"]; foreach (string filePath in ipFileList.Split(';')) { var xmlDoc = new XmlDocument(); xmlDoc.Load(ipFilePath + @"\IpRangeFiles\Azure\" + filePath); var root = xmlDoc.DocumentElement; foreach (XmlElement ele in root) { string region = ele.GetAttribute("Name"); var ipRange = new IpRangeViewModel { Cloud = "Azure", Region = region, Subnet = new List <string>() }; foreach (XmlElement xe in ele) { string subnet = xe.GetAttribute("Subnet"); ipRange.Subnet.Add(subnet); var network = IPNetwork.Parse(subnet); ipRange.TotalIpCount += network.Total; } result.Add(ipRange); } } // Get AWS ip range data string awsIpFile = ConfigurationManager.AppSettings["AwsIpRangeFile"]; string json = System.IO.File.ReadAllText(ipFilePath + @"\IpRangeFiles\AWS\" + awsIpFile); var jsSerializer = new JavaScriptSerializer(); var awsIpRangeData = jsSerializer.Deserialize <AwsIpRangeData>(json); foreach (var prefix in awsIpRangeData.prefixes) { string region = prefix.region; string subnet = prefix.ip_prefix; if (result.Any(v => v.Region == region)) { var ipRange = result.First(v => v.Region == region); ipRange.Subnet.Add(subnet); var network = IPNetwork.Parse(subnet); ipRange.TotalIpCount += network.Total; } else { var ipRange = new IpRangeViewModel { Cloud = "AWS", Region = region, Subnet = new List <string>() }; ipRange.Subnet.Add(subnet); var network = IPNetwork.Parse(subnet); ipRange.TotalIpCount += network.Total; result.Add(ipRange); } } // Get AliCloud ip range data string aliCloudIpFile = ConfigurationManager.AppSettings["AliCloudIpRangeFile"]; string[] lines = System.IO.File.ReadAllLines(ipFilePath + @"\IpRangeFiles\AliCloud\" + aliCloudIpFile); var aliIpRange = new IpRangeViewModel { Cloud = "AliCloud", Region = "AliCloud", Subnet = new List <string>() }; foreach (var line in lines) { string subnet = line; aliIpRange.Subnet.Add(subnet); var network = IPNetwork.Parse(subnet); aliIpRange.TotalIpCount += network.Total; } result.Add(aliIpRange); return(result); }
public List <IpRangeViewModel> GetIpRange() { var result = new List <IpRangeViewModel>(); string ipFileList = this.azureIpFileList; foreach (string filePath in ipFileList.Split(';')) { var xmlDoc = new XmlDocument(); xmlDoc.Load(this.dataFilePath + @"\Data\IpRangeFiles\Azure\" + filePath); var root = xmlDoc.DocumentElement; foreach (XmlElement ele in root) { string region = ele.GetAttribute("Name"); var ipRange = new IpRangeViewModel { Cloud = "Azure", Region = region, Subnet = new List <string>() }; foreach (XmlElement xe in ele) { string subnet = xe.GetAttribute("Subnet"); ipRange.Subnet.Add(subnet); ipRange.TotalIpCount += IPNetwork.Parse(subnet).Total; } result.Add(ipRange); } } // Load AWS ip range data string json = File.ReadAllText(this.dataFilePath + @"\Data\IpRangeFiles\AWS\" + this.awsIpFile); var awsIpRangeData = JsonConvert.DeserializeObject <AwsIpRangeData>(json); foreach (var prefix in awsIpRangeData.Prefixes) { string region = prefix.Region; string subnet = prefix.IpPrefix; if (result.Any(v => v.Region == region)) { var ipRange = result.First(v => v.Region == region); ipRange.Subnet.Add(subnet); ipRange.TotalIpCount += IPNetwork.Parse(subnet).Total; } else { var ipRange = new IpRangeViewModel { Cloud = "AWS", Region = region, Subnet = new List <string>() }; ipRange.Subnet.Add(subnet); ipRange.TotalIpCount += IPNetwork.Parse(subnet).Total; result.Add(ipRange); } } // Load AliCloud ip range data string aliCloudIpFile = this.aliCloudIpFile; string[] lines = File.ReadAllLines(this.dataFilePath + @"\Data\IpRangeFiles\AliCloud\" + aliCloudIpFile); var aliIpRange = new IpRangeViewModel { Cloud = "AliCloud", Region = "AliCloud", Subnet = new List <string>() }; foreach (var line in lines) { string subnet = line; aliIpRange.Subnet.Add(subnet); aliIpRange.TotalIpCount += IPNetwork.Parse(subnet).Total; } result.Add(aliIpRange); return(result); }