3X-UI 用户管理、访问日报与月度流量重置配置记录
本文档记录本次 3X-UI 配置流程,包括:新增独立用户、开启访问日志、设置日志保留、配置每日访问报告邮件,以及每月自动清空流量统计。
说明:文档中的 API_KEY、面板路径、邮箱授权码等均应使用占位符保存,不建议明文记录真实密钥。
1. 当前目标
本次配置完成后,实现以下功能:
1 2 3 4 5 6
| 1. 在现有 443 VLESS Reality Vision 入站中新增一个独立 client 2. 为该 client 设置独立流量限制 3. 开启访问日志,记录用户访问域名 4. 设置访问日志只保留 7 天 5. 每天 09:00 自动发送访问统计邮件 6. 每月 1 号 00:00 自动清空流量统计
|
2. 新增 client 用户
2.1 进入 Clients 模块
在当前 3X-UI 版本中,Clients 和 Inbounds 是并列模块,client 不是在 Inbound 编辑页中新增,而是在独立的 Clients 页面中管理。
进入路径:
然后在新增 client 时,通过 Attached Inbounds 选择需要绑定的入站,例如:
也就是说,当前结构是:
1 2 3 4 5 6
| Clients(用户管理) ├── REDACTED_USER_1 └── REDACTED_USER_2
Inbounds(入站管理) └── vless-reality-vision:443
|
2.2 client 配置
本次新增用户示例:
1 2 3 4 5 6 7
| Email / Tag: REDACTED_USER_2 UUID: 自动生成 Attached Inbounds: vless-reality-vision Traffic Limit: 100 GB IP Limit: 1 Enabled: ON Comment: REDACTED_USER_2
|
2.3 配置含义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Email / Tag: 用于日志中识别具体用户,例如 email: REDACTED_USER_2
Attached Inbounds: 将该 client 绑定到指定入站。本次绑定到已有的 vless-reality-vision 入站,不需要新开端口。
Traffic Limit: 限制该用户累计可用流量,本例为 100GB。
IP Limit: 限制同一账号同时允许多少个公网 IP 使用。 设置为 1 表示同一时间只允许一个公网 IP 使用。 用户更换网络后仍可继续使用,但不能多地同时在线。
Enabled: 是否启用该 client。
|
2.4 与旧版操作方式的区别
旧版或部分教程中常见路径是:
1
| Inbounds → Edit → Clients
|
但当前版本采用独立 Clients 管理方式,因此正确路径应为:
1
| Clients → Add Client → 选择 Attached Inbounds
|
3. Sniffing 与访问日志配置
3.1 Sniffing 配置
在入站中开启 Sniffing:
1 2 3 4 5 6
| Sniffing: ON HTTP: ON TLS: ON QUIC: ON Metadata Only: ON Route Only: OFF
|
3.2 作用
开启后可以记录域名级访问情况,例如:
1 2 3 4
| chatgpt.com google.com youtube.com telegram.org
|
注意:HTTPS 下只能记录域名,不能看到完整 URL、网页内容、聊天内容、密码等。
4. Access Log 配置
4.1 日志路径
在 3X-UI 的 Xray 日志配置中启用:
1 2 3
| Access Log: /var/log/x-ui/access.log Error Log: /var/log/x-ui/error.log Log Level: warning
|
4.2 查看实时访问日志
1
| tail -f /var/log/x-ui/access.log
|
4.3 按用户筛选日志
1
| grep REDACTED_USER_2 /var/log/x-ui/access.log
|
5. 日志只保留 7 天
为防止访问日志无限增长,使用 logrotate 自动清理。
5.1 编辑 logrotate 配置
1
| nano /etc/logrotate.d/xui
|
写入:
1 2 3 4 5 6 7 8
| /var/log/x-ui/*.log { daily rotate 7 compress missingok notifempty copytruncate }
|
5.2 配置含义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| daily: 每天切割一次日志
rotate 7: 只保留最近 7 份日志,约 7 天
compress: 压缩旧日志,节省磁盘空间
missingok: 日志不存在时不报错
notifempty: 空日志不处理
copytruncate: 不中断 Xray / 3X-UI 进程,直接切割日志
|
5.3 测试 logrotate
1
| logrotate -d /etc/logrotate.d/xui
|
如果没有报错,说明配置正常。
6. 每日访问报告邮件
6.1 脚本路径
每日访问报告脚本保存为:
6.2 脚本功能
该脚本会:
1 2 3 4 5
| 1. 读取 /var/log/x-ui/access.log 2. 只统计当天日志 3. 按 email 用户分组 4. 统计每个用户访问的 Top 10 域名 5. 使用 QQ 邮箱 SMTP 发送日报
|
6.3 脚本示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| import smtplib from email.mime.text import MIMEText from collections import defaultdict, Counter import datetime
LOG_FILE = "/var/log/x-ui/access.log"
EMAIL_FROM = "你的QQ号@qq.com" EMAIL_TO = "你的QQ号@qq.com" SMTP_SERVER = "smtp.qq.com" SMTP_PORT = 587 SMTP_PASS = "你的QQ邮箱SMTP授权码"
def parse_log(): user_data = defaultdict(list) today = datetime.datetime.now().strftime("%Y/%m/%d")
try: with open(LOG_FILE, "r") as f: for line in f: if today not in line: continue
if "email:" in line: try: user = line.split("email:")[1].split()[0].strip() except: continue else: continue
if "accepted tcp:" in line: try: domain = line.split("accepted tcp:")[1].split(":")[0] except: continue else: continue
user_data[user].append(domain)
except Exception as e: print("log error:", e)
return user_data
def send_email(content): msg = MIMEText(content, "plain", "utf-8") msg["Subject"] = "X-UI 每日用户访问报告" msg["From"] = EMAIL_FROM msg["To"] = EMAIL_TO
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) server.starttls() server.login(EMAIL_FROM, SMTP_PASS) server.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string()) server.quit()
def main(): data = parse_log() today = datetime.datetime.now().strftime("%Y-%m-%d")
report = f''' ========================= X-UI 每日访问报告 日期: {today} =========================
'''
if not data: report += "\n今日无访问记录\n" else: for user, sites in data.items(): report += f"\n用户:{user}\n" report += f"总访问次数:{len(sites)}\n"
counter = Counter(sites) report += "Top 网站:\n"
for site, count in counter.most_common(10): report += f" - {site}: {count}次\n"
send_email(report)
if __name__ == "__main__": main()
|
6.4 手动测试日报脚本
1
| python3 /root/xui_report.py
|
收到邮件后说明脚本正常。
7. 设置每日 09:00 自动发送报告
7.1 编辑 crontab
添加:
1
| 0 9 * * * /usr/bin/python3 /root/xui_report.py
|
7.2 查看定时任务
应看到:
1
| 0 9 * * * /usr/bin/python3 /root/xui_report.py
|
8. 每月自动清空流量统计
8.1 正确 API
本次确认的 3X-UI API 中,清空流量需要同时处理:
对应接口:
1 2
| POST /panel/api/inbounds/resetAllTraffics POST /panel/api/clients/resetAllTraffics
|
其中:
1 2 3 4 5
| /panel/api/inbounds/resetAllTraffics 用于清空入站总流量
/panel/api/clients/resetAllTraffics 用于清空网页端每个 client 用户的 up/down 流量
|
8.2 重置脚本路径
脚本保存为:
8.3 脚本内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #!/bin/bash
API_KEY="你的新API_KEY" BASE_URL="https://127.0.0.1:55555/你的URI_PATH"
curl -k -s -X POST \ -H "Authorization: Bearer ${API_KEY}" \ "${BASE_URL}/panel/api/inbounds/resetAllTraffics"
echo ""
curl -k -s -X POST \ -H "Authorization: Bearer ${API_KEY}" \ "${BASE_URL}/panel/api/clients/resetAllTraffics"
echo ""
|
示例中的 BASE_URL 格式:
1
| https://127.0.0.1:55555/你的URI_PATH
|
注意:真实 URI Path 以面板实际显示为准。
8.4 赋予执行权限
1
| chmod +x /root/reset_traffic.sh
|
8.5 手动测试
成功返回示例:
1
| {"success":true,"msg":"All traffic has been reset.","obj":null}
|
9. 设置每月 1 号 00:00 自动清空流量
9.1 编辑 crontab
添加:
1
| 0 0 1 * * /root/reset_traffic.sh
|
9.2 含义
1
| 每月 1 号 00:00 自动执行 /root/reset_traffic.sh
|
9.3 查看 crontab
应看到类似:
1 2
| 0 9 * * * /usr/bin/python3 /root/xui_report.py 0 0 1 * * /root/reset_traffic.sh
|
含义:
1 2
| 每天 09:00:发送访问日报 每月 1 号 00:00:清空流量统计
|
10. 安全注意事项
10.1 API Key 不应公开
API Key 具有管理权限,不应:
1 2 3 4
| 1. 发送给他人 2. 写入公开博客 3. 上传到 GitHub 4. 出现在截图中
|
如果 API Key 已暴露,应立即:
1
| 3X-UI → API Token → 删除旧 Key → 新建 Key
|
然后更新:
10.2 避免泄露节点配置
以下信息也属于敏感内容:
1 2 3 4 5 6 7
| UUID Reality privateKey Reality shortId subscription ID client password / auth 面板 URI Path API Key
|
10.3 HTTPS 访问本机 API
脚本中使用:
是因为本机访问:
证书域名通常不匹配 127.0.0.1,因此需要忽略证书校验。
11. 常用检查命令
11.1 查看访问日志
1
| tail -f /var/log/x-ui/access.log
|
11.2 查看某个用户访问
1
| grep REDACTED_USER_2 /var/log/x-ui/access.log
|
11.3 查看日志目录大小
11.4 查看定时任务
11.5 查看 cron 服务状态
11.6 启动并设置 cron 开机自启
1 2
| systemctl start cron systemctl enable cron
|