Add IP origin enrichment with DB-IP and world map
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
536aee1f31
commit
3671019e60
47 files changed
+1389
-12
No files matched your search
@@ -0,0 +1,108 @@
|
||||
using System.Net;
|
||||
using Flypaper.Core.Models;
|
||||
using Flypaper.GeoIp;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.GeoIp;
|
||||
|
||||
public sealed class DbIpEnricherTests
|
||||
{
|
||||
private static readonly IpOrigin Germany = new("DE", "Deutschland", "Berlin", 3320, "Deutsche Telekom AG");
|
||||
private static readonly IPAddress PublicIp = IPAddress.Parse("203.0.113.9");
|
||||
|
||||
private readonly IGeoIpDatabaseFactory _factory = Substitute.For<IGeoIpDatabaseFactory>();
|
||||
private readonly IGeoIpDatabase _database = Substitute.For<IGeoIpDatabase>();
|
||||
|
||||
[Fact]
|
||||
public void Returns_origin_for_public_ip()
|
||||
{
|
||||
_database.Find(PublicIp).Returns(Germany);
|
||||
_factory.Open().Returns(_database);
|
||||
|
||||
var enricher = CreateEnricher();
|
||||
|
||||
enricher.IsAvailable.ShouldBeTrue();
|
||||
enricher.Lookup(PublicIp).ShouldBe(Germany);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Looks_up_ipv4_mapped_addresses_as_ipv4()
|
||||
{
|
||||
_database.Find(PublicIp).Returns(Germany);
|
||||
_factory.Open().Returns(_database);
|
||||
|
||||
CreateEnricher().Lookup(PublicIp.MapToIPv6()).ShouldBe(Germany);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("10.1.2.3")]
|
||||
[InlineData("172.19.0.5")]
|
||||
[InlineData("192.168.1.14")]
|
||||
[InlineData("127.0.0.1")]
|
||||
[InlineData("::1")]
|
||||
[InlineData("fe80::1")]
|
||||
public void Does_not_look_up_private_ips(string address)
|
||||
{
|
||||
_factory.Open().Returns(_database);
|
||||
|
||||
CreateEnricher().Lookup(IPAddress.Parse(address)).ShouldBeNull();
|
||||
|
||||
_database.DidNotReceiveWithAnyArgs().Find(default!);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Returns_nothing_without_database()
|
||||
{
|
||||
_factory.Open().Returns((IGeoIpDatabase?)null);
|
||||
|
||||
var enricher = CreateEnricher();
|
||||
|
||||
enricher.IsAvailable.ShouldBeFalse();
|
||||
enricher.Lookup(PublicIp).ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Swallows_lookup_errors()
|
||||
{
|
||||
_database.Find(PublicIp).Throws(new InvalidOperationException("corrupt"));
|
||||
_factory.Open().Returns(_database);
|
||||
|
||||
CreateEnricher().Lookup(PublicIp).ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reload_switches_to_new_database_and_raises_updated()
|
||||
{
|
||||
_factory.Open().Returns((IGeoIpDatabase?)null, _database);
|
||||
_database.Find(PublicIp).Returns(Germany);
|
||||
var enricher = CreateEnricher();
|
||||
var raised = 0;
|
||||
enricher.Updated += (_, _) => raised++;
|
||||
|
||||
enricher.Reload();
|
||||
|
||||
raised.ShouldBe(1);
|
||||
enricher.IsAvailable.ShouldBeTrue();
|
||||
enricher.Lookup(PublicIp).ShouldBe(Germany);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reload_keeps_current_database_when_files_are_missing()
|
||||
{
|
||||
_factory.Open().Returns(_database, (IGeoIpDatabase?)null);
|
||||
_database.Find(PublicIp).Returns(Germany);
|
||||
var enricher = CreateEnricher();
|
||||
var raised = 0;
|
||||
enricher.Updated += (_, _) => raised++;
|
||||
|
||||
enricher.Reload();
|
||||
|
||||
raised.ShouldBe(0);
|
||||
enricher.Lookup(PublicIp).ShouldBe(Germany);
|
||||
}
|
||||
|
||||
private DbIpEnricher CreateEnricher() => new(_factory, NullLogger<DbIpEnricher>.Instance);
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
using System.IO.Compression;
|
||||
using System.Net;
|
||||
using Flypaper.GeoIp;
|
||||
using Flypaper.Tests.Fakes;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NSubstitute;
|
||||
using Shouldly;
|
||||
|
||||
namespace Flypaper.Tests.GeoIp;
|
||||
|
||||
public sealed class GeoIpUpdaterTests : IDisposable
|
||||
{
|
||||
private const string BaseUrl = "https://download.test/free/";
|
||||
private static readonly DateTimeOffset Now = new(2026, 11, 3, 8, 0, 0, TimeSpan.Zero);
|
||||
|
||||
private readonly string _directory = Path.Combine(Path.GetTempPath(), $"flypaper-geoip-{Guid.NewGuid():N}");
|
||||
private readonly FakeHttpHandler _http = new();
|
||||
|
||||
[Fact]
|
||||
public async Task Falls_back_to_previous_month_when_current_is_not_released()
|
||||
{
|
||||
_http.Respond($"{BaseUrl}dbip-city-lite-2026-10.mmdb.gz", () => Gzip("city-october"));
|
||||
_http.Respond($"{BaseUrl}dbip-asn-lite-2026-10.mmdb.gz", () => Gzip("asn-october"));
|
||||
|
||||
(await CreateUpdater().UpdateAsync(CancellationToken.None)).ShouldBeTrue();
|
||||
|
||||
File.ReadAllText(Path.Combine(_directory, "dbip-city-lite.mmdb")).ShouldBe("city-october");
|
||||
File.ReadAllText(Path.Combine(_directory, "dbip-asn-lite.mmdb")).ShouldBe("asn-october");
|
||||
File.ReadAllText(Path.Combine(_directory, "dbip-city-lite.mmdb.version")).ShouldBe("2026-10");
|
||||
_http.Requests.ShouldContain($"{BaseUrl}dbip-city-lite-2026-11.mmdb.gz");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Prefers_current_month()
|
||||
{
|
||||
_http.Respond($"{BaseUrl}dbip-city-lite-2026-11.mmdb.gz", () => Gzip("city-november"));
|
||||
_http.Respond($"{BaseUrl}dbip-asn-lite-2026-11.mmdb.gz", () => Gzip("asn-november"));
|
||||
|
||||
(await CreateUpdater().UpdateAsync(CancellationToken.None)).ShouldBeTrue();
|
||||
|
||||
File.ReadAllText(Path.Combine(_directory, "dbip-city-lite.mmdb.version")).ShouldBe("2026-11");
|
||||
_http.Requests.ShouldNotContain($"{BaseUrl}dbip-city-lite-2026-10.mmdb.gz");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Skips_download_when_previous_month_is_installed_and_current_is_missing()
|
||||
{
|
||||
Install("city", "2026-10");
|
||||
Install("asn", "2026-10");
|
||||
|
||||
(await CreateUpdater().UpdateAsync(CancellationToken.None)).ShouldBeFalse();
|
||||
|
||||
_http.Requests.ShouldBe([$"{BaseUrl}dbip-city-lite-2026-11.mmdb.gz", $"{BaseUrl}dbip-asn-lite-2026-11.mmdb.gz"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Skips_requests_when_current_month_is_installed()
|
||||
{
|
||||
Install("city", "2026-11");
|
||||
Install("asn", "2026-11");
|
||||
|
||||
(await CreateUpdater().UpdateAsync(CancellationToken.None)).ShouldBeFalse();
|
||||
|
||||
_http.Requests.ShouldBeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Fails_without_touching_files_when_server_errors()
|
||||
{
|
||||
_http.Respond($"{BaseUrl}dbip-city-lite-2026-11.mmdb.gz", () => new HttpResponseMessage(HttpStatusCode.InternalServerError));
|
||||
|
||||
await Should.ThrowAsync<HttpRequestException>(() => CreateUpdater().UpdateAsync(CancellationToken.None));
|
||||
|
||||
File.Exists(Path.Combine(_directory, "dbip-city-lite.mmdb")).ShouldBeFalse();
|
||||
}
|
||||
|
||||
private GeoIpUpdater CreateUpdater()
|
||||
{
|
||||
var options = Options.Create(new GeoIpOptions { Directory = _directory, BaseUrl = BaseUrl });
|
||||
var httpClientFactory = Substitute.For<IHttpClientFactory>();
|
||||
httpClientFactory.CreateClient(Arg.Any<string>()).Returns(_ => new HttpClient(_http, disposeHandler: false));
|
||||
var databaseFactory = Substitute.For<IGeoIpDatabaseFactory>();
|
||||
var enricher = new DbIpEnricher(databaseFactory, NullLogger<DbIpEnricher>.Instance);
|
||||
|
||||
return new GeoIpUpdater(httpClientFactory, options, enricher, new FixedTimeProvider(Now), NullLogger<GeoIpUpdater>.Instance);
|
||||
}
|
||||
|
||||
private void Install(string kind, string month)
|
||||
{
|
||||
Directory.CreateDirectory(_directory);
|
||||
File.WriteAllText(Path.Combine(_directory, $"dbip-{kind}-lite.mmdb"), kind);
|
||||
File.WriteAllText(Path.Combine(_directory, $"dbip-{kind}-lite.mmdb.version"), month);
|
||||
}
|
||||
|
||||
private static HttpResponseMessage Gzip(string content)
|
||||
{
|
||||
using var buffer = new MemoryStream();
|
||||
using (var gzip = new GZipStream(buffer, CompressionMode.Compress, leaveOpen: true))
|
||||
using (var writer = new StreamWriter(gzip))
|
||||
{
|
||||
writer.Write(content);
|
||||
}
|
||||
|
||||
return new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(buffer.ToArray()) };
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Directory.Exists(_directory))
|
||||
{
|
||||
Directory.Delete(_directory, recursive: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user