Skip to content

Commit 15f30d4

Browse files
committed
fix(security): 修复CC防火墙逻辑并优化用户IP检测
- 移除了过时的注释代码块 - 添加了UserCache依赖用于IP用户检测 - 实现了基于用户状态的动态阈值控制 - 修复了计数器输出的日志信息显示
1 parent 77d318d commit 15f30d4

File tree

2 files changed

+4
-19
lines changed

2 files changed

+4
-19
lines changed

src/main/java/org/b3log/symphony/processor/BeforeRequestHandler.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,6 @@ public void handle(final RequestContext context) {
9696
return;
9797
}
9898

99-
/*try {
100-
String method = context.getRequest().getMethod();
101-
String uri = context.getRequest().getRequestURI();
102-
String ip = Requests.getRemoteAddr(context.getRequest());
103-
String union = ip + " " + method + " " + uri;
104-
if (!whiteList.contains(ip)) {
105-
if (!antiCCLimiter.access(union)) {
106-
context.sendStatus(503);
107-
return;
108-
}
109-
}
110-
} catch (Exception e) {
111-
e.printStackTrace();
112-
context.sendStatus(503);
113-
return;
114-
}*/
115-
11699
Locales.setLocale(Latkes.getLocale());
117100

118101
Sessions.setTemplateDir(Symphonys.SKIN_DIR_NAME);

src/main/java/org/b3log/symphony/util/Firewall.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.logging.log4j.LogManager;
2424
import org.apache.logging.log4j.Logger;
2525
import org.b3log.latke.util.Execs;
26+
import org.b3log.symphony.cache.UserCache;
2627

2728
import java.util.Map;
2829
import java.util.Set;
@@ -84,6 +85,7 @@ public static boolean recordAndMaybeBan(final String ip) {
8485
}
8586

8687
final long nowBucket = System.currentTimeMillis() / WINDOW_MILLIS;
88+
final int effectiveThreshold = UserCache.hasUserByIP(ip) ? threshold : Math.min(threshold, 250);
8789
final Counter counter = COUNTERS.compute(ip, (key, existing) -> {
8890
if (existing == null || existing.bucket != nowBucket) {
8991
return new Counter(nowBucket, 1);
@@ -97,7 +99,7 @@ public static boolean recordAndMaybeBan(final String ip) {
9799
cleanupOldBuckets(nowBucket);
98100
}
99101

100-
if (counter.count.sum() > threshold && BANNED.add(ip)) {
102+
if (counter.count.sum() > effectiveThreshold && BANNED.add(ip)) {
101103
// Run ban asynchronously on a virtual thread to keep request path light.
102104
Thread.startVirtualThread(() -> {
103105
try {
@@ -110,7 +112,7 @@ public static boolean recordAndMaybeBan(final String ip) {
110112
return false;
111113
}
112114

113-
System.out.println("CC firewall allowed " + ip + " [" + counter.count + "]");
115+
System.out.println("CC firewall allowed " + ip + " [" + counter.count.sum() + "]");
114116

115117
return !BANNED.contains(ip);
116118
}

0 commit comments

Comments
 (0)