Adopt new zeroconf API

This commit is contained in:
Milan Stute
2019-12-17 12:55:18 +01:00
parent b2293bf309
commit 9b7fe93c30
3 changed files with 18 additions and 13 deletions
+12 -5
View File
@@ -111,22 +111,27 @@ class AirDropCli:
def _send_discover(self, info):
try:
address = ipaddress.ip_address(info.address).compressed
except ValueError:
return # not a valid address
address = info.parsed_addresses()[0] # there should only be one address
except IndexError:
logger.warn('Ignoring receiver with missing address {}'.format(info))
return
id = info.name.split('.')[0]
hostname = info.server
port = int(info.port)
logger.debug('AirDrop service found: {}, {}:{}, ID {}'.format(hostname, address, port, id))
client = AirDropClient(self.config, (address, int(port)))
flags = int(info.properties[b'flags'])
try:
flags = int(info.properties[b'flags'])
except KeyError:
# TODO in some cases, `flags` are not set in service info; for now we'll try anyway
flags = AirDropReceiverFlags.SUPPORTS_DISCOVER_MAYBE
pass
if flags & AirDropReceiverFlags.SUPPORTS_DISCOVER_MAYBE:
try:
receiver_name = client.send_discover()
except TimeoutError:
receiver_name = None
pass
else:
receiver_name = None
discoverable = receiver_name is not None
@@ -144,6 +149,8 @@ class AirDropCli:
self.discover.append(node_info)
if discoverable:
logger.info('Found index {} ID {} name {}'.format(index, id, receiver_name))
else:
logger.debug('Receiver ID {} is not discoverable'.format(id))
self.lock.release()
def receive(self):
+4 -5
View File
@@ -18,7 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import fleep
import http
from http.client import HTTPSConnection
import ipaddress
import logging
import os
@@ -27,10 +27,9 @@ import libarchive
import platform
import plistlib
import socket
from http import client
from .util import AirDropUtil, AbsArchiveWrite
from .zeroconf import ServiceBrowser, Zeroconf
from .zeroconf import ServiceBrowser, Zeroconf, IPVersion
logger = logging.getLogger(__name__)
@@ -46,7 +45,7 @@ class AirDropBrowser:
else:
raise RuntimeError('Interface {} does not have an IPv6 address'.format(config.interface))
self.zeroconf = Zeroconf(interfaces=[self.ip_addr], ipv6_interface_name=config.interface)
self.zeroconf = Zeroconf(interfaces=[str(self.ip_addr)], ip_version=IPVersion.V6Only, apple_p2p=True)
self.callback_add = None
self.callback_remove = None
@@ -209,7 +208,7 @@ class AirDropClient:
return headers
class HTTPSConnectionAWDL(http.client.HTTPSConnection):
class HTTPSConnectionAWDL(HTTPSConnection):
"""
This class allows to bind the HTTPConnection to a specific network interface
"""
+2 -3
View File
@@ -27,7 +27,7 @@ import json
import libarchive
import libarchive.extract
import libarchive.read
from .zeroconf import Zeroconf, ServiceInfo
from .zeroconf import Zeroconf, ServiceInfo, IPVersion
import time
from .util import AirDropUtil
@@ -59,8 +59,7 @@ class AirDropServer:
self.Handler = AirDropServerHandler
self.Handler.config = self.config
self.zeroconf = Zeroconf(interfaces=[self.ip_addr], ipv6_interface_name=self.config.interface,
apple_mdns=True)
self.zeroconf = Zeroconf(interfaces=[str(self.ip_addr)], ip_version=IPVersion.V6Only, apple_p2p=True)
self.http_server = self._init_server()
self.service_info = self._init_service()