Skip to content

Commit

Permalink
Refactor connection test to timer implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Loren Eteval <[email protected]>
  • Loading branch information
LorenEteval committed Sep 20, 2023
1 parent d074948 commit d6e4780
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions Furious/Action/Connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,17 @@ def __init__(self):
self.testPool = [
# Messaging
'https://telegram.org/',
# Search
'https://www.google.com/',
# Social media
'https://twitter.com/',
# Videos
'https://www.youtube.com/',
]
self.testTime = 0
self.testFinished = True
self.testTimeoutTimer = QtCore.QTimer()
self.testTimeoutTimer.setSingleShot(True)

self.XrayCore = XrayCore()
self.Hysteria1 = Hysteria1()
Expand Down Expand Up @@ -1056,7 +1060,7 @@ def stopTun2socks(self):
self.Tun2socks.registerExitCallback(None)
self.Tun2socks.stop()

def waitForDNSResolutionIfNecessary(self, startCounter=0, timeout=10000, step=1):
def waitForDNSResolutionIfNecessary(self, startCounter=0, timeout=10000, step=100):
# Wait for potentially DNS resolve in VPN Mode
if self.coreRunning and self.networkReply is not None:
logger.info('dns resolve in progress. Wait')
Expand All @@ -1082,7 +1086,7 @@ def waitForDNSResolutionIfNecessary(self, startCounter=0, timeout=10000, step=1)
# Reset reply
self.networkReply = None

def startTorRelay(self, core, proxyServer, startCounter=0, step=1):
def startTorRelay(self, core, proxyServer, startCounter=0, step=100):
# Redirect Proxy
self.httpsProxyServer = f'127.0.0.1:{self.torRelayStorageObj.get("httpsTunnelPort", DEFAULT_TOR_HTTPS_PORT)}'
self.socksProxyServer = f'127.0.0.1:{self.torRelayStorageObj.get("socksTunnelPort", DEFAULT_TOR_SOCKS_PORT)}'
Expand Down Expand Up @@ -1171,6 +1175,7 @@ def finishedCallback():
)
else:
self.testFinished = True
self.testTimeoutTimer.stop()

if self.disconnectReason:
self.disconnectAction(self.disconnectReason)
Expand All @@ -1182,6 +1187,7 @@ def finishedCallback():
logger.info(f'{self.coreName}: connection test success. Connected')

self.testFinished = True
self.testTimeoutTimer.stop()
# Connected status
self.setConnectedStatus()

Expand Down Expand Up @@ -1261,20 +1267,22 @@ def connectAction(self):

self.connectingAction()

def waitForConnectionTest(self, startCounter=0, timeout=30000, step=1):
while self.coreRunning and not self.testFinished and startCounter < timeout:
QTest.qWait(step)
def checkConnectionTestTimeout(self, timeout=30000):
logger.info(f'connection test timeout is {timeout // 1000}s')

startCounter += step
def handleTimeout():
if self.coreRunning and not self.testFinished:
# Timeout
logger.error('connection test timeout. Abort')

if self.coreRunning and not self.testFinished:
# Timeout
logger.error('connection test timeout. Abort')
assert isinstance(self.networkReply, QNetworkReply)

assert isinstance(self.networkReply, QNetworkReply)
self.coreRunning = False
self.networkReply.abort()

self.coreRunning = False
self.networkReply.abort()
# Check is done by timer
self.testTimeoutTimer.timeout.connect(handleTimeout)
self.testTimeoutTimer.start(timeout)

def connectingAction(
self,
Expand Down Expand Up @@ -1331,7 +1339,7 @@ def connectingAction(
self.startConnectionTest(
showRoutingChangedMessage, currentRouting, isBuiltinRouting
)
self.waitForConnectionTest()
self.checkConnectionTestTimeout()

def disconnectAction(self, reason=''):
Proxy.off()
Expand Down

0 comments on commit d6e4780

Please sign in to comment.