以周/月为单位汇总蜜罐命中,把来源 IP 通过公共服务查地理位置,画到地图上,
生成 HTML 报告并发邮件。同时补齐 ipinfo 无法回答「这个 IP 在哪儿」的问题。
新增 IP 地理定位层 internal/security/geoip:
- 与 internal/security/ipinfo 正交。ipinfo 走 cdncheck 离线指纹,只认云厂商/CDN/WAF,
答的是「是不是代理」;肉鸡(家宽、企业宽带、移动网络)必然被判为
「未识别(普通网络)」,所以「在哪儿」必须另建一套。
- ip-api.com 批量打底(100 个/请求、1.4s 节流)+ 腾讯位置服务精修国内
(一 IP 一请求,只精修命中数最高的前 N 个,受 ip_geo_tencent_key 控制)。
- Result{Geo, Unresolved, Requests, Failures} 三态。服务明确回答「没有这个地址」
才允许写失败缓存;请求超时/限流/5xx 一律不写 —— 否则一次抖动会把整批 IP
钉成「查不到」三天,此后每份报告的定位覆盖率都莫名变成 0。
- addrs 按调用方给的顺序构建(不是 map 遍历):腾讯精修只取前 N 个,乱序会让
每次运行精修到不同 IP,配额花在长尾上。
报告生成 internal/security/report:
- period:PreviousWeek / PreviousMonth,窗口一律 [start, end) 左闭右开,
相邻两期不重叠也不漏边界整点;period_key(week:2026-W44)做幂等。
- aggregate:按 shared.LimitKey 合并(IPv4 精确 / IPv6 折 /64);国家、城市、
路径、每日趋势;显式统计未定位命中数(覆盖率 60% 的图看起来和 100% 一样)。
- mapdraw:自绘等距圆柱投影气泡图。零海岸线零国界 —— 国界/台湾/南海诸岛绘制
必须符合国家标准,自绘极易画错,真实底图交给腾讯。经纬网 + 度数刻度 +
标签防重叠(同一都市圈两城市坐标差 0.01°,曾把「AU 55」和「AU 30」叠成
看起来像 AU 305 的假数字)。
- html:自包含报告页,内嵌 SVG + 腾讯交互地图(无 key 时给申请指引,
代码里不预置任何 key)。
- countryname:港澳台统一显示「中国香港/中国澳门/中国台湾」,常见国家用中文名。
存储与调度:
- ip_geo_cache(主键 = LimitKey,成功 TTL 90 天 / 明确无数据 TTL 3 天)
- security_report_logs(period_key 唯一索引 → 重启、错过时间窗都能正确补发,
失败重试上限 5 次后停止)
- honeypot report --period week|month [--to] [--dir] [--no-email]
- web 启动时按配置挂 30 分钟一轮的调度器(与保留期清理共用停止信号,
defer 移出 if 外,避免「清理关掉、报告开着」时 goroutine 泄漏)
配置([security]):ip_geo_provider=auto|ipapi|tencent|none、ip_geo_tencent_key、
honeypot_report_enabled / _weekly / _monthly / _email / _dir。
顺带修复:
- route/honeypot_test.go 的 captureStore 缺 AggregateSources/AggregatePaths/
TimestampsBetween,整个 route 包编译不过。
- templates/render_test.go 的 mockLoggedUser 缺 TelegramChatID,3 个渲染测试失败。
104 lines
3.6 KiB
Go
104 lines
3.6 KiB
Go
// Copyright 2026 tamakyi. All rights reserved.
|
||
// Use of this source code is governed by a MIT-style
|
||
// license that can be found in the LICENSE file.
|
||
|
||
package cmd
|
||
|
||
import (
|
||
"context"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/stretchr/testify/require"
|
||
|
||
"github.com/tamakyi/TamaBox/internal/db"
|
||
"github.com/tamakyi/TamaBox/internal/dbutil"
|
||
"github.com/tamakyi/TamaBox/internal/security/report"
|
||
)
|
||
|
||
// TestMergeKeysByHits 覆盖「按封禁口径合并后再定位」的键生成。
|
||
//
|
||
// 顺序很重要:腾讯精修有配额上限,只处理前 N 个 —— 必须优先给最活跃的来源,
|
||
// 否则配额会花在只命中一次的长尾地址上,而报告的主要来源反而定位不到。
|
||
func TestMergeKeysByHits(t *testing.T) {
|
||
rows := []*db.HoneypotSourceAgg{
|
||
{IP: "2408:8207:1234:5678::1", Hits: 5},
|
||
{IP: "2408:8207:1234:5678::2", Hits: 3}, // 同 /64 → 合成 8
|
||
{IP: "203.0.113.7", Hits: 6},
|
||
{IP: "198.51.100.1", Hits: 1},
|
||
}
|
||
|
||
got := mergeKeysByHits(rows)
|
||
require.Len(t, got, 3)
|
||
// 8 次(合并后的 /64)> 6 次 > 1 次
|
||
require.Equal(t, "2408:8207:1234:5678::/64", got[0])
|
||
require.Equal(t, "203.0.113.7", got[1])
|
||
require.Equal(t, "198.51.100.1", got[2])
|
||
}
|
||
|
||
// TestMergeKeysByHitsStable 覆盖同命中数时的稳定排序。
|
||
//
|
||
// 不稳定的话,每次跑报告被腾讯精修覆盖的那批 IP 会随机漂移,
|
||
// 两个周期的定位精度就不一致了,对比起来像是数据变了。
|
||
func TestMergeKeysByHitsStable(t *testing.T) {
|
||
rows := []*db.HoneypotSourceAgg{
|
||
{IP: "203.0.113.9", Hits: 2},
|
||
{IP: "203.0.113.2", Hits: 2},
|
||
{IP: "203.0.113.5", Hits: 2},
|
||
}
|
||
first := mergeKeysByHits(rows)
|
||
for i := 0; i < 20; i++ {
|
||
require.Equal(t, first, mergeKeysByHits(rows), "同命中数时顺序必须稳定")
|
||
}
|
||
}
|
||
|
||
// TestSchedulerDue 覆盖周报/月报的发送判定,这是「不重复发、不永远不发」的核心。
|
||
func TestSchedulerDue(t *testing.T) {
|
||
database, cleanup := dbutil.NewTestDB(t, db.AllTables...)
|
||
t.Cleanup(func() {
|
||
if err := cleanup("security_report_logs"); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
})
|
||
|
||
s := NewSecurityReportScheduler(database)
|
||
ctx := context.Background()
|
||
loc := time.Local
|
||
|
||
// now = 2026-10-26(周一)02:00,上一周是 10-19 ~ 10-25
|
||
now := time.Date(2026, 10, 26, 2, 0, 0, 0, loc)
|
||
period := report.PreviousWeek(now)
|
||
require.Equal(t, "week:2026-W43", period.Key)
|
||
|
||
// 1) 台账里没有记录 → 该发
|
||
require.True(t, s.due(period, now))
|
||
|
||
// 2) 已成功发送 → 不再发(哪怕重启一百次)
|
||
store := db.NewSecurityReportStore(database)
|
||
require.NoError(t, store.Save(ctx, &db.SecurityReportLog{
|
||
PeriodKey: period.Key, Kind: period.Kind,
|
||
WindowStart: period.Start, WindowEnd: period.End, Sent: true, Attempts: 1,
|
||
}))
|
||
require.False(t, s.due(period, now))
|
||
|
||
// 3) 发送失败但未超重试上限 → 继续重试
|
||
require.NoError(t, store.Save(ctx, &db.SecurityReportLog{
|
||
PeriodKey: period.Key, Kind: period.Kind,
|
||
WindowStart: period.Start, WindowEnd: period.End,
|
||
Sent: false, Attempts: 3, Error: "smtp auth failed",
|
||
}))
|
||
require.True(t, s.due(period, now))
|
||
|
||
// 4) 超过重试上限 → 停下来等人工介入,避免刷爆日志与邮箱配额
|
||
require.NoError(t, store.Save(ctx, &db.SecurityReportLog{
|
||
PeriodKey: period.Key, Kind: period.Kind,
|
||
WindowStart: period.Start, WindowEnd: period.End,
|
||
Sent: false, Attempts: 5, Error: "smtp auth failed",
|
||
}))
|
||
require.False(t, s.due(period, now))
|
||
|
||
// 5) 周期还没结束 → 不发(哪怕台账里没有记录)
|
||
future := report.PreviousWeek(now.AddDate(0, 0, 7))
|
||
require.False(t, s.due(future, now), "窗口未结束时不能发不完整的报告")
|
||
}
|