Removed netifaces dependency from AutoInterface

This commit is contained in:
Mark Qvist 2023-05-04 17:55:58 +02:00
parent fab83ec399
commit a3bee4baa9
1 changed files with 18 additions and 16 deletions

View File

@ -62,16 +62,18 @@ class AutoInterface(Interface):
link_local_addr = re.sub(r"fe80:[0-9a-f]*::","fe80::", link_local_addr) link_local_addr = re.sub(r"fe80:[0-9a-f]*::","fe80::", link_local_addr)
return link_local_addr return link_local_addr
def __init__(self, owner, name, group_id=None, discovery_scope=None, discovery_port=None, data_port=None, allowed_interfaces=None, ignored_interfaces=None, configured_bitrate=None): def list_interfaces(self):
import importlib ifs = self.netinfo.interfaces()
if importlib.util.find_spec('netifaces') != None: return ifs
import netifaces
else: def list_addresses(self, ifname):
RNS.log("Using AutoInterface requires the netifaces module.", RNS.LOG_CRITICAL) ifas = self.netinfo.ifaddresses(ifname)
RNS.log("You can install it with the command: python3 -m pip install netifaces", RNS.LOG_CRITICAL) return ifas
RNS.panic()
def __init__(self, owner, name, group_id=None, discovery_scope=None, discovery_port=None, data_port=None, allowed_interfaces=None, ignored_interfaces=None, configured_bitrate=None):
from RNS.vendor.ifaddr import niwrapper
self.netinfo = niwrapper
self.netifaces = netifaces
self.rxb = 0 self.rxb = 0
self.txb = 0 self.txb = 0
@ -148,7 +150,7 @@ class AutoInterface(Interface):
self.mcast_discovery_address = "ff1"+self.discovery_scope+":"+gt self.mcast_discovery_address = "ff1"+self.discovery_scope+":"+gt
suitable_interfaces = 0 suitable_interfaces = 0
for ifname in self.netifaces.interfaces(): for ifname in self.list_interfaces():
if RNS.vendor.platformutils.is_darwin() and ifname in AutoInterface.DARWIN_IGNORE_IFS and not ifname in self.allowed_interfaces: if RNS.vendor.platformutils.is_darwin() and ifname in AutoInterface.DARWIN_IGNORE_IFS and not ifname in self.allowed_interfaces:
RNS.log(str(self)+" skipping Darwin AWDL or tethering interface "+str(ifname), RNS.LOG_EXTREME) RNS.log(str(self)+" skipping Darwin AWDL or tethering interface "+str(ifname), RNS.LOG_EXTREME)
elif RNS.vendor.platformutils.is_darwin() and ifname == "lo0": elif RNS.vendor.platformutils.is_darwin() and ifname == "lo0":
@ -163,10 +165,10 @@ class AutoInterface(Interface):
if len(self.allowed_interfaces) > 0 and not ifname in self.allowed_interfaces: if len(self.allowed_interfaces) > 0 and not ifname in self.allowed_interfaces:
RNS.log(str(self)+" ignoring interface "+str(ifname)+" since it was not allowed", RNS.LOG_EXTREME) RNS.log(str(self)+" ignoring interface "+str(ifname)+" since it was not allowed", RNS.LOG_EXTREME)
else: else:
addresses = self.netifaces.ifaddresses(ifname) addresses = self.list_addresses(ifname)
if self.netifaces.AF_INET6 in addresses: if self.netinfo.AF_INET6 in addresses:
link_local_addr = None link_local_addr = None
for address in addresses[self.netifaces.AF_INET6]: for address in addresses[self.netinfo.AF_INET6]:
if "addr" in address: if "addr" in address:
if address["addr"].startswith("fe80:"): if address["addr"].startswith("fe80:"):
link_local_addr = self.descope_linklocal(address["addr"]) link_local_addr = self.descope_linklocal(address["addr"])
@ -287,10 +289,10 @@ class AutoInterface(Interface):
for ifname in self.adopted_interfaces: for ifname in self.adopted_interfaces:
# Check that the link-local address has not changed # Check that the link-local address has not changed
try: try:
addresses = self.netifaces.ifaddresses(ifname) addresses = self.list_addresses(ifname)
if self.netifaces.AF_INET6 in addresses: if self.netinfo.AF_INET6 in addresses:
link_local_addr = None link_local_addr = None
for address in addresses[self.netifaces.AF_INET6]: for address in addresses[self.netinfo.AF_INET6]:
if "addr" in address: if "addr" in address:
if address["addr"].startswith("fe80:"): if address["addr"].startswith("fe80:"):
link_local_addr = self.descope_linklocal(address["addr"]) link_local_addr = self.descope_linklocal(address["addr"])