Add Debian deployment, MariaDB storage and randomized proxy routing

This commit is contained in:
Codex
2026-10-11 19:07:27 +08:00
parent 2e3439dbb9
commit 915241a2fd
27 changed files with 896 additions and 32 deletions
+35 -4
View File
@@ -2,6 +2,7 @@
import ctypes
import json
import os
import platform
import threading
import time
from pathlib import Path
@@ -40,15 +41,41 @@ class CookieVault:
self.path = Path(directory) / "chsi-cookie.enc"
self.path.parent.mkdir(parents=True, exist_ok=True)
def _cipher(self):
from cryptography.fernet import Fernet
key_path = self.path.parent / "cookie.key"
if not key_path.exists():
try:
descriptor = os.open(key_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
except FileExistsError:
pass
else:
with os.fdopen(descriptor, "wb") as stream:
stream.write(Fernet.generate_key())
return Fernet(key_path.read_bytes())
def save(self, cookies):
temporary = self.path.with_suffix(".tmp")
temporary.write_bytes(protect(json.dumps(cookies, ensure_ascii=False).encode()))
payload = json.dumps(cookies, ensure_ascii=False).encode()
encrypted = protect(payload) if platform.system() == "Windows" else self._cipher().encrypt(payload)
descriptor = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
with os.fdopen(descriptor, "wb") as stream:
stream.write(encrypted)
temporary.replace(self.path)
def load(self):
if not self.path.exists():
return {}
return json.loads(protect(self.path.read_bytes(), decrypt=True))
payload = self.path.read_bytes()
if platform.system() == "Windows":
payload = protect(payload, decrypt=True)
else:
from cryptography.fernet import InvalidToken
try:
payload = self._cipher().decrypt(payload)
except InvalidToken:
raise ValueError("保存的 Cookie 无法解密,请重新登录") from None
return json.loads(payload)
def clear(self):
self.path.unlink(missing_ok=True)
@@ -94,7 +121,7 @@ class AuthService:
with self.state_lock:
if self.valid and self.verified_route != self.transport.router.status()["route"]:
self.invalidate("网络出口已变化,请验证目录访问后继续采集", state="unverified")
return {"saved": self.vault.path.exists(), "valid": self.valid, "state": self.state, "message": self.message, "checked_at": self.checked_at, "browser_state": self.browser_state, "browser_message": self.browser_message}
return {"saved": self.vault.path.exists(), "valid": self.valid, "state": self.state, "message": self.message, "checked_at": self.checked_at, "browser_state": self.browser_state, "browser_message": self.browser_message, "browser_supported": platform.system() == "Windows"}
def invalidate(self, message, state="login_required"):
with self.state_lock:
@@ -128,7 +155,7 @@ class AuthService:
return self.status()
def check(self):
with self.check_lock:
with self.check_lock, self.transport.router.hold_route():
with self.state_lock:
self.invalidate("正在同步研招目录会话并验证访问权限", state="unverified")
revision = self.revision
@@ -182,6 +209,10 @@ class AuthService:
return self.status()
def browser(self):
if platform.system() != "Windows":
self.browser_state = "unsupported"
self.browser_message = "服务器无桌面环境:请在自己电脑的浏览器完成学信网登录,再粘贴研招网 Cookie 并验证目录访问"
return self.status()
if self.browser_thread and self.browser_thread.is_alive() and not self.browser_cancel.is_set():
return self.status()
proxy = self.transport.router.resolve()