-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
51 lines (46 loc) · 1.7 KB
/
Program.cs
File metadata and controls
51 lines (46 loc) · 1.7 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace IPInfo.IO.IPAddressBlockParser
{
class Program
{
private static readonly Regex SubnetRegex = new Regex(@"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2})", RegexOptions.Compiled);
private static async Task Main(string[] args)
{
// Matches the extended 4 octet ASN: https://tools.ietf.org/html/rfc4893
if (args.Length != 1 || !Regex.IsMatch(args[0], "^AS\\d{1,5}$"))
{
Console.WriteLine("Please provide the ASN.\r\nExample: AS1234");
return;
}
var asn = args[0];
using (var client = new HttpClient())
{
var blocks = GetIpBlocks(asn, client);
await foreach (var block in blocks)
{
Console.WriteLine(block);
}
}
}
private static async IAsyncEnumerable<string> GetIpBlocks(string asn, HttpClient client)
{
var response = await client.GetAsync($"https://ipinfo.io/{asn}");
var stream = await response.Content.ReadAsStreamAsync();
using (var reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
var line = await reader.ReadLineAsync();
var match = SubnetRegex.Match(line);
if (match.Success)
yield return match.Groups[1].Value;
}
}
}
}
}