diff --git a/src/Masuit.MyBlogs.Core/Common/CommonHelper.cs b/src/Masuit.MyBlogs.Core/Common/CommonHelper.cs index 14f4a469..bfe35e4b 100644 --- a/src/Masuit.MyBlogs.Core/Common/CommonHelper.cs +++ b/src/Masuit.MyBlogs.Core/Common/CommonHelper.cs @@ -204,7 +204,7 @@ public static IPLocation GetIPLocation(this IPAddress ip) parts[3] = parts[3] != "0" ? parts[3] : cityName; return new IPLocation(parts[0], parts[2], parts[3], network?.Trim('/'), asn.AutonomousSystemNumber) { - Address2 = countryName + cityName, + Address2 = new IPLocation.CountryCity(countryName, cityName), Coodinate = city.Location }; } @@ -429,7 +429,7 @@ public IPLocation(string country, string province, string city, string isp, long public string Address => new[] { Country, Province, City }.Where(s => !string.IsNullOrEmpty(s)).Distinct().Join(""); - public string Address2 { get; set; } + public CountryCity Address2 { get; set; } = new("", ""); public string Network => ASN.HasValue ? ISP + "(AS" + ASN + ")" : ISP; @@ -449,7 +449,7 @@ public override string ToString() network = "未知网络"; } - return new[] { address, Address2, network }.Where(s => !string.IsNullOrEmpty(s)).Distinct().Join("|"); + return new[] { address, Address2?.ToString(), network }.Where(s => !string.IsNullOrEmpty(s)).Distinct().Join("|"); } public static implicit operator string(IPLocation entry) @@ -473,5 +473,24 @@ public bool Contains(params string[] s) { return ToString().Contains(s); } + + public record CountryCity(string Country, string City) + { + public void Deconstruct(out string country, out string city) + { + country = Country; + city = City; + } + + public static implicit operator string(CountryCity entry) + { + return entry.ToString(); + } + + public override string ToString() + { + return Country + City; + } + } } } diff --git a/src/Masuit.MyBlogs.Core/Extensions/Firewall/IRequestLogger.cs b/src/Masuit.MyBlogs.Core/Extensions/Firewall/IRequestLogger.cs index 217d92d5..2ecd212c 100644 --- a/src/Masuit.MyBlogs.Core/Extensions/Firewall/IRequestLogger.cs +++ b/src/Masuit.MyBlogs.Core/Extensions/Firewall/IRequestLogger.cs @@ -1,10 +1,11 @@ -using System.Collections.Concurrent; -using System.Diagnostics; -using Masuit.MyBlogs.Core.Common; +using Masuit.MyBlogs.Core.Common; using Masuit.MyBlogs.Core.Infrastructure; using Masuit.MyBlogs.Core.Models.Entity; +using Masuit.Tools; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection.Extensions; +using System.Collections.Concurrent; +using System.Diagnostics; namespace Masuit.MyBlogs.Core.Extensions.Firewall; @@ -80,9 +81,11 @@ public void Process() while (Queue.TryDequeue(out var result)) { - var (location, network, info) = result.IP.GetIPLocation(); + var location = result.IP.GetIPLocation(); result.Location = location; - result.Network = network; + result.Country = new[] { location.Country, location.Address2.Country }.Where(s => !string.IsNullOrEmpty(s)).Distinct().Join("|"); + result.City = new[] { location.City, location.Address2.City }.Where(s => !string.IsNullOrEmpty(s)).Distinct().Join("|"); + result.Network = location.Network; _dataContext.Add(result); } diff --git a/src/Masuit.MyBlogs.Core/Extensions/TranslateMiddleware.cs b/src/Masuit.MyBlogs.Core/Extensions/TranslateMiddleware.cs index fa7b85f5..04add026 100644 --- a/src/Masuit.MyBlogs.Core/Extensions/TranslateMiddleware.cs +++ b/src/Masuit.MyBlogs.Core/Extensions/TranslateMiddleware.cs @@ -56,17 +56,19 @@ private async Task Traditional(HttpContext context) { //设置stream存放ResponseBody var responseOriginalBody = context.Response.Body; - var memStream = new MemoryStream(); + using var memStream = new MemoryStream(); context.Response.Body = memStream; // 执行其他中间件 await _next(context); //处理执行其他中间件后的ResponseBody - memStream.Position = 0; - var responseReader = new StreamReader(memStream, Encoding.UTF8); + memStream.Seek(0, SeekOrigin.Begin); + using var responseReader = new StreamReader(memStream, Encoding.UTF8); var responseBody = await responseReader.ReadToEndAsync(); - memStream = new MemoryStream(Encoding.UTF8.GetBytes(ChineseConverter.Convert(responseBody, ChineseConversionDirection.SimplifiedToTraditional))); + memStream.Seek(0, SeekOrigin.Begin); + await memStream.WriteAsync(Encoding.UTF8.GetBytes(ChineseConverter.Convert(responseBody, ChineseConversionDirection.SimplifiedToTraditional)).AsMemory()); + memStream.Seek(0, SeekOrigin.Begin); await memStream.CopyToAsync(responseOriginalBody); context.Response.Body = responseOriginalBody; } diff --git a/src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj b/src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj index 0f5f87c5..91dfe1c1 100644 --- a/src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj +++ b/src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj @@ -39,15 +39,15 @@ - - + + - + @@ -60,7 +60,7 @@ - + diff --git a/src/Masuit.MyBlogs.Core/Models/Entity/RequestLogDetail.cs b/src/Masuit.MyBlogs.Core/Models/Entity/RequestLogDetail.cs index ca15e4fa..e4c5a56c 100644 --- a/src/Masuit.MyBlogs.Core/Models/Entity/RequestLogDetail.cs +++ b/src/Masuit.MyBlogs.Core/Models/Entity/RequestLogDetail.cs @@ -16,24 +16,57 @@ public RequestLogDetail() [StringLength(32)] public string Id { get; set; } + /// + /// 请求时间 + /// [Column(TypeName = "timestamp")] public DateTime Time { get; set; } + /// + /// 用户代理 + /// [StringLength(1024), Unicode] public string UserAgent { get; set; } + /// + /// 请求路径 + /// [StringLength(4096), Unicode] public string RequestUrl { get; set; } + /// + /// 客户端IP + /// [StringLength(128), Unicode] public string IP { get; set; } + /// + /// 客户端完整地理信息 + /// [StringLength(256), Unicode] public string Location { get; set; } + /// + /// 国家 + /// + [StringLength(256), Unicode] + public string Country { get; set; } + + /// + /// 城市 + /// + [StringLength(256), Unicode] + public string City { get; set; } + + /// + /// 运营商网络 + /// [StringLength(256)] public string Network { get; set; } + /// + /// 跟踪id + /// [StringLength(128)] public string TraceId { get; set; } } diff --git a/src/Masuit.MyBlogs.Core/MyEFCacheManagerCoreProvider.cs b/src/Masuit.MyBlogs.Core/MyEFCacheManagerCoreProvider.cs deleted file mode 100644 index b5013fcc..00000000 --- a/src/Masuit.MyBlogs.Core/MyEFCacheManagerCoreProvider.cs +++ /dev/null @@ -1,125 +0,0 @@ -using CacheManager.Core; -using EFCoreSecondLevelCacheInterceptor; - -namespace Masuit.MyBlogs.Core -{ - public class MyEFCacheManagerCoreProvider : IEFCacheServiceProvider - { - private readonly IReaderWriterLockProvider _readerWriterLockProvider; - private readonly ICacheManager> _dependenciesCacheManager; - private readonly ICacheManager _valuesCacheManager; - private readonly string _keyPrefix = "EFCache:"; - - /// - /// Using IMemoryCache as a cache service. - /// - public MyEFCacheManagerCoreProvider(ICacheManager> dependenciesCacheManager, ICacheManager valuesCacheManager, IReaderWriterLockProvider readerWriterLockProvider) - { - _readerWriterLockProvider = readerWriterLockProvider; - _dependenciesCacheManager = dependenciesCacheManager ?? throw new ArgumentNullException(nameof(dependenciesCacheManager), "Please register the `ICacheManager`."); - _valuesCacheManager = valuesCacheManager ?? throw new ArgumentNullException(nameof(valuesCacheManager), "Please register the `ICacheManager`."); - _dependenciesCacheManager.OnRemoveByHandle += (sender, args) => ClearAllCachedEntries(); - } - - /// - /// Adds a new item to the cache. - /// - /// key - /// value - /// Defines the expiration mode of the cache item. - public void InsertValue(EFCacheKey cacheKey, EFCachedData value, EFCachePolicy cachePolicy) - { - _readerWriterLockProvider.TryWriteLocked(() => - { - if (value == null) - { - value = new EFCachedData - { - IsNull = true - }; - } - - var keyHash = cacheKey.KeyHash; - - foreach (var rootCacheKey in cacheKey.CacheDependencies) - { - _dependenciesCacheManager.AddOrUpdate(_keyPrefix + rootCacheKey, new HashSet(StringComparer.OrdinalIgnoreCase) - { - keyHash - }, updateValue: set => - { - set.Add(keyHash); - return set; - }); - } - - if (cachePolicy == null) - { - _valuesCacheManager.Add(_keyPrefix + keyHash, value); - } - else - { - _valuesCacheManager.Add(new CacheItem(_keyPrefix + keyHash, value, cachePolicy.CacheExpirationMode == CacheExpirationMode.Absolute ? ExpirationMode.Absolute : ExpirationMode.Sliding, cachePolicy.CacheTimeout)); - } - }); - } - - /// - /// Removes the cached entries added by this library. - /// - public void ClearAllCachedEntries() - { - _readerWriterLockProvider.TryWriteLocked(() => - { - _valuesCacheManager.Clear(); - _dependenciesCacheManager.Clear(); - }); - } - - /// - /// Gets a cached entry by key. - /// - /// key to find - /// cached value - /// Defines the expiration mode of the cache item. - public EFCachedData GetValue(EFCacheKey cacheKey, EFCachePolicy cachePolicy) - { - return _readerWriterLockProvider.TryReadLocked(() => _valuesCacheManager.Get(_keyPrefix + cacheKey.KeyHash)); - } - - /// - /// Invalidates all of the cache entries which are dependent on any of the specified root keys. - /// - /// Stores information of the computed key of the input LINQ query. - public void InvalidateCacheDependencies(EFCacheKey cacheKey) - { - _readerWriterLockProvider.TryWriteLocked(() => - { - foreach (var rootCacheKey in cacheKey.CacheDependencies) - { - if (string.IsNullOrWhiteSpace(rootCacheKey)) - { - continue; - } - - clearDependencyValues(rootCacheKey); - _dependenciesCacheManager.Remove(_keyPrefix + rootCacheKey); - } - }); - } - - private void clearDependencyValues(string rootCacheKey) - { - var dependencyKeys = _dependenciesCacheManager.Get(_keyPrefix + rootCacheKey); - if (dependencyKeys == null) - { - return; - } - - foreach (var dependencyKey in dependencyKeys) - { - _valuesCacheManager.Remove(_keyPrefix + dependencyKey); - } - } - } -} diff --git a/src/Masuit.MyBlogs.Core/Startup.cs b/src/Masuit.MyBlogs.Core/Startup.cs index 66f5b9da..f9a71299 100644 --- a/src/Masuit.MyBlogs.Core/Startup.cs +++ b/src/Masuit.MyBlogs.Core/Startup.cs @@ -83,7 +83,7 @@ void BindConfig() /// public void ConfigureServices(IServiceCollection services) { - services.AddEFSecondLevelCache(options => options.UseCustomCacheProvider(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(5)).DisableLogging(true)); + services.AddEFSecondLevelCache(options => options.UseCustomCacheProvider(CacheExpirationMode.Absolute, TimeSpan.FromMinutes(5)).DisableLogging(true).UseCacheKeyPrefix("EFCore:")); services.AddDbContext((serviceProvider, opt) => opt.UseNpgsql(AppConfig.ConnString, builder => builder.EnableRetryOnFailure(10)).AddInterceptors(serviceProvider.GetRequiredService()).EnableSensitiveDataLogging()); //配置数据库 services.AddDbContext(opt => opt.UseNpgsql(AppConfig.ConnString)); //配置数据库 services.ConfigureOptions();