Skip to content

Commit

Permalink
Enables warnings as errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanedwardes committed Nov 14, 2023
1 parent 60b0424 commit cafdbb8
Show file tree
Hide file tree
Showing 44 changed files with 358 additions and 136 deletions.
57 changes: 29 additions & 28 deletions misc/Ae.Dns.Console/Ae.Dns.Console.csproj
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<WarningsAsErrors>true</WarningsAsErrors>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AK.App.Metrics.Reporting.InfluxDB2" Version="4.2.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageReference Include="System.Collections.Concurrent" Version="4.3.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AK.App.Metrics.Reporting.InfluxDB2" Version="4.2.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
<PackageReference Include="System.Collections.Concurrent" Version="4.3.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Ae.Dns.AppMetrics\Ae.Dns.AppMetrics.csproj" />
<ProjectReference Include="..\..\src\Ae.Dns.Client\Ae.Dns.Client.csproj" />
<ProjectReference Include="..\..\src\Ae.Dns.Server.Http\Ae.Dns.Server.Http.csproj" />
<ProjectReference Include="..\..\src\Ae.Dns.Server\Ae.Dns.Server.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Ae.Dns.AppMetrics\Ae.Dns.AppMetrics.csproj" />
<ProjectReference Include="..\..\src\Ae.Dns.Client\Ae.Dns.Client.csproj" />
<ProjectReference Include="..\..\src\Ae.Dns.Server.Http\Ae.Dns.Server.Http.csproj" />
<ProjectReference Include="..\..\src\Ae.Dns.Server\Ae.Dns.Server.csproj" />
</ItemGroup>

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

</Project>
</Project>
8 changes: 4 additions & 4 deletions misc/Ae.Dns.Console/DnsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public sealed class DnsConfiguration
public string[] DisallowedDomainSuffixes { get; set; } = new string[0];
public string[] DisallowedQueryTypes { get; set; } = new string[0];
public string[] HostFiles { get; set; } = new string[0];
public string DhcpdLeasesFile { get; set; }
public string DhcpdConfigFile { get; set; }
public string DhcpdLeasesHostnameSuffix { get; set; }
public DnsInfluxDbConfiguration InfluxDbMetrics { get; set; }
public string? DhcpdLeasesFile { get; set; }
public string? DhcpdConfigFile { get; set; }
public string? DhcpdLeasesHostnameSuffix { get; set; }
public DnsInfluxDbConfiguration? InfluxDbMetrics { get; set; }
}
}
8 changes: 4 additions & 4 deletions misc/Ae.Dns.Console/DnsInfluxDbConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Ae.Dns.Console
{
public sealed class DnsInfluxDbConfiguration
{
public Uri BaseUri { get; set; }
public string Organization { get; set; }
public string Bucket { get; set; }
public string Token { get; set; }
public Uri? BaseUri { get; set; }
public string? Organization { get; set; }
public string? Bucket { get; set; }
public string? Token { get; set; }
}
}
4 changes: 2 additions & 2 deletions misc/Ae.Dns.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ async Task ReportStats(CancellationToken token)

if (!string.IsNullOrWhiteSpace(dnsConfiguration.DhcpdConfigFile))
{
staticLookupSources.Add(ActivatorUtilities.CreateInstance<DhcpdConfigDnsLookupSource>(provider, new FileInfo(dnsConfiguration.DhcpdConfigFile), dnsConfiguration.DhcpdLeasesHostnameSuffix));
staticLookupSources.Add(ActivatorUtilities.CreateInstance<DhcpdConfigDnsLookupSource>(provider, new FileInfo(dnsConfiguration.DhcpdConfigFile), dnsConfiguration.DhcpdLeasesHostnameSuffix ?? throw new NullReferenceException("DhcpdLeasesHostnameSuffix is null")));
}

if (!string.IsNullOrWhiteSpace(dnsConfiguration.DhcpdLeasesFile))
{
staticLookupSources.Add(ActivatorUtilities.CreateInstance<DhcpdLeasesDnsLookupSource>(provider, new FileInfo(dnsConfiguration.DhcpdLeasesFile), dnsConfiguration.DhcpdLeasesHostnameSuffix));
staticLookupSources.Add(ActivatorUtilities.CreateInstance<DhcpdLeasesDnsLookupSource>(provider, new FileInfo(dnsConfiguration.DhcpdLeasesFile), dnsConfiguration.DhcpdLeasesHostnameSuffix ?? throw new NullReferenceException("DhcpdLeasesHostnameSuffix is null")));
}

if (staticLookupSources.Count > 0)
Expand Down
65 changes: 32 additions & 33 deletions misc/Ae.Dns.Console/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ public void Configure(IApplicationBuilder app)

app.Run(async context =>
{
async Task WriteHeader(string header)
{
await context.Response.WriteAsync(header);
await context.Response.WriteAsync(Environment.NewLine);
await context.Response.WriteAsync(new string('=', header.Length));
await context.Response.WriteAsync(Environment.NewLine);
}
async Task WriteTable(DataTable table)
{
await context.Response.WriteAsync("<table>");
Expand All @@ -73,7 +65,7 @@ async Task WriteTable(DataTable table)
await context.Response.WriteAsync("</table>");
}
async Task GroupToTable(IEnumerable<IGrouping<string, DnsQuery>> groups, params string[] headings)
async Task GroupToTable(IEnumerable<IGrouping<string?, DnsQuery>> groups, params string[] headings)
{
var table = new DataTable();
Expand All @@ -84,7 +76,7 @@ async Task GroupToTable(IEnumerable<IGrouping<string, DnsQuery>> groups, params
table.Columns.Add("Percentage");
var itemCounts = groups.Select(x => KeyValuePair.Create(x.Key, x.Count())).OrderByDescending(x => x.Value).ToList();
var itemCounts = groups.Select(x => KeyValuePair.Create<string?, int>(x.Key, x.Count())).OrderByDescending(x => x.Value).ToList();
var totalCount = itemCounts.Sum(x => x.Value);
int CalculatePercentage(int count) => (int)(count / (double)totalCount * (double)100d);
Expand Down Expand Up @@ -165,10 +157,14 @@ async Task GroupToTable(IEnumerable<IGrouping<string, DnsQuery>> groups, params
context.Response.StatusCode = StatusCodes.Status200OK;
context.Response.ContentType = "text/html; charset=utf-8";
string CreateQueryString(string name, object value)
string CreateQueryString(string name, object? value)
{
var filters = context.Request.Query.ToDictionary(x => x.Key, x => x.Value.ToString());
filters[name] = value.ToString();
IDictionary<string, string?> filters = context.Request.Query.ToDictionary(x => x.Key, x => (string?)x.Value.ToString());
var valueString = value?.ToString();
if (valueString != null)
{
filters[name] = valueString;
}
return QueryHelpers.AddQueryString(context.Request.Path, filters);
}
Expand All @@ -179,7 +175,7 @@ string CreateQueryStringWithout(string name)
IEnumerable<DnsQuery> query = _queries.OrderByDescending(x => x.Created);
if (context.Request.Query.ContainsKey("sender") && IPAddress.TryParse(context.Request.Query["sender"], out IPAddress sender))
if (context.Request.Query.ContainsKey("sender") && IPAddress.TryParse(context.Request.Query["sender"], out IPAddress? sender))
{
query = query.Where(x => x.Sender.Equals(sender));
}
Expand Down Expand Up @@ -242,11 +238,11 @@ string SenderFilter(DnsQuery dnsQuery)
return $"<a href=\"{CreateQueryString("sender", dnsQuery.Sender)}\">{reverseLookups[dnsQuery.Sender] ?? dnsQuery.Sender.ToString()}</a>";
}
string ResponseFilter(DnsQuery dnsQuery)
string? ResponseFilter(DnsQuery dnsQuery)
{
if (dnsQuery.Answer == null)
{
return dnsQuery.Answer?.ResponseCode.ToString();
return null;
}
else
{
Expand All @@ -259,11 +255,11 @@ string QueryTypeFilter(DnsQuery dnsQuery)
return $"<a href=\"{CreateQueryString("type", dnsQuery.Query.QueryType)}\">{dnsQuery.Query.QueryType}</a>";
}
string ResolverFilter(DnsQuery dnsQuery)
string? ResolverFilter(DnsQuery dnsQuery)
{
if (dnsQuery.Answer == null)
{
return dnsQuery.Answer.Resolver;
return null;
}
else
{
Expand Down Expand Up @@ -334,9 +330,9 @@ public DnsHeaderLight(DnsHeader header)
ResponseCode = header.ResponseCode;
QueryType = header.QueryType;
Host = header.Host;
Resolver = header.Tags.ContainsKey("Resolver") ? header.Tags["Resolver"].ToString() : "Unknown";
BlockReason = header.Tags.ContainsKey("BlockReason") ? header.Tags["BlockReason"].ToString() : "None";
Server = header.Tags.ContainsKey("Server") ? header.Tags["Server"].ToString() : "Unknown";
Resolver = (header.Tags.ContainsKey("Resolver") ? header.Tags["Resolver"].ToString() : null) ?? "Unknown";
BlockReason = (header.Tags.ContainsKey("BlockReason") ? header.Tags["BlockReason"].ToString() : null) ?? "None";
Server = (header.Tags.ContainsKey("Server") ? header.Tags["Server"].ToString() : null) ?? "Unknown";
}

public DnsResponseCode ResponseCode { get; }
Expand All @@ -349,24 +345,30 @@ public DnsHeaderLight(DnsHeader header)

private sealed class DnsQuery
{
public DnsHeaderLight Query { get; set; }
public DnsQuery(DnsHeaderLight query, IPAddress sender)
{
Query = query;
Sender = sender;
}

public DnsHeaderLight Query { get; }
public DnsHeaderLight? Answer { get; set; }
public IPAddress Sender { get; set; }
public IPAddress Sender { get; }
public TimeSpan? Elapsed { get; set; }
public DateTime Created { get; set; }
public DateTime Created { get; } = DateTime.UtcNow;
}

private readonly ConcurrentQueue<DnsQuery> _queries = new();

private void OnMeasurementRecorded(Instrument instrument, int measurement, ReadOnlySpan<KeyValuePair<string, object>> tags, object state)
private void OnMeasurementRecorded(Instrument instrument, int measurement, ReadOnlySpan<KeyValuePair<string, object?>> tags, object? state)
{
static TObject? GetObjectFromTags<TObject>(ReadOnlySpan<KeyValuePair<string, object>> _tags, string name)
static TObject? GetObjectFromTags<TObject>(ReadOnlySpan<KeyValuePair<string, object?>> _tags, string name)
{
foreach (var tag in _tags)
{
if (tag.Key == name)
{
return (TObject)tag.Value;
return (TObject?)tag.Value;
}
}

Expand All @@ -375,19 +377,16 @@ private void OnMeasurementRecorded(Instrument instrument, int measurement, ReadO

if (instrument.Meter.Name == DnsMetricsClient.MeterName)
{
var query = GetObjectFromTags<DnsMessage>(tags, "Query");
var query = GetObjectFromTags<DnsMessage>(tags, "Query") ?? throw new NullReferenceException("No query specified");
var answer = GetObjectFromTags<DnsMessage>(tags, "Answer");
var elapsed = GetObjectFromTags<TimeSpan?>(tags, "Elapsed");
var sender = query.Header.Tags.TryGetValue("Sender", out var rawEndpoint) && rawEndpoint is not null && rawEndpoint is IPEndPoint endpoint ? endpoint.Address : null;
if (sender != null)
{
_queries.Enqueue(new DnsQuery
_queries.Enqueue(new DnsQuery(new DnsHeaderLight(query.Header), sender)
{
Query = new DnsHeaderLight(query.Header),
Answer = answer != null ? new DnsHeaderLight(answer.Header) : null,
Sender = sender,
Elapsed = elapsed,
Created = DateTime.UtcNow
Elapsed = elapsed
});

if (_queries.Count > 100_000)
Expand Down
1 change: 1 addition & 0 deletions samples/AdvancedHttpClient/AdvancedHttpClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions samples/AdvancedUdpServer/AdvancedUdpServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
21 changes: 11 additions & 10 deletions samples/BasicHttpClient/BasicHttpClient.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;netcoreapp3.1</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;netcoreapp3.1</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Ae.Dns.Client\Ae.Dns.Client.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Ae.Dns.Client\Ae.Dns.Client.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions samples/BasicHttpServer/BasicHttpServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions samples/BasicUdpClient/BasicUdpClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions samples/BasicUdpServer/BasicUdpServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions samples/CachingClient/CachingClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions samples/RacerClient/RacerClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions samples/RandomClient/RandomClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Ae.Dns.AppMetrics/Ae.Dns.AppMetrics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<Version>2.0.0</Version>
<WarningsAsErrors>true</WarningsAsErrors>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Authors>alanedwardes</Authors>
<Description>AppMetrics support for Ae.Dns.</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/Ae.Dns.Client/Ae.Dns.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<Version>2.0.0</Version>
<WarningsAsErrors>true</WarningsAsErrors>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Authors>alanedwardes</Authors>
<Description>DNS over HTTPS (DoH) client, DNS UDP client and DNS caching client for .NET Core</Description>
Expand Down
Loading

0 comments on commit cafdbb8

Please sign in to comment.