From 694d1e4b65a21c8fc1b6880d9534753d68014e08 Mon Sep 17 00:00:00 2001 From: Loren Eteval Date: Tue, 2 Dec 2025 16:40:47 +0800 Subject: [PATCH] Switch to enum implementation for routing and proxy mode Signed-off-by: Loren Eteval --- Furious/Core/CoreManager.py | 12 ++++++------ Furious/Frozenlib/{AppCommands.py => Enum.py} | 19 +++++++++++++++++-- Furious/Frozenlib/Utility.py | 4 ++-- Furious/Frozenlib/__init__.py | 2 +- Furious/TrayActions/EditConfiguration.py | 2 ++ Furious/TrayActions/Exit.py | 2 ++ Furious/TrayActions/Import.py | 2 ++ Furious/TrayActions/Language.py | 2 ++ Furious/TrayActions/Routing.py | 16 +++++++++------- Furious/TrayActions/Settings.py | 2 ++ Furious/TrayActions/SystemProxy.py | 18 +++++++++++------- Furious/Utility/SystemProxy.py | 2 +- Furious/Widget/Application.py | 14 +++++++------- Furious/Widget/UserServersQTableWidget.py | 4 ++-- Furious/Window/AppMainWindow.py | 4 ++-- 15 files changed, 68 insertions(+), 37 deletions(-) rename Furious/Frozenlib/{AppCommands.py => Enum.py} (72%) diff --git a/Furious/Core/CoreManager.py b/Furious/Core/CoreManager.py index f36fe62..896259a 100644 --- a/Furious/Core/CoreManager.py +++ b/Furious/Core/CoreManager.py @@ -155,7 +155,7 @@ class CoreManager(Mixins.CleanupOnExit): for attr in ['access', 'error']: fixLogObjectPath(config, attr, logRedirectValue, log) - if routing == 'Bypass Mainland China': + if routing == AppBuiltinRouting.BypassMainlandChina.value: # TUN Mode handling if not proxyModeOnly and SystemRuntime.isTUNMode(): showMBoxDirectRulesNotAllowed() @@ -195,9 +195,9 @@ class CoreManager(Mixins.CleanupOnExit): }, ], } - elif routing == 'Global': + elif routing == AppBuiltinRouting.Global.value: routingObject = {} - elif routing == 'Custom': + elif routing == AppBuiltinRouting.Custom.value: routingObject = config.get('routing', {}) else: routingObject = {} @@ -225,7 +225,7 @@ class CoreManager(Mixins.CleanupOnExit): log=True, **kwargs, ): - if routing == 'Bypass Mainland China': + if routing == AppBuiltinRouting.BypassMainlandChina.value: # TUN Mode handling if not proxyModeOnly and SystemRuntime.isTUNMode(): showMBoxDirectRulesNotAllowed() @@ -236,12 +236,12 @@ class CoreManager(Mixins.CleanupOnExit): 'rule': DATA_DIR / 'hysteria' / 'bypass-mainland-China.acl', 'mmdb': DATA_DIR / 'hysteria' / 'country.mmdb', } - elif routing == 'Global': + elif routing == AppBuiltinRouting.Global.value: routingObject = { 'rule': '', 'mmdb': '', } - elif routing == 'Custom': + elif routing == AppBuiltinRouting.Custom.value: routingObject = { 'rule': config.get('acl', ''), 'mmdb': config.get('mmdb', ''), diff --git a/Furious/Frozenlib/AppCommands.py b/Furious/Frozenlib/Enum.py similarity index 72% rename from Furious/Frozenlib/AppCommands.py rename to Furious/Frozenlib/Enum.py index 26618e2..590b7aa 100644 --- a/Furious/Frozenlib/AppCommands.py +++ b/Furious/Frozenlib/Enum.py @@ -19,9 +19,24 @@ from __future__ import annotations from enum import Enum -__all__ = ['AppCommands'] +__all__ = [ + 'AppBuiltinCommand', + 'AppBuiltinRouting', + 'AppBuiltinProxyMode', +] -class AppCommands(Enum): +class AppBuiltinCommand(Enum): Empty = 'empty' RunAs = 'runas' + + +class AppBuiltinRouting(Enum): + BypassMainlandChina = 'Bypass Mainland China' + Global = 'Global' + Custom = 'Custom' + + +class AppBuiltinProxyMode(Enum): + Auto = 'Auto' + NoChanges = 'NoChanges' diff --git a/Furious/Frozenlib/Utility.py b/Furious/Frozenlib/Utility.py index bc4bbfb..3d15be3 100644 --- a/Furious/Frozenlib/Utility.py +++ b/Furious/Frozenlib/Utility.py @@ -19,10 +19,10 @@ from __future__ import annotations from Furious.Frozenlib.Constants import * +from enum import Enum from typing import AnyStr, Tuple import os -import enum import time import pathlib import operator @@ -43,7 +43,7 @@ __all__ = [ ] -class Protocol(enum.Enum): +class Protocol(Enum): Unknown = 'Unknown' VMess = 'VMess' VLESS = 'VLESS' diff --git a/Furious/Frozenlib/__init__.py b/Furious/Frozenlib/__init__.py index b0b85bf..e9f2dfc 100644 --- a/Furious/Frozenlib/__init__.py +++ b/Furious/Frozenlib/__init__.py @@ -15,9 +15,9 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from .AppCommands import * from .AppSettings import * from .Constants import * +from .Enum import * from .Globals import * from .Mixins import * from .PySide6Legacy import * diff --git a/Furious/TrayActions/EditConfiguration.py b/Furious/TrayActions/EditConfiguration.py index 28d8119..63283ac 100644 --- a/Furious/TrayActions/EditConfiguration.py +++ b/Furious/TrayActions/EditConfiguration.py @@ -15,6 +15,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from __future__ import annotations + from Furious.Frozenlib import * from Furious.Qt import * from Furious.Qt import gettext as _ diff --git a/Furious/TrayActions/Exit.py b/Furious/TrayActions/Exit.py index a51e85b..7542f6f 100644 --- a/Furious/TrayActions/Exit.py +++ b/Furious/TrayActions/Exit.py @@ -15,6 +15,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from __future__ import annotations + from Furious.Frozenlib import * from Furious.Qt import * from Furious.Qt import gettext as _ diff --git a/Furious/TrayActions/Import.py b/Furious/TrayActions/Import.py index 2512886..b2fcb42 100644 --- a/Furious/TrayActions/Import.py +++ b/Furious/TrayActions/Import.py @@ -15,6 +15,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from __future__ import annotations + from Furious.Frozenlib import * from Furious.Library import * from Furious.Qt import * diff --git a/Furious/TrayActions/Language.py b/Furious/TrayActions/Language.py index 8b2029b..960a97b 100644 --- a/Furious/TrayActions/Language.py +++ b/Furious/TrayActions/Language.py @@ -15,6 +15,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from __future__ import annotations + from Furious.Frozenlib import * from Furious.Qt import * from Furious.Qt import gettext as _ diff --git a/Furious/TrayActions/Routing.py b/Furious/TrayActions/Routing.py index f06a0e3..9dd11d8 100644 --- a/Furious/TrayActions/Routing.py +++ b/Furious/TrayActions/Routing.py @@ -15,15 +15,17 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from __future__ import annotations + from Furious.Frozenlib import * from Furious.Qt import * from Furious.Qt import gettext as _ __all__ = ['RoutingAction'] -BUILTIN_ROUTING = ['Bypass Mainland China', 'Global', 'Custom'] - -registerAppSettings('Routing', validRange=BUILTIN_ROUTING) +registerAppSettings( + 'Routing', validRange=list(routing.value for routing in AppBuiltinRouting) +) # ALL BUILTIN ROUTING VALUE _TRANSLATABLE_BUILTIN_ROUTING = [ @@ -51,7 +53,7 @@ class RoutingAction(AppQAction): def __init__(self, **kwargs): if AppSettings.get('Routing') == 'Bypass': # Update value for backward compatibility - AppSettings.set('Routing', 'Bypass Mainland China') + AppSettings.set('Routing', AppBuiltinRouting.BypassMainlandChina.value) super().__init__( _('Routing'), @@ -59,11 +61,11 @@ class RoutingAction(AppQAction): menu=AppQMenu( *list( RoutingChildAction( - _(routing), + _(routing.value), checkable=True, - checked=AppSettings.get('Routing') == routing, + checked=AppSettings.get('Routing') == routing.value, ) - for routing in BUILTIN_ROUTING + for routing in AppBuiltinRouting ), ), **kwargs, diff --git a/Furious/TrayActions/Settings.py b/Furious/TrayActions/Settings.py index 3ee74cd..690c77b 100644 --- a/Furious/TrayActions/Settings.py +++ b/Furious/TrayActions/Settings.py @@ -15,6 +15,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from __future__ import annotations + from Furious.Frozenlib import * from Furious.Qt import * from Furious.Qt import gettext as _ diff --git a/Furious/TrayActions/SystemProxy.py b/Furious/TrayActions/SystemProxy.py index 6321899..94b02ae 100644 --- a/Furious/TrayActions/SystemProxy.py +++ b/Furious/TrayActions/SystemProxy.py @@ -15,15 +15,17 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from __future__ import annotations + from Furious.Frozenlib import * from Furious.Qt import * from Furious.Qt import gettext as _ __all__ = ['SystemProxyAction'] -BUILTIN_PROXY_MODE = ['Auto', 'NoChanges'] - -registerAppSettings('SystemProxyMode', validRange=BUILTIN_PROXY_MODE) +registerAppSettings( + 'SystemProxyMode', validRange=list(mode.value for mode in AppBuiltinProxyMode) +) class SystemProxyChildAction(AppQAction): @@ -32,9 +34,9 @@ class SystemProxyChildAction(AppQAction): def triggeredCallback(self, checked): if self.textCompare('Automatically Configure System Proxy'): - AppSettings.set('SystemProxyMode', 'Auto') + AppSettings.set('SystemProxyMode', AppBuiltinProxyMode.Auto.value) elif self.textCompare('Do Not Change System Proxy'): - AppSettings.set('SystemProxyMode', 'NoChanges') + AppSettings.set('SystemProxyMode', AppBuiltinProxyMode.NoChanges.value) class SystemProxyAction(AppQAction): @@ -46,12 +48,14 @@ class SystemProxyAction(AppQAction): SystemProxyChildAction( _('Automatically Configure System Proxy'), checkable=True, - checked=AppSettings.get('SystemProxyMode') == 'Auto', + checked=AppSettings.get('SystemProxyMode') + == AppBuiltinProxyMode.Auto.value, ), SystemProxyChildAction( _('Do Not Change System Proxy'), checkable=True, - checked=AppSettings.get('SystemProxyMode') == 'NoChanges', + checked=AppSettings.get('SystemProxyMode') + == AppBuiltinProxyMode.NoChanges.value, ), ), **kwargs, diff --git a/Furious/Utility/SystemProxy.py b/Furious/Utility/SystemProxy.py index d2b7792..332a177 100644 --- a/Furious/Utility/SystemProxy.py +++ b/Furious/Utility/SystemProxy.py @@ -29,7 +29,7 @@ logger = logging.getLogger(__name__) def handleAppSystemProxyMode() -> bool: try: - if AppSettings.get('SystemProxyMode') == 'Auto': + if AppSettings.get('SystemProxyMode') == AppBuiltinProxyMode.Auto.value: # Automatically configure return True else: diff --git a/Furious/Widget/Application.py b/Furious/Widget/Application.py index 60418b2..98276d7 100644 --- a/Furious/Widget/Application.py +++ b/Furious/Widget/Application.py @@ -89,17 +89,17 @@ class SingletonApplication(ApplicationExitHelper): if self.socket.waitForConnected(1000): if len(sys.argv) == 1: - command = AppCommands.Empty.value + command = AppBuiltinCommand.Empty.value else: command = sys.argv[1] self.socket.write(command.encode()) self.socket.flush() - if command == AppCommands.Empty.value: + if command == AppBuiltinCommand.Empty.value: # Show tray message in the started instance. Do not start return True - elif command == AppCommands.RunAs.value: + elif command == AppBuiltinCommand.RunAs.value: if self.socket.waitForDisconnected(3000): # The other instance have been exited. Start return False @@ -198,7 +198,7 @@ class Application(ApplicationFactory, SingletonApplication): else: datastr = str(data) - if datastr == AppCommands.Empty.value: + if datastr == AppBuiltinCommand.Empty.value: if isinstance(self.systemTray, SystemTrayIcon): logger.info('attempting to start multiple instance. Show tray message') @@ -206,7 +206,7 @@ class Application(ApplicationFactory, SingletonApplication): else: # The tray hasn't been initialized. Do nothing pass - elif datastr == AppCommands.RunAs.value: + elif datastr == AppBuiltinCommand.RunAs.value: logger.info('detected requests to start as admin in new instance. Exiting') self.exit() @@ -301,7 +301,7 @@ class Application(ApplicationFactory, SingletonApplication): def cleanup(): Mixins.CleanupOnExit.cleanupAll() - if AppSettings.get('SystemProxyMode') == 'Auto': + if AppSettings.get('SystemProxyMode') == AppBuiltinProxyMode.Auto.value: # Automatically configure SystemProxy.off() SystemProxy.daemonOff() @@ -452,7 +452,7 @@ class Application(ApplicationFactory, SingletonApplication): Win32Session.set(Application.cleanup) Win32Session.run() - if AppSettings.get('SystemProxyMode') == 'Auto': + if AppSettings.get('SystemProxyMode') == AppBuiltinProxyMode.Auto.value: # Automatically configure SystemProxy.off() SystemProxy.daemonOn_() diff --git a/Furious/Widget/UserServersQTableWidget.py b/Furious/Widget/UserServersQTableWidget.py index 741d4d0..9bd2306 100644 --- a/Furious/Widget/UserServersQTableWidget.py +++ b/Furious/Widget/UserServersQTableWidget.py @@ -527,7 +527,7 @@ class TestDownloadSpeedWorker(WebGETManager): self.coreManager.start( configcopy, - 'Global', + AppBuiltinRouting.Global.value, self.coreExitCallback, msgCallbackCore=self.coreMsgCallback, deepcopy=False, @@ -555,7 +555,7 @@ class TestDownloadSpeedWorker(WebGETManager): self.coreManager.start( configcopy, - 'Global', + AppBuiltinRouting.Global.value, self.coreExitCallback, msgCallbackCore=self.coreMsgCallback, deepcopy=False, diff --git a/Furious/Window/AppMainWindow.py b/Furious/Window/AppMainWindow.py index 2882848..4b4c086 100644 --- a/Furious/Window/AppMainWindow.py +++ b/Furious/Window/AppMainWindow.py @@ -502,7 +502,7 @@ class AppMainWindow(AppQMainWindow): arguments=[ '-Command', f'Start-Process \'{APP().applicationFilePath()}\' ' - f'\'{AppCommands.RunAs.value}\' -Verb runAs', + f'\'{AppBuiltinCommand.RunAs.value}\' -Verb runAs', ], ) elif PLATFORM == 'Darwin': @@ -511,7 +511,7 @@ class AppMainWindow(AppQMainWindow): arguments=[ '-e', f'do shell script \"{APP().applicationFilePath()} ' - f'{AppCommands.RunAs.value}\" with administrator privileges', + f'{AppBuiltinCommand.RunAs.value}\" with administrator privileges', ], ) else: