forked from dinowang/azure-public-ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
154 lines (123 loc) · 5.92 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using CloudRiches.Azure.DataCenter.Models;
using HtmlAgilityPack;
using Newtonsoft.Json;
namespace CloudRiches.Azure.DataCenter
{
class Program
{
static HttpClient _client = new HttpClient();
static async Task Main(string[] args)
{
// Microsoft Azure Datacenter IP Ranges
var ipRangeFiles = await GetMicrosoftUpdateDocument(41653, async x => await IpRangeToCsv(x));
CopyToLastest(ipRangeFiles);
// Microsoft Azure Datacenter IP Ranges in China
var chinaIpRangeFiles = await GetMicrosoftUpdateDocument(42064, async x => await IpRangeToCsv(x));
CopyToLastest(chinaIpRangeFiles);
// Azure IP Ranges and Service Tags – Public Cloud
var serviceTagNames = await GetMicrosoftUpdateDocument(56519, async x => await ServiceTagProcess(x));
CopyToLastest(serviceTagNames);
// Azure IP Ranges and Service Tags – US Government Cloud
var usGovServiceTagNames = await GetMicrosoftUpdateDocument(57063, async x => await ServiceTagProcess(x));
CopyToLastest(usGovServiceTagNames);
// Microsoft IP Range GeoLocation
var microsoftIpRangeGeolocations = await GetMicrosoftUpdateDocument(53601);
CopyToLastest(microsoftIpRangeGeolocations);
// Microsoft Public IP Space
var microsoftPublicIpSpaces = await GetMicrosoftUpdateDocument(53602);
CopyToLastest(microsoftPublicIpSpaces);
}
static async Task<IList<string>> GetMicrosoftUpdateDocument(int id, Func<string, Task<string>> postAction = null)
{
return await GetMicrosoftUpdateDocument($"https://www.microsoft.com/en-us/download/details.aspx?id={id}", postAction);
}
static async Task<IList<string>> GetMicrosoftUpdateDocument(string refUrl, Func<string, Task<string>> postAction = null)
{
var html = await _client.GetStringAsync(refUrl);
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var downloadButton = htmlDoc.DocumentNode.QuerySelector("a.mscom-link.download-button");
var confirmUrl = downloadButton.GetAttributeValue("href", null);
html = await _client.GetStringAsync($"https://www.microsoft.com/en-us/download/{confirmUrl}");
htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var failoverLink = htmlDoc.DocumentNode.QuerySelector("a.mscom-link.failoverLink");
var downloadUrl = failoverLink.GetAttributeValue("href", null);
var uri = new Uri(downloadUrl);
var fileName = Path.GetFileName(uri.PathAndQuery);
var outputFileName = $"output/{fileName}";
using (var output = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
using (var writer = new StreamWriter(output))
{
var content = await _client.GetStringAsync(downloadUrl);
await writer.WriteLineAsync(content);
}
var files = new List<string> { outputFileName };
if (postAction != null)
{
var additionalFileName = await postAction(outputFileName);
if (!string.IsNullOrEmpty(additionalFileName))
{
files.Add(additionalFileName);
}
}
return files;
}
public static async Task<string> IpRangeToCsv(string outputFileName)
{
var cancellationToken = new CancellationToken();
using (var reader = new StreamReader(outputFileName))
{
var xdoc = await XDocument.LoadAsync(reader, LoadOptions.None, cancellationToken);
var regions = xdoc.Descendants(XName.Get("Region"));
var regionIpRanges = regions.Select(x => new
{
IpRanges = x.Elements().Select(e => new
{
Region = x.Attribute("Name").Value,
IpRange = e.Attribute("Subnet").Value
})
})
.SelectMany(x => x.IpRanges);
var csvFileName = Path.ChangeExtension(outputFileName, ".csv");
using (var output = new FileStream(csvFileName, FileMode.Create, FileAccess.ReadWrite))
using (var writer = new StreamWriter(output))
{
await writer.WriteLineAsync("Region,IpRange");
foreach (var ipRange in regionIpRanges)
{
await writer.WriteLineAsync($"{ipRange.Region},{ipRange.IpRange}");
}
}
return csvFileName;
}
}
public static async Task<string> ServiceTagProcess(string outputFileName)
{
var json = await File.ReadAllTextAsync(outputFileName);
var serviceTag = JsonConvert.DeserializeObject<ServiceTag>(json);
return null;
}
static void CopyToLastest(IEnumerable<string> files)
{
foreach (var file in files)
{
var fileWithoutTimestamp = Regex.Replace(file, @"_20\d{6}\.", ".");
if (file != fileWithoutTimestamp)
{
File.Copy(file, fileWithoutTimestamp, true);
}
}
}
}
}