mirror of
https://github.com/fergalmoran/DnsServer.git
synced 2025-12-22 09:29:50 +00:00
AutoPtr: added new app.
This commit is contained in:
144
Apps/AutoPtrApp/App.cs
Normal file
144
Apps/AutoPtrApp/App.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
Technitium DNS Server
|
||||
Copyright (C) 2023 Shreyas Zare (shreyas@technitium.com)
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
using DnsServerCore.ApplicationCommon;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using TechnitiumLibrary.Net;
|
||||
using TechnitiumLibrary.Net.Dns;
|
||||
using TechnitiumLibrary.Net.Dns.ResourceRecords;
|
||||
|
||||
namespace AutoPtr
|
||||
{
|
||||
public class App : IDnsApplication, IDnsAppRecordRequestHandler
|
||||
{
|
||||
#region IDisposable
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
//do nothing
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region public
|
||||
|
||||
public Task InitializeAsync(IDnsServer dnsServer, string config)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task<DnsDatagram> ProcessRequestAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, bool isRecursionAllowed, string zoneName, string appRecordName, uint appRecordTtl, string appRecordData)
|
||||
{
|
||||
DnsQuestionRecord question = request.Question[0];
|
||||
|
||||
if (question.Type != DnsResourceRecordType.PTR)
|
||||
return Task.FromResult<DnsDatagram>(null);
|
||||
|
||||
string qname = question.Name;
|
||||
|
||||
if (qname.Length == appRecordName.Length)
|
||||
return Task.FromResult<DnsDatagram>(null);
|
||||
|
||||
if (!IPAddressExtensions.TryParseReverseDomain(qname, out IPAddress address))
|
||||
return Task.FromResult<DnsDatagram>(null);
|
||||
|
||||
string domain = null;
|
||||
|
||||
using (JsonDocument jsonDocument = JsonDocument.Parse(appRecordData))
|
||||
{
|
||||
JsonElement jsonAppRecordData = jsonDocument.RootElement;
|
||||
|
||||
string ipSeparator;
|
||||
|
||||
if (jsonAppRecordData.TryGetProperty("ipSeparator", out JsonElement jsonSeparator) && (jsonSeparator.ValueKind != JsonValueKind.Null))
|
||||
ipSeparator = jsonSeparator.ToString();
|
||||
else
|
||||
ipSeparator = string.Empty;
|
||||
|
||||
switch (address.AddressFamily)
|
||||
{
|
||||
case AddressFamily.InterNetwork:
|
||||
{
|
||||
byte[] buffer = address.GetAddressBytes();
|
||||
|
||||
foreach (byte b in buffer)
|
||||
{
|
||||
if (domain is null)
|
||||
domain = b.ToString();
|
||||
else
|
||||
domain += ipSeparator + b.ToString();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case AddressFamily.InterNetworkV6:
|
||||
{
|
||||
byte[] buffer = address.GetAddressBytes();
|
||||
|
||||
for (int i = 0; i < buffer.Length; i += 2)
|
||||
{
|
||||
if (domain is null)
|
||||
domain = buffer[i].ToString("x2") + buffer[i + 1].ToString("x2");
|
||||
else
|
||||
domain += ipSeparator + buffer[i].ToString("x2") + buffer[i + 1].ToString("x2");
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return Task.FromResult<DnsDatagram>(null);
|
||||
}
|
||||
|
||||
if (jsonAppRecordData.TryGetProperty("prefix", out JsonElement jsonPrefix) && (jsonPrefix.ValueKind != JsonValueKind.Null))
|
||||
domain = jsonPrefix.GetString() + domain;
|
||||
|
||||
if (jsonAppRecordData.TryGetProperty("suffix", out JsonElement jsonSuffix) && (jsonSuffix.ValueKind != JsonValueKind.Null))
|
||||
domain += jsonSuffix.GetString();
|
||||
}
|
||||
|
||||
DnsResourceRecord[] answer = new DnsResourceRecord[] { new DnsResourceRecord(qname, DnsResourceRecordType.PTR, DnsClass.IN, appRecordTtl, new DnsPTRRecordData(domain)) };
|
||||
|
||||
return Task.FromResult(new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, answer));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region properties
|
||||
|
||||
public string Description
|
||||
{ get { return "Returns automatically generated response for a PTR request for both IPv4 and IPv6."; } }
|
||||
|
||||
public string ApplicationRecordDataTemplate
|
||||
{
|
||||
get
|
||||
{
|
||||
return @"{
|
||||
""prefix"": """",
|
||||
""suffix"": "".example.com"",
|
||||
""ipSeparator"": ""-""
|
||||
}";
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
39
Apps/AutoPtrApp/AutoPtrApp.csproj
Normal file
39
Apps/AutoPtrApp/AutoPtrApp.csproj
Normal file
@@ -0,0 +1,39 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
||||
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
|
||||
<Version>1.0</Version>
|
||||
<Company>Technitium</Company>
|
||||
<Product>Technitium DNS Server</Product>
|
||||
<Authors>Shreyas Zare</Authors>
|
||||
<AssemblyName>AutoPtrApp</AssemblyName>
|
||||
<RootNamespace>AutoPtr</RootNamespace>
|
||||
<PackageProjectUrl>https://technitium.com/dns/</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/TechnitiumSoftware/DnsServer</RepositoryUrl>
|
||||
<Description>Allows creating APP records in a primary and forwarder zones that can return automatically generated response for a PTR request for both IPv4 and IPv6.</Description>
|
||||
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
|
||||
<OutputType>Library</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\DnsServerCore.ApplicationCommon\DnsServerCore.ApplicationCommon.csproj">
|
||||
<Private>false</Private>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="TechnitiumLibrary.Net">
|
||||
<HintPath>..\..\..\TechnitiumLibrary\bin\TechnitiumLibrary.Net.dll</HintPath>
|
||||
<Private>false</Private>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="dnsApp.config">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
1
Apps/AutoPtrApp/dnsApp.config
Normal file
1
Apps/AutoPtrApp/dnsApp.config
Normal file
@@ -0,0 +1 @@
|
||||
#This app requires no config.
|
||||
Reference in New Issue
Block a user