# -*- coding: utf-8 -*-
r'''把管理页的内嵌 JS 抠出来,看第 46 行为什么语法错。
背景:_tests/test_admin_live.py 用 node --check 检查页面上的内嵌脚本,
报第 46 行 SyntaxError: Invalid or unexpected token。
但同一个页面上的管理接口(/v1/admin/allow_new、/v1/admin/set_code)
是能用的 —— 说明后端没问题,问题在浏览器端。
两种可能:
A. 真的有语法错 -> 用户点按钮没反应(严重)
B. 我抠脚本的方式不对(比如页面里字符串里含 ,
或者 JS 用到了 node 不认的语法)-> 假警报
这个脚本把可疑的那一行原样打出来,人肉判断是哪种。
'''
from __future__ import annotations
import json
import re
import subprocess
import sys
import tempfile
import urllib.request
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "_build"))
from devssh import run as ssh_run # noqa: E402
BASE = "https://relay.xxmxxs.top"
UA = {"User-Agent": "LanRemote/1.8.2"}
def main() -> int:
_c, out, _e = ssh_run(
"cat /root/lanremote-relay/data/admin_key.txt", timeout=60, quiet=True)
key = (out or "").strip().splitlines()[0]
req = urllib.request.Request(BASE + "/zhongzhuanzhan",
headers={**UA, "X-Admin": key})
with urllib.request.urlopen(req, timeout=25) as resp:
html = resp.read().decode("utf-8", "replace")
scripts = re.findall(r"", html, re.S)
inline = [s for s in scripts if s.strip()]
print(" 页面上有 %d 段内嵌脚本,长度: %s"
% (len(inline), [len(s) for s in inline]))
js = inline[0]
lines = js.splitlines()
print(" 第 1 段共 %d 行" % len(lines))
print()
print(" ════ 第 40~54 行原样 ════")
for i in range(39, min(54, len(lines))):
marker = " <<<< 报错行" if i == 45 else ""
print(" %3d| %s%s" % (i + 1, lines[i], marker))
print()
# 把这一段单独跑 node --check,确认能复现
with tempfile.NamedTemporaryFile("w", suffix=".js", delete=False,
encoding="utf-8") as fh:
fh.write(js)
path = fh.name
proc = subprocess.run(["node", "--check", path],
capture_output=True, text=True,
encoding="utf-8", errors="replace")
print(" node --check 退出码: %d" % proc.returncode)
if proc.stderr:
print(" stderr:")
for line in proc.stderr.splitlines()[:12]:
print(" " + line)
Path(path).unlink(missing_ok=True)
# 也检查每一行里有没有裸控制字符
print()
print(" ════ 有没有裸控制字符 ════")
bad = 0
for i, line in enumerate(lines, 1):
for ch in line:
if ord(ch) < 32 and ch not in "\t":
print(" 第 %d 行有控制字符 0x%02x" % (i, ord(ch)))
bad += 1
break
if not bad:
print(" ✓ 没有")
# 反引号 / 模板字面量 / 正则 里可能藏着截断问题
print()
print(" ════ 源 HTML 里 出现几次 ════")
print(" %d 次(正常应该等于 "))
print("