From 5197e12482ae48e2dd0d42b2362677bb62f4614e Mon Sep 17 00:00:00 2001 From: NearlyHeadlessJack Date: Tue, 21 Jan 2025 16:58:40 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E5=A4=87=E4=BB=BD=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 3 +- weauth/constants/core_constant.py | 4 +- weauth/utils/backup.py | 61 ++++++++++++++++++++++++++ weauth/utils/create_config_yaml.py | 6 +++ weauth/weauth_boostrap.py | 69 +++++++++++++++++++++--------- 5 files changed, 119 insertions(+), 24 deletions(-) create mode 100644 weauth/utils/backup.py diff --git a/requirements.txt b/requirements.txt index 7e05672..49010e5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,5 @@ requests>=2.32.3 rcon>=2.1.1 tcping==0.1.1rc1 gevent>=24.2.1 -prettytable>=0.2.13 \ No newline at end of file +prettytable>=0.2.13 +crontab>=1.0.1 \ No newline at end of file diff --git a/weauth/constants/core_constant.py b/weauth/constants/core_constant.py index 730b2e9..e6bbe3d 100644 --- a/weauth/constants/core_constant.py +++ b/weauth/constants/core_constant.py @@ -18,8 +18,8 @@ CLI_COMMAND = PACKAGE_NAME # WeAuth Version Storage -VERSION_PYPI: str = '1.6.1' -VERSION: str = '1.6.1' +VERSION_PYPI: str = '1.7.0' +VERSION: str = '1.7.0' # URLs diff --git a/weauth/utils/backup.py b/weauth/utils/backup.py new file mode 100644 index 0000000..d287d54 --- /dev/null +++ b/weauth/utils/backup.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3.10 +# -*- coding: utf-8 -*- +# author: NearlyHeadlessJack +# email: wang@rjack.cn +# datetime: 2025/1/21 15:41 +# ide: PyCharm +# file: backup.py +from crontab import CronTab +from datetime import datetime +import time +import warnings +import os +import shutil + + +class BackUp: + def __init__(self, cron: str): + self.entry = CronTab(cron) + self.__flag = False + os.makedirs('backup', exist_ok=True) + + def next(self) -> float: + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + return self.entry.next() + + def run(self): + while True: + next = self.next() + if next < 60.0 and not self.__flag: + try: + self.backup() + self.__flag = True + except FileNotFoundError: + print('-未找到备份文件!') + elif next >= 60.0: + self.__flag = False + time.sleep(40) + + def backup(self): + print('-准备开始备份...') + now = datetime.now().strftime("%Y_%m_%d_%H%M") + path = 'backup/' + now + os.makedirs(path, exist_ok=True) + self.copy('config.yaml', path) + self.copy('WeAuth.db', path) + self.copy('ops.yaml', path) + self.copy('cdkey.yaml', path) + self.copy('gift_list.yaml', path) + print('-备份完成,./backup/' + path) + + def copy(self, filename: str, path: str) -> None: + try: + shutil.copyfile(filename, path + '/' + filename) + except FileNotFoundError: + print(f'-未找到{filename}!') + + +if __name__ == '__main__': + backup = BackUp('* * * * *') + backup.backup() diff --git a/weauth/utils/create_config_yaml.py b/weauth/utils/create_config_yaml.py index 94df500..28c00b6 100644 --- a/weauth/utils/create_config_yaml.py +++ b/weauth/utils/create_config_yaml.py @@ -14,6 +14,8 @@ def create_config_yaml(config: dict, default_config:dict) -> int: required_keys = [ 'server_connect', + 'backup', + 'backup_cron', 'welcome', 'mcsm_adr', 'mcsm_api', @@ -39,6 +41,8 @@ def create_config_yaml(config: dict, default_config:dict) -> int: config_dict = { 'version': VERSION_PYPI, + 'backup': config['backup'], + 'backup_cron': config['backup_cron'], 'server_connect': config['server_connect'], 'welcome': config['welcome'], 'mcsm_adr': config['mcsm_adr'], @@ -63,6 +67,8 @@ def create_config_yaml(config: dict, default_config:dict) -> int: # 生成 comment 字典 comment_dict = { 'version': '版本号,请勿修改', + 'backup': '是否开启配置及数据库备份 0 为不开启,1 为开启', + 'backup_cron': '备份系统定时,crontab格式,默认为每天2:00进行备份', 'server_connect': '游戏服务器连接方式,0 为MCSManager,1 为rcon', 'welcome': '玩家成功加入白名单后,微信的回复消息', 'mcsm_adr': 'MCSM的url地址', diff --git a/weauth/weauth_boostrap.py b/weauth/weauth_boostrap.py index 48b59f0..8228109 100644 --- a/weauth/weauth_boostrap.py +++ b/weauth/weauth_boostrap.py @@ -3,6 +3,7 @@ # WeAuth is released under the GNU GENERAL PUBLIC LICENSE v3 (GPLv3.0) license. # https://github.com/nearlyheadlessjack/weauth # 程序总入口 +import time from email.policy import default import sys from http.client import responses @@ -20,7 +21,9 @@ from weauth.constants.core_constant import * from weauth.constants import exit_code from weauth.mc_server import MCServerConnection - +from multiprocessing import Process +import threading +from weauth.utils.backup import BackUp def main(args) -> None: """应用程序入口""" @@ -44,6 +47,8 @@ def main(args) -> None: DB.check_database() default_config = { 'server_connect': 0, + 'backup': 1, + 'backup_cron': '* * */1 * *', 'welcome': '欢迎加入我的服务器!\\n如果仍然无法加入服务器, 请联系管理员。\\n祝您游戏愉快!', 'mcsm_adr': 'http://127.0.0.1:23333/', 'mcsm_api': '12345', @@ -109,6 +114,9 @@ def main(args) -> None: url = config['url'] + backup_ = config['backup'] + cron = config['backup_cron'] + if args.url != '/wx': url = args.url @@ -120,27 +128,17 @@ def main(args) -> None: 'welcome': config['welcome'] # 玩家注册白名单成功 } - # 创建Flask实例 - print("-正在启动监听......\n") - listener = Listener(wx_user_name=config['WxUserName'], responses=responses, url=url, game_server=game_server) + # 创建监听进程 + weauth_server = Process(target=server_start, args=(config, responses, url, game_server)) - if config['ssl'] == 1: - ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) - try: - ssl_context.load_cert_chain(certfile=config['ssl_cer'], keyfile=config['ssl_key']) - except FileNotFoundError: - print('-未找到ssl证书文件!') - sys.exit(0) - server = pywsgi.WSGIServer(('0.0.0.0', 443), listener.wx_service, - ssl_context=ssl_context) - print(f"-开始在 https://0.0.0.0{url} 进行监听") - server.serve_forever() - else: - # 核心监听程序运行 - server = pywsgi.WSGIServer(('0.0.0.0', 80), listener.wx_service) - print(f"-开始在 http://0.0.0.0{url} 进行监听") - server.serve_forever() + if backup_ == 1: + # 创建备份管理系统线程 + print('-正在创建备份任务') + backup_thread = threading.Thread(target=backup_server, args=(cron,)) + weauth_server.daemon = True + backup_thread.start() + weauth_server.start() # 读取配置文件 @@ -190,6 +188,35 @@ def check_config_version(config:dict,default_config:dict): create_config_yaml(config=config,default_config=default_config) +def server_start(config: dict, responses: dict, url: str, game_server: MCServerConnection) -> None: + # 创建Flask实例 + print("-正在启动监听......\n") + listener = Listener(wx_user_name=config['WxUserName'], responses=responses, url=url, game_server=game_server) + if config['ssl'] == 1: + ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + try: + ssl_context.load_cert_chain(certfile=config['ssl_cer'], keyfile=config['ssl_key']) + except FileNotFoundError: + print('-未找到ssl证书文件!') + sys.exit(0) + server = pywsgi.WSGIServer(('0.0.0.0', 443), listener.wx_service, + ssl_context=ssl_context) + print(f"-开始在 https://0.0.0.0{url} 进行监听") + server.serve_forever() + else: + # 核心监听程序运行 + server = pywsgi.WSGIServer(('0.0.0.0', 80), listener.wx_service) + print(f"-开始在 http://0.0.0.0{url} 进行监听") + server.serve_forever() + + +def backup_server(cron: str) -> None: + try: + backup_task = BackUp(cron) + print(backup_task.next()) + backup_task.run() - \ No newline at end of file + except ValueError: + print('\033[31m-备份系统计划设置错误,请检查config.yaml中的backup_cron设置是否正确\033[0m') + sys.exit(0) From 2814fc0e5469516b46db7c4032ebb5de301973e8 Mon Sep 17 00:00:00 2001 From: NearlyHeadlessJack Date: Tue, 21 Jan 2025 19:41:28 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E5=A4=87=E4=BB=BD=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- weauth/weauth_boostrap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/weauth/weauth_boostrap.py b/weauth/weauth_boostrap.py index 8228109..dde4a5d 100644 --- a/weauth/weauth_boostrap.py +++ b/weauth/weauth_boostrap.py @@ -48,7 +48,7 @@ def main(args) -> None: default_config = { 'server_connect': 0, 'backup': 1, - 'backup_cron': '* * */1 * *', + 'backup_cron': '0 2 * * *', 'welcome': '欢迎加入我的服务器!\\n如果仍然无法加入服务器, 请联系管理员。\\n祝您游戏愉快!', 'mcsm_adr': 'http://127.0.0.1:23333/', 'mcsm_api': '12345',