Skip to content

Commit

Permalink
Added .NET5 target and DnsSuffix to NameServer
Browse files Browse the repository at this point in the history
Added DnsSuffix property to NameServer (will be populated if available)
Added DnsString concat
Changed NameServer.ValidateNameServers call to be done only once for fixed name server configuration. No need to do that with every query.
Added an example to MiniDig to use the DnsSuffix in case the query is just a HostName
  • Loading branch information
MichaCo committed May 16, 2021
1 parent 1ecf870 commit b223f3f
Show file tree
Hide file tree
Showing 13 changed files with 787 additions and 552 deletions.
27 changes: 26 additions & 1 deletion samples/MiniDig/DigCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public static string Version

public CommandOption ReversArg { get; }

public DigCommand(CommandLineApplication app, string[] originalArgs) : base(app, originalArgs)
public DigCommand(CommandLineApplication app, string[] originalArgs)
: base(app, originalArgs)
{
DomainArg = app.Argument("domain", "domain name", false);
QTypeArg = app.Argument("q-type", "QType", false);
Expand Down Expand Up @@ -102,6 +103,7 @@ protected override async Task<int> Execute()
}
}


try
{
// finally running the command
Expand All @@ -111,6 +113,29 @@ protected override async Task<int> Execute()

Console.WriteLine($"; Servers: {string.Join(", ", lookup.NameServers)}");


var parsedDnsString = DnsString.Parse(useDomain);
if (parsedDnsString.NumberOfLabels == 1 && !parsedDnsString.Original.EndsWith("."))
{
foreach(var server in lookup.NameServers)
{
if(server.DnsSuffix != null)
{
var newQuery = parsedDnsString + server.DnsSuffix;

var serverResult = useQClass == 0 ?
await lookup.QueryServerAsync(new[] { server }, newQuery, useQType).ConfigureAwait(false) :
await lookup.QueryServerAsync(new[] { server }, newQuery, useQType, useQClass).ConfigureAwait(false);

if (!serverResult.HasError)
{
Console.WriteLine(serverResult.AuditTrail);
return 0;
}
}
}
}

var result = useQClass == 0 ?
await lookup.QueryAsync(useDomain, useQType).ConfigureAwait(false) :
await lookup.QueryAsync(useDomain, useQType, useQClass).ConfigureAwait(false);
Expand Down
3 changes: 1 addition & 2 deletions samples/MiniDig/DnsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public bool GetUseCache()

protected virtual void Configure()
{
App.HelpOption("-? | -h | --help | --helpme");
ServerArg = App.Option(
"-s | --server",
"The DNS server <name|ip>#<port> (multiple)",
Expand Down Expand Up @@ -193,8 +194,6 @@ protected virtual void Configure()
"--dnssec",
"Request DNS SEC records (do flag).",
CommandOptionType.NoValue);

App.HelpOption("-? | -h | --help | --helpme");
}

protected abstract Task<int> Execute();
Expand Down
4 changes: 2 additions & 2 deletions samples/MiniDig/MiniDig.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;net471;netcoreapp3.1;</TargetFrameworks>
<TargetFramework>net50</TargetFramework>
<AssemblyName>MiniDig</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>MiniDig</PackageId>
<LangVersion>7.2</LangVersion>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
26 changes: 20 additions & 6 deletions samples/MiniDig/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,40 @@ namespace DigApp
{
public class Program
{
public static async Task Main(string[] args)
public static async Task<int> Main(string[] args)
{
DnsClient.Tracing.Source.Switch.Level = SourceLevels.Information;
DnsClient.Tracing.Source.Listeners.Add(new ConsoleTraceListener());

var app = new CommandLineApplication(throwOnUnexpectedArg: true);

app.Command("perf", (perfApp) => new PerfCommand(perfApp, args), throwOnUnexpectedArg: true);
app.Command("random", (randApp) => new RandomCommand(randApp, args), throwOnUnexpectedArg: true);
try
{
app.Command("perf", (perfApp) => new PerfCommand(perfApp, args), throwOnUnexpectedArg: true);
app.Command("random", (randApp) => new RandomCommand(randApp, args), throwOnUnexpectedArg: true);

var defaultCommand = new DigCommand(app, args);
app.Command("dig", (digApp) => new DigCommand(digApp, args), throwOnUnexpectedArg: true);

try
var defaultCommand = new DigCommand(app, args);
return await app.ExecuteAsync(args).ConfigureAwait(false);
}
catch (UnrecognizedCommandParsingException ex)
{
await app.ExecuteAsync(args).ConfigureAwait(false);
Console.WriteLine(ex.Message);
app.ShowHelp();
}
catch (CommandParsingException ex)
{
Console.WriteLine(ex.Message);
app.ShowHelp();
}
catch (Exception ex)
{
Console.WriteLine(ex);
return 500;
}

return -1;
}
}

Expand Down
2 changes: 1 addition & 1 deletion samples/MiniDig/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"MiniDig": {
"commandName": "Project",
"commandLineArgs": "mx3.mailbox.org spf --time -1"
"commandLineArgs": "google.com"
}
}
}
8 changes: 4 additions & 4 deletions src/DnsClient/DnsClient.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>1.4.0</VersionPrefix>
<VersionPrefix>1.5.0</VersionPrefix>
<VersionSuffix Condition="'$(VersionSuffix)'!='' AND '$(BuildNumber)' != ''">$(VersionSuffix)-$(BuildNumber)</VersionSuffix>

<TargetFrameworks>netstandard1.3;netstandard2.0;netstandard2.1;net45;net471</TargetFrameworks>
<TargetFrameworks>netstandard1.3;netstandard2.0;netstandard2.1;net50;net45;net471</TargetFrameworks>
<DebugType>full</DebugType>

<Product>DnsClient.NET</Product>
<Description>DnsClient.NET is a simple yet very powerful and high performance open source library for the .NET Framework to do DNS lookups</Description>

<Copyright>Copyright (c) 2020 MichaConrad</Copyright>
<Copyright>Copyright (c) 2021 Michael Conrad</Copyright>
<Authors>MichaCo</Authors>
<AssemblyName>DnsClient</AssemblyName>

Expand All @@ -28,7 +28,7 @@
<RepositoryUrl>https://github.com/MichaCo/DnsClient.NET</RepositoryUrl>

<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>7.2</LangVersion>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
77 changes: 59 additions & 18 deletions src/DnsClient/DnsString.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace DnsClient
{
Expand Down Expand Up @@ -45,10 +43,17 @@ public class DnsString
/// </summary>
public string Value { get; }

internal DnsString(string original, string value)
/// <summary>
/// Gets the number of labels of this <see cref="DnsString"/> or null if not applicable.
/// This property is only set if the <see cref="Parse(string)"/> method was used to create this instance.
/// </summary>
public int? NumberOfLabels { get; }

internal DnsString(string original, string value, int? numLabels = null)
{
Original = original;
Value = value;
NumberOfLabels = numLabels;
}

/// <summary>
Expand All @@ -60,6 +65,52 @@ internal DnsString(string original, string value)
/// </returns>
public static implicit operator string(DnsString name) => name?.Value;

/// <summary>
///
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static DnsString operator +(DnsString a, DnsString b)
{
if (a is null)
{
throw new ArgumentNullException(nameof(a));
}

if (b is null)
{
throw new ArgumentNullException(nameof(b));
}

var result = a.Value + (b.Value.Length > 1 ? b.Value : string.Empty);
return new DnsString(result, result);
}

/// <summary>
///
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static DnsString operator +(DnsString a, string b)
{
if (a is null)
{
throw new ArgumentNullException(nameof(a));
}

if (string.IsNullOrWhiteSpace(b))
{
throw new ArgumentException($"'{nameof(b)}' cannot be null or empty.", nameof(b));
}

b = b[0] == Dot ? b.Substring(1) : b;

var parsed = Parse(b);
return a + parsed;
}

/// <inheritdoc />
public override string ToString()
{
Expand All @@ -83,18 +134,6 @@ public override bool Equals(object obj)
return obj.ToString().Equals(Value);
}

// removed as this is actually the wrong label representation (also, doesn't work if there are escaped \. in the value
/////// <summary>
/////// Returns labels representation of the <see cref="Value"/>.
/////// </summary>
////public IReadOnlyList<string> Labels
////{
//// get
//// {
//// return Value.Split('.').Reverse().Select(p => p + DotStr).ToArray();
//// }
////}

/// <summary>
/// Parses the given <paramref name="query"/> and validates all labels.
/// </summary>
Expand Down Expand Up @@ -155,7 +194,9 @@ public static DnsString Parse(string query)
result += Dot;
}

return new DnsString(query, result);
var labels = result.Split(new[] { Dot }, StringSplitOptions.RemoveEmptyEntries);

return new DnsString(query, result, labels.Length);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -185,10 +226,10 @@ public static DnsString Parse(string query)

if (query[query.Length - 1] != Dot)
{
return new DnsString(query, query + Dot);
return new DnsString(query, query + Dot, labelsCount);
}

return new DnsString(query, query);
return new DnsString(query, query, labelsCount);
}

/// <summary>
Expand Down

0 comments on commit b223f3f

Please sign in to comment.