From 3d4ac0126b857478b882f54a8571cc92a8de1ca7 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Fri, 24 Sep 2021 16:09:07 +0200 Subject: [PATCH] Added signal handler and interface detachment oon exit. --- RNS/Interfaces/Interface.py | 5 ++++- RNS/Interfaces/TCPInterface.py | 10 ++++++++++ RNS/Reticulum.py | 10 +++++++++- RNS/Transport.py | 6 ++++++ RNS/__init__.py | 5 ++++- 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/RNS/Interfaces/Interface.py b/RNS/Interfaces/Interface.py index 02c4357..440200a 100755 --- a/RNS/Interfaces/Interface.py +++ b/RNS/Interfaces/Interface.py @@ -11,4 +11,7 @@ class Interface: pass def get_hash(self): - return RNS.Identity.full_hash(str(self).encode("utf-8")) \ No newline at end of file + return RNS.Identity.full_hash(str(self).encode("utf-8")) + + def detach(self): + pass \ No newline at end of file diff --git a/RNS/Interfaces/TCPInterface.py b/RNS/Interfaces/TCPInterface.py index 92db9c8..d39b26c 100644 --- a/RNS/Interfaces/TCPInterface.py +++ b/RNS/Interfaces/TCPInterface.py @@ -96,6 +96,15 @@ class TCPClientInterface(Interface): sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) sock.setsockopt(socket.IPPROTO_TCP, TCP_KEEPIDLE, int(TCPClientInterface.TCP_PROBE_AFTER)) + def detach(self): + if self.socket != None: + if hasattr(self.socket, "close"): + if callable(self.socket.close): + RNS.log("Detaching "+str(self), RNS.LOG_DEBUG) + self.socket.shutdown(socket.SHUT_RDWR) + self.socket.close() + self.socket = None + def connect(self, initial=False): try: self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -276,6 +285,7 @@ class TCPServerInterface(Interface): self.owner = owner address = (self.bind_ip, self.bind_port) + ThreadingTCPServer.allow_reuse_address = True self.server = ThreadingTCPServer(address, handlerFactory(self.incoming_connection)) thread = threading.Thread(target=self.server.serve_forever) diff --git a/RNS/Reticulum.py b/RNS/Reticulum.py index ab67414..feb5108 100755 --- a/RNS/Reticulum.py +++ b/RNS/Reticulum.py @@ -2,6 +2,7 @@ from .Interfaces import * import configparser from .vendor.configobj import ConfigObj import RNS +import signal import atexit import struct import array @@ -78,6 +79,12 @@ class Reticulum: RNS.Transport.exit_handler() RNS.Identity.exit_handler() + @staticmethod + def sigint_handler(signal, frame): + RNS.Transport.detach_interfaces() + RNS.exit() + + def __init__(self,configdir=None, loglevel=None): """ Initialises and starts a Reticulum instance. This must be @@ -109,7 +116,7 @@ class Reticulum: self.requested_loglevel = RNS.LOG_EXTREME if self.requested_loglevel < RNS.LOG_CRITICAL: self.requested_loglevel = RNS.LOG_CRITICAL - + RNS.loglevel = self.requested_loglevel self.is_shared_instance = False @@ -147,6 +154,7 @@ class Reticulum: RNS.Transport.start(self) atexit.register(Reticulum.exit_handler) + signal.signal(signal.SIGINT, Reticulum.sigint_handler) def __start_local_interface(self): if self.share_instance: diff --git a/RNS/Transport.py b/RNS/Transport.py index 12a1c27..8b7af39 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -1422,6 +1422,12 @@ class Transport: else: return False + @staticmethod + def detach_interfaces(): + for interface in Transport.interfaces: + interface.detach() + + @staticmethod def exit_handler(): try: diff --git a/RNS/__init__.py b/RNS/__init__.py index ff395b3..e77b1ed 100755 --- a/RNS/__init__.py +++ b/RNS/__init__.py @@ -108,4 +108,7 @@ def prettyhexrep(data): return hexrep def panic(): - os._exit(255) \ No newline at end of file + os._exit(255) + +def exit(): + sys.exit(0) \ No newline at end of file