Files
ykxmz/_build/rotate_password.sh
T
xmz7891 e1c04d617a 清掉工作区最后两处明文密码 + 加 .gitignore 规则
演练 scrub_password_history.sh 时抓出来的:

1. _build/rotate_password.sh —— **我自己写的脚本**,
   为了测「老密码应该登不上」,把老密码写进去了。
   改成从 OLD_ADMIN_PASSWORD 环境变量取,不设就跳过。
   (又一次证明:写测试的时候很容易顺手把秘密抄进去)

2. _build/_hdr_data/data/admin_password.txt —— 测试数据文件,
   居然被 git 跟踪着。已 git rm --cached。

3. .gitignore 加了规则,以后 admin_password.txt 进不来:
       admin_password.txt
       *admin_password*.txt
       /_build/_hdr_data/
       /_build/relay_info.json

现在工作区搜不到明文密码了,只剩 git 历史里那 7 个提交
(等用户确认后再做历史重写 + 强制推送)。
2026-10-07 20:50:25 +08:00

76 lines
2.9 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 设新密码 + 重启中转 + 自检
set -u
D=/root/lanremote-relay
NEWPW="$1"
echo " ── 0. 记录 A/B 对照当前进度(重启会打断它,记下来)──"
date '+ 现在 %H:%M:%S'
[ -f /root/ab_A.out ] && cat /root/ab_A.out | sed 's/^/ /'
[ -f /root/ab_A_health.tsv ] && echo " A 组样本: $(wc -l < /root/ab_A_health.tsv)"
[ -f /root/ab_B.out ] && cat /root/ab_B.out | sed 's/^/ /'
echo
echo " ── 1. 备份旧密码文件 ──"
if [ -f "$D/data/admin_password.txt" ]; then
cp "$D/data/admin_password.txt" "$D/data/admin_password.txt.bak-$(date +%H%M%S)"
echo " 旧长度: $(wc -c < "$D/data/admin_password.txt"),已备份"
fi
echo
echo " ── 2. 写入新密码 ──"
printf '%s' "$NEWPW" > "$D/data/admin_password.txt"
chmod 600 "$D/data/admin_password.txt"
echo " 新长度: $(wc -c < "$D/data/admin_password.txt") 换行数: $(wc -l < "$D/data/admin_password.txt")"
ls -la "$D/data/admin_password.txt" | sed 's/^/ /'
echo
echo " ── 3. 重启中转 ──"
systemctl restart lanremote-relay
sleep 4
echo " 状态: $(systemctl is-active lanremote-relay)"
echo
echo " ── 4. 自检 ──"
curl -s --max-time 10 -o /dev/null -w ' 本机 /v1/health: HTTP %{http_code}\n' \
-H 'User-Agent: LanRemote/1.8.2' http://127.0.0.1:8080/v1/health
KEY=$(cat "$D/data/admin_key.txt" 2>/dev/null)
echo
echo " ⚠ 密码不在这个脚本输出里出现(避免进日志)"
echo " 只报长度和是否能登录:"
# 用新密码试登录,只看有没有拿到 cookie,不打印密码
code=$(curl -s --max-time 15 -o /dev/null -w '%{http_code}' \
-c /tmp/ck.txt \
-d "username=xmz7891&password=$NEWPW" \
"http://127.0.0.1:8080/admin/login" 2>/dev/null)
if grep -q lr_admin /tmp/ck.txt 2>/dev/null; then
echo " ✓ 新密码能登录(HTTP $code,拿到 lr_admin cookie)"
else
echo " ✗ 新密码登录失败(HTTP $code)"
fi
rm -f /tmp/ck.txt
# 老密码应该登不上。
# ⚠ 这里**不能把老密码写进来** —— 这个脚本会进仓库,
# 写进来就等于又泄漏一次(我第一版就是这么干的,
# 被 _build/scrub_password_history.sh 的演练抓出来了)。
# 改成从环境变量取,不设就跳过这项检查。
if [ -n "${OLD_ADMIN_PASSWORD:-}" ]; then
code2=$(curl -s --max-time 15 -o /dev/null -w '%{http_code}' \
-c /tmp/ck2.txt \
-d "username=xmz7891&password=$OLD_ADMIN_PASSWORD" \
"http://127.0.0.1:8080/admin/login" 2>/dev/null)
if grep -q lr_admin /tmp/ck2.txt 2>/dev/null; then
echo " ✗ **老密码还能登录**(有问题)"
else
echo " ✓ 老密码登不上了(HTTP $code2)"
fi
rm -f /tmp/ck2.txt
else
echo " ⊘ 跳过「老密码应失效」检查(没设 OLD_ADMIN_PASSWORD)"
fi
echo
echo " ── 5. 中转日志最后 5 行 ──"
journalctl -u lanremote-relay -n 5 --no-pager 2>/dev/null | sed 's/^/ /'