mirror of
https://github.com/fergalmoran/DnsServer.git
synced 2026-01-06 08:45:32 +00:00
FilterAaaa: updated InitializeAsync() to update config json with new filterDomains array in existing installations. Updated PostProcessAsync() to move the filterDomains feature code after bypassDomains code.
This commit is contained in:
@@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
using DnsServerCore.ApplicationCommon;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
@@ -55,7 +56,7 @@ namespace FilterAaaa
|
||||
|
||||
#region public
|
||||
|
||||
public Task InitializeAsync(IDnsServer dnsServer, string config)
|
||||
public async Task InitializeAsync(IDnsServer dnsServer, string config)
|
||||
{
|
||||
_dnsServer = dnsServer;
|
||||
|
||||
@@ -76,11 +77,18 @@ namespace FilterAaaa
|
||||
_bypassDomains = [];
|
||||
|
||||
if (jsonConfig.TryReadArray("filterDomains", out string[] filterDomains))
|
||||
{
|
||||
_filterDomains = filterDomains;
|
||||
}
|
||||
else
|
||||
{
|
||||
_filterDomains = [];
|
||||
|
||||
return Task.CompletedTask;
|
||||
//update config for new feature
|
||||
config = config.TrimEnd('\r', '\n', ' ', '}');
|
||||
config += ",\r\n \"filterDomains\": [\r\n ]\r\n}";
|
||||
await File.WriteAllTextAsync(Path.Combine(dnsServer.ApplicationFolder, "dnsApp.config"), config);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<DnsDatagram> PostProcessAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, DnsDatagram response)
|
||||
@@ -101,19 +109,6 @@ namespace FilterAaaa
|
||||
if (question.Type != DnsResourceRecordType.AAAA)
|
||||
return response;
|
||||
|
||||
bool filterDomain = _filterDomains.Length == 0;
|
||||
foreach (string filterDomain in _filterDomains)
|
||||
{
|
||||
if (qname.Equals(filterDomain, StringComparison.OrdinalIgnoreCase) || qname.EndsWith("." + filterDomain, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
filterDomain = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!filterDomain)
|
||||
return response;
|
||||
|
||||
bool hasAAAA = false;
|
||||
|
||||
foreach (DnsResourceRecord record in response.Answer)
|
||||
@@ -144,6 +139,20 @@ namespace FilterAaaa
|
||||
return response;
|
||||
}
|
||||
|
||||
bool filterDomain = _filterDomains.Length == 0;
|
||||
|
||||
foreach (string blockedDomain in _filterDomains)
|
||||
{
|
||||
if (qname.Equals(blockedDomain, StringComparison.OrdinalIgnoreCase) || qname.EndsWith("." + blockedDomain, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
filterDomain = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!filterDomain)
|
||||
return response;
|
||||
|
||||
DnsDatagram aResponse = await _dnsServer.DirectQueryAsync(new DnsQuestionRecord(qname, DnsResourceRecordType.A, DnsClass.IN), 2000);
|
||||
|
||||
if (aResponse.RCODE != DnsResponseCode.NoError)
|
||||
|
||||
Reference in New Issue
Block a user