🐛 Refactor to fix GeoIP
This commit is contained in:
		@@ -2,6 +2,7 @@ using System.ComponentModel.DataAnnotations;
 | 
			
		||||
using System.ComponentModel.DataAnnotations.Schema;
 | 
			
		||||
using System.Text.Json.Serialization;
 | 
			
		||||
using DysonNetwork.Shared.Data;
 | 
			
		||||
using DysonNetwork.Shared.GeoIp;
 | 
			
		||||
using DysonNetwork.Shared.Proto;
 | 
			
		||||
using NodaTime.Serialization.Protobuf;
 | 
			
		||||
using Point = NetTopologySuite.Geometries.Point;
 | 
			
		||||
@@ -15,7 +16,7 @@ public class ActionLog : ModelBase
 | 
			
		||||
    [Column(TypeName = "jsonb")] public Dictionary<string, object> Meta { get; set; } = new();
 | 
			
		||||
    [MaxLength(512)] public string? UserAgent { get; set; }
 | 
			
		||||
    [MaxLength(128)] public string? IpAddress { get; set; }
 | 
			
		||||
    [JsonIgnore] public Point? Location { get; set; }
 | 
			
		||||
    [Column(TypeName = "jsonb")] public GeoPoint? Location { get; set; }
 | 
			
		||||
 | 
			
		||||
    public Guid AccountId { get; set; }
 | 
			
		||||
    public Account Account { get; set; } = null!;
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ using System.ComponentModel.DataAnnotations;
 | 
			
		||||
using System.ComponentModel.DataAnnotations.Schema;
 | 
			
		||||
using System.Text.Json.Serialization;
 | 
			
		||||
using DysonNetwork.Shared.Data;
 | 
			
		||||
using DysonNetwork.Shared.GeoIp;
 | 
			
		||||
using NodaTime;
 | 
			
		||||
using NodaTime.Serialization.Protobuf;
 | 
			
		||||
using Point = NetTopologySuite.Geometries.Point;
 | 
			
		||||
@@ -16,11 +17,11 @@ public class AuthSession : ModelBase
 | 
			
		||||
 | 
			
		||||
    public Guid AccountId { get; set; }
 | 
			
		||||
    [JsonIgnore] public Account.Account Account { get; set; } = null!;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    // When the challenge is null, indicates the session is for an API Key
 | 
			
		||||
    public Guid? ChallengeId { get; set; }
 | 
			
		||||
    public AuthChallenge? Challenge { get; set; } = null!;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    // Indicates the session is for an OIDC connection
 | 
			
		||||
    public Guid? AppId { get; set; }
 | 
			
		||||
 | 
			
		||||
@@ -69,7 +70,7 @@ public class AuthChallenge : ModelBase
 | 
			
		||||
    [MaxLength(128)] public string? IpAddress { get; set; }
 | 
			
		||||
    [MaxLength(512)] public string? UserAgent { get; set; }
 | 
			
		||||
    [MaxLength(1024)] public string? Nonce { get; set; }
 | 
			
		||||
    [JsonIgnore] public Point? Location { get; set; }
 | 
			
		||||
    [Column(TypeName = "jsonb")] public GeoPoint? Location { get; set; }
 | 
			
		||||
 | 
			
		||||
    public Guid AccountId { get; set; }
 | 
			
		||||
    [JsonIgnore] public Account.Account Account { get; set; } = null!;
 | 
			
		||||
@@ -129,4 +130,4 @@ public class AuthClientWithChallenge : AuthClient
 | 
			
		||||
            AccountId = client.AccountId,
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										2027
									
								
								DysonNetwork.Pass/Migrations/20250907065433_RefactorGeoIpPoint.Designer.cs
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										2027
									
								
								DysonNetwork.Pass/Migrations/20250907065433_RefactorGeoIpPoint.Designer.cs
									
									
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -0,0 +1,63 @@
 | 
			
		||||
using DysonNetwork.Shared.GeoIp;
 | 
			
		||||
using Microsoft.EntityFrameworkCore.Migrations;
 | 
			
		||||
using NetTopologySuite.Geometries;
 | 
			
		||||
 | 
			
		||||
#nullable disable
 | 
			
		||||
 | 
			
		||||
namespace DysonNetwork.Pass.Migrations
 | 
			
		||||
{
 | 
			
		||||
    /// <inheritdoc />
 | 
			
		||||
    public partial class RefactorGeoIpPoint : Migration
 | 
			
		||||
    {
 | 
			
		||||
        /// <inheritdoc />
 | 
			
		||||
        protected override void Up(MigrationBuilder migrationBuilder)
 | 
			
		||||
        {
 | 
			
		||||
            migrationBuilder.Sql("UPDATE auth_challenges SET location = NULL;");
 | 
			
		||||
            migrationBuilder.Sql("UPDATE action_logs SET location = NULL;");
 | 
			
		||||
 | 
			
		||||
            migrationBuilder.DropColumn(
 | 
			
		||||
                name: "location",
 | 
			
		||||
                table: "auth_challenges");
 | 
			
		||||
 | 
			
		||||
            migrationBuilder.AddColumn<GeoPoint>(
 | 
			
		||||
                name: "location",
 | 
			
		||||
                table: "auth_challenges",
 | 
			
		||||
                type: "jsonb",
 | 
			
		||||
                nullable: true);
 | 
			
		||||
 | 
			
		||||
            migrationBuilder.DropColumn(
 | 
			
		||||
                name: "location",
 | 
			
		||||
                table: "action_logs");
 | 
			
		||||
 | 
			
		||||
            migrationBuilder.AddColumn<GeoPoint>(
 | 
			
		||||
                name: "location",
 | 
			
		||||
                table: "action_logs",
 | 
			
		||||
                type: "jsonb",
 | 
			
		||||
                nullable: true);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <inheritdoc />
 | 
			
		||||
        protected override void Down(MigrationBuilder migrationBuilder)
 | 
			
		||||
        {
 | 
			
		||||
            migrationBuilder.DropColumn(
 | 
			
		||||
                name: "location",
 | 
			
		||||
                table: "auth_challenges");
 | 
			
		||||
 | 
			
		||||
            migrationBuilder.AddColumn<Point>(
 | 
			
		||||
                name: "location",
 | 
			
		||||
                table: "auth_challenges",
 | 
			
		||||
                type: "geometry",
 | 
			
		||||
                nullable: true);
 | 
			
		||||
 | 
			
		||||
            migrationBuilder.DropColumn(
 | 
			
		||||
                name: "location",
 | 
			
		||||
                table: "action_logs");
 | 
			
		||||
 | 
			
		||||
            migrationBuilder.AddColumn<Point>(
 | 
			
		||||
                name: "location",
 | 
			
		||||
                table: "action_logs",
 | 
			
		||||
                type: "geometry",
 | 
			
		||||
                nullable: true);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -6,10 +6,10 @@ using DysonNetwork.Pass;
 | 
			
		||||
using DysonNetwork.Pass.Account;
 | 
			
		||||
using DysonNetwork.Pass.Wallet;
 | 
			
		||||
using DysonNetwork.Shared.Data;
 | 
			
		||||
using DysonNetwork.Shared.GeoIp;
 | 
			
		||||
using Microsoft.EntityFrameworkCore;
 | 
			
		||||
using Microsoft.EntityFrameworkCore.Infrastructure;
 | 
			
		||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
 | 
			
		||||
using NetTopologySuite.Geometries;
 | 
			
		||||
using NodaTime;
 | 
			
		||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
 | 
			
		||||
 | 
			
		||||
@@ -525,8 +525,8 @@ namespace DysonNetwork.Pass.Migrations
 | 
			
		||||
                        .HasColumnType("character varying(128)")
 | 
			
		||||
                        .HasColumnName("ip_address");
 | 
			
		||||
 | 
			
		||||
                    b.Property<Point>("Location")
 | 
			
		||||
                        .HasColumnType("geometry")
 | 
			
		||||
                    b.Property<GeoPoint>("Location")
 | 
			
		||||
                        .HasColumnType("jsonb")
 | 
			
		||||
                        .HasColumnName("location");
 | 
			
		||||
 | 
			
		||||
                    b.Property<Dictionary<string, object>>("Meta")
 | 
			
		||||
@@ -901,8 +901,8 @@ namespace DysonNetwork.Pass.Migrations
 | 
			
		||||
                        .HasColumnType("character varying(128)")
 | 
			
		||||
                        .HasColumnName("ip_address");
 | 
			
		||||
 | 
			
		||||
                    b.Property<Point>("Location")
 | 
			
		||||
                        .HasColumnType("geometry")
 | 
			
		||||
                    b.Property<GeoPoint>("Location")
 | 
			
		||||
                        .HasColumnType("jsonb")
 | 
			
		||||
                        .HasColumnName("location");
 | 
			
		||||
 | 
			
		||||
                    b.Property<string>("Nonce")
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,6 @@
 | 
			
		||||
using MaxMind.GeoIP2;
 | 
			
		||||
using Microsoft.Extensions.Options;
 | 
			
		||||
using NetTopologySuite.Geometries;
 | 
			
		||||
using Point = NetTopologySuite.Geometries.Point;
 | 
			
		||||
 | 
			
		||||
namespace DysonNetwork.Shared.GeoIp;
 | 
			
		||||
 | 
			
		||||
@@ -15,7 +14,7 @@ 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)
 | 
			
		||||
    public GeoPoint? GetPointFromIp(string? ipAddress)
 | 
			
		||||
    {
 | 
			
		||||
        if (string.IsNullOrEmpty(ipAddress))
 | 
			
		||||
            return null;
 | 
			
		||||
@@ -28,9 +27,14 @@ public class GeoIpService(IOptions<GeoIpOptions> options)
 | 
			
		||||
            if (city?.Location is not { HasCoordinates: true })
 | 
			
		||||
                return null;
 | 
			
		||||
 | 
			
		||||
            return _geometryFactory.CreatePoint(new Coordinate(
 | 
			
		||||
                city.Location.Longitude ?? 0,
 | 
			
		||||
                city.Location.Latitude ?? 0));
 | 
			
		||||
            return new GeoPoint()
 | 
			
		||||
            {
 | 
			
		||||
                City = city.City.Name,
 | 
			
		||||
                Country = city.Country.Name,
 | 
			
		||||
                CountryCode = city.Country.IsoCode,
 | 
			
		||||
                Longitude = city.Location.Longitude,
 | 
			
		||||
                Latitude = city.Location.Latitude,
 | 
			
		||||
            };
 | 
			
		||||
        }
 | 
			
		||||
        catch (Exception)
 | 
			
		||||
        {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										10
									
								
								DysonNetwork.Shared/GeoIp/GeoPoint.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								DysonNetwork.Shared/GeoIp/GeoPoint.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
namespace DysonNetwork.Shared.GeoIp;
 | 
			
		||||
 | 
			
		||||
public class GeoPoint
 | 
			
		||||
{
 | 
			
		||||
    public double? Latitude { get; set; }
 | 
			
		||||
    public double? Longitude { get; set; }
 | 
			
		||||
    public string? CountryCode { get; set; }
 | 
			
		||||
    public string? Country { get; set; }
 | 
			
		||||
    public string? City { get; set; }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user