解锁并提取Linux客户端微信数据库 (vibe coded)
at 65 lines 2.2 kB view raw
1import os 2import subprocess 3from pathlib import Path 4from typing import Optional 5 6 7def _extract_wechat_files_path_from_cmdline(cmdline): 8 if not cmdline: 9 return None 10 for i, arg in enumerate(cmdline): 11 if arg.startswith("--wechat-files-path="): 12 return arg.split("=", 1)[1] 13 if arg == "--wechat-files-path" and i + 1 < len(cmdline): 14 return cmdline[i + 1] 15 return None 16 17 18def find_wechat_files_path() -> Optional[str]: 19 # 1) env override 20 for env_key in ("WECHAT_FILES_PATH", "WX_FILES_PATH", "XWECHAT_FILES_PATH"): 21 val = os.environ.get(env_key) 22 if val and os.path.exists(val): 23 return val 24 25 # 2) parse running WeChat cmdline via /proc to avoid psutil dependency 26 try: 27 pgrep = subprocess.run(["pgrep", "-f", "WeChat"], capture_output=True, text=True, check=False) 28 if pgrep.returncode == 0: 29 for line in pgrep.stdout.splitlines(): 30 pid = line.strip() 31 if not pid.isdigit(): 32 continue 33 try: 34 with open(f"/proc/{pid}/cmdline", "rb") as f: 35 cmdline = f.read().split(b"\x00") 36 cmdline = [c.decode(errors="ignore") for c in cmdline if c] 37 except Exception: 38 cmdline = [] 39 path = _extract_wechat_files_path_from_cmdline(cmdline) 40 if path and os.path.exists(path): 41 return path 42 except Exception: 43 pass 44 45 # 3) common locations for Linux WeChat AppImage 46 home = Path.home() 47 candidates = [ 48 home / "文档" / "xwechat_files", 49 home / "Documents" / "xwechat_files", 50 home / "xwechat_files", 51 home / ".xwechat" / "xwechat_files", 52 home / ".local" / "share" / "wechat" / "xwechat_files", 53 home / ".local" / "share" / "WeChat Files", 54 ] 55 for p in candidates: 56 if p.exists(): 57 return str(p) 58 59 # 4) shallow search (one level) for xwechat_files 60 for p in (home / "文档", home / "Documents", home): 61 if p.exists(): 62 target = p / "xwechat_files" 63 if target.exists(): 64 return str(target) 65 return None