From c2992d583973fef3bc1b30610abd10f650f384e9 Mon Sep 17 00:00:00 2001 From: Loren Eteval Date: Wed, 6 Sep 2023 15:47:12 +0800 Subject: [PATCH] Add errors/info tray messages in VPN mode Signed-off-by: Loren Eteval --- Furious/Action/Connect.py | 44 +++++++++++++++++++---------------- Furious/Action/Settings.py | 17 ++++++++++---- Furious/Utility/Translator.py | 30 ++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 25 deletions(-) diff --git a/Furious/Action/Connect.py b/Furious/Action/Connect.py index 056e1076..436563d4 100644 --- a/Furious/Action/Connect.py +++ b/Furious/Action/Connect.py @@ -608,7 +608,6 @@ class ConnectAction(Action): logger.error('find Tor CLI in path failed') self.coreRunning = False - self.disconnectReason = ( f'{XrayCore.name()}: {_("Cannot find Tor CLI in PATH")}' ) @@ -737,7 +736,6 @@ class ConnectAction(Action): logger.error('find Tor CLI in path failed') self.coreRunning = False - self.disconnectReason = ( f'{Hysteria.name()}: {_("Cannot find Tor CLI in PATH")}' ) @@ -793,7 +791,7 @@ class ConnectAction(Action): # No matching core return '' - def startTun2socks(self): + def startTun2socks(self, successCallback=None): if not self.coreRunning: # Core has exited. Do nothing return @@ -807,28 +805,31 @@ class ConnectAction(Action): ) if len(defaultGateway) != 1: - # TODO: Error handling - print('Fall in error handling default gateway!!!') + logger.error(f'found multiple gateway addresses: {defaultGateway}') + + self.coreRunning = False + self.disconnectReason = ( + _('Unable to connect') + ': ' + _('Multiple gateway addresses found') + ) + + return coreAddr = Intellisense.getCoreAddr(self.coreJSON) if not coreAddr: - # TODO: Error handling - print('Fall in error handling coreAddr!!!') + logger.error(f'invalid server address: {coreAddr}') + + self.coreRunning = False + self.disconnectReason = _('Server address invalid') + f': {coreAddr}' + + return def start(): assert RoutingTable.Relations if PLATFORM == 'Darwin': for source in [ - '1.0.0.0/8', - '2.0.0.0/7', - '4.0.0.0/6', - '8.0.0.0/5', - '16.0.0.0/4', - '32.0.0.0/3', - '64.0.0.0/2', - '128.0.0.0/1', + *list(f'{2 ** (8 - x)}.0.0.0/{x}' for x in range(8, 0, -1)), '198.18.0.0/15', ]: RoutingTable.Relations.append( @@ -850,6 +851,9 @@ class ConnectAction(Action): ) RoutingTable.addRelations() + if callable(successCallback): + successCallback() + if not isValidIPAddress(coreAddr): logger.info(f'dns resolve uses proxy server {self.httpsProxyServer}') @@ -882,12 +886,11 @@ class ConnectAction(Action): if self.networkReply.error() != QNetworkReply.NetworkError.NoError: logger.error( - f'dns resolve for {coreAddr} failed. {self.networkReply.errorString()}' + f'DNS resolution for {coreAddr} failed. {self.networkReply.errorString()}' ) self.coreRunning = False - # TODO - self.disconnectReason = '' + self.disconnectReason = _('DNS resolution failed') + f': {coreAddr}' # Reset reply self.networkReply = None else: @@ -940,9 +943,10 @@ class ConnectAction(Action): if self.networkReply is not None: if self.coreRunning: - logger.error('dns resolve timeout') + logger.error('DNS resolution timeout') - # TODO: Error + self.coreRunning = False + self.disconnectReason = _('DNS resolution timeout') # Reset reply self.networkReply = None diff --git a/Furious/Action/Settings.py b/Furious/Action/Settings.py index c6a592a6..040c93e0 100644 --- a/Furious/Action/Settings.py +++ b/Furious/Action/Settings.py @@ -85,22 +85,29 @@ class VPNModeAction(Action): def triggeredCallback(self, checked): assert isAdministrator() - # TODO: Tray messages? + # Reference + ConnectAction = APP().tray.ConnectAction if checked: APP().VPNMode = Switch.ON_ - if APP().tray.ConnectAction.isConnected(): + if ConnectAction.isConnected(): if isVPNMode(): if PLATFORM == 'Windows' or PLATFORM == 'Darwin': # Currently VPN Mode is only supported on Windows and macOS - APP().tray.ConnectAction.startTun2socks() + ConnectAction.startTun2socks( + successCallback=lambda: APP().tray.showMessage( + _('VPN mode started') + ) + ) else: - if APP().tray.ConnectAction.isConnected(): + if ConnectAction.isConnected(): if isVPNMode(): if PLATFORM == 'Windows' or PLATFORM == 'Darwin': # Currently VPN Mode is only supported on Windows and macOS - APP().tray.ConnectAction.stopTun2socks() + ConnectAction.stopTun2socks() + + APP().tray.showMessage(_('VPN mode stopped')) APP().VPNMode = Switch.OFF diff --git a/Furious/Utility/Translator.py b/Furious/Utility/Translator.py index 4394e836..c700fc2c 100644 --- a/Furious/Utility/Translator.py +++ b/Furious/Utility/Translator.py @@ -946,6 +946,36 @@ TRANSLATION = { 'ZH': '在PATH中找不到Tor CLI', 'TW': '在PATH中找不到Tor CLI', }, + 'VPN mode started': { + 'ES': 'Modo VPN detenido', + 'ZH': 'VPN模式已启动', + 'TW': 'VPN模式已啓動', + }, + 'VPN mode stopped': { + 'ES': 'Modo VPN iniciado', + 'ZH': 'VPN模式已停止', + 'TW': 'VPN模式已停止', + }, + 'Multiple gateway addresses found': { + 'ES': 'Se encontraron varias direcciones de puerta de enlace', + 'ZH': '找到多个网关地址', + 'TW': '找到多個網關地址', + }, + 'Server address invalid': { + 'ES': 'Dirección del servidor no válida', + 'ZH': '服务器地址无效', + 'TW': '服務器地址無效', + }, + 'DNS resolution failed': { + 'ES': 'La resolución DNS falló', + 'ZH': 'DNS解析失败', + 'TW': 'DNS解析失敗', + }, + 'DNS resolution timeout': { + 'ES': 'Tiempo de espera de resolución DNS', + 'ZH': 'DNS解析超时', + 'TW': 'DNS解析超時', + }, # Errors f'{APPLICATION_NAME} is not be able to run on this operating system.': { 'ES': f'{APPLICATION_NAME} no puede ejecutarse en este sistema operativo.',