Repository navigation
Expand file tree
/
Copy pathASNConvert.ps1
More file actions
154 lines (125 loc) · 3.83 KB
/
Copy pathASNConvert.ps1
File metadata and controls
154 lines (125 loc) · 3.83 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
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
$Asn = 'AS43444'
$Uri = "https://stat.ripe.net/data/announced-prefixes/data.json?resource=$Asn"
function Convert-IPv4ToUInt32 {
param([Parameter(Mandatory)][string]$Ip)
$bytes = [System.Net.IPAddress]::Parse($Ip).GetAddressBytes()
if ($bytes.Length -ne 4) { throw "Not an IPv4 address: $Ip" }
[array]::Reverse($bytes)
[BitConverter]::ToUInt32($bytes, 0)
}
function Convert-UInt32ToIPv4 {
param([Parameter(Mandatory)][UInt32]$Value)
$bytes = [BitConverter]::GetBytes($Value)
[array]::Reverse($bytes)
([System.Net.IPAddress]::new($bytes)).ToString()
}
function Convert-CIDRToRange {
param([Parameter(Mandatory)][string]$Cidr)
$parts = $Cidr.Split('/')
if ($parts.Count -ne 2) { throw "Invalid CIDR: $Cidr" }
$ip = $parts[0]
$prefix = [int]$parts[1]
if ($prefix -lt 0 -or $prefix -gt 32) {
throw "Invalid prefix length in $Cidr"
}
$ipNum = [uint64](Convert-IPv4ToUInt32 $ip)
if ($prefix -eq 0) {
$mask = [uint64]0
$size = [uint64]4294967296
}
elseif ($prefix -eq 32) {
$mask = [uint64]4294967295
$size = [uint64]1
}
else {
$hostBits = 32 - $prefix
$size = [uint64][math]::Pow(2, $hostBits)
$mask = [uint64](4294967296 - $size)
}
$start = $ipNum -band $mask
$end = $start + $size - 1
[pscustomobject]@{
CIDR = $Cidr
Start = [uint64]$start
End = [uint64]$end
}
}
function Get-FloorLog2 {
param([Parameter(Mandatory)][UInt64]$Value)
$n = 0
while ($Value -gt 1) {
$Value = [UInt64]($Value / 2)
$n++
}
$n
}
function Convert-RangeToCidrs {
param(
[Parameter(Mandatory)][UInt64]$Start,
[Parameter(Mandatory)][UInt64]$End
)
$result = New-Object System.Collections.Generic.List[string]
$current = $Start
while ($current -le $End) {
if ($current -eq 0) {
$maxBlock = [uint64]4294967296
}
else {
$maxBlock = [uint64]1
while (($current % ($maxBlock * 2)) -eq 0 -and ($maxBlock * 2) -le 4294967296) {
$maxBlock = [uint64]($maxBlock * 2)
}
}
$remaining = [uint64]($End - $current + 1)
while ($maxBlock -gt $remaining) {
$maxBlock = [uint64]($maxBlock / 2)
}
$prefix = 32 - (Get-FloorLog2 $maxBlock)
$cidr = ("{0}/{1}" -f (Convert-UInt32ToIPv4 ([uint32]$current)), $prefix)
$result.Add($cidr)
$current = [uint64]($current + $maxBlock)
}
$result
}
$data = Invoke-RestMethod -Uri $Uri -Method Get
$prefixes = $data.data.prefixes |
ForEach-Object { $_.prefix } |
Where-Object { $_ -match '^\d{1,3}(\.\d{1,3}){3}/\d{1,2}$' } |
Sort-Object -Unique
if (-not $prefixes) {
throw "No IPv4 prefixes returned for $Asn"
}
$ranges = $prefixes |
ForEach-Object { Convert-CIDRToRange $_ } |
Sort-Object Start, End
$merged = New-Object System.Collections.Generic.List[object]
$current = $ranges[0]
for ($i = 1; $i -lt $ranges.Count; $i++) {
$next = $ranges[$i]
if ($next.Start -le ($current.End + 1)) {
if ($next.End -gt $current.End) {
$current.End = $next.End
}
}
else {
$merged.Add([pscustomobject]@{
Start = $current.Start
End = $current.End
})
$current = $next
}
}
$merged.Add([pscustomobject]@{
Start = $current.Start
End = $current.End
})
$collapsed = foreach ($r in $merged) {
Convert-RangeToCidrs -Start $r.Start -End $r.End
}
$collapsed |
Sort-Object {
$p = $_.Split('/')
[uint32](Convert-IPv4ToUInt32 $p[0])
}, {
[int](($_.Split('/'))[1])
}