miguan v1

This commit is contained in:
tamakyi
2026-10-21 12:09:28 +08:00
parent 2d4609dc0e
commit 79051acae8
28 changed files with 2128 additions and 182 deletions
+8 -3
View File
@@ -6,6 +6,7 @@ import (
"github.com/flamego/flamego"
"github.com/tamakyi/TamaBox/internal/security/autoban"
"github.com/tamakyi/TamaBox/internal/security/honeypot"
"github.com/tamakyi/TamaBox/internal/shared"
)
@@ -15,7 +16,7 @@ import (
// - 真正的 404 探测由 f.NotFound 处理器单独计数(见 route.go),
// 不再把业务性的"302 → /"(语言切换 / 登出 / 投票不存在 / 黑名单跳转等)误判为扫描。
// - 核心检测逻辑见 internal/security/autoban(可独立测试);告警发送函数由本层注入。
func autobanMiddleware(b *autoban.Banner) flamego.Handler {
func autobanMiddleware(b *autoban.Banner, hp *honeypot.Honeypot) flamego.Handler {
return func(ctx flamego.Context) {
// 经 shared.LimitKey 归一化后再计数与判定:IPv4 原样,IPv6 聚合到 /64。
// 否则攻击者可在单个 /64 内轮换地址,使每个键只命中一次而永不触发阈值。
@@ -32,8 +33,12 @@ func autobanMiddleware(b *autoban.Banner) flamego.Handler {
path := ctx.Request().URL.Path
kind := autoban.KindNone
if autoban.IsSensitivePath(path) {
kind = autoban.KindSensitive
// 蜜罐命中的路径由蜜罐处理器自己计数(它需要同时知道「是否触发了封禁」
// 以便落库)。若这里再记一次,同一次请求会被计两遍,阈值实际减半。
if hp == nil || !hp.Match(path) {
if autoban.IsSensitivePath(path) {
kind = autoban.KindSensitive
}
}
if kind != autoban.KindNone {
defer b.Record(ip, path, kind)