Where a server is, on the public instance API
/api/privapub/v1/instances/:host gains `geo`, the public projection of a server's place already decided for public server locations (PublicGeo.Project): city, coordinates and network for servers reporting at least 10 users and not behind a CDN, the country otherwise, the CDN's name for a CDN-fronted one, with DB-IP's attribution. `?host[]=` answers up to 40 servers at once, and this server describes itself: its host's address located once a day (SelfLocation), or Statistics:Geo:Self when the owner sets it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LsXgEaXee4GCU1hwYgPJXw
This commit is contained in:
1 parent
2cd75176a4
commit
d9fb5c582f
8 files changed
+429
-15
No files matched your search
@@ -0,0 +1,103 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
using PrivaPub.Infrastructure.Geo;
|
||||
using PrivaPub.Infrastructure.Statistics;
|
||||
using PrivaPub.Models;
|
||||
using PrivaPub.Models.Jobs;
|
||||
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace PrivaPub.Domain.Statistics
|
||||
{
|
||||
public interface ISelfLocation
|
||||
{
|
||||
//where this server is, or default when it cannot tell; Configured when the owner set it (Statistics:Geo:Self)
|
||||
Task<(InstanceGeo Geo, bool Configured)> Get(CancellationToken token);
|
||||
}
|
||||
|
||||
// This server's own place, for the instance API (the client's globe draws every server from it). The owner may set it,
|
||||
// for a server behind a proxy or a CDN; otherwise the server's own host name is resolved and located like any other
|
||||
// server's, once a day. Never the address of a request.
|
||||
public class SelfLocation : ISelfLocation
|
||||
{
|
||||
static readonly TimeSpan Lifetime = TimeSpan.FromDays(1);
|
||||
|
||||
readonly IGeoLocator _geo;
|
||||
readonly IOptionsMonitor<StatisticsOptions> _options;
|
||||
readonly IOptionsMonitor<AppConfiguration> _app;
|
||||
readonly ILogger<SelfLocation> _logger;
|
||||
readonly SemaphoreSlim _gate = new(1, 1);
|
||||
|
||||
InstanceGeo _located;
|
||||
DateTime _locatedAt = DateTime.MinValue;
|
||||
|
||||
public SelfLocation(IGeoLocator geo, IOptionsMonitor<StatisticsOptions> options, IOptionsMonitor<AppConfiguration> app, ILogger<SelfLocation> logger)
|
||||
{
|
||||
_geo = geo;
|
||||
_options = options;
|
||||
_app = app;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<(InstanceGeo Geo, bool Configured)> Get(CancellationToken token)
|
||||
{
|
||||
if (_options.CurrentValue.Geo.Self is { Latitude: { } latitude, Longitude: { } longitude } self)
|
||||
return (new InstanceGeo
|
||||
{
|
||||
Country = self.Country,
|
||||
City = self.City,
|
||||
Latitude = Math.Round(latitude, 1),
|
||||
Longitude = Math.Round(longitude, 1),
|
||||
Source = "configured"
|
||||
}, true);
|
||||
|
||||
if (DateTime.UtcNow - _locatedAt < Lifetime)
|
||||
return (_located, false);
|
||||
await _gate.WaitAsync(token);
|
||||
try
|
||||
{
|
||||
if (DateTime.UtcNow - _locatedAt >= Lifetime)
|
||||
{
|
||||
_located = await Locate(token);
|
||||
_locatedAt = DateTime.UtcNow;
|
||||
}
|
||||
return (_located, false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_gate.Release();
|
||||
}
|
||||
}
|
||||
|
||||
async Task<InstanceGeo> Locate(CancellationToken token)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!Uri.TryCreate(_app.CurrentValue.BackendBaseAddress, UriKind.Absolute, out var self))
|
||||
return default;
|
||||
var addresses = await Dns.GetHostAddressesAsync(self.Host, token);
|
||||
var address = addresses.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork) ?? addresses.FirstOrDefault();
|
||||
var fix = address == default ? default : _geo.Locate(address);
|
||||
return fix == default ? default : new InstanceGeo
|
||||
{
|
||||
Country = fix.Country,
|
||||
City = fix.City,
|
||||
Latitude = fix.Latitude,
|
||||
Longitude = fix.Longitude,
|
||||
Asn = fix.Asn,
|
||||
AsnOrg = fix.AsnOrg,
|
||||
Cdn = CdnNetworks.Of(fix.Asn),
|
||||
Ipv6 = address.AddressFamily == AddressFamily.InterNetworkV6,
|
||||
Source = _geo.Source,
|
||||
LocatedAt = DateTime.UtcNow
|
||||
};
|
||||
}
|
||||
catch (Exception ex) when (ex is SocketException or ArgumentException)
|
||||
{
|
||||
_logger.LogInformation("{Service}: this server's host does not resolve", nameof(SelfLocation));
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user