verify_guanlan_simsys.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """验证 /sim/sys/ (release/sim_sys_server.py) 恢复后: 页面能出 + 脱敏回扫干净。"""
  4. import sys
  5. import urllib.request
  6. import zipfile
  7. import pathlib
  8. ROOT = pathlib.Path(r"F:\temp\guanlan-rudong-v2_0.2.0")
  9. sys.path.insert(0, str(ROOT))
  10. from src.windscada import deid_public as DP
  11. def readable(name):
  12. try:
  13. return name.encode('cp437').decode('utf-8')
  14. except UnicodeError:
  15. return name
  16. zip_path = ROOT / 'release' / '如东SWT40_控制律仿真台_20260906.zip'
  17. with zipfile.ZipFile(zip_path) as z:
  18. pages = [readable(n.rsplit('/', 1)[-1]) for n in z.namelist()
  19. if n.endswith('.html') and readable(n.rsplit('/', 1)[-1])[:1] in {'0', '1', '2', '3', '4'}]
  20. print(f"页面 {len(pages)} 个")
  21. bad = 0
  22. for name in sorted(pages):
  23. url = 'http://127.0.0.1:18792/' + urllib.parse.quote(name)
  24. try:
  25. with urllib.request.urlopen(url, timeout=20) as r:
  26. body = r.read().decode('utf-8')
  27. code = r.status
  28. except Exception as ex:
  29. print(f" [X] {name}: {ex.__class__.__name__}: {ex}")
  30. bad += 1
  31. continue
  32. leaks = DP.audit(body)
  33. leak_txt = '干净' if not leaks else '; '.join(f'{n}×{c} {s}' for n, c, s in leaks)
  34. print(f" [{code}] {name:28s} {len(body):7d} 字节 回扫: {leak_txt}")
  35. bad += bool(leaks)
  36. print("\n网关路由:")
  37. for path in ('/healthz', '/sim/sys/'):
  38. try:
  39. with urllib.request.urlopen('http://127.0.0.1:28084' + path, timeout=20) as r:
  40. txt = r.read().decode('utf-8', 'replace')
  41. if path == '/healthz':
  42. import json
  43. d = json.loads(txt)
  44. print(f" [{r.status}] {path} status={d['status']} {d['mode']}")
  45. for m in d['modules']:
  46. print(f" {'ok ' if m['ok'] else 'DOWN'} {m['path']} {m['name']}")
  47. else:
  48. print(f" [{r.status}] {path} {len(txt)} 字节, 含返回链接: {'guanlan-top' in txt}")
  49. except Exception as ex:
  50. print(f" [X] {path}: {ex.__class__.__name__}: {ex}")
  51. bad += 1
  52. print("\n结论:", "全部通过" if not bad else f"有 {bad} 项需要注意")
  53. sys.exit(0 if not bad else 1)