forked from BornToBeRoot/NETworkManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate-WhoisServerListFromWebAndWhois.ps1
More file actions
73 lines (48 loc) · 2.16 KB
/
Copy pathCreate-WhoisServerListFromWebAndWhois.ps1
File metadata and controls
73 lines (48 loc) · 2.16 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
# Filepath in the resources
[string]$OutFilePath = Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath "Source\NETworkManager.Models\Resources\WhoisServers.xml"
$IANA_TLDs = (Invoke-WebRequest -Uri "https://data.iana.org/TLD/tlds-alpha-by-domain.txt").Content -split "[`r|`n]"
# Create xml document
[xml]$Document = New-Object System.Xml.XmlDocument
$Declaration = $Document.CreateXmlDeclaration("1.0", "UTF-8", $null)
[void]$Document.AppendChild($Declaration)
# Description
$Description = @"
Whois servers by domain from IANA
Generated $(Get-Date)
"@
[void]$Document.AppendChild($Document.CreateComment($Description))
# Root node
$RootNode = $Document.CreateNode("element", "WhoisServers", $null)
$ProgressCount = 0
foreach ($Tld in $IANA_TLDs) {
if ($Tld.StartsWith("#")) {
continue
}
$currentTld = $Tld.Trim()
$tcpClient = New-Object System.Net.Sockets.TcpClient("whois.iana.org", 43)
$networkStream = $tcpClient.GetStream()
$bufferedStream = New-Object System.IO.BufferedStream($networkStream)
$streamWriter = New-Object System.IO.StreamWriter($bufferedStream)
$streamWriter.WriteLine($currentTld)
$streamWriter.Flush()
$streamReader = New-Object System.IO.StreamReader($bufferedStream)
$stringBuilder = New-Object System.Text.StringBuilder
while (!$streamReader.EndOfStream) {
$stringBuilder.Append($streamReader.ReadLine())
}
$WhoisServer = (($stringBuilder.ToString() -split "whois:")[1] -split "status:")[0].Trim()
$WhoisServerNode = $Document.CreateNode("element", "WhoisServer", $null)
$TldElement = $Document.CreateElement("TLD")
$TldElement.InnerText = $currentTld
[void]$WhoisServerNode.AppendChild($TldElement)
$ServerElement = $Document.CreateElement("Server")
$ServerElement.InnerText = $WhoisServer
[void]$WhoisServerNode.AppendChild($ServerElement)
[void]$RootNode.AppendChild($WhoisServerNode)
Write-Host -Object "Progress: $ProgressCount from $($IANA_TLDs.Count)"
$ProgressCount++
# Sleep 1 because there is a rate limit
Start-Sleep -Seconds 1
}
[void]$Document.AppendChild($RootNode)
$Document.Save($OutFilePath)