Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions GenOnlineService/Controllers/WebSocket/WebSocketController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,23 @@ public WebSocketController(LobbyManager lobbyManager, IDbContextFactory<AppDbCon
AllowOutOfOrderMetadataProperties = true
};

// GeoIP DB is designed to be reused; opening per request is expensive
private static readonly DatabaseReader GeoIpReader = new("data/GeoLite2-City.mmdb");
// GeoIP DB is designed to be reused; opening per request is expensive.
// It is gitignored and absent in fresh clones, so a failure to open must
// fall back to the lookup defaults rather than fail static init.
private static readonly DatabaseReader? GeoIpReader = TryOpenGeoIp();

private static DatabaseReader? TryOpenGeoIp()
{
try
{
return new DatabaseReader("data/GeoLite2-City.mmdb");
}
catch (Exception ex)
{
Console.WriteLine($"[WS] GeoIP DB unavailable ({ex.Message}); using fallback coordinates");
return null;
}
}

private struct WSMessageEnvelope
{
Expand Down Expand Up @@ -86,19 +101,22 @@ public async Task Get([FromHeader(Name = "is-reconnect")] bool bIsReconnect)

try
{
var city = GeoIpReader.City(ipAddress);
if (GeoIpReader != null)
{
var city = GeoIpReader.City(ipAddress);

ipContinent = city.Continent.Code;
ipCountry = city.Country.IsoCode;
ipContinent = city.Continent.Code;
ipCountry = city.Country.IsoCode;

if (city.Location.Longitude != null)
{
dLongitude = (double)city.Location.Longitude;
}
if (city.Location.Longitude != null)
{
dLongitude = (double)city.Location.Longitude;
}

if (city.Location.Latitude != null)
{
dLatitude = (double)city.Location.Latitude;
if (city.Location.Latitude != null)
{
dLatitude = (double)city.Location.Latitude;
}
}
}
catch
Expand Down