.github
.idx
DysonNetwork.Sphere
Account
Activity
Auth
Chat
Connection
Handlers
GeoIpService.cs
IWebSocketPacketHandler.cs
WebSocketController.cs
WebSocketPacket.cs
WebSocketService.cs
Developer
Email
Localization
Migrations
Pages
Permission
Post
Properties
Publisher
Realm
Resources
Sticker
Storage
Wallet
wwwroot
.DS_Store
.gitignore
AppDatabase.cs
Dockerfile
DysonNetwork.Sphere.csproj
DysonNetwork.Sphere.csproj.DotSettings.user
DysonNetwork.Sphere.http
Program.cs
appsettings.json
package.json
postcss.config.js
tailwind.config.js
.dockerignore
.gitignore
DysonNetwork.sln
DysonNetwork.sln.DotSettings.user
compose.yaml
56 lines
1.5 KiB
C#
56 lines
1.5 KiB
C#
using MaxMind.GeoIP2;
|
|
using NetTopologySuite.Geometries;
|
|
using Microsoft.Extensions.Options;
|
|
using Point = NetTopologySuite.Geometries.Point;
|
|
|
|
namespace DysonNetwork.Sphere.Connection;
|
|
|
|
public class GeoIpOptions
|
|
{
|
|
public string DatabasePath { get; set; } = null!;
|
|
}
|
|
|
|
public class GeoIpService(IOptions<GeoIpOptions> options)
|
|
{
|
|
private readonly string _databasePath = options.Value.DatabasePath;
|
|
private readonly GeometryFactory _geometryFactory = new(new PrecisionModel(), 4326); // 4326 is the SRID for WGS84
|
|
|
|
public Point? GetPointFromIp(string? ipAddress)
|
|
{
|
|
if (string.IsNullOrEmpty(ipAddress))
|
|
return null;
|
|
|
|
try
|
|
{
|
|
using var reader = new DatabaseReader(_databasePath);
|
|
var city = reader.City(ipAddress);
|
|
|
|
if (city?.Location == null || !city.Location.HasCoordinates)
|
|
return null;
|
|
|
|
return _geometryFactory.CreatePoint(new Coordinate(
|
|
city.Location.Longitude ?? 0,
|
|
city.Location.Latitude ?? 0));
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public MaxMind.GeoIP2.Responses.CityResponse? GetFromIp(string? ipAddress)
|
|
{
|
|
if (string.IsNullOrEmpty(ipAddress))
|
|
return null;
|
|
|
|
try
|
|
{
|
|
using var reader = new DatabaseReader(_databasePath);
|
|
return reader.City(ipAddress);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
} |