v-fixed34:访问日志与使用统计([ACCESS]真实IP/UA + usage-stats.sh)

This commit is contained in:
jinghui committed 2026-10-29 23:31:41 +08:00
1 parent 6e5b7fa7f4
commit e90d022ae4
6 files changed
+91 -4

No files matched your search

+5
View File
@@ -2,6 +2,11 @@
格式参考本机发版惯例:按日期分条,`🐛修复` / `✨新功能`。
## 2026-10-29 → v-fixed34
- ✨ **访问日志与使用统计**:向 Next `start-server.js` 注入 `[ACCESS]` 请求日志(时间/IP/方法/路径/UA/Referer,跳过静态资源),nps 隧道透传 `X-Forwarded-For` 可拿到外网真实 IP;新增 `usage-stats.sh` + `usage-stats.py` 一键统计:总请求数、独立 IP 数、按天分布、Top IP、直播观看人数
- 📖 补丁 `patches/patch_access_log.js` 入仓(幂等,上游升级后可重放);`docker commit` 固化为 `katelyatv:fixed34`
## 2026-10-29 → v-fixed33
- 📦 **镜像发布渠道(双公共仓库)**:`gitea.88531.cn/jinghui/katelyatv`(Gitea 包仓库)与 `jpccr.ccs.tencentyun.com/jinghui/katelyatv`(腾讯云容器镜像服务·东京)均免登录 `docker pull`,含 `latest` 与固定版本 tag,两渠道匿名拉取已实测通过
+5 -3
View File
@@ -47,7 +47,7 @@ git clone https://gitea.88531.cn/jinghui/katelyatv.git && cd katelyatv
docker pull gitea.88531.cn/jinghui/katelyatv:latest
# 国内慢可换腾讯云镜像(东京):
# docker pull jpccr.ccs.tencentyun.com/jinghui/katelyatv:latest
docker tag gitea.88531.cn/jinghui/katelyatv:latest katelyatv:fixed33 # 对齐 compose 的 image 名
docker tag gitea.88531.cn/jinghui/katelyatv:latest katelyatv:fixed34 # 对齐 compose 的 image 名
cp .env.example .env
vi .env # 至少填:PASSWORD(站点密码)、NEXTAUTH_SECRET(随机串)
@@ -79,6 +79,7 @@ docker restart katelyatv
## 📝 更新日志(最近)
- **2026-10-29 v-fixed34**:新增访问日志与使用统计 —— 外网访问者真实 IP/UA 全记录,`./usage-stats.sh [24h|all]` 一键查看独立 IP 数、按天分布、直播观看人数
- **2026-10-29 v-fixed33**:桌面导航与直播页右上角新增「⭐ 开源地址」入口;镜像发布至 `gitea.88531.cn/jinghui/katelyatv` 公共仓库(免登录 `docker pull`)
- **2026-10-26 v-fixed32**:站点更名为「米视TV」(界面 / 标题 / TVBOX 内置名称 / manifest 全量替换,支持 `SITE_NAME` 环境变量自定义)
- **2026-10-26 v-fixed31**:加载提示文字改为白色 19px 加粗 + 阴影,播放器内更醒目
@@ -125,13 +126,14 @@ docker/katelyatv/
| `routes-manifest.json` | 路径式 TVBOX 站点 API:`/api/tvbox/<site>?ac=videolist` |
| `filter-page-chunk.js` | 筛选页加载更多去重 |
| `live-inject.js` + `inject_live.py` | 港澳台直播全套(解码、反代、播放页、EPG、TVBOX 直播源) |
| `patch_access_log.js` | 访问日志注入([ACCESS] IP/UA/路径,供 `usage-stats.sh` 统计使用人数) |
补丁固化在本地镜像(compose 引用,当前 `katelyatv:fixed33`)。上游大版本升级后重放补丁:
补丁固化在本地镜像(compose 引用,当前 `katelyatv:fixed34`)。上游大版本升级后重放补丁:
```bash
bash patches/apply.sh # 重放补丁
python3 patches/inject_live.py # 重放直播注入(幂等)
docker commit katelyatv katelyatv:fixed33 # 固化新镜像
docker commit katelyatv katelyatv:fixed34 # 固化新镜像
docker compose -f docker-compose.redis.yml up -d
```
+1 -1
View File
@@ -3,7 +3,7 @@ version: '3.8'
services:
# KatelyaTV 应用服务
katelyatv:
image: katelyatv:fixed33
image: katelyatv:fixed34
container_name: katelyatv
ports:
- "3030:3000"
+28
View File
@@ -0,0 +1,28 @@
// [ACCESS-LOG-V1] 访问日志注入补丁:给 Next startServer 的 http server 加 request 监听
const fs = require('fs');
const p = '/app/node_modules/next/dist/server/lib/start-server.js';
let s = fs.readFileSync(p, 'utf8');
if (s.includes('ACCESS-LOG-V1')) { console.log('已存在,跳过'); process.exit(0); }
const anchor = 'let portRetryCount = 0;';
if (s.indexOf(anchor) < 0) { console.error('锚点未找到'); process.exit(1); }
const inject = `// >>> ACCESS-LOG-V1
server.on("request", (req, res) => {
const u = String(req.url || "");
if (u.startsWith("/_next/static") || u.startsWith("/favicon")) return;
res.on("finish", () => {
try {
const xff = req.headers["x-forwarded-for"];
const ip = (xff ? String(xff).split(",")[0].trim() : "") ||
(req.socket && req.socket.remoteAddress) || "-";
console.log("[ACCESS] " + new Date().toISOString() +
" ip=" + ip + " " + req.method + " " + u +
" ua=" + JSON.stringify(String(req.headers["user-agent"] || "-").slice(0, 90)) +
(req.headers["referer"] ? " ref=" + String(req.headers["referer"]).slice(0, 80) : ""));
} catch (e) {}
});
});
// <<< ACCESS-LOG-V1
`;
s = s.replace(anchor, inject + anchor);
fs.writeFileSync(p, s);
console.log('注入完成');
+43
View File
@@ -0,0 +1,43 @@
#!/usr/bin/env python3
"""米视TV 使用统计解析器 —— 读 stdin 的 [ACCESS] 日志行,argv[1]=时间窗描述"""
import sys, re, collections
since = sys.argv[1] if len(sys.argv) > 1 else '168h'
rx = re.compile(r'^\[ACCESS\] (\S+) ip=(\S+) (\S+) (\S+) ua="([^"]*)"')
rows = []
for line in sys.stdin:
m = rx.match(line.strip())
if m:
rows.append(m.groups())
if not rows:
print(f"最近 {since} 没有访问记录([ACCESS] 日志从 v-fixed34 起启用)")
sys.exit(0)
def is_live(p): return 'live=ts' in p or 'live=m3u8' in p or 'live=lines' in p or 'live=ch' in p
def is_page(p): return p in ('/', '/live', '/login') or (not p.startswith(('/api', '/_next', '/screenshot', '/manifest')))
days = collections.defaultdict(set)
per_ip = collections.Counter()
live_ips = collections.Counter()
pages = collections.Counter()
for ts, ip, method, path, ua in rows:
days[ts[:10]].add(ip)
per_ip[ip] += 1
if is_live(path): live_ips[ip] += 1
if is_page(path): pages[path] += 1
print(f"=== 米视TV 使用统计(最近 {since})===")
print(f"日志范围: {rows[0][0]} ~ {rows[-1][0]}")
print(f"总请求数: {len(rows)} 独立 IP 数: {len(set(per_ip))}")
print(f"看过直播的独立 IP: {len(live_ips)}")
print("\n按天独立 IP:")
for d in sorted(days):
print(f" {d}: {len(days[d])}")
print("\nTop 访问 IP (IP | 请求 | 其中直播):")
for ip, c in per_ip.most_common(10):
print(f" {ip:40s} {c:6d} {live_ips.get(ip, 0)}")
if pages:
print("\n页面访问:")
for p, c in pages.most_common(8):
print(f" {p:24s} {c}")
print("\n提示: NAT/共享出口 IP 会把多人算一个;同一人换网络会算多个 —— 数字为近似值。")
+9
View File
@@ -0,0 +1,9 @@
#!/bin/bash
# 米视TV 使用统计 —— 基于容器 [ACCESS] 访问日志
# 用法: ./usage-stats.sh # 默认最近 7 天
# ./usage-stats.sh 24h # 最近 24 小时
# ./usage-stats.sh all # 全部历史
set -euo pipefail
SINCE="${1:-168h}"
docker logs katelyatv --since "$SINCE" 2>&1 | grep '\[ACCESS\]' | \
python3 "$(dirname "$0")/usage-stats.py" "$SINCE"