GeoDistance.CNAME: Implemented support for ISP/ASN database.

This commit is contained in:
Shreyas Zare
2024-03-16 12:42:10 +05:30
parent 190c408450
commit 7c5b45e655

View File

@@ -1,6 +1,6 @@
/* /*
Technitium DNS Server Technitium DNS Server
Copyright (C) 2023 Shreyas Zare (shreyas@technitium.com) Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com)
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@@ -100,18 +100,22 @@ namespace GeoDistance
Location location = null; Location location = null;
bool ecsUsed = false; byte scopePrefixLength = 0;
EDnsClientSubnetOptionData requestECS = request.GetEDnsClientSubnetOption(); EDnsClientSubnetOptionData requestECS = request.GetEDnsClientSubnetOption();
if (requestECS is not null) if (requestECS is not null)
{ {
if (_maxMind.DatabaseReader.TryCity(requestECS.Address, out CityResponse csResponse) && csResponse.Location.HasCoordinates) if ((_maxMind.IspReader is not null) && _maxMind.IspReader.TryIsp(requestECS.Address, out IspResponse csIsp) && (csIsp.Network is not null))
{ scopePrefixLength = (byte)csIsp.Network.PrefixLength;
ecsUsed = true; else if ((_maxMind.AsnReader is not null) && _maxMind.AsnReader.TryAsn(requestECS.Address, out AsnResponse csAsn) && (csAsn.Network is not null))
scopePrefixLength = (byte)csAsn.Network.PrefixLength;
else
scopePrefixLength = requestECS.SourcePrefixLength;
if (_maxMind.CityReader.TryCity(requestECS.Address, out CityResponse csResponse) && csResponse.Location.HasCoordinates)
location = csResponse.Location; location = csResponse.Location;
} }
}
if ((location is null) && _maxMind.DatabaseReader.TryCity(remoteEP.Address, out CityResponse response) && response.Location.HasCoordinates) if ((location is null) && _maxMind.CityReader.TryCity(remoteEP.Address, out CityResponse response) && response.Location.HasCoordinates)
location = response.Location; location = response.Location;
using JsonDocument jsonDocument = JsonDocument.Parse(appRecordData); using JsonDocument jsonDocument = JsonDocument.Parse(appRecordData);
@@ -156,19 +160,10 @@ namespace GeoDistance
else else
answers = new DnsResourceRecord[] { new DnsResourceRecord(question.Name, DnsResourceRecordType.CNAME, DnsClass.IN, appRecordTtl, new DnsCNAMERecordData(cname)) }; answers = new DnsResourceRecord[] { new DnsResourceRecord(question.Name, DnsResourceRecordType.CNAME, DnsClass.IN, appRecordTtl, new DnsCNAMERecordData(cname)) };
EDnsOption[] options; EDnsOption[] options = null;
if (requestECS is null) if (requestECS is not null)
{ options = EDnsClientSubnetOptionData.GetEDnsClientSubnetOption(requestECS.SourcePrefixLength, scopePrefixLength, requestECS.Address);
options = null;
}
else
{
if (ecsUsed)
options = EDnsClientSubnetOptionData.GetEDnsClientSubnetOption(requestECS.SourcePrefixLength, requestECS.SourcePrefixLength, requestECS.Address);
else
options = EDnsClientSubnetOptionData.GetEDnsClientSubnetOption(requestECS.SourcePrefixLength, 0, requestECS.Address);
}
return Task.FromResult(new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, answers, null, null, _dnsServer.UdpPayloadSize, EDnsHeaderFlags.None, options)); return Task.FromResult(new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, answers, null, null, _dnsServer.UdpPayloadSize, EDnsHeaderFlags.None, options));
} }