GeoContinent.Address: Implemented support for ISP/ASN database.

This commit is contained in:
Shreyas Zare
2024-03-16 12:36:12 +05:30
parent e73b1aabe9
commit 4d5250babf

View File

@@ -1,6 +1,6 @@
/*
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
it under the terms of the GNU General Public License as published by
@@ -92,13 +92,19 @@ namespace GeoContinent
JsonElement jsonAppRecordData = jsonDocument.RootElement;
JsonElement jsonContinent = default;
bool ecsUsed = false;
byte scopePrefixLength = 0;
EDnsClientSubnetOptionData requestECS = request.GetEDnsClientSubnetOption();
if (requestECS is not null)
{
if (_maxMind.DatabaseReader.TryCountry(requestECS.Address, out CountryResponse csResponse))
if ((_maxMind.IspReader is not null) && _maxMind.IspReader.TryIsp(requestECS.Address, out IspResponse csIsp) && (csIsp.Network is not null))
scopePrefixLength = (byte)csIsp.Network.PrefixLength;
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.CountryReader.TryCountry(requestECS.Address, out CountryResponse csResponse))
{
ecsUsed = true;
if (!jsonAppRecordData.TryGetProperty(csResponse.Continent.Code, out jsonContinent))
jsonAppRecordData.TryGetProperty("default", out jsonContinent);
}
@@ -106,7 +112,7 @@ namespace GeoContinent
if (jsonContinent.ValueKind == JsonValueKind.Undefined)
{
if (_maxMind.DatabaseReader.TryCountry(remoteEP.Address, out CountryResponse response))
if (_maxMind.CountryReader.TryCountry(remoteEP.Address, out CountryResponse response))
{
if (!jsonAppRecordData.TryGetProperty(response.Continent.Code, out jsonContinent))
jsonAppRecordData.TryGetProperty("default", out jsonContinent);
@@ -151,19 +157,10 @@ namespace GeoContinent
if (answers.Count > 1)
answers.Shuffle();
EDnsOption[] options;
EDnsOption[] options = null;
if (requestECS is null)
{
options = null;
}
else
{
if (ecsUsed)
options = EDnsClientSubnetOptionData.GetEDnsClientSubnetOption(requestECS.SourcePrefixLength, requestECS.SourcePrefixLength, requestECS.Address);
else
options = EDnsClientSubnetOptionData.GetEDnsClientSubnetOption(requestECS.SourcePrefixLength, 0, requestECS.Address);
}
if (requestECS is not null)
options = EDnsClientSubnetOptionData.GetEDnsClientSubnetOption(requestECS.SourcePrefixLength, scopePrefixLength, 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));
}