diagnostic.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. """导出脱敏后的配置摘要与最近日志为 zip,用于风控对照分析。不导出账号/token/密码。"""
  4. import json
  5. import platform
  6. import re
  7. import sys
  8. import zipfile
  9. from datetime import datetime
  10. from pathlib import Path
  11. from module.config.utils import read_file, deep_get
  12. from module.server.config_manager import ConfigManager
  13. from module.logger import logger
  14. PROJECT_ROOT = Path.cwd().resolve()
  15. LOG_DIR = PROJECT_ROOT / 'log'
  16. OUT_DIR = LOG_DIR / 'diagnostic'
  17. _RECENT_INSTANCE_LOGS = 4
  18. _RECENT_SHARED_LOGS = 3
  19. _RECENT_ERROR_LOGS = 10
  20. # 白名单:只导出这些字段,不带出 URS 凭据
  21. _DEVICE_FIELDS = [
  22. 'control_method', 'screenshot_method', 'serial', 'package_name',
  23. 'emulatorinfo_type', 'emulatorinfo_name',
  24. ]
  25. # 值匹配到分隔符为止(涵盖 base64 的 +/= 等), 避免只按 \w 截断导致凭据残留
  26. _V = r'[^\s"\',:;&?{}\[\]]'
  27. _SCRUB_PATTERNS = [
  28. (re.compile(r'(账号[::]\s*)(' + _V + r'{3,})'), r'\1***'),
  29. (re.compile(r'(account["\'\s::=]+)(' + _V + r'{3,})', re.I), r'\1***'),
  30. (re.compile(r'(token["\'\s::=]+)(' + _V + r'{6,})', re.I), r'\1***'),
  31. (re.compile(r'(urs[\w]*["\'\s::=]+)(' + _V + r'{6,})', re.I), r'\1***'),
  32. (re.compile(r'(password["\'\s::=]+)(' + _V + r'+)', re.I), r'\1***'),
  33. (re.compile(r'\b(1[3-9]\d)\d{4}(\d{4})\b'), r'\1****\2'),
  34. ]
  35. def _scrub(text: str) -> str:
  36. for pat, repl in _SCRUB_PATTERNS:
  37. text = pat.sub(repl, text)
  38. return text
  39. def _config_summary(name: str) -> dict:
  40. data = read_file(str(PROJECT_ROOT / 'config' / f'{name}.json'))
  41. if not isinstance(data, dict):
  42. return {'instance': name, 'error': 'config unreadable'}
  43. device = {k: deep_get(data, f'script.device.{k}') for k in _DEVICE_FIELDS}
  44. optimization = data.get('script', {}).get('optimization', {})
  45. anti_ban = data.get('script', {}).get('anti_ban', {})
  46. enabled = []
  47. for task, cfg in data.items():
  48. if isinstance(cfg, dict) and deep_get(cfg, 'scheduler.enable') is True:
  49. enabled.append(task)
  50. return {
  51. 'instance': name,
  52. 'device': device,
  53. 'optimization': optimization,
  54. 'anti_ban': anti_ban,
  55. 'enabled_tasks': sorted(enabled),
  56. }
  57. def _recent_files(pattern: str, limit: int) -> list:
  58. try:
  59. candidates = [p for p in LOG_DIR.glob(pattern) if p.is_file()]
  60. except OSError:
  61. return []
  62. recent = []
  63. for p in candidates:
  64. try:
  65. recent.append((p.stat().st_mtime, p))
  66. except OSError:
  67. continue
  68. recent.sort(key=lambda item: item[0], reverse=True)
  69. return [p for _, p in recent[:limit]]
  70. def build_diagnostic_zip(config_name: str = '') -> Path:
  71. """config_name 为空或 'Home' 时导出全部实例。"""
  72. # 只接受已存在的实例名, 防止 config_name 带 ../ 造成路径穿越
  73. valid = ConfigManager.all_script_files()
  74. if config_name and config_name in valid:
  75. instances = [config_name]
  76. tag = config_name
  77. else:
  78. instances = valid
  79. tag = 'all'
  80. summary = {
  81. 'generated_at': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
  82. 'platform': platform.platform(),
  83. 'python': sys.version.split()[0],
  84. 'instances': [_config_summary(n) for n in instances],
  85. }
  86. OUT_DIR.mkdir(parents=True, exist_ok=True)
  87. stamp = datetime.now().strftime('%Y%m%d_%H%M%S_%f')
  88. zip_path = OUT_DIR / f'oas_diag_{tag}_{stamp}.zip'
  89. log_files = []
  90. for name in instances:
  91. log_files += _recent_files(f'*_{name}.txt', _RECENT_INSTANCE_LOGS)
  92. log_files += _recent_files('*_api.txt', _RECENT_SHARED_LOGS)
  93. log_files += _recent_files('*_server.txt', _RECENT_SHARED_LOGS)
  94. error_logs = []
  95. error_root = LOG_DIR / 'error'
  96. if error_root.exists():
  97. try:
  98. err = [p for p in error_root.glob('*/log.txt') if p.is_file()]
  99. err.sort(key=lambda p: p.stat().st_mtime, reverse=True)
  100. error_logs = err[:_RECENT_ERROR_LOGS]
  101. except OSError:
  102. error_logs = []
  103. with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
  104. zf.writestr('summary.json', json.dumps(summary, ensure_ascii=False, indent=2))
  105. for f in dict.fromkeys(log_files):
  106. try:
  107. zf.writestr(f'log/{f.name}', _scrub(f.read_text(encoding='utf-8', errors='replace')))
  108. except OSError as e:
  109. logger.warning(f'diagnostic: skip {f}: {e}')
  110. for f in error_logs:
  111. try:
  112. zf.writestr(f'error/{f.parent.name}/log.txt',
  113. _scrub(f.read_text(encoding='utf-8', errors='replace')))
  114. except OSError as e:
  115. logger.warning(f'diagnostic: skip {f}: {e}')
  116. logger.info(f'诊断日志已导出: {zip_path}')
  117. return zip_path