65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""本机激活信息:通用包首次激活后写入 device.json。"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import platform
|
|
import uuid
|
|
from pathlib import Path
|
|
from typing import Any, Optional
|
|
|
|
APP_DIR = Path(os.environ.get("GOLD_MONITOR_DATA", Path.home() / ".gold_monitor"))
|
|
DEVICE_FILE = APP_DIR / "device.json"
|
|
|
|
|
|
def ensure_dir() -> None:
|
|
APP_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
def machine_fingerprint() -> str:
|
|
"""相对稳定的本机指纹(非绝对防伪,够用区分机器)。"""
|
|
node = uuid.getnode()
|
|
name = platform.node() or "pc"
|
|
return f"{name}-{node:012x}"
|
|
|
|
|
|
def load_device() -> dict[str, Any]:
|
|
ensure_dir()
|
|
if not DEVICE_FILE.is_file():
|
|
return {
|
|
"server": "",
|
|
"token": "",
|
|
"account_id": None,
|
|
"machine_id": None,
|
|
"fingerprint": machine_fingerprint(),
|
|
"machine_name": platform.node() or "PC",
|
|
}
|
|
try:
|
|
data = json.loads(DEVICE_FILE.read_text(encoding="utf-8"))
|
|
except (OSError, json.JSONDecodeError):
|
|
data = {}
|
|
data.setdefault("fingerprint", machine_fingerprint())
|
|
data.setdefault("machine_name", platform.node() or "PC")
|
|
data.setdefault("server", "")
|
|
data.setdefault("token", "")
|
|
return data
|
|
|
|
|
|
def save_device(data: dict[str, Any]) -> None:
|
|
ensure_dir()
|
|
DEVICE_FILE.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
|
|
|
|
def is_activated() -> bool:
|
|
d = load_device()
|
|
return bool(d.get("token") and d.get("server"))
|
|
|
|
|
|
def clear_activation() -> None:
|
|
d = load_device()
|
|
d["token"] = ""
|
|
d["account_id"] = None
|
|
d["machine_id"] = None
|
|
save_device(d)
|