feat(core):初步完成主调度自动代理功能开发

This commit is contained in:
DLmaster
2025-01-26 07:58:33 +08:00
parent 7e08c88a3e
commit c625354dec
41 changed files with 1645 additions and 694 deletions

View File

@@ -29,8 +29,8 @@ __version__ = "4.2.0"
__author__ = "DLmaster361 <DLmaster_361@163.com>"
__license__ = "GPL-3.0 license"
from .notification import Notification
from .security import CryptoHandler
from .system import SystemHandler
from .notification import Notify
from .security import Crypto
from .system import System
__all__ = ["Notification", "CryptoHandler", "SystemHandler"]
__all__ = ["Notify", "Crypto", "System"]

View File

@@ -30,27 +30,22 @@ import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr
import os
from app.core import AppConfig
from app.core import Config
class Notification:
def __init__(self, config: AppConfig):
self.config = config
def push_notification(self, title, message, ticker, t):
"""推送系统通知"""
if self.config.global_config.get(self.config.global_config.notify_IfPushPlyer):
if Config.global_config.get(Config.global_config.notify_IfPushPlyer):
notification.notify(
title=title,
message=message,
app_name="AUTO_MAA",
app_icon=str(self.config.app_path / "resources/icons/AUTO_MAA.ico"),
app_icon=str(Config.app_path / "resources/icons/AUTO_MAA.ico"),
timeout=t,
ticker=ticker,
toast=True,
@@ -64,7 +59,7 @@ class Notification:
# 声明此邮箱为AUTO_MAA项目组资产未经授权不得私自使用
# 注意此声明注释只有使用者更换发信邮箱时才能删除本条规则优先级高于GPLv3
if self.config.global_config.get(self.config.global_config.notify_IfSendMail):
if Config.global_config.get(Config.global_config.notify_IfSendMail):
# 第三方 SMTP 服务配置
mail_host = "smtp.163.com" # 设置服务器
@@ -82,9 +77,7 @@ class Notification:
message["To"] = formataddr(
(
Header("AUTO_MAA用户", "utf-8").encode(),
self.config.global_config.get(
self.config.global_config.notify_MailAddress
),
Config.global_config.get(Config.global_config.notify_MailAddress),
)
) # 收件人显示的名字
message["Subject"] = Header(title, "utf-8")
@@ -94,9 +87,7 @@ class Notification:
smtpObj.login(mail_sender, mail_key)
smtpObj.sendmail(
mail_sender,
self.config.global_config.get(
self.config.global_config.notify_MailAddress
),
Config.global_config.get(Config.global_config.notify_MailAddress),
message.as_string(),
)
return True
@@ -104,3 +95,6 @@ class Notification:
return f"发送邮件时出错:\n{e}"
finally:
smtpObj.quit()
Notify = Notification()

View File

@@ -25,7 +25,6 @@ v4.2
作者DLmaster_361
"""
import os
import hashlib
import random
import secrets
@@ -34,37 +33,33 @@ from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Util.Padding import pad, unpad
from app.core import AppConfig
from app.core import Config
class CryptoHandler:
def __init__(self, config: AppConfig):
self.config = config
def get_PASSWORD(self, PASSWORD: str) -> None:
"""配置管理密钥"""
# 生成目录
self.config.key_path.mkdir(parents=True, exist_ok=True)
Config.key_path.mkdir(parents=True, exist_ok=True)
# 生成RSA密钥对
key = RSA.generate(2048)
public_key_local = key.publickey()
private_key = key
# 保存RSA公钥
(self.config.app_path / "data/key/public_key.pem").write_bytes(
(Config.app_path / "data/key/public_key.pem").write_bytes(
public_key_local.exportKey()
)
# 生成密钥转换与校验随机盐
PASSWORD_salt = secrets.token_hex(random.randint(32, 1024))
(self.config.app_path / "data/key/PASSWORDsalt.txt").write_text(
(Config.app_path / "data/key/PASSWORDsalt.txt").write_text(
PASSWORD_salt,
encoding="utf-8",
)
verify_salt = secrets.token_hex(random.randint(32, 1024))
(self.config.app_path / "data/key/verifysalt.txt").write_text(
(Config.app_path / "data/key/verifysalt.txt").write_text(
verify_salt,
encoding="utf-8",
)
@@ -76,22 +71,20 @@ class CryptoHandler:
AES_password_verify = hashlib.sha256(
AES_password + verify_salt.encode("utf-8")
).digest()
(self.config.app_path / "data/key/AES_password_verify.bin").write_bytes(
(Config.app_path / "data/key/AES_password_verify.bin").write_bytes(
AES_password_verify
)
# AES-256加密RSA私钥并保存密文
AES_key = AES.new(AES_password, AES.MODE_ECB)
private_key_local = AES_key.encrypt(pad(private_key.exportKey(), 32))
(self.config.app_path / "data/key/private_key.bin").write_bytes(
private_key_local
)
(Config.app_path / "data/key/private_key.bin").write_bytes(private_key_local)
def encryptx(self, note: str) -> bytes:
"""加密数据"""
# 读取RSA公钥
public_key_local = RSA.import_key(
(self.config.app_path / "data/key/public_key.pem").read_bytes()
(Config.app_path / "data/key/public_key.pem").read_bytes()
)
# 使用RSA公钥对数据进行加密
cipher = PKCS1_OAEP.new(public_key_local)
@@ -103,22 +96,20 @@ class CryptoHandler:
# 读入RSA私钥密文、盐与校验哈希值
private_key_local = (
(self.config.app_path / "data/key/private_key.bin").read_bytes().strip()
(Config.app_path / "data/key/private_key.bin").read_bytes().strip()
)
PASSWORD_salt = (
(self.config.app_path / "data/key/PASSWORDsalt.txt")
(Config.app_path / "data/key/PASSWORDsalt.txt")
.read_text(encoding="utf-8")
.strip()
)
verify_salt = (
(self.config.app_path / "data/key/verifysalt.txt")
(Config.app_path / "data/key/verifysalt.txt")
.read_text(encoding="utf-8")
.strip()
)
AES_password_verify = (
(self.config.app_path / "data/key/AES_password_verify.bin")
.read_bytes()
.strip()
(Config.app_path / "data/key/AES_password_verify.bin").read_bytes().strip()
)
# 将管理密钥转化为AES-256密钥并验证
AES_password = hashlib.sha256(
@@ -149,7 +140,7 @@ class CryptoHandler:
# 使用新管理密钥重新加密
self.get_PASSWORD(PASSWORD_new)
for i in range(len(data)):
self.config.cur.execute(
Config.cur.execute(
"UPDATE adminx SET password = ? WHERE mode = ? AND uid = ?",
(
self.encryptx(new_data[i]),
@@ -157,7 +148,7 @@ class CryptoHandler:
data[i][16],
),
)
self.config.db.commit(),
Config.db.commit(),
new_data[i] = None
del new_data
@@ -165,3 +156,6 @@ class CryptoHandler:
"""验证管理密钥"""
return bool(self.decryptx(self.encryptx(""), PASSWORD) != "管理密钥错误")
Crypto = CryptoHandler()

View File

@@ -31,7 +31,7 @@ import win32process
import winreg
import psutil
from app.core import AppConfig
from app.core import Config
class SystemHandler:
@@ -39,9 +39,7 @@ class SystemHandler:
ES_CONTINUOUS = 0x80000000
ES_SYSTEM_REQUIRED = 0x00000001
def __init__(self, config: AppConfig):
self.config = config
def __init__(self):
self.set_Sleep()
self.set_SelfStart()
@@ -49,9 +47,7 @@ class SystemHandler:
def set_Sleep(self):
"""同步系统休眠状态"""
if self.config.global_config.get(
self.config.global_config.function_IfAllowSleep
):
if Config.global_config.get(Config.global_config.function_IfAllowSleep):
# 设置系统电源状态
ctypes.windll.kernel32.SetThreadExecutionState(
self.ES_CONTINUOUS | self.ES_SYSTEM_REQUIRED
@@ -64,7 +60,7 @@ class SystemHandler:
"""同步开机自启"""
if (
self.config.global_config.get(self.config.global_config.start_IfSelfStart)
Config.global_config.get(Config.global_config.start_IfSelfStart)
and not self.is_startup()
):
key = winreg.OpenKey(
@@ -73,14 +69,10 @@ class SystemHandler:
winreg.KEY_SET_VALUE,
winreg.KEY_ALL_ACCESS | winreg.KEY_WRITE | winreg.KEY_CREATE_SUB_KEY,
)
winreg.SetValueEx(
key, "AUTO_MAA", 0, winreg.REG_SZ, self.config.app_path_sys
)
winreg.SetValueEx(key, "AUTO_MAA", 0, winreg.REG_SZ, Config.app_path_sys)
winreg.CloseKey(key)
elif (
not self.config.global_config.get(
self.config.global_config.start_IfSelfStart
)
not Config.global_config.get(Config.global_config.start_IfSelfStart)
and self.is_startup()
):
key = winreg.OpenKey(
@@ -123,3 +115,6 @@ class SystemHandler:
window_info = []
win32gui.EnumWindows(callback, window_info)
return window_info
System = SystemHandler()