-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathWhoIsClient.cs
More file actions
208 lines (175 loc) · 5.45 KB
/
WhoIsClient.cs
File metadata and controls
208 lines (175 loc) · 5.45 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Waher.Networking.Sniffers;
namespace Waher.Networking.WHOIS
{
/// <summary>
/// WHOIS client, as defined in RFC 3912:
/// https://tools.ietf.org/html/rfc3912
/// </summary>
public partial class WhoIsClient : TextTcpClient
{
private readonly StringBuilder receivedText = new StringBuilder();
private readonly AutoResetEvent received = new AutoResetEvent(false);
private readonly ManualResetEvent closed = new ManualResetEvent(false);
/// <summary>
/// Default WHOIS Port = 43
/// </summary>
public const int DefaultWhoIsPort = 43;
/// <summary>
/// WHOIS client, as defined in RFC 3912:
/// https://tools.ietf.org/html/rfc3912
/// </summary>
/// <param name="Sniffers">Sniffers.</param>
public WhoIsClient(params ISniffer[] Sniffers)
: this(Encoding.ASCII, Sniffers)
{
}
/// <summary>
/// WHOIS client, as defined in RFC 3912:
/// https://tools.ietf.org/html/rfc3912
/// </summary>
/// <param name="Encoding">Text encoding to use. (Default=<see cref="Encoding.ASCII"/>)</param>
/// <param name="Sniffers">Sniffers.</param>
public WhoIsClient(Encoding Encoding, params ISniffer[] Sniffers)
: base(Encoding, false, Sniffers)
{
}
/// <summary>
/// Queries an Internet Registry about an entity.
/// </summary>
/// <param name="InternetRegistry">Domain name of Internet Registry to query.</param>
/// <param name="Entity">Entity to be queried.</param>
/// <returns>WHOIS Information</returns>
public async Task<string> DoQuery(string InternetRegistry, string Entity)
{
this.received.Reset();
await this.ConnectAsync(InternetRegistry, DefaultWhoIsPort);
this.received.WaitOne(500);
lock (this.receivedText)
{
this.receivedText.Clear();
}
this.closed.Reset();
await this.SendAsync(Entity + "\r\n");
this.closed.WaitOne(5000);
string s;
lock (this.receivedText)
{
s = this.receivedText.ToString();
this.receivedText.Clear();
}
return s.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n");
}
/// <summary>
/// Method called when text data has been received.
/// </summary>
/// <param name="Data">Text data received.</param>
protected override Task<bool> TextDataReceived(string Data)
{
lock (this.receivedText)
{
this.receivedText.Append(Data);
}
this.received.Set();
return Task.FromResult(true);
}
/// <summary>
/// Method called when the connection has been disconnected.
/// </summary>
protected override Task Disconnected()
{
this.closed.Set();
return base.Disconnected();
}
/// <summary>
/// Disposes of the object. The underlying <see cref="TcpClient"/> is either disposed directly, or when asynchronous
/// operations have ceased.
/// </summary>
public override Task DisposeAsync()
{
this.received.Dispose();
this.closed.Dispose();
this.receivedText.Clear();
return base.DisposeAsync();
}
/// <summary>
/// Queries an Internet Registry about an entity.
/// </summary>
/// <param name="InternetRegistry">Domain name of Internet Registry to query.</param>
/// <param name="Entity">Entity to be queried.</param>
/// <returns>WHOIS Information</returns>
public static async Task<string> Query(string InternetRegistry, string Entity)
{
using (WhoIsClient Client = new WhoIsClient())
{
return await Client.DoQuery(InternetRegistry, Entity);
}
}
/// <summary>
/// Queries the corresponding Internet Registry about an IP Address.
/// </summary>
/// <param name="Address">IP Address to query</param>
/// <returns>WHOIS Information, if available, null if not.</returns>
public static async Task<string> Query(IPAddress Address)
{
switch (Address.AddressFamily)
{
case AddressFamily.InterNetwork:
byte[] Bytes = Address.GetAddressBytes();
byte b = Bytes[0];
if (b >= ipv4ToWhoIsService.Length)
return null;
WhoIsIpv4ServiceEnum Service = ipv4ToWhoIsService[b];
string InternetRegistry = ipv4WhoIsServices[(int)Service];
string Command;
switch (Service)
{
case WhoIsIpv4ServiceEnum.whois_apnic_net:
case WhoIsIpv4ServiceEnum.whois_arin_net:
case WhoIsIpv4ServiceEnum.whois_lacnic_net:
Command = "n ";
break;
case WhoIsIpv4ServiceEnum.whois_ripe_net:
case WhoIsIpv4ServiceEnum.whois_afrinic_net:
default:
Command = string.Empty;
break;
}
using (WhoIsClient Client = new WhoIsClient())
{
return await Client.DoQuery(InternetRegistry, Command + Address.ToString());
}
default:
return null;
}
}
/// <summary>
/// Gets the RDAP URI for an IP Address. It points to a JSON object containing WHOIS information about the IP Address.
/// </summary>
/// <param name="Address">IP Address</param>
/// <returns>RDAP URI, if available, null if not.</returns>
public static Uri RdapUri(IPAddress Address)
{
switch (Address.AddressFamily)
{
case AddressFamily.InterNetwork:
byte[] Bytes = Address.GetAddressBytes();
byte b = Bytes[0];
if (b >= ipv4ToWhoIsService.Length)
return null;
WhoIsIpv4ServiceEnum Service = ipv4ToWhoIsService[b];
string RdapService = rdapServices[(int)Service];
if (string.IsNullOrEmpty(RdapService))
return null;
return new Uri(RdapService + "ip/" + Address.ToString());
default:
return null;
}
}
}
}