Skip to content

Commit

Permalink
Further improvements to Ae.Dns.Console.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanedwardes committed Oct 5, 2020
1 parent 1fa6093 commit d9e0d7a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 39 deletions.
12 changes: 9 additions & 3 deletions misc/Ae.Dns.Console/Ae.Dns.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.8" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="3.1.8" />
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="System.Collections.Concurrent" Version="4.3.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.8" />
</ItemGroup>
Expand All @@ -22,4 +22,10 @@
<ProjectReference Include="..\..\src\Ae.Dns.Server\Ae.Dns.Server.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="config.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions misc/Ae.Dns.Console/DnsConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Ae.Dns.Console
{
public sealed class DnsConfiguration
{
public Uri[] HttpsUpstreams { get; set; } = new Uri[0];
public string[] UdpUpstreams { get; set; } = new string[0];
public Uri[] RemoteBlocklists { get; set; } = new Uri[0];
public string[] AllowlistedDomains { get; set; } = new string[0];
}
}
45 changes: 13 additions & 32 deletions misc/Ae.Dns.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,40 @@
using Microsoft.Extensions.Logging;
using Polly;
using Serilog;
using Serilog.Events;
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.Caching;
using System.Threading;
using System.Threading.Tasks;

namespace Ae.Dns.Console
{
public sealed class DnsConfiguration
{
public Uri HttpClientResolver { get; set; } = new Uri("https://1.1.1.1/");
public Uri[] HttpsUpstreams { get; set; } = new Uri[0];
public string[] UdpUpstreams { get; set; } = new string[0];
public Uri[] RemoteBlocklists { get; set; } = new Uri[0];
public string[] AllowlistedDomains { get; set; } = new string[0];
}

class Program
{
static void Main(string[] args) => DoWork(args).GetAwaiter().GetResult();

private static async Task DoWork(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.AddJsonFile("config.json", true)
.Build();

var dnsConfiguration = new DnsConfiguration();
configuration.Bind(dnsConfiguration);

const string staticDnsResolver = "StaticResolver";

var logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.File("dns.log", LogEventLevel.Warning)
.WriteTo.Console()
.ReadFrom.Configuration(configuration)
.CreateLogger();

var dnsConfiguration = new DnsConfiguration();
configuration.Bind(dnsConfiguration);

var services = new ServiceCollection();
services.AddLogging(x => x.AddSerilog(logger));

services.AddHttpClient(staticDnsResolver, x => x.BaseAddress = dnsConfiguration.HttpClientResolver);

static DnsDelegatingHandler CreateDnsDelegatingHandler(IServiceProvider serviceProvider)
{
var httpClient = serviceProvider.GetRequiredService<IHttpClientFactory>().CreateClient(staticDnsResolver);
return new DnsDelegatingHandler(new DnsHttpClient(httpClient));
}

foreach (Uri httpsUpstream in dnsConfiguration.HttpsUpstreams)
{
services.AddHttpClient<IDnsClient, DnsHttpClient>(x => x.BaseAddress = httpsUpstream)
.AddHttpMessageHandler(CreateDnsDelegatingHandler)
.AddTransientHttpErrorPolicy(x => x.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))));
}

Expand All @@ -72,24 +50,25 @@ static DnsDelegatingHandler CreateDnsDelegatingHandler(IServiceProvider serviceP
}

services.AddHttpClient<DnsRemoteSetFilter>()
.AddHttpMessageHandler(CreateDnsDelegatingHandler)
.AddTransientHttpErrorPolicy(x => x.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))));

IServiceProvider provider = services.BuildServiceProvider();

var selfLogger = provider.GetRequiredService<ILogger<Program>>();

var remoteFilter = provider.GetRequiredService<DnsRemoteSetFilter>();

selfLogger.LogInformation("Adding {RemoteBlocklistCount} remote blocklists", dnsConfiguration.RemoteBlocklists.Length);

foreach (Uri remoteBlockList in dnsConfiguration.RemoteBlocklists)
{
_ = remoteFilter.AddRemoteBlockList(remoteBlockList);
}

var selfLogger = provider.GetRequiredService<ILogger<Program>>();

var upstreams = provider.GetServices<IDnsClient>().ToArray();
if (!upstreams.Any())
{
throw new Exception("No upstream DNS servers specified");
throw new Exception("No upstream DNS servers specified - you must specify at least one");
}

selfLogger.LogInformation("Using {UpstreamCount} DNS upstreams", upstreams.Length);
Expand All @@ -98,6 +77,8 @@ static DnsDelegatingHandler CreateDnsDelegatingHandler(IServiceProvider serviceP

IDnsClient cache = new DnsCachingClient(provider.GetRequiredService<ILogger<DnsCachingClient>>(), combinedDnsClient, new MemoryCache("dns"));

selfLogger.LogInformation("Adding {AllowListedDomains} domains to explicit allow list", dnsConfiguration.AllowlistedDomains.Length);

var staticFilter = new DnsDelegateFilter(x => dnsConfiguration.AllowlistedDomains.Contains(x.Host));

IDnsClient filter = new DnsFilterClient(provider.GetRequiredService<ILogger<DnsFilterClient>>(), new DnsCompositeOrFilter(remoteFilter, staticFilter), cache);
Expand Down
12 changes: 12 additions & 0 deletions misc/Ae.Dns.Console/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"serilog": {
"using": [ "Serilog.Sinks.Console" ],
"writeTo": [
{ "name": "Console" }
]
},
"httpsUpstreams": [
"https://dns.google/",
"https://cloudflare-dns.com/"
]
}
4 changes: 2 additions & 2 deletions src/Ae.Dns.Client/Ae.Dns.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.8" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.8" />
<PackageReference Include="System.Runtime.Caching" Version="4.7.0" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions tests/Ae.Dns.Tests/Ae.Dns.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
<PackageReference Include="Moq" Version="4.14.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Moq" Version="4.14.6" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit d9e0d7a

Please sign in to comment.