36 lines
919 B
C#
36 lines
919 B
C#
using System.Net;
|
|
using Flypaper.Core.Models;
|
|
using MaxMind.GeoIP2;
|
|
using MaxMind.GeoIP2.Responses;
|
|
|
|
namespace Flypaper.GeoIp;
|
|
|
|
internal sealed class MaxMindGeoIpDatabase(DatabaseReader? city, DatabaseReader? asn) : IGeoIpDatabase
|
|
{
|
|
public IpOrigin? Find(IPAddress address)
|
|
{
|
|
CityResponse? cityResponse = null;
|
|
AsnResponse? asnResponse = null;
|
|
city?.TryCity(address, out cityResponse);
|
|
asn?.TryAsn(address, out asnResponse);
|
|
|
|
if (cityResponse is null && asnResponse is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return new IpOrigin(
|
|
cityResponse?.Country.IsoCode,
|
|
cityResponse?.Country.Name,
|
|
cityResponse?.City.Name,
|
|
asnResponse?.AutonomousSystemNumber,
|
|
asnResponse?.AutonomousSystemOrganization);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
city?.Dispose();
|
|
asn?.Dispose();
|
|
}
|
|
}
|