Skip to content

Commit

Permalink
Fallback to the gateway's DNS when resolving local addresses (leak pr…
Browse files Browse the repository at this point in the history
…evention will disable upstream sources)
  • Loading branch information
jdomnitz committed Aug 6, 2024
1 parent a527a46 commit ac7faca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions TinyDNS/DNSResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ private void ReloadNameservers()
{
globalNameservers.Clear();
var nics = NetworkInterface.GetAllNetworkInterfaces();
IPAddress? gateway = null;
foreach (var nic in nics)
{
if (nic.OperationalStatus == OperationalStatus.Up && !nic.IsReceiveOnly && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
{
foreach (IPAddress ns in nic.GetIPProperties().DnsAddresses)
globalNameservers.Add(ns);
gateway ??= nic.GetIPProperties().GatewayAddresses.FirstOrDefault()?.Address;
}
}
if (gateway != null)
globalNameservers.Add(gateway); //Always fall back to the gateway
}

/// <summary>
Expand Down
24 changes: 24 additions & 0 deletions TinyDNS/MDNS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,30 @@ public async Task Query(List<string> domain, DNSRecordType type, bool unicastRes
await SendMessage(msg);
}

/// <summary>
/// Lookup the domain name for an IP address
/// </summary>
/// <param name="address"></param>
/// <returns>The domain</returns>
public async Task<string?> ResolveIP(IPAddress address)
{
List<Message> responses = await ResolveInverseQuery(address);
foreach (Message response in responses)
{
foreach (ResourceRecord answer in response.Answers)
{
if (answer is PtrRecord ptr)
{
var labels = ptr.DomainLabels;
if (labels.Count > 1)
labels.RemoveAt(labels.Count - 1); //Remove .local
return string.Join('.', labels);
}
}
}
return null;
}

public async Task<List<Message>> ResolveInverseQuery(IPAddress address, bool unicastResponse = false)
{
var domain = DomainParser.FromIP(address);
Expand Down
2 changes: 1 addition & 1 deletion TinyDNS/TinyDNS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net80</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>0.7.1</Version>
<Version>0.7.2</Version>
<PackageLicenseExpression>AGPL-3.0-or-later</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Authors>jdomnitz</Authors>
Expand Down

0 comments on commit ac7faca

Please sign in to comment.