From d9e0d7adf5b20db921e2f07f14863629aebda5f0 Mon Sep 17 00:00:00 2001 From: Alan Edwardes Date: Mon, 5 Oct 2020 21:29:13 +0100 Subject: [PATCH] Further improvements to Ae.Dns.Console. --- misc/Ae.Dns.Console/Ae.Dns.Console.csproj | 12 ++++-- misc/Ae.Dns.Console/DnsConfiguration.cs | 12 ++++++ misc/Ae.Dns.Console/Program.cs | 45 +++++++---------------- misc/Ae.Dns.Console/config.json | 12 ++++++ src/Ae.Dns.Client/Ae.Dns.Client.csproj | 4 +- tests/Ae.Dns.Tests/Ae.Dns.Tests.csproj | 4 +- 6 files changed, 50 insertions(+), 39 deletions(-) create mode 100644 misc/Ae.Dns.Console/DnsConfiguration.cs create mode 100644 misc/Ae.Dns.Console/config.json diff --git a/misc/Ae.Dns.Console/Ae.Dns.Console.csproj b/misc/Ae.Dns.Console/Ae.Dns.Console.csproj index f85c9de..0354280 100644 --- a/misc/Ae.Dns.Console/Ae.Dns.Console.csproj +++ b/misc/Ae.Dns.Console/Ae.Dns.Console.csproj @@ -8,11 +8,11 @@ - - - + + + @@ -22,4 +22,10 @@ + + + Always + + + diff --git a/misc/Ae.Dns.Console/DnsConfiguration.cs b/misc/Ae.Dns.Console/DnsConfiguration.cs new file mode 100644 index 0000000..a71945b --- /dev/null +++ b/misc/Ae.Dns.Console/DnsConfiguration.cs @@ -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]; + } +} diff --git a/misc/Ae.Dns.Console/Program.cs b/misc/Ae.Dns.Console/Program.cs index 61e6f85..f6d2474 100644 --- a/misc/Ae.Dns.Console/Program.cs +++ b/misc/Ae.Dns.Console/Program.cs @@ -7,26 +7,15 @@ 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(); @@ -34,35 +23,24 @@ class Program 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().CreateClient(staticDnsResolver); - return new DnsDelegatingHandler(new DnsHttpClient(httpClient)); - } - foreach (Uri httpsUpstream in dnsConfiguration.HttpsUpstreams) { services.AddHttpClient(x => x.BaseAddress = httpsUpstream) - .AddHttpMessageHandler(CreateDnsDelegatingHandler) .AddTransientHttpErrorPolicy(x => x.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))); } @@ -72,24 +50,25 @@ static DnsDelegatingHandler CreateDnsDelegatingHandler(IServiceProvider serviceP } services.AddHttpClient() - .AddHttpMessageHandler(CreateDnsDelegatingHandler) .AddTransientHttpErrorPolicy(x => x.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))); IServiceProvider provider = services.BuildServiceProvider(); + var selfLogger = provider.GetRequiredService>(); + var remoteFilter = provider.GetRequiredService(); + selfLogger.LogInformation("Adding {RemoteBlocklistCount} remote blocklists", dnsConfiguration.RemoteBlocklists.Length); + foreach (Uri remoteBlockList in dnsConfiguration.RemoteBlocklists) { _ = remoteFilter.AddRemoteBlockList(remoteBlockList); } - var selfLogger = provider.GetRequiredService>(); - var upstreams = provider.GetServices().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); @@ -98,6 +77,8 @@ static DnsDelegatingHandler CreateDnsDelegatingHandler(IServiceProvider serviceP IDnsClient cache = new DnsCachingClient(provider.GetRequiredService>(), 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>(), new DnsCompositeOrFilter(remoteFilter, staticFilter), cache); diff --git a/misc/Ae.Dns.Console/config.json b/misc/Ae.Dns.Console/config.json new file mode 100644 index 0000000..f2a8a3b --- /dev/null +++ b/misc/Ae.Dns.Console/config.json @@ -0,0 +1,12 @@ +{ + "serilog": { + "using": [ "Serilog.Sinks.Console" ], + "writeTo": [ + { "name": "Console" } + ] + }, + "httpsUpstreams": [ + "https://dns.google/", + "https://cloudflare-dns.com/" + ] +} \ No newline at end of file diff --git a/src/Ae.Dns.Client/Ae.Dns.Client.csproj b/src/Ae.Dns.Client/Ae.Dns.Client.csproj index f71fcbe..5d51c98 100644 --- a/src/Ae.Dns.Client/Ae.Dns.Client.csproj +++ b/src/Ae.Dns.Client/Ae.Dns.Client.csproj @@ -14,8 +14,8 @@ - - + + diff --git a/tests/Ae.Dns.Tests/Ae.Dns.Tests.csproj b/tests/Ae.Dns.Tests/Ae.Dns.Tests.csproj index 2390d25..fc7ca43 100644 --- a/tests/Ae.Dns.Tests/Ae.Dns.Tests.csproj +++ b/tests/Ae.Dns.Tests/Ae.Dns.Tests.csproj @@ -7,8 +7,8 @@ - - + + all