diff --git a/main.py b/main.py index b0f3156..89a2dac 100644 --- a/main.py +++ b/main.py @@ -52,7 +52,7 @@ def create_system_tray() -> bool: def on_open_browser(icon, item): try: - webbrowser.open("http://localhost:5000") + webbrowser.open(f"http://localhost:{config_manager.app_config.port}") except Exception: pass @@ -91,16 +91,16 @@ def create_system_tray() -> bool: return False -def open_browser_delayed() -> None: +def open_browser_delayed(port: int) -> None: """延迟打开浏览器""" time.sleep(2) try: - webbrowser.open("http://localhost:5000") + webbrowser.open(f"http://localhost:{port}") if config_manager.app_config.show_console: - print(t("main.browser_opened")) + print(t("main.browser_opened", port=port)) except Exception: if config_manager.app_config.show_console: - print(t("main.browser_open_failed")) + print(t("main.browser_open_failed", port=port)) def start_web_server() -> None: @@ -109,7 +109,11 @@ def start_web_server() -> None: from uvicorn import Config from uvicorn.server import Server - server = Server(Config(app, host="0.0.0.0", port=5000, log_level="error")) + server = Server( + Config( + app, host="0.0.0.0", port=config_manager.app_config.port, log_level="error" + ) + ) server.run() @@ -133,7 +137,9 @@ def main() -> None: print(t("main.tray_created")) # 启动浏览器 - browser_thread = threading.Thread(target=open_browser_delayed) + browser_thread = threading.Thread( + target=open_browser_delayed, args=(config.port,) + ) browser_thread.daemon = True browser_thread.start() diff --git a/src/config.py b/src/config.py index 21fd404..24e89ac 100644 --- a/src/config.py +++ b/src/config.py @@ -11,6 +11,7 @@ from .utils.i18n import t DEFAULT_CONFIG = { "KEY": "", + "Port": 5000, "Debug_Mode": False, "Logging_Files": True, "Show_Console": False, @@ -53,6 +54,7 @@ class ConfigManager: self.app_config = AppConfig( key=self._config_data.get("KEY", ""), + port=self._config_data.get("Port", 5000), custom_steam_path=self._config_data.get("Custom_Steam_Path", ""), debug_mode=self._config_data.get("Debug_Mode", False), logging_files=self._config_data.get("Logging_Files", True), @@ -67,6 +69,7 @@ class ConfigManager: print(t("config.regenerated")) self.app_config = AppConfig( key=DEFAULT_CONFIG.get("KEY", ""), + port=DEFAULT_CONFIG.get("Port", 5000), custom_steam_path=DEFAULT_CONFIG.get("Custom_Steam_Path", ""), debug_mode=DEFAULT_CONFIG.get("Debug_Mode", False), logging_files=DEFAULT_CONFIG.get("Logging_Files", True), @@ -82,6 +85,7 @@ class ConfigManager: print(t("config.use_default")) self.app_config = AppConfig( key=DEFAULT_CONFIG.get("KEY", ""), + port=DEFAULT_CONFIG.get("Port", 5000), custom_steam_path=DEFAULT_CONFIG.get("Custom_Steam_Path", ""), debug_mode=DEFAULT_CONFIG.get("Debug_Mode", False), logging_files=DEFAULT_CONFIG.get("Logging_Files", True), diff --git a/src/constants.py b/src/constants.py index f097e09..29d4ab8 100644 --- a/src/constants.py +++ b/src/constants.py @@ -17,7 +17,7 @@ def check_ip(): body = req.json() print("已获取IP属地") return bool(body["flag"]) - except: + except BaseException: print("获取IP属地失败, 默认您位于中国大陆境内") return True diff --git a/src/models.py b/src/models.py index 79ed38c..19caf0a 100644 --- a/src/models.py +++ b/src/models.py @@ -46,6 +46,7 @@ class AppConfig: """应用配置""" key: str = "" + port: int = 5000 custom_steam_path: str = "" debug_mode: bool = False logging_files: bool = True diff --git a/src/utils/i18n.py b/src/utils/i18n.py index b1e1a6d..f3bb6ef 100644 --- a/src/utils/i18n.py +++ b/src/utils/i18n.py @@ -21,11 +21,13 @@ class I18n: "main.starting": "正在启动Onekey...", "main.tray_created": "系统托盘已创建", "main.browser_opened": "浏览器已自动打开", - "main.browser_open_failed": "无法自动打开浏览器,请手动访问: http://localhost:5000", + "main.browser_open_failed": "无法自动打开浏览器,请手动访问: http://localhost:{port}", "main.exit": "程序已退出", "main.start_error": "启动错误: {error}", "main.press_enter": "按回车键退出...", "main.startup_failed": "启动失败: {error}", + "main.checkip_success": "已获取IP属地", + "main.checkip_failed": "获取IP属地失败: {error}, 默认您在中国大陆境内", # 配置 "config.generated": "配置文件已生成", "config.create_failed": "配置文件创建失败: {error}", @@ -70,7 +72,7 @@ class I18n: "error.ensure_root": "请确保在项目根目录中运行此程序", # Web相关 "web.starting": "启动Onekey Web GUI...", - "web.visit": "请在浏览器中访问: http://localhost:5000", + "web.visit": "请在浏览器中访问: http://localhost:{port}", "web.task_running": "已有任务正在运行", "web.invalid_appid": "请输入有效的App ID", "web.invalid_format": "App ID格式无效", @@ -109,11 +111,13 @@ class I18n: "main.starting": "Starting Onekey...", "main.tray_created": "System tray created", "main.browser_opened": "Browser opened automatically", - "main.browser_open_failed": "Failed to open browser automatically, please visit: http://localhost:5000", + "main.browser_open_failed": "Failed to open browser automatically, please visit: http://localhost:{port}", "main.exit": "Program exited", "main.start_error": "Startup error: {error}", "main.press_enter": "Press Enter to exit...", "main.startup_failed": "Startup failed: {error}", + "main.checkip_success": "Obtained IP territory", + "main.checkip_failed": "Failed to obtain IP territory: {error}, by default you are in mainland China", # Configuration "config.generated": "Configuration file generated", "config.create_failed": "Failed to create configuration file: {error}", @@ -158,7 +162,7 @@ class I18n: "error.ensure_root": "Please ensure running this program from project root", # Web related "web.starting": "Starting Onekey Web GUI...", - "web.visit": "Please visit: http://localhost:5000", + "web.visit": "Please visit: http://localhost:{port}", "web.task_running": "A task is already running", "web.invalid_appid": "Please enter a valid App ID", "web.invalid_format": "Invalid App ID format", diff --git a/web/app.py b/web/app.py index 62d6d91..b2c63ee 100644 --- a/web/app.py +++ b/web/app.py @@ -63,7 +63,7 @@ class ConnectionManager: for connection in self.active_connections: try: await connection.send_text(message) - except: + except BaseException: # 连接可能已关闭 pass @@ -121,7 +121,7 @@ class WebOnekeyApp: try: if hasattr(self.onekey_app, "client"): await self.onekey_app.client.close() - except: + except BaseException: pass self.onekey_app = None @@ -302,7 +302,7 @@ async def get_task_status(): @app.get("/about") -async def settings_page(request: Request): +async def about_page(request: Request): """关于页面""" return templates.TemplateResponse("about.html", {"request": request}) @@ -329,6 +329,7 @@ async def update_config(request: Request): # 准备新的配置数据 new_config = { "KEY": data.get("key", ""), + "Port": config_manager.app_config.port, "Custom_Steam_Path": data.get("steam_path", ""), "Debug_Mode": data.get("debug_mode", False), "Logging_Files": data.get("logging_files", True), @@ -444,4 +445,4 @@ async def websocket_endpoint(websocket: WebSocket): print(t("web.starting")) -print(t("web.visit")) +print(t("web.visit", port=config.app_config.port)) diff --git a/web/en/templates/about.html b/web/en/templates/about.html index bf5130d..4120b67 100644 --- a/web/en/templates/about.html +++ b/web/en/templates/about.html @@ -33,7 +33,7 @@
直观,优雅的游戏解锁解决方案