PrivaPub now finds CDNs three ways, best first: the address ranges the CDNs publish (Cloudflare, Fastly, Amazon CloudFront, Bunny, Gcore, Imperva), downloaded daily by CdnUpdater and kept in CdnRangeSet; the CDN's fingerprint in the responses it already gets from a server (EdgeHintsHandler on the federation client); and the networks that carry only a CDN. The fixed ASN list is gone; ASNs shared with plain hosting (AWS, DataPacket) no longer hide a server. A server's Geo records the CDN, its domain and how it was found, and weekly snapshots now keep the city and coordinates too. Servers through time (ServerPlaces): /instances/:host/history lists a server's weekly snapshots, a CDN-fronted server's geo names the CDN's domain and where the server was before it (before_cdn), and /api/privapub/v1/cdns and /cdns/:domain group servers by CDN with week by week who joined and who left. Owner decisions recorded in ROADMAP. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LsXgEaXee4GCU1hwYgPJXw
70 lines
2.2 KiB
C#
70 lines
2.2 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
using PrivaPub.Infrastructure.Geo;
|
|
using PrivaPub.Infrastructure.Http;
|
|
using PrivaPub.Infrastructure.Statistics;
|
|
using PrivaPub.Tests.Support;
|
|
|
|
using System.Net;
|
|
|
|
namespace PrivaPub.Tests.Infrastructure
|
|
{
|
|
public class GeoLocatorTests
|
|
{
|
|
static DbIpLocator Locator(string directory) =>
|
|
new(new StaticOptions<StatisticsOptions>(new StatisticsOptions { GeoDirectory = directory }), NullLogger<DbIpLocator>.Instance);
|
|
|
|
[Fact]
|
|
public void Without_databases_nothing_is_located()
|
|
{
|
|
using var locator = Locator(Path.Combine(Path.GetTempPath(), $"no-geo-{Guid.NewGuid():N}"));
|
|
|
|
Assert.Null(locator.Locate(IPAddress.Parse("1.1.1.1")));
|
|
Assert.Null(locator.Source);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_private_address_is_never_looked_up()
|
|
{
|
|
using var locator = Locator(Path.GetTempPath());
|
|
|
|
Assert.Null(locator.Locate(IPAddress.Parse("10.1.2.3")));
|
|
Assert.Null(locator.Locate(IPAddress.Loopback));
|
|
Assert.Null(locator.Locate(default));
|
|
}
|
|
|
|
[Fact]
|
|
public void The_real_databases_give_a_country_a_city_and_a_network()
|
|
{
|
|
var directory = Environment.GetEnvironmentVariable("PRIVAPUB_TEST_GEO");
|
|
Assert.SkipWhen(string.IsNullOrEmpty(directory), "set PRIVAPUB_TEST_GEO to a directory holding the DB-IP Lite city and ASN files");
|
|
using var locator = Locator(directory);
|
|
|
|
var fix = locator.Locate(IPAddress.Parse("1.1.1.1"));
|
|
|
|
Assert.NotNull(fix);
|
|
Assert.Equal(2, fix.Country.Length);
|
|
Assert.Equal(13335, fix.Asn);
|
|
Assert.Equal("Cloudflare", CdnCatalog.OfAsn(fix.Asn)?.Name);
|
|
Assert.NotNull(fix.AsnOrg);
|
|
Assert.NotNull(fix.Latitude);
|
|
Assert.Equal(Math.Round(fix.Latitude.Value, 1), fix.Latitude);
|
|
Assert.StartsWith("DB-IP Lite ", locator.Source);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_connection_remembers_its_address_as_ipv4()
|
|
{
|
|
var connected = new ConnectedAddresses();
|
|
|
|
connected.Remember("Social.Example", IPAddress.Parse("::ffff:203.0.113.7"));
|
|
|
|
Assert.Equal(IPAddress.Parse("203.0.113.7"), connected.Of("social.example"));
|
|
Assert.Null(connected.Of("other.example"));
|
|
Assert.Equal("Fastly", CdnCatalog.OfAsn(54113)?.Name);
|
|
Assert.Null(CdnCatalog.OfAsn(64496));
|
|
Assert.Null(CdnCatalog.OfAsn(default));
|
|
}
|
|
}
|
|
}
|