From 571ad2c8fb36b08e2a660e23f798149bff0e7789 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Thu, 15 Sep 2022 15:35:28 +0200 Subject: [PATCH] Added initial connection timeout option to TCPClientInterface --- RNS/Interfaces/TCPInterface.py | 47 +++++++++++++++++++++++++--------- RNS/Reticulum.py | 4 ++- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/RNS/Interfaces/TCPInterface.py b/RNS/Interfaces/TCPInterface.py index 606a858..2bda1f6 100644 --- a/RNS/Interfaces/TCPInterface.py +++ b/RNS/Interfaces/TCPInterface.py @@ -70,12 +70,15 @@ class TCPClientInterface(Interface): TCP_PROBE_INTERVAL = 2 TCP_PROBES = 12 + INITIAL_CONNECT_TIMEOUT = 5 + SYNCHRONOUS_START = True + I2P_USER_TIMEOUT = 45 I2P_PROBE_AFTER = 10 I2P_PROBE_INTERVAL = 9 I2P_PROBES = 5 - def __init__(self, owner, name, target_ip=None, target_port=None, connected_socket=None, max_reconnect_tries=None, kiss_framing=False, i2p_tunneled = False): + def __init__(self, owner, name, target_ip=None, target_port=None, connected_socket=None, max_reconnect_tries=None, kiss_framing=False, i2p_tunneled = False, connect_timeout = None): self.rxb = 0 self.txb = 0 @@ -119,18 +122,30 @@ class TCPClientInterface(Interface): self.target_ip = target_ip self.target_port = target_port self.initiator = True - - if not self.connect(initial=True): - thread = threading.Thread(target=self.reconnect) - thread.setDaemon(True) - thread.start() - else: - thread = threading.Thread(target=self.read_loop) - thread.setDaemon(True) - thread.start() - if not self.kiss_framing: - self.wants_tunnel = True + if connect_timeout != None: + self.connect_timeout = connect_timeout + else: + self.connect_timeout = TCPClientInterface.INITIAL_CONNECT_TIMEOUT + + if TCPClientInterface.SYNCHRONOUS_START: + self.initial_connect() + else: + thread = threading.Thread(target=self.initial_connect) + thread.setDaemon(True) + thread.start() + + def initial_connect(self): + if not self.connect(initial=True): + thread = threading.Thread(target=self.reconnect) + thread.setDaemon(True) + thread.start() + else: + thread = threading.Thread(target=self.read_loop) + thread.setDaemon(True) + thread.start() + if not self.kiss_framing: + self.wants_tunnel = True def set_timeouts_linux(self): if not self.i2p_tunneled: @@ -181,9 +196,17 @@ class TCPClientInterface(Interface): def connect(self, initial=False): try: + if initial: + RNS.log("Establishing TCP connection for "+str(self)+"...", RNS.LOG_DEBUG) + self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.socket.settimeout(TCPClientInterface.INITIAL_CONNECT_TIMEOUT) self.socket.connect((self.target_ip, self.target_port)) + self.socket.settimeout(None) self.online = True + + if initial: + RNS.log("TCP connection for "+str(self)+" established", RNS.LOG_DEBUG) except Exception as e: if initial: diff --git a/RNS/Reticulum.py b/RNS/Reticulum.py index 9606cc6..8c66057 100755 --- a/RNS/Reticulum.py +++ b/RNS/Reticulum.py @@ -590,6 +590,7 @@ class Reticulum: if "kiss_framing" in c and c.as_bool("kiss_framing") == True: kiss_framing = True i2p_tunneled = c.as_bool("i2p_tunneled") if "i2p_tunneled" in c else False + tcp_connect_timeout = c.as_int("connect_timeout") if "connect_timeout" in c else None interface = TCPInterface.TCPClientInterface( @@ -598,7 +599,8 @@ class Reticulum: c["target_host"], int(c["target_port"]), kiss_framing = kiss_framing, - i2p_tunneled = i2p_tunneled + i2p_tunneled = i2p_tunneled, + connect_timeout = tcp_connect_timeout, ) if "outgoing" in c and c.as_bool("outgoing") == False: