Added initial connection timeout option to TCPClientInterface

This commit is contained in:
Mark Qvist 2022-09-15 15:35:28 +02:00
parent 0c47ff1ccc
commit 571ad2c8fb
2 changed files with 38 additions and 13 deletions

View File

@ -70,12 +70,15 @@ class TCPClientInterface(Interface):
TCP_PROBE_INTERVAL = 2 TCP_PROBE_INTERVAL = 2
TCP_PROBES = 12 TCP_PROBES = 12
INITIAL_CONNECT_TIMEOUT = 5
SYNCHRONOUS_START = True
I2P_USER_TIMEOUT = 45 I2P_USER_TIMEOUT = 45
I2P_PROBE_AFTER = 10 I2P_PROBE_AFTER = 10
I2P_PROBE_INTERVAL = 9 I2P_PROBE_INTERVAL = 9
I2P_PROBES = 5 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.rxb = 0
self.txb = 0 self.txb = 0
@ -120,6 +123,19 @@ class TCPClientInterface(Interface):
self.target_port = target_port self.target_port = target_port
self.initiator = True self.initiator = 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): if not self.connect(initial=True):
thread = threading.Thread(target=self.reconnect) thread = threading.Thread(target=self.reconnect)
thread.setDaemon(True) thread.setDaemon(True)
@ -131,7 +147,6 @@ class TCPClientInterface(Interface):
if not self.kiss_framing: if not self.kiss_framing:
self.wants_tunnel = True self.wants_tunnel = True
def set_timeouts_linux(self): def set_timeouts_linux(self):
if not self.i2p_tunneled: if not self.i2p_tunneled:
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_USER_TIMEOUT, int(TCPClientInterface.TCP_USER_TIMEOUT * 1000)) self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_USER_TIMEOUT, int(TCPClientInterface.TCP_USER_TIMEOUT * 1000))
@ -181,10 +196,18 @@ class TCPClientInterface(Interface):
def connect(self, initial=False): def connect(self, initial=False):
try: 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 = 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.connect((self.target_ip, self.target_port))
self.socket.settimeout(None)
self.online = True self.online = True
if initial:
RNS.log("TCP connection for "+str(self)+" established", RNS.LOG_DEBUG)
except Exception as e: except Exception as e:
if initial: if initial:
RNS.log("Initial connection for "+str(self)+" could not be established: "+str(e), RNS.LOG_ERROR) RNS.log("Initial connection for "+str(self)+" could not be established: "+str(e), RNS.LOG_ERROR)

View File

@ -590,6 +590,7 @@ class Reticulum:
if "kiss_framing" in c and c.as_bool("kiss_framing") == True: if "kiss_framing" in c and c.as_bool("kiss_framing") == True:
kiss_framing = True kiss_framing = True
i2p_tunneled = c.as_bool("i2p_tunneled") if "i2p_tunneled" in c else False 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( interface = TCPInterface.TCPClientInterface(
@ -598,7 +599,8 @@ class Reticulum:
c["target_host"], c["target_host"],
int(c["target_port"]), int(c["target_port"]),
kiss_framing = kiss_framing, 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: if "outgoing" in c and c.as_bool("outgoing") == False: