mirror of
https://github.com/fergalmoran/DnsServer.git
synced 2025-12-22 09:29:50 +00:00
WeightedRoundRobin: added new app.
This commit is contained in:
196
Apps/WeightedRoundRobinApp/Address.cs
Normal file
196
Apps/WeightedRoundRobinApp/Address.cs
Normal file
@@ -0,0 +1,196 @@
|
||||
/*
|
||||
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;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using TechnitiumLibrary.Net.Dns;
|
||||
using TechnitiumLibrary.Net.Dns.ResourceRecords;
|
||||
|
||||
namespace WeightedRoundRobin
|
||||
{
|
||||
public class Address : 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.Name.Equals(appRecordName, StringComparison.OrdinalIgnoreCase))
|
||||
return Task.FromResult<DnsDatagram>(null);
|
||||
|
||||
string jsonPropertyName;
|
||||
|
||||
switch (question.Type)
|
||||
{
|
||||
case DnsResourceRecordType.A:
|
||||
jsonPropertyName = "ipv4Addresses";
|
||||
break;
|
||||
|
||||
case DnsResourceRecordType.AAAA:
|
||||
jsonPropertyName = "ipv6Addresses";
|
||||
break;
|
||||
|
||||
default:
|
||||
return Task.FromResult<DnsDatagram>(null);
|
||||
}
|
||||
|
||||
List<WeightedAddress> addresses;
|
||||
int totalWeight = 0;
|
||||
|
||||
using (JsonDocument jsonDocument = JsonDocument.Parse(appRecordData))
|
||||
{
|
||||
JsonElement jsonAppRecordData = jsonDocument.RootElement;
|
||||
|
||||
if (!jsonAppRecordData.TryGetProperty(jsonPropertyName, out JsonElement jsonAddresses) || (jsonAddresses.ValueKind == JsonValueKind.Null))
|
||||
return Task.FromResult<DnsDatagram>(null);
|
||||
|
||||
addresses = new List<WeightedAddress>(jsonAddresses.GetArrayLength());
|
||||
|
||||
foreach (JsonElement jsonAddressEntry in jsonAddresses.EnumerateArray())
|
||||
{
|
||||
if (jsonAddressEntry.TryGetProperty("enabled", out JsonElement jsonEnabled) && (jsonEnabled.ValueKind != JsonValueKind.Null) && !jsonEnabled.GetBoolean())
|
||||
continue;
|
||||
|
||||
if (!jsonAddressEntry.TryGetProperty("address", out JsonElement jsonAddress) || (jsonAddress.ValueKind == JsonValueKind.Null) || !IPAddress.TryParse(jsonAddress.GetString(), out IPAddress address))
|
||||
continue;
|
||||
|
||||
if (!jsonAddressEntry.TryGetProperty("weight", out JsonElement jsonWeight) || (jsonWeight.ValueKind == JsonValueKind.Null))
|
||||
continue;
|
||||
|
||||
int weight = jsonWeight.GetInt32();
|
||||
if (weight < 1)
|
||||
continue;
|
||||
|
||||
addresses.Add(new WeightedAddress() { Address = address, Weight = weight });
|
||||
totalWeight += weight;
|
||||
}
|
||||
}
|
||||
|
||||
if (addresses.Count == 0)
|
||||
return Task.FromResult<DnsDatagram>(null);
|
||||
|
||||
int randomSelection = RandomNumberGenerator.GetInt32(1, 101);
|
||||
int rangeFrom;
|
||||
int rangeTo = 0;
|
||||
DnsResourceRecord answer = null;
|
||||
|
||||
for (int i = 0; i < addresses.Count; i++)
|
||||
{
|
||||
rangeFrom = rangeTo + 1;
|
||||
|
||||
if (i == addresses.Count - 1)
|
||||
rangeTo = 100;
|
||||
else
|
||||
rangeTo += addresses[i].Weight * 100 / totalWeight;
|
||||
|
||||
if ((rangeFrom <= randomSelection) && (randomSelection <= rangeTo))
|
||||
{
|
||||
switch (question.Type)
|
||||
{
|
||||
case DnsResourceRecordType.A:
|
||||
answer = new DnsResourceRecord(question.Name, question.Type, DnsClass.IN, appRecordTtl, new DnsARecordData(addresses[i].Address));
|
||||
break;
|
||||
|
||||
case DnsResourceRecordType.AAAA:
|
||||
answer = new DnsResourceRecord(question.Name, question.Type, DnsClass.IN, appRecordTtl, new DnsAAAARecordData(addresses[i].Address));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (answer is null)
|
||||
throw new InvalidOperationException();
|
||||
|
||||
return Task.FromResult(new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, new DnsResourceRecord[] { answer }));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region properties
|
||||
|
||||
public string Description
|
||||
{ get { return "Returns an A or AAAA record using weighted round-robin load balancing."; } }
|
||||
|
||||
public string ApplicationRecordDataTemplate
|
||||
{
|
||||
get
|
||||
{
|
||||
return @"{
|
||||
""ipv4Addresses"": [
|
||||
{
|
||||
""address"": ""1.1.1.1"",
|
||||
""weight"": 5,
|
||||
""enabled"": true
|
||||
},
|
||||
{
|
||||
""address"": ""2.2.2.2"",
|
||||
""weight"": 3,
|
||||
""enabled"": true
|
||||
}
|
||||
],
|
||||
""ipv6Addresses"": [
|
||||
{
|
||||
""address"": ""::1"",
|
||||
""weight"": 2,
|
||||
""enabled"": true
|
||||
},
|
||||
{
|
||||
""address"": ""::2"",
|
||||
""weight"": 3,
|
||||
""enabled"": true
|
||||
}
|
||||
]
|
||||
}";
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
struct WeightedAddress
|
||||
{
|
||||
public IPAddress Address;
|
||||
public int Weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
159
Apps/WeightedRoundRobinApp/CNAME.cs
Normal file
159
Apps/WeightedRoundRobinApp/CNAME.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
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;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using TechnitiumLibrary.Net.Dns;
|
||||
using TechnitiumLibrary.Net.Dns.ResourceRecords;
|
||||
|
||||
namespace WeightedRoundRobin
|
||||
{
|
||||
public class CNAME : 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.Name.Equals(appRecordName, StringComparison.OrdinalIgnoreCase))
|
||||
return Task.FromResult<DnsDatagram>(null);
|
||||
|
||||
List<WeightedDomain> domainNames;
|
||||
int totalWeight = 0;
|
||||
|
||||
using (JsonDocument jsonDocument = JsonDocument.Parse(appRecordData))
|
||||
{
|
||||
JsonElement jsonAppRecordData = jsonDocument.RootElement;
|
||||
|
||||
if (!jsonAppRecordData.TryGetProperty("cnames", out JsonElement jsonCnames) || (jsonCnames.ValueKind == JsonValueKind.Null))
|
||||
return Task.FromResult<DnsDatagram>(null);
|
||||
|
||||
domainNames = new List<WeightedDomain>(jsonCnames.GetArrayLength());
|
||||
|
||||
foreach (JsonElement jsonCnameEntry in jsonCnames.EnumerateArray())
|
||||
{
|
||||
if (jsonCnameEntry.TryGetProperty("enabled", out JsonElement jsonEnabled) && (jsonEnabled.ValueKind != JsonValueKind.Null) && !jsonEnabled.GetBoolean())
|
||||
continue;
|
||||
|
||||
if (!jsonCnameEntry.TryGetProperty("domain", out JsonElement jsonDomain) || (jsonDomain.ValueKind == JsonValueKind.Null))
|
||||
continue;
|
||||
|
||||
if (!jsonCnameEntry.TryGetProperty("weight", out JsonElement jsonWeight) || (jsonWeight.ValueKind == JsonValueKind.Null))
|
||||
continue;
|
||||
|
||||
int weight = jsonWeight.GetInt32();
|
||||
if (weight < 1)
|
||||
continue;
|
||||
|
||||
domainNames.Add(new WeightedDomain() { Domain = jsonDomain.GetString(), Weight = weight });
|
||||
totalWeight += weight;
|
||||
}
|
||||
}
|
||||
|
||||
if (domainNames.Count == 0)
|
||||
return Task.FromResult<DnsDatagram>(null);
|
||||
|
||||
int randomSelection = RandomNumberGenerator.GetInt32(1, 101);
|
||||
int rangeFrom;
|
||||
int rangeTo = 0;
|
||||
DnsResourceRecord answer = null;
|
||||
|
||||
for (int i = 0; i < domainNames.Count; i++)
|
||||
{
|
||||
rangeFrom = rangeTo + 1;
|
||||
|
||||
if (i == domainNames.Count - 1)
|
||||
rangeTo = 100;
|
||||
else
|
||||
rangeTo += domainNames[i].Weight * 100 / totalWeight;
|
||||
|
||||
if ((rangeFrom <= randomSelection) && (randomSelection <= rangeTo))
|
||||
{
|
||||
if (question.Name.Equals(zoneName, StringComparison.OrdinalIgnoreCase)) //check for zone apex
|
||||
answer = new DnsResourceRecord(question.Name, DnsResourceRecordType.ANAME, DnsClass.IN, appRecordTtl, new DnsANAMERecordData(domainNames[i].Domain)); //use ANAME
|
||||
else
|
||||
answer = new DnsResourceRecord(question.Name, DnsResourceRecordType.CNAME, DnsClass.IN, appRecordTtl, new DnsCNAMERecordData(domainNames[i].Domain));
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (answer is null)
|
||||
throw new InvalidOperationException();
|
||||
|
||||
return Task.FromResult(new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, new DnsResourceRecord[] { answer }));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region properties
|
||||
|
||||
public string Description
|
||||
{ get { return "Returns a CNAME record using weighted round-robin load balancing."; } }
|
||||
|
||||
public string ApplicationRecordDataTemplate
|
||||
{
|
||||
get
|
||||
{
|
||||
return @"{
|
||||
""cnames"": [
|
||||
{
|
||||
""domain"": ""example.com"",
|
||||
""weight"": 5,
|
||||
""enabled"": true
|
||||
},
|
||||
{
|
||||
""domain"": ""example.net"",
|
||||
""weight"": 3,
|
||||
""enabled"": true
|
||||
}
|
||||
]
|
||||
}";
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
struct WeightedDomain
|
||||
{
|
||||
public string Domain;
|
||||
public int Weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
42
Apps/WeightedRoundRobinApp/WeightedRoundRobinApp.csproj
Normal file
42
Apps/WeightedRoundRobinApp/WeightedRoundRobinApp.csproj
Normal file
@@ -0,0 +1,42 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
||||
<Version>1.0</Version>
|
||||
<Company>Technitium</Company>
|
||||
<Product>Technitium DNS Server</Product>
|
||||
<Authors>Shreyas Zare</Authors>
|
||||
<AssemblyName>WeightedRoundRobinApp</AssemblyName>
|
||||
<RootNamespace>WeightedRoundRobin</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 A or AAAA records, or CNAME record using weighted round-robin load balancing.</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">
|
||||
<HintPath>..\..\..\TechnitiumLibrary\bin\TechnitiumLibrary.dll</HintPath>
|
||||
<Private>false</Private>
|
||||
</Reference>
|
||||
<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/WeightedRoundRobinApp/dnsApp.config
Normal file
1
Apps/WeightedRoundRobinApp/dnsApp.config
Normal file
@@ -0,0 +1 @@
|
||||
#This app requires no config.
|
||||
Reference in New Issue
Block a user