-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
45 lines (34 loc) · 1.63 KB
/
Program.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
using Ae.Dns.Client;
using Ae.Dns.Client.Filters;
using Ae.Dns.Protocol;
using Ae.Dns.Server;
using System.Net;
using System.Runtime.Caching;
using IDnsClient cloudFlare1 = new DnsUdpClient(IPAddress.Parse("1.1.1.1"));
using IDnsClient cloudFlare2 = new DnsUdpClient(IPAddress.Parse("1.0.0.1"));
using IDnsClient google1 = new DnsUdpClient(IPAddress.Parse("8.8.8.8"));
using IDnsClient google2 = new DnsUdpClient(IPAddress.Parse("8.8.4.4"));
// Aggregate all clients into one
using IDnsClient randomClient = new DnsRandomClient(cloudFlare1, cloudFlare2, google1, google2);
// Add the caching layer
using IDnsClient cacheClient = new DnsCachingClient(randomClient, new MemoryCache("dns"));
using var httpClient = new HttpClient();
// Create a remote blocklist filter
var remoteSetFilter = new DnsRemoteSetFilter(httpClient);
// Block all domains from https://github.com/StevenBlack/hosts
// don't await the task to allow the server to start (class is thread safe)
var hostsFile = new Uri("https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts");
_ = remoteSetFilter.AddRemoteBlockList(hostsFile);
// Add the filtering layer
using IDnsClient filterClient = new DnsFilterClient(remoteSetFilter, cacheClient);
// Listen on 127.0.0.1
var serverOptions = new DnsUdpServerOptions
{
Endpoint = new IPEndPoint(IPAddress.Loopback, 53)
};
// Create a "raw" client (efficiently deals with network buffers)
using IDnsRawClient rawClient = new DnsRawClient(filterClient);
// Create the server using the caching client and remote set filter
using IDnsServer server = new DnsUdpServer(rawClient, serverOptions);
// Listen until cancelled
await server.Listen();