File size: 1,768 Bytes
9157f90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""运行时补丁:包装 save_account_to_db 并在成功后发送 webhook。"""

from __future__ import annotations

import json
import os
import sys
import traceback
from urllib import error, request
from message_producer import publish_to_queue

print("正在加载 bootstrap.py...")


def _log(message: str) -> None:
    print(f"[Hook] {message}", file=sys.stderr)


def _install_patch() -> None:
    import utils.db_manager as db_manager

    if getattr(db_manager.save_account_to_db, "__sitecustomize_patched__", False):
        _log("save_account_to_db 已补丁,跳过")
        return

    original_save = db_manager.save_account_to_db

    def patched_save_account_to_db(
        email: str, password: str, token_json_str: str
    ) -> bool:
        ok = original_save(email, password, token_json_str)
        if ok:
            print(f"save_account_to_db 成功,准备发送消息: {email}")
            try:
                publish_to_queue(
                    {
                        "action": "create",
                        "data": {
                            "email": email,
                            "password": password,
                            "token": token_json_str,
                        },
                    }
                )
            except error.URLError as exc:
                _log(f"发送消息失败: {exc}")
            except Exception:
                _log("执行异常")
                traceback.print_exc()
        return ok

    patched_save_account_to_db.__sitecustomize_patched__ = True
    db_manager.save_account_to_db = patched_save_account_to_db
    _log("save_account_to_db 补丁已安装")


try:
    _install_patch()
except Exception:
    _log("补丁安装失败")
    traceback.print_exc()