This repository has been archived by the owner on Apr 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpamhausResolver.cs
286 lines (251 loc) · 9.15 KB
/
SpamhausResolver.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using DnsClient;
using DnsClient.Protocol;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Tireless.Net.Blocking;
namespace Tireless.Net.Mail
{
public class SpamhausResolver
{
private IPTree blockTree;
private IPAddress[] spamhausNameserverV4;
private IPAddress[] spamhausNameserverV6;
public IPAddress[] SpamhausNameserver
{
get { return this.UseIPv6 && this.spamhausNameserverV6.Length > 0 ? this.spamhausNameserverV6 : this.spamhausNameserverV4; }
}
/// <summary>
/// Use ipv6 instead of ipv4.
/// </summary>
public Boolean UseIPv6
{
get;
set;
}
/// <summary>
/// Cache all results.
/// </summary>
public Boolean UseCache
{
get;
set;
}
/// <summary>
/// No exceptions are thrown.
/// </summary>
public Boolean QuietMode
{
get;
set;
}
public IPAddress[] NameserverV4
{
get;
set;
}
public IPAddress[] NameserverV6
{
get;
set;
}
private IPAddress[] Nameserver
{
get { return this.UseIPv6 ? this.NameserverV6 : this.NameserverV4; }
}
public SpamhausResolver()
{
this.blockTree = new IPTree();
this.NameserverV4 = new IPAddress[] { IPAddress.Parse("8.8.8.8"), IPAddress.Parse("8.8.4.4") };
this.NameserverV6 = new IPAddress[] { IPAddress.Parse("2001:4860:4860::8888"), IPAddress.Parse("2001:4860:4860::8844") };
this.spamhausNameserverV4 = new IPAddress[0];
this.spamhausNameserverV6 = new IPAddress[0];
this.UseCache = true;
}
public async Task InitializeAsync(TimeSpan? timeout = null)
{
try
{
var lookup = new LookupClient(this.NameserverV4)
{
UseCache = false,
Timeout = timeout ?? TimeSpan.FromSeconds(3),
};
var nsLookupResult = await lookup.QueryAsync("zen.spamhaus.org", QueryType.NS);
if (nsLookupResult.Answers.Count > 0)
{
var nsRndResult = nsLookupResult.Answers.ElementAt((int)(DateTime.UtcNow.Ticks % nsLookupResult.Answers.Count));
if (nsRndResult is NsRecord nsRecord)
{
var hostV4List = new List<IPAddress>();
var hostV6List = new List<IPAddress>();
var hostEntryResult = await lookup.GetHostEntryAsync(nsRecord.NSDName);
foreach (var item in hostEntryResult.AddressList)
{
if (item.AddressFamily == AddressFamily.InterNetwork)
hostV4List.Add(item);
else if (item.AddressFamily == AddressFamily.InterNetworkV6)
hostV6List.Add(item);
}
this.spamhausNameserverV4 = hostV4List.ToArray();
this.spamhausNameserverV6 = hostV6List.ToArray();
}
}
}
catch (Exception)
{
if (this.QuietMode == false)
throw;
}
}
/***********************************************************************/
public async Task AddUrlAsync(String url)
{
try
{
using (var httpClient = new HttpClient())
{
using (var httpStream = await httpClient.GetStreamAsync(url))
{
await this.AddStreamAsync(httpStream);
}
}
}
catch (Exception)
{
if (this.QuietMode == false)
throw;
}
}
public async Task AddFileAsync(String path)
{
try
{
using (var fileStream = File.OpenRead(path))
{
await this.AddStreamAsync(fileStream);
}
}
catch (Exception)
{
if (this.QuietMode == false)
throw;
}
}
public async Task AddStreamAsync(Stream baseStream)
{
try
{
using (var sr = new StreamReader(baseStream))
{
String line;
while ((line = await sr.ReadLineAsync()) != null)
{
if (line.Length == 0 || line[0] == ';')
continue;
var regex = new Regex(@"^(.*)\/([0-9]+) ; (.*)");
var matches = regex.Matches(line);
if (matches.Count > 0)
{
var ip = IPAddress.Parse(matches[0].Groups[1].Value);
var netmask = Byte.Parse(matches[0].Groups[2].Value);
var ident = matches[0].Groups[3].Value;
blockTree.AddNetwork(ip, netmask, ident);
}
}
}
}
catch (Exception)
{
if (this.QuietMode == false)
throw;
}
}
public void AddNetwork(IPAddress network, Int32 mask, String identifier = null)
{
this.blockTree.AddNetwork(network, mask, identifier);
}
public void AddIPAddress(IPAddress client, String identifier = null)
{
this.blockTree.AddNetwork(client, client.GetAddressBytes().Length * 8, identifier);
}
/***********************************************************************/
/// <summary>
/// Calls also InitSpamhausNameservers if not happened before.
/// </summary>
/// <param name="client"></param>
/// <param name="timeout"></param>
/// <returns></returns>
public async Task<String> IsBlockedAsync(IPAddress client, TimeSpan? timeout = null)
{
try
{
if (this.UseCache)
{
String identifier;
if ((identifier = this.blockTree.IsBlocked(client)) != null)
{
if (identifier != SpamhausResult.NL.ToString())
return identifier;
return null;
}
}
if (this.SpamhausNameserver.Length == 0)
await InitializeAsync(timeout);
if (this.SpamhausNameserver.Length == 0)
return null;
var lookup = new LookupClient(this.SpamhausNameserver)
{
UseCache = false,
Timeout = timeout ?? TimeSpan.FromSeconds(3),
};
String reverseIPAddress;
var ipBytes = new List<Byte>(client.GetAddressBytes()).Reverse<Byte>();
if (client.AddressFamily == AddressFamily.InterNetwork)
{
reverseIPAddress = String.Join(".", ipBytes);
}
else if (client.AddressFamily == AddressFamily.InterNetworkV6)
{
reverseIPAddress = "";
foreach (var item in ipBytes.ToArray())
{
var hex = item.ToString("X2");
reverseIPAddress += "." + hex[1] + "." + hex[0];
}
reverseIPAddress = reverseIPAddress.Substring(1);
}
else
throw new NotSupportedException();
var lookupResult = await lookup.QueryAsync(reverseIPAddress.ToLowerInvariant() + ".zen.spamhaus.org", QueryType.A);
SpamhausResult spamhausResult = SpamhausResult.NL;
foreach (var item in lookupResult.Answers)
{
if (item is AddressRecord addressRecord)
{
spamhausResult |= (SpamhausResult)(addressRecord.Address.GetAddressBytes()[3]);
}
}
if (this.UseCache)
{
this.blockTree.AddIPAddress(client, spamhausResult.ToString());
}
return spamhausResult != SpamhausResult.NL ? spamhausResult.ToString() : null;
}
catch (Exception)
{
if (this.QuietMode == false)
throw;
return null;
}
}
/***********************************************************************/
}
}