-
Notifications
You must be signed in to change notification settings - Fork 0
/
torModule.cs
57 lines (56 loc) · 2.18 KB
/
torModule.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Diagnostics;
namespace adfsTorBlock
{
internal class TorModule
{
private static HashSet<IPAddress> torExitNodes = new HashSet<IPAddress>();
internal static void LoadTorExitNodes()
{
string url = "https://check.torproject.org/exit-addresses";
using (WebClient client = new WebClient())
{
try
{
string text = client.DownloadString(url);
using (StringReader reader = new StringReader(text))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (line.StartsWith("ExitAddress"))
{
string[] parts = line.Split(' ');
if (parts.Length >= 2)
{
string address = parts[1];
try
{
Debug.WriteLine($"adfsTorBlock:torModule:LoadTorExitNodes: adding '{address}' to Tor Exit Nodes");
torExitNodes.Add(IPAddress.Parse(address));
}
catch (FormatException ex)
{
Debug.WriteLine($"adfsTorBlock:torModule:LoadTorExitNodes: Exception parsing IP Address: {ex}");
}
}
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"adfsTorBlock:torModule:LoadTorExitNodes: Exception loading Tor Nodes IP Address: {ex}");
throw;
}
}
}
internal static bool IsTorExitNode(IPAddress address)
{
return torExitNodes.Contains(address);
}
}
}