Skip to content

Commit 0c1bb54

Browse files
committed
拼写错误
1 parent b076698 commit 0c1bb54

File tree

11 files changed

+60
-52
lines changed

11 files changed

+60
-52
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,5 @@ Test
275275
/src/Masuit.MyBlogs.Core/OneDrive.db
276276
/src/Masuit.MyBlogs.Core/App_Data/OneDrive.template.db
277277
/src/Masuit.MyBlogs.Core/App_Data/OneDrive.db
278-
/src/Masuit.MyBlogs.Core/.config
278+
/src/Masuit.MyBlogs.Core/.config
279+
static

src/Masuit.MyBlogs.Core/Controllers/PassportController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public sealed class PassportController : Controller
2525
/// </summary>
2626
public IUserInfoService UserInfoService { get; set; }
2727

28-
public IFirewallRepoter FirewallRepoter { get; set; }
28+
public IFirewallReporter FirewallReporter { get; set; }
2929

3030
public IMapper Mapper { get; set; }
3131

@@ -153,7 +153,7 @@ public ActionResult Login([FromServices] IRedisClient cacheManager, string usern
153153
var times = cacheManager.Incr("LoginError:" + ClientIP);
154154
if (times > 30)
155155
{
156-
FirewallRepoter.ReportAsync(IPAddress.Parse(ClientIP)).ContinueWith(_ => LogManager.Info($"多次登录用户名或密码错误,疑似爆破行为,已上报IP{ClientIP}至:" + FirewallRepoter.ReporterName));
156+
FirewallReporter.ReportAsync(IPAddress.Parse(ClientIP)).ContinueWith(_ => LogManager.Info($"多次登录用户名或密码错误,疑似爆破行为,已上报IP{ClientIP}至:" + FirewallReporter.ReporterName));
157157
}
158158

159159
return ResultData(null, false, "用户名或密码错误");

src/Masuit.MyBlogs.Core/Controllers/SystemController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,10 @@ public async Task<ActionResult> AddToWhiteList([FromBodyOrDefault] string ip)
386386
/// <summary>
387387
/// 将IP添加到黑名单
388388
/// </summary>
389-
/// <param name="firewallRepoter"></param>
389+
/// <param name="firewallReporter"></param>
390390
/// <param name="ip"></param>
391391
/// <returns></returns>
392-
public async Task<ActionResult> AddToBlackList([FromServices] IFirewallRepoter firewallRepoter, [FromBodyOrDefault] string ip)
392+
public async Task<ActionResult> AddToBlackList([FromServices] IFirewallReporter firewallReporter, [FromBodyOrDefault] string ip)
393393
{
394394
if (!ip.MatchInetAddress())
395395
{
@@ -401,9 +401,9 @@ public async Task<ActionResult> AddToBlackList([FromServices] IFirewallRepoter f
401401
await new FileInfo(Path.Combine(basedir, "App_Data", "denyip.txt")).ShareReadWrite().WriteAllTextAsync(CommonHelper.DenyIP, Encoding.UTF8);
402402
CommonHelper.IPWhiteList.Remove(ip);
403403
await new FileInfo(Path.Combine(basedir, "App_Data", "whitelist.txt")).ShareReadWrite().WriteAllTextAsync(string.Join(",", CommonHelper.IPWhiteList.Distinct()), Encoding.UTF8);
404-
await firewallRepoter.ReportAsync(IPAddress.Parse(ip));
404+
await firewallReporter.ReportAsync(IPAddress.Parse(ip));
405405
return ResultData(null);
406406
}
407407

408408
#endregion 网站防火墙
409-
}
409+
}

src/Masuit.MyBlogs.Core/Extensions/Firewall/CloudflareRepoter.cs renamed to src/Masuit.MyBlogs.Core/Extensions/Firewall/CloudflareReporter.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
using Polly;
33
using System.Net;
44
using System.Net.Sockets;
5+
using Masuit.Tools.Core;
56

67
namespace Masuit.MyBlogs.Core.Extensions.Firewall;
78

8-
public sealed class CloudflareRepoter(HttpClient httpClient, IConfiguration configuration, DataContext dataContext) : IFirewallRepoter
9+
public sealed class CloudflareReporter(HttpClient httpClient, IConfiguration configuration, DataContext dataContext) : IFirewallReporter
910
{
1011
public string ReporterName { get; set; } = "cloudflare";
1112

@@ -14,12 +15,12 @@ public void Report(IPAddress ip)
1415
ReportAsync(ip).Wait();
1516
}
1617

17-
public Task<bool> ReportAsync(IPAddress ip)
18+
public async Task<bool> ReportAsync(IPAddress ip)
1819
{
1920
var s = ip.ToString();
20-
if (dataContext.IpReportLogs.Any(e => e.IP == s))
21+
if (await dataContext.IpReportLogs.AnyWithNoLockAsync(e => e.IP == s))
2122
{
22-
return Task.FromResult(false);
23+
return false;
2324
}
2425

2526
var scope = configuration["FirewallService:Cloudflare:Scope"];
@@ -30,7 +31,7 @@ public Task<bool> ReportAsync(IPAddress ip)
3031
return Task.FromResult(false);
3132
});
3233
var retryPolicy = Policy.HandleInner<HttpRequestException>().RetryAsync(3);
33-
return fallbackPolicy.WrapAsync(retryPolicy).ExecuteAsync(async () =>
34+
return await fallbackPolicy.WrapAsync(retryPolicy).ExecuteAsync(async () =>
3435
{
3536
await httpClient.PostAsJsonAsync($"https://api.cloudflare.com/client/v4/{scope}/{zoneid}/firewall/access_rules/rules", new
3637
{
@@ -61,4 +62,4 @@ public Task<bool> ReportAsync(IPAddress ip)
6162
return true;
6263
});
6364
}
64-
}
65+
}

src/Masuit.MyBlogs.Core/Extensions/Firewall/DefaultFirewallRepoter.cs renamed to src/Masuit.MyBlogs.Core/Extensions/Firewall/DefaultFirewallReporter.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using System.Net;
2+
using Masuit.Tools.Core;
23

34
namespace Masuit.MyBlogs.Core.Extensions.Firewall;
45

5-
public sealed class DefaultFirewallRepoter(DataContext dataContext) : IFirewallRepoter
6+
public sealed class DefaultFirewallReporter(DataContext dataContext) : IFirewallReporter
67
{
78
public string ReporterName { get; set; }
89

@@ -13,7 +14,7 @@ public sealed class DefaultFirewallRepoter(DataContext dataContext) : IFirewallR
1314
public void Report(IPAddress ip)
1415
{
1516
var s = ip.ToString();
16-
if (dataContext.IpReportLogs.Any(e => e.IP == s))
17+
if (dataContext.IpReportLogs.AnyWithNoLock(e => e.IP == s))
1718
{
1819
return;
1920
}
@@ -33,7 +34,7 @@ public void Report(IPAddress ip)
3334
public async Task<bool> ReportAsync(IPAddress ip)
3435
{
3536
var s = ip.ToString();
36-
if (dataContext.IpReportLogs.Any(e => e.IP == s))
37+
if (await dataContext.IpReportLogs.AnyWithNoLockAsync(e => e.IP == s))
3738
{
3839
return false;
3940
}
@@ -44,4 +45,4 @@ public async Task<bool> ReportAsync(IPAddress ip)
4445
});
4546
return await dataContext.SaveChangesAsync() > 0;
4647
}
47-
}
48+
}

src/Masuit.MyBlogs.Core/Extensions/Firewall/FirewallAttribute.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Masuit.MyBlogs.Core.Extensions.Firewall;
1717

1818
public sealed class FirewallAttribute : IAsyncActionFilter
1919
{
20-
public IFirewallRepoter FirewallRepoter { get; set; }
20+
public IFirewallReporter FirewallReporter { get; set; }
2121

2222
public IMemoryCache MemoryCache { get; set; }
2323

@@ -269,15 +269,15 @@ await FirewallService.AddInterceptAsync(new IpInterceptLog
269269
request.Headers
270270
}.ToJsonString()
271271
});
272-
var key = "FirewallRepoter:" + FirewallRepoter.ReporterName + ":" + ip;
272+
var key = "FirewallReporter:" + FirewallReporter.ReporterName + ":" + ip;
273273
if (!MemoryCache.TryGetValue(key, out _) && FirewallService.InterceptCount(ip) >= CommonHelper.SystemSettings.GetOrAdd("LimitIPInterceptTimes", "30").ToInt32())
274274
{
275-
LogManager.Info($"准备上报IP{ip}{FirewallRepoter.ReporterName}");
276-
await FirewallRepoter.ReportAsync(IPAddress.Parse(ip)).ContinueWith(_ =>
275+
LogManager.Info($"准备上报IP{ip}{FirewallReporter.ReporterName}");
276+
await FirewallReporter.ReportAsync(IPAddress.Parse(ip)).ContinueWith(_ =>
277277
{
278278
MemoryCache.Set(key, 1, TimeSpan.FromDays(1));
279-
LogManager.Info($"访问频次限制,已上报IP{ip}至:" + FirewallRepoter.ReporterName);
279+
LogManager.Info($"访问频次限制,已上报IP{ip}至:" + FirewallReporter.ReporterName);
280280
});
281281
}
282282
}
283-
}
283+
}

src/Masuit.MyBlogs.Core/Extensions/Firewall/FirewallServiceCollectionExt.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22

33
public static class FirewallServiceCollectionExt
44
{
5-
public static IServiceCollection AddFirewallReporter(this IServiceCollection services, IConfiguration configuration)
6-
{
7-
switch (configuration["FirewallService:type"])
8-
{
9-
case "Cloudflare":
10-
case "cloudflare":
11-
case "cf":
12-
services.AddHttpClient<IFirewallRepoter, CloudflareRepoter>().ConfigureHttpClient(c =>
13-
{
14-
c.DefaultRequestHeaders.Add("X-Auth-Email", configuration["FirewallService:Cloudflare:AuthEmail"]);
15-
c.DefaultRequestHeaders.Add("X-Auth-Key", configuration["FirewallService:Cloudflare:AuthKey"]);
16-
});
17-
break;
18-
default:
19-
services.AddSingleton<IFirewallRepoter, DefaultFirewallRepoter>();
20-
break;
21-
}
5+
public static IServiceCollection AddFirewallReporter(this IServiceCollection services, IConfiguration configuration)
6+
{
7+
switch (configuration["FirewallService:type"])
8+
{
9+
case "Cloudflare":
10+
case "cloudflare":
11+
case "cf":
12+
services.AddHttpClient<IFirewallReporter, CloudflareReporter>().ConfigureHttpClient(c =>
13+
{
14+
c.DefaultRequestHeaders.Add("X-Auth-Email", configuration["FirewallService:Cloudflare:AuthEmail"]);
15+
c.DefaultRequestHeaders.Add("X-Auth-Key", configuration["FirewallService:Cloudflare:AuthKey"]);
16+
});
17+
break;
2218

23-
return services;
24-
}
19+
default:
20+
services.AddSingleton<IFirewallReporter, DefaultFirewallReporter>();
21+
break;
22+
}
23+
24+
return services;
25+
}
2526
}

src/Masuit.MyBlogs.Core/Extensions/Firewall/IFirewallRepoter.cs renamed to src/Masuit.MyBlogs.Core/Extensions/Firewall/IFirewallReporter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Masuit.MyBlogs.Core.Extensions.Firewall;
44

5-
public interface IFirewallRepoter
5+
public interface IFirewallReporter
66
{
77
string ReporterName { get; set; }
88

@@ -18,4 +18,4 @@ public interface IFirewallRepoter
1818
/// <param name="ip"></param>
1919
/// <returns></returns>
2020
Task<bool> ReportAsync(IPAddress ip);
21-
}
21+
}

src/Masuit.MyBlogs.Core/Extensions/Firewall/IpInterceptLog.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class IpInterceptLog
55
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
66
public int Id { get; set; }
77

8-
[StringLength(32)]
8+
[StringLength(64)]
99
public string IP { get; set; }
1010

1111
public string RequestUrl { get; set; }
@@ -30,8 +30,8 @@ public class IpInterceptLog
3030

3131
public class IpReportLog
3232
{
33-
[Key, StringLength(32), DatabaseGenerated(DatabaseGeneratedOption.Identity), ConcurrencyCheck]
33+
[Key, StringLength(64), ConcurrencyCheck]
3434
public string IP { get; set; }
3535

3636
public DateTime Time { get; set; }
37-
}
37+
}

src/Masuit.MyBlogs.Core/Infrastructure/Services/FirewallService.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,37 @@ namespace Masuit.MyBlogs.Core.Infrastructure.Services;
88
public class FirewallService(DataContext dataContext, IRedisClient redis) : IFirewallService
99
{
1010
private static readonly ConcurrentQueue<IpInterceptLog> Buffer = new();
11+
private DateTime _lastSave = DateTime.Now;
1112

1213
public void AddIntercept(IpInterceptLog log)
1314
{
1415
Buffer.Enqueue(log);
1516
redis.IncrBy("interceptCount", 1);
16-
if (Buffer.Count >= 100)
17+
if (Buffer.Count >= 10 || DateTime.Now - _lastSave > TimeSpan.FromMinutes(10))
1718
{
1819
while (Buffer.TryDequeue(out var item))
1920
{
2021
dataContext.Add(item);
2122
}
2223

2324
dataContext.SaveChanges();
25+
_lastSave = DateTime.Now;
2426
}
2527
}
2628

2729
public async Task AddInterceptAsync(IpInterceptLog log)
2830
{
2931
Buffer.Enqueue(log);
3032
await redis.IncrByAsync("interceptCount", 1);
31-
if (Buffer.Count >= 100)
33+
if (Buffer.Count >= 10 || DateTime.Now - _lastSave > TimeSpan.FromMinutes(10))
3234
{
3335
while (Buffer.TryDequeue(out var item))
3436
{
3537
dataContext.Add(item);
3638
}
39+
3740
await dataContext.SaveChangesAsync();
41+
_lastSave = DateTime.Now;
3842
}
3943
}
4044

0 commit comments

Comments
 (0)