From 4d5250babfbdb17c9908e5139bea86123431990e Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 16 Mar 2024 12:36:12 +0530 Subject: [PATCH] GeoContinent.Address: Implemented support for ISP/ASN database. --- Apps/GeoContinentApp/Address.cs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/Apps/GeoContinentApp/Address.cs b/Apps/GeoContinentApp/Address.cs index 4848a39b..d0145865 100644 --- a/Apps/GeoContinentApp/Address.cs +++ b/Apps/GeoContinentApp/Address.cs @@ -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)); }