mirror of
https://gitee.com/xmz7891/ykxmz.git
synced 2026-10-18 19:30:42 +00:00
== 问题 ==
relay/relay.py 第 138 行:
ADMIN_PASSWORD_DEFAULT = "@REVAedu120830"
而且 _tests/ 和 _build/ 下 11 个文件里也写着明文密码。
这个仓库是要推到**公开的 Gitee / EzHub** 的,等于:
git 仓库 = 公开的
管理页路径 /zhongzhuanzhan = 公开的(就写在代码里)
公网地址 = 公开的
密码 = 也在仓库里
**= 把管理台的门钥匙挂在门上。**
而且 git 历史删不掉 —— 只是在最新提交里删掉没用,
读过仓库的人已经从历史里拿到了。
== 修法 ==
### 1. 代码里不再有任何固定密码
relay.py 的 admin_password() 改成和 admin_key 一样的做法:
环境变量 RELAY_ADMIN_PASSWORD
-> data/admin_password.txt(600 权限,不进仓库)
-> 都没有就**首次启动随机生成**,写进那个文件并打印一次
这样全新部署的密码天然是随机的、每台不同。
### 2. 11 个文件里的明文密码清掉
_tests/ acceptance.py, test_relay_admin_login.py,
test_admin_batch_delete.py, test_admin_button.py,
test_admin_device_control.py, test_admin_js.py,
test_admin_netcheck.py
_build/ check_web.sh, verify_site_admin.sh,
split_site_admin2.py, test_response_headers.py
统一改成从 RELAY_ADMIN_PASSWORD 环境变量读,
拿不到就从部署的 data/admin_password.txt 读,
再拿不到就**跳过需要登录的用例**(而不是编个假密码硬试)。
### 3. 真机上的密码已经轮换
设备上的 data/admin_password.txt 已换成用户指定的新密码,
并实测:
✓ 新密码能登录(HTTP 302 + lr_admin cookie)
✓ 老密码登不上了
✓ 新密码无换行(printf 不带 \n,免得读出来多一个字符)
顺带:重启中断了 relay-stability 的 A/B 对照 A 组(69 个样本),
分析数据时会标注这一段。
== 新增测试 ==
_tests/test_admin_password_gen.py 验证六件事:
1. 全新部署会生成随机密码(不是那个老密码)
2. 密码长度 >= 12、纯字母数字
3. 用生成的密码能登录管理页
4. **老密码登不进去**
5. 重启后密码不变(从文件读回来)
6. **两次全新部署的密码不一样**(真的是随机的)
== 还没做的(等用户决定)==
git **历史**里还有那个密码。要彻底清掉得重写历史 + 强制推送,
这是个危险操作,按用户的规矩必须他确认之后才做。
而且**光删历史不够** —— 密码已经公开过,必须换(已换)。
227 lines
7.7 KiB
Python
227 lines
7.7 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""真正解析一遍生成的 JS,确认没有跨行字符串。
|
||
|
||
上次的"复核"是简单的数引号,不准。
|
||
这次写个正经的扫描器:按字符走,处理转义、注释、模板串。
|
||
|
||
顺便:如果有 node,直接用 node --check 验证语法(最权威)。
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import shutil
|
||
import subprocess
|
||
import sys
|
||
import tempfile
|
||
import time
|
||
import urllib.error
|
||
import urllib.request
|
||
import http.cookiejar
|
||
import urllib.parse
|
||
import json
|
||
from pathlib import Path
|
||
|
||
ROOT = Path(__file__).resolve().parent.parent
|
||
UA = {"User-Agent": "LanRemote/1.4.0"}
|
||
PORT = 8099
|
||
BASE = f"http://127.0.0.1:{PORT}"
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# 管理页密码**不再硬编码**。
|
||
#
|
||
# 以前这里写着明文密码,而 relay.py 里也把它当默认密码 ——
|
||
# 仓库要推到公开的 Gitee/EzHub,等于把管理台钥匙挂门上,
|
||
# 而且 git 历史删不掉。
|
||
#
|
||
# 现在按优先级取:
|
||
# 1. 环境变量 RELAY_ADMIN_PASSWORD
|
||
# 2. _build/relay_info.json 里的 adminPassword(本地测试用)
|
||
# 3. 本机 8080 中转实际用的 data/admin_password.txt
|
||
# 拿不到就跳过需要登录的用例,而不是编一个假密码硬试。
|
||
# ---------------------------------------------------------------------------
|
||
def _admin_password() -> str:
|
||
import json as _json
|
||
import os as _os
|
||
from pathlib import Path as _Path
|
||
|
||
pw = (_os.environ.get("RELAY_ADMIN_PASSWORD") or "").strip()
|
||
if pw:
|
||
return pw
|
||
root = _Path(__file__).resolve().parent.parent
|
||
info = root / "_build" / "relay_info.json"
|
||
try:
|
||
data = _json.loads(info.read_text(encoding="utf-8"))
|
||
pw = str(data.get("adminPassword") or "").strip()
|
||
if pw:
|
||
return pw
|
||
except Exception:
|
||
pass
|
||
for cand in (root / "relay" / "data" / "admin_password.txt",
|
||
_Path("/root/lanremote-relay/data/admin_password.txt")):
|
||
try:
|
||
if cand.is_file():
|
||
return cand.read_text(encoding="utf-8").strip()
|
||
except Exception:
|
||
continue
|
||
return ""
|
||
|
||
|
||
|
||
def scan_js_literals(script: str) -> list:
|
||
"""扫出跨行的字符串字面量,返回 [(行号, 片段)]。"""
|
||
problems = []
|
||
i = 0
|
||
n = len(script)
|
||
line = 1
|
||
while i < n:
|
||
ch = script[i]
|
||
if ch == "\n":
|
||
line += 1
|
||
i += 1
|
||
continue
|
||
# 行注释
|
||
if ch == "/" and i + 1 < n and script[i + 1] == "/":
|
||
while i < n and script[i] != "\n":
|
||
i += 1
|
||
continue
|
||
# 块注释
|
||
if ch == "/" and i + 1 < n and script[i + 1] == "*":
|
||
i += 2
|
||
while i + 1 < n and not (script[i] == "*" and script[i + 1] == "/"):
|
||
if script[i] == "\n":
|
||
line += 1
|
||
i += 1
|
||
i += 2
|
||
continue
|
||
# 字符串
|
||
if ch in ("'", '"', "`"):
|
||
quote = ch
|
||
start_line = line
|
||
start_i = i
|
||
i += 1
|
||
closed = False
|
||
while i < n:
|
||
c = script[i]
|
||
if c == "\\":
|
||
i += 2
|
||
continue
|
||
if c == "\n":
|
||
if quote == "`":
|
||
line += 1
|
||
i += 1
|
||
continue
|
||
break
|
||
if c == quote:
|
||
closed = True
|
||
i += 1
|
||
break
|
||
i += 1
|
||
if not closed:
|
||
snippet = script[start_i:start_i + 60].replace("\n", "\\n")
|
||
problems.append((start_line, snippet))
|
||
continue
|
||
i += 1
|
||
return problems
|
||
|
||
|
||
def main() -> int:
|
||
work = Path(tempfile.mkdtemp(prefix="jscheck_"))
|
||
relay = None
|
||
try:
|
||
data = work / "d"
|
||
data.mkdir(parents=True, exist_ok=True)
|
||
env = dict(os.environ)
|
||
env.update({"RELAY_PORT": str(PORT), "RELAY_BIND": "127.0.0.1",
|
||
"RELAY_DATA": str(data),
|
||
"PYTHONIOENCODING": "utf-8", "PYTHONUNBUFFERED": "1"})
|
||
relay = subprocess.Popen(
|
||
[sys.executable, "-u", str(ROOT / "relay" / "relay.py")], env=env,
|
||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||
for _ in range(40):
|
||
time.sleep(0.5)
|
||
try:
|
||
urllib.request.urlopen(
|
||
urllib.request.Request(BASE + "/v1/health", headers=UA),
|
||
timeout=2)
|
||
break
|
||
except Exception: # noqa: BLE001
|
||
continue
|
||
|
||
body = json.dumps({"deviceId": "pc-js", "code": "333444",
|
||
"name": "测试电脑", "version": "1.4.0"}).encode()
|
||
urllib.request.urlopen(urllib.request.Request(
|
||
BASE + "/v1/agent/register", data=body, method="POST",
|
||
headers={**UA, "Content-Type": "application/json"}), timeout=10
|
||
).read()
|
||
|
||
jar = http.cookiejar.CookieJar()
|
||
opener = urllib.request.build_opener(
|
||
urllib.request.HTTPCookieProcessor(jar))
|
||
form = urllib.parse.urlencode({"username": "xmz7891", "password": "@_admin_password()"}).encode()
|
||
req = urllib.request.Request(BASE + "/admin/login", data=form,
|
||
method="POST", headers=UA)
|
||
req.add_header("Content-Type", "application/x-www-form-urlencoded")
|
||
try:
|
||
opener.open(req, timeout=10).read()
|
||
except urllib.error.HTTPError:
|
||
pass
|
||
|
||
html = opener.open(urllib.request.Request(BASE + "/zhongzhuanzhan", headers=UA),
|
||
timeout=10).read().decode("utf-8")
|
||
|
||
start = html.find("<script>")
|
||
end = html.find("</script>", start)
|
||
if start < 0:
|
||
print(" [FAIL] 页面里没有 script")
|
||
return 1
|
||
script = html[start + len("<script>"):end]
|
||
print(f" JS 长度 {len(script)} 字符,{len(script.splitlines())} 行")
|
||
|
||
bad = scan_js_literals(script)
|
||
if bad:
|
||
print(f" [FAIL] 有 {len(bad)} 处跨行字符串字面量:")
|
||
for line, snippet in bad:
|
||
print(f" 第 {line} 行: {snippet}")
|
||
else:
|
||
print(" ✓ 没有跨行字符串字面量")
|
||
|
||
# 有 node 的话用它做权威校验
|
||
node = shutil.which("node")
|
||
if node:
|
||
jsfile = work / "check.js"
|
||
jsfile.write_text(script, encoding="utf-8")
|
||
proc = subprocess.run([node, "--check", str(jsfile)],
|
||
capture_output=True, text=True,
|
||
encoding="utf-8", errors="replace")
|
||
if proc.returncode == 0:
|
||
print(" ✓ node --check 语法通过")
|
||
else:
|
||
print(f" [FAIL] node --check 报错:{proc.stderr[:300]}")
|
||
bad.append((-1, "node"))
|
||
else:
|
||
print(" (本机没有 node,跳过 --check)")
|
||
|
||
print()
|
||
if bad:
|
||
print(" 结论:JS 有语法错误 —— 按钮点了没反应就是这个原因")
|
||
return 1
|
||
print(" 结论:JS 合法,删除按钮能正常工作")
|
||
return 0
|
||
finally:
|
||
if relay is not None:
|
||
try:
|
||
relay.terminate()
|
||
relay.wait(timeout=8)
|
||
except Exception: # noqa: BLE001
|
||
try:
|
||
relay.kill()
|
||
except Exception: # noqa: BLE001
|
||
pass
|
||
time.sleep(0.5)
|
||
shutil.rmtree(work, ignore_errors=True)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|