From 1fef5509d132ca6669dbbabfe85f079c2457460b Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Mon, 7 Apr 2025 15:24:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A7=A3=E6=9E=90IP=E5=8C=BA?= =?UTF-8?q?=E5=9F=9F=E4=BD=8D=E7=BD=AE=E7=9A=84=E5=BA=93=20=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E5=BC=82=E5=9C=B0=E7=99=BB=E5=BD=95=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Helper/IPCityHelper.cs | 42 +++++++++++++++++++ .../Management/DTO/UserLogViewModel.cs | 2 +- .../Service/Management/UserService.cs | 36 ++++++++-------- IRaCIS.Core.Application/TestService.cs | 26 +----------- .../BeforeSaveTrigger/UserLogTrigger.cs | 7 +++- 5 files changed, 69 insertions(+), 44 deletions(-) create mode 100644 IRaCIS.Core.Application/Helper/IPCityHelper.cs diff --git a/IRaCIS.Core.Application/Helper/IPCityHelper.cs b/IRaCIS.Core.Application/Helper/IPCityHelper.cs new file mode 100644 index 000000000..46a91a642 --- /dev/null +++ b/IRaCIS.Core.Application/Helper/IPCityHelper.cs @@ -0,0 +1,42 @@ +using IRaCIS.Core.Domain.Models; +using MaxMind.GeoIP2; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IRaCIS.Core.Application.Helper +{ + public static class IPCityHelper + { + + public static string GetCityResponse(string ip) + { + var path = Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources, "GeoLite2-City.mmdb"); + + try + { + using (var reader = new DatabaseReader(path)) + { + + var city = reader.City(ip); + + //Console.WriteLine(city.Country.IsoCode); // 'US' 'CN' + //Console.WriteLine(city.Country.Name); // 'United States' 'China' + ////Console.WriteLine(city.Country.Names["zh-CN"]); // '美国' + //Console.WriteLine(city.MostSpecificSubdivision.Name); // 'Minnesota' 'Hubei' + //Console.WriteLine(city.City.Name); // 'Minneapolis' 'WUHan' + + return $"{city.Country.Name} | {city.MostSpecificSubdivision.Name} | {city.City.Name}"; + + } + } + catch (Exception ex) + { + + return $"UN | UN | {ip}"; + } + } + } +} diff --git a/IRaCIS.Core.Application/Service/Management/DTO/UserLogViewModel.cs b/IRaCIS.Core.Application/Service/Management/DTO/UserLogViewModel.cs index 3e8c4f01e..a0784541e 100644 --- a/IRaCIS.Core.Application/Service/Management/DTO/UserLogViewModel.cs +++ b/IRaCIS.Core.Application/Service/Management/DTO/UserLogViewModel.cs @@ -48,7 +48,7 @@ namespace IRaCIS.Core.Application.ViewModel { public Guid? TrialId { get; set; } - public List OptTypeList { get; set; } + public List? OptTypeList { get; set; } public string? IP { get; set; } diff --git a/IRaCIS.Core.Application/Service/Management/UserService.cs b/IRaCIS.Core.Application/Service/Management/UserService.cs index cbca0e14a..4aedcfe5e 100644 --- a/IRaCIS.Core.Application/Service/Management/UserService.cs +++ b/IRaCIS.Core.Application/Service/Management/UserService.cs @@ -850,7 +850,7 @@ namespace IRaCIS.Core.Application.Service .WhereIf(inQuery.TargetIdentityUserId != null, t => t.TargetIdentityUserId == inQuery.TargetIdentityUserId) .WhereIf(inQuery.TrialId != null, t => t.ActionIdentityUser.UserTrialList.Any(c => c.TrialId == inQuery.TrialId) || t.TargetIdentityUser.UserTrialList.Any(c => c.TrialId == inQuery.TrialId)) .WhereIf(trialCreateTime != null, t => t.CreateTime >= trialCreateTime) - .WhereIf(inQuery.OptTypeList != null && inQuery.OptTypeList.Count>0, t => inQuery.OptTypeList.Contains(t.OptType)) + .WhereIf(inQuery.OptTypeList != null && inQuery.OptTypeList.Count > 0, t => inQuery.OptTypeList.Contains(t.OptType)) .WhereIf(inQuery.BeginDate != null, t => t.CreateTime >= inQuery.BeginDate) .WhereIf(inQuery.EndDate != null, t => t.CreateTime <= inQuery.EndDate) .WhereIf(inQuery.IsLoginUncommonly != null, t => t.IsLoginUncommonly == inQuery.IsLoginUncommonly) @@ -958,29 +958,31 @@ namespace IRaCIS.Core.Application.Service //账号在系统存在 if (isExistAccount || loginUser != null) { - var ipinfo = _searcher.Search(_userInfo.IP); + //var ipinfo = _searcher.Search(_userInfo.IP); - var iPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3)); + //var iPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3)); - string SplitAndConcatenate(string input) + //string SplitAndConcatenate(string input) + //{ + // string[] parts = input.Split('|'); + // return parts.Length >= 3 ? parts[0] + parts[1] : string.Join("", parts); + //} + + var iPRegion = IPCityHelper.GetCityResponse(_userInfo.IP); + + //设置本次登录的IP + await _identityUserRepository.BatchUpdateNoTrackingAsync(x => x.Id == existUserLoginInfo.Id, x => new IdentityUser() { - string[] parts = input.Split('|'); - return parts.Length >= 3 ? parts[0] + parts[1] : string.Join("", parts); - } + LastLoginIP = iPRegion, + LastLoginTime = DateTime.Now + + }); if (existUserLoginInfo.LastLoginIP != string.Empty) { - //设置上次登录的IP - await _identityUserRepository.BatchUpdateNoTrackingAsync(x => x.Id == existUserLoginInfo.Id, x => new IdentityUser() - { - LastLoginIP = iPRegion, - LastLoginTime = DateTime.Now - - }); - - // 与上一次区域不一致 - if (SplitAndConcatenate(existUserLoginInfo.LastLoginIP) != SplitAndConcatenate(iPRegion)) + //if (SplitAndConcatenate(existUserLoginInfo.LastLoginIP) != SplitAndConcatenate(iPRegion)) + if (existUserLoginInfo.LastLoginIP != iPRegion) { isLoginUncommonly = true; diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index 49176cf72..9053cb529 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -106,34 +106,12 @@ namespace IRaCIS.Core.Application.Service foreach (var ip in ipList) { - try - { - using (var reader = new DatabaseReader(path)) - { + var ipRegion = IPCityHelper.GetCityResponse(ip); - var city = reader.City(ip); + await _userLogRepository.BatchUpdateNoTrackingAsync(t => t.IP == ip, u => new UserLog() { IPRegion = ipRegion }); - Console.WriteLine(city.Country.IsoCode); // 'US' 'CN' - Console.WriteLine(city.Country.Name); // 'United States' 'China' - //Console.WriteLine(city.Country.Names["zh-CN"]); // '美国' - Console.WriteLine(city.MostSpecificSubdivision.Name); // 'Minnesota' 'Hubei' - Console.WriteLine(city.City.Name); // 'Minneapolis' 'WUHan' - - - Console.WriteLine("----------------"); - } - } - catch (Exception ex) - { - - Console.WriteLine($"{ip}在数据库无法解析 {ex.Message}"); // 44.9733 - } - } - - - return ResponseOutput.Ok(); } diff --git a/IRaCIS.Core.Application/Triggers/BeforeSaveTrigger/UserLogTrigger.cs b/IRaCIS.Core.Application/Triggers/BeforeSaveTrigger/UserLogTrigger.cs index ca7dd488a..17147b556 100644 --- a/IRaCIS.Core.Application/Triggers/BeforeSaveTrigger/UserLogTrigger.cs +++ b/IRaCIS.Core.Application/Triggers/BeforeSaveTrigger/UserLogTrigger.cs @@ -1,5 +1,7 @@ using EntityFrameworkCore.Triggered; using IP2Region.Net.Abstractions; +using IRaCIS.Core.Application.Helper; +using MaxMind.GeoIP2; namespace IRaCIS.Core.Application.Triggers { @@ -18,10 +20,11 @@ namespace IRaCIS.Core.Application.Triggers if (context.ChangeType == ChangeType.Added) { - var ipinfo = _searcher.Search(userLog.IP); + //var ipinfo = _searcher.Search(userLog.IP); - userLog.IPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3)); + //userLog.IPRegion = string.Join('|', ipinfo.Split('|').TakeLast(3)); + userLog.IPRegion = IPCityHelper.GetCityResponse(userLog.IP); } }