Move from netifaces to ifaddr and add better error handling

Display message if `owl` is not running (fixes #7, fixes #12)
This commit is contained in:
Milan Stute
2019-08-20 11:48:15 +02:00
parent b8b942d0c0
commit 8427681641
4 changed files with 35 additions and 25 deletions
+6 -3
View File
@@ -40,10 +40,13 @@ class AirDropBrowser:
def __init__(self, config):
self.ip_interface_name = config.interface
self.ip_addr, self.byte_address = AirDropUtil.get_ip_for_interface(self.ip_interface_name, ipv6=True)
self.ip_addr = AirDropUtil.get_ip_for_interface(self.ip_interface_name, ipv6=True)
if self.ip_addr is None:
raise RuntimeError('Interface {} does not have IP(v6) address'.format(self.ip_interface_name))
if self.ip_interface_name is 'awdl0':
raise RuntimeError('Interface {} does not have an IPv6 address. '
'Make sure that `owl` is running.'.format(self.ip_interface_name))
else:
raise RuntimeError('Interface {} does not have an IPv6 address'.format(self.ip_interface_name))
self.zeroconf = Zeroconf(interfaces=[self.ip_addr], ipv6_interface_name=self.ip_interface_name)
+8 -2
View File
@@ -52,7 +52,13 @@ class AirDropServer:
self.ServerClass.allow_reuse_address = False
self.ip_addr, self.byte_address = AirDropUtil.get_ip_for_interface(self.ip_interface_name, ipv6=True)
self.ip_addr = AirDropUtil.get_ip_for_interface(self.ip_interface_name, ipv6=True)
if self.ip_addr is None:
if self.ip_interface_name is 'awdl0':
raise RuntimeError('Interface {} does not have an IPv6 address. '
'Make sure that `owl` is running.'.format(self.ip_interface_name))
else:
raise RuntimeError('Interface {} does not have an IPv6 address'.format(self.ip_interface_name))
self.Handler = AirDropServerHandler
self.Handler.config = self.config
@@ -69,7 +75,7 @@ class AirDropServer:
service_name = self.config.service_id + '._airdrop._tcp.local.'
info = ServiceInfo(
'_airdrop._tcp.local.', service_name,
self.byte_address, self.config.port, 0, 0, properties, server)
self.ip_addr.packed, self.config.port, 0, 0, properties, server)
return info
def start_service(self):
+20 -19
View File
@@ -17,8 +17,6 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import netifaces
import base64
import datetime
import io
@@ -26,8 +24,8 @@ import ipaddress
import os
import platform
import plistlib
import socket
import hashlib
import ifaddr
from PIL import Image, ExifTags
from libarchive import ffi
from libarchive.entry import new_archive_entry, ArchiveEntry
@@ -199,30 +197,33 @@ class AirDropUtil:
return file_icon
@staticmethod
def get_ip_for_interface(interface_name, ipv6=False):
"""
Get the ip address in IPv4 or IPv6 for a specific network interface
:param str interace_name: declares the network interface name for which the ip should be accessed
:param bool ipv6: Boolean indicating if the ipv6 address should be rertrieved
:return: (str ipaddress, byte ipaddress_bytes) returns a tuple with the ip address as a string and in bytes
:param str interface_name: declares the network interface name for which the ip should be accessed
:param bool ipv6: Boolean indicating if the ipv6 address should be retrieved
:return: IPv4Address or IPv6Address object or None
"""
addresses = netifaces.ifaddresses(interface_name)
if netifaces.AF_INET6 in addresses and ipv6:
# Use the normal ipv6 address
addr = addresses[netifaces.AF_INET6][0]['addr'].split('%')[0]
bytes_addr = ipaddress.IPv6Address(addr).packed
elif netifaces.AF_INET in addresses and not ipv6:
addr = addresses[netifaces.AF_INET][0]['addr']
bytes_addr = socket.inet_aton(addr)
else:
addr = None
bytes_addr = None
def get_interface_by_name(name):
for interface in ifaddr.get_adapters():
if interface.name == name:
return interface
return None
return addr, bytes_addr
interface = get_interface_by_name(interface_name)
if interface is None:
return None
for ip in interface.ips:
if ip.is_IPv6 and ipv6:
return ipaddress.IPv6Address(ip.ip[0]) # first of (ip, flowinfo, scope_id) tuple
if ip.is_IPv4 and not ipv6:
return ipaddress.IPv4Address(ip.ip)
return None
@staticmethod
def write_debug(config, data, file_name):
+1 -1
View File
@@ -29,7 +29,7 @@ setup(
package_data={
'opendrop': ['certs/*.pem']
},
install_requires=['requests', 'fleep', 'netifaces', 'Pillow',
install_requires=['requests', 'fleep', 'ifaddr', 'Pillow',
'requests_toolbelt', 'ctypescrypto', 'libarchive-c'],
entry_points={
'console_scripts': [