From 652b884d7284e4b4b078c0fa947a28063d0e1e65 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Wed, 1 Dec 2021 11:39:40 +0100 Subject: [PATCH] Added conditional import of netifaces --- RNS/Interfaces/TCPInterface.py | 20 +++++++++++++++++--- RNS/Interfaces/UDPInterface.py | 20 +++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/RNS/Interfaces/TCPInterface.py b/RNS/Interfaces/TCPInterface.py index 4202c44..c7d0deb 100644 --- a/RNS/Interfaces/TCPInterface.py +++ b/RNS/Interfaces/TCPInterface.py @@ -1,7 +1,6 @@ from .Interface import Interface import socketserver import threading -import netifaces import platform import socket import time @@ -286,10 +285,25 @@ class TCPClientInterface(Interface): class TCPServerInterface(Interface): @staticmethod def get_address_for_if(name): - return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['addr'] + import importlib + if importlib.find_loader('netifaces') != None: + import netifaces + return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['addr'] + else: + RNS.log("Getting interface addresses from device names requires the netifaces module.", RNS.LOG_CRITICAL) + RNS.log("You can install it with the command: python3 -m pip install netifaces", RNS.LOG_CRITICAL) + RNS.panic() + @staticmethod def get_broadcast_for_if(name): - return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['broadcast'] + import importlib + if importlib.find_loader('netifaces') != None: + import netifaces + return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['broadcast'] + else: + RNS.log("Getting interface addresses from device names requires the netifaces module.", RNS.LOG_CRITICAL) + RNS.log("You can install it with the command: python3 -m pip install netifaces", RNS.LOG_CRITICAL) + RNS.panic() def __init__(self, owner, name, device=None, bindip=None, bindport=None): self.rxb = 0 diff --git a/RNS/Interfaces/UDPInterface.py b/RNS/Interfaces/UDPInterface.py index 29c39c2..2f1670f 100644 --- a/RNS/Interfaces/UDPInterface.py +++ b/RNS/Interfaces/UDPInterface.py @@ -1,7 +1,6 @@ from .Interface import Interface import socketserver import threading -import netifaces import socket import time import sys @@ -12,10 +11,25 @@ class UDPInterface(Interface): @staticmethod def get_address_for_if(name): - return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['addr'] + import importlib + if importlib.find_loader('netifaces') != None: + import netifaces + return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['addr'] + else: + RNS.log("Getting interface addresses from device names requires the netifaces module.", RNS.LOG_CRITICAL) + RNS.log("You can install it with the command: python3 -m pip install netifaces", RNS.LOG_CRITICAL) + RNS.panic() + @staticmethod def get_broadcast_for_if(name): - return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['broadcast'] + import importlib + if importlib.find_loader('netifaces') != None: + import netifaces + return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['broadcast'] + else: + RNS.log("Getting interface addresses from device names requires the netifaces module.", RNS.LOG_CRITICAL) + RNS.log("You can install it with the command: python3 -m pip install netifaces", RNS.LOG_CRITICAL) + RNS.panic() def __init__(self, owner, name, device=None, bindip=None, bindport=None, forwardip=None, forwardport=None): self.rxb = 0