解锁并提取Linux客户端微信数据库 (vibe coded)
at 187 lines 5.2 kB view raw
1# -*- coding: utf-8 -*-# 2# ------------------------------------------------------------------------------- 3# Name: config.py 4# Description: Configuration data management 5# Author: xaoyaoo 6# Date: 2024/01/16 7# ------------------------------------------------------------------------------- 8import json 9import os 10import logging 11 12 13class ConfData(object): 14 _instances = None 15 16 def __new__(cls, *args, **kwargs): 17 if cls._instances: 18 return cls._instances 19 cls._instances = object.__new__(cls) 20 return cls._instances 21 22 def __init__(self): 23 self._work_path = None 24 self.conf_file = None 25 self.auto_setting = None 26 27 self.is_init = False 28 29 self.conf = {} 30 31 self.init() 32 33 @property 34 def cf(self): 35 if not self.is_init: 36 self.init() 37 return self.conf_file 38 39 @property 40 def work_path(self): 41 if not self.is_init: 42 self.init() 43 return self._work_path 44 45 @property 46 def at(self): 47 if not self.is_init: 48 self.init() 49 return self.auto_setting 50 51 def init(self): 52 self.is_init = False 53 54 work_path = os.getenv("WXDUMP_WORK_PATH") 55 conf_file = os.getenv("WXDUMP_CONF_FILE") 56 auto_setting = os.getenv("WXDUMP_AUTO_SETTING") 57 58 if work_path is None or conf_file is None or auto_setting is None: 59 return False 60 61 self._work_path = work_path 62 self.conf_file = conf_file 63 self.auto_setting = auto_setting 64 self.is_init = True 65 66 if not os.path.exists(self.conf_file): 67 self.set_conf(self.auto_setting, "last", "") 68 self.read_conf() 69 return True 70 71 def read_conf(self): 72 if not os.path.exists(self.conf_file): 73 return False 74 try: 75 with open(self.conf_file, 'r') as f: 76 conf = json.load(f) 77 self.conf = conf 78 return True 79 except FileNotFoundError: 80 logging.error(f"Session file not found: {self.conf_file}") 81 return False 82 except json.JSONDecodeError as e: 83 logging.error(f"Error decoding JSON file: {e}") 84 return False 85 86 def write_conf(self): 87 if not self.is_init: 88 self.init() 89 try: 90 with open(self.conf_file, 'w') as f: 91 json.dump(self.conf, f, indent=4, ensure_ascii=False) 92 return True 93 except Exception as e: 94 logging.error(f"Error writing to file: {e}") 95 return False 96 97 def set_conf(self, wxid, arg, value): 98 if not self.is_init: 99 self.init() 100 if wxid not in self.conf: 101 self.conf[wxid] = {} 102 if not isinstance(self.conf[wxid], dict): 103 self.conf[wxid] = {} 104 self.conf[wxid][arg] = value 105 self.write_conf() 106 107 def get_conf(self, wxid, arg): 108 if not self.is_init: 109 self.init() 110 return self.conf.get(wxid, {}).get(arg, None) 111 112 def get_local_wxids(self): 113 if not self.is_init: 114 self.init() 115 return list(self.conf.keys()) 116 117 def get_db_config(self): 118 if not self.is_init: 119 self.init() 120 my_wxid = self.get_conf(self.at, "last") 121 return self.get_conf(my_wxid, "db_config") 122 123 124gc: ConfData = ConfData() 125 126 127def get_conf_local_wxid(conf_file): 128 try: 129 with open(conf_file, 'r') as f: 130 conf = json.load(f) 131 except FileNotFoundError: 132 logging.error(f"Session file not found: {conf_file}") 133 return None 134 except json.JSONDecodeError as e: 135 logging.error(f"Error decoding JSON file: {e}") 136 return None 137 return list(conf.keys()) 138 139 140def get_conf(conf_file, wxid, arg): 141 try: 142 with open(conf_file, 'r') as f: 143 conf = json.load(f) 144 except FileNotFoundError: 145 logging.error(f"Session file not found: {conf_file}") 146 return None 147 except json.JSONDecodeError as e: 148 logging.error(f"Error decoding JSON file: {e}") 149 return None 150 return conf.get(wxid, {}).get(arg, None) 151 152 153def get_conf_wxids(conf_file): 154 try: 155 with open(conf_file, 'r') as f: 156 conf = json.load(f) 157 except FileNotFoundError: 158 logging.error(f"Session file not found: {conf_file}") 159 return None 160 except json.JSONDecodeError as e: 161 logging.error(f"Error decoding JSON file: {e}") 162 return None 163 return list(conf.keys()) 164 165 166def set_conf(conf_file, wxid, arg, value): 167 try: 168 with open(conf_file, 'r') as f: 169 conf = json.load(f) 170 except FileNotFoundError: 171 conf = {} 172 except json.JSONDecodeError as e: 173 logging.error(f"Error decoding JSON file: {e}") 174 return False 175 176 if wxid not in conf: 177 conf[wxid] = {} 178 if not isinstance(conf[wxid], dict): 179 conf[wxid] = {} 180 conf[wxid][arg] = value 181 try: 182 with open(conf_file, 'w') as f: 183 json.dump(conf, f, indent=4, ensure_ascii=False) 184 except Exception as e: 185 logging.error(f"Error writing to file: {e}") 186 return False 187 return True