예제 #1
0
            def add_service(self, zeroconf, type, name):
                self._logger.debug(
                    "Got a browsing result for Zeroconf resolution of {}, resolving...".format(
                        type
                    )
                )
                info = zeroconf.get_service_info(
                    type, name, timeout=resolve_timeout * 1000
                )
                if info:

                    def to_result(info, address):
                        n = info.name[: -(len(type) + 1)]
                        p = info.port

                        self._logger.debug(
                            "Resolved a result for Zeroconf resolution of {}: {} @ {}:{}".format(
                                type, n, address, p
                            )
                        )

                        return {
                            "name": n,
                            "host": address,
                            "port": p,
                            "txt_record": info.properties,
                        }

                    for address in map(lambda x: socket.inet_ntoa(x), info.addresses):
                        result.append(to_result(info, address))
예제 #2
0
파일: main.py 프로젝트: IhorRudych/BR
 def add_service(self, zeroconf, type, name):
     if 'axcend.bridge' in name:
         info = zeroconf.get_service_info(type, name)
         if info:
             address = "{}".format(socket.inet_ntoa(info.address))
             print("Service %s added (%s)" % (name, address))
             self.directory[name] = {'addr': address, 'port': info.port}
예제 #3
0
 def add_service(self, zeroconf, type, name):
     global scanner, sb
     info = zeroconf.get_service_info(type, name)
     # TODO: verify that info.rs = "eSCL"?
     # TODO: verify that info.is = "platen"?
     scanner = "http://%s:%d/eSCL/" % (info.server, info.port)
     print("Found scanner '%s' at '%s'" % (info.name, info.server))
예제 #4
0
    def add_service(self, zeroconf, type, name):
        info = zeroconf.get_service_info(type, name)
        addr = get_addr_from_info(info)

        if addr not in self.found_devices:
            dev = self.check_and_create_device(info, addr)
            self.found_devices[addr] = dev
예제 #5
0
    def add_service(self, zeroconf: "zeroconf.Zeroconf", type_: str, name: str) -> None:
        """Callback for discovery responses."""
        info = zeroconf.get_service_info(type_, name)
        addr = get_addr_from_info(info)

        if addr not in self.found_devices:
            dev = self.check_and_create_device(info, addr)
            if dev is not None:
                self.found_devices[addr] = dev
예제 #6
0
 def on_service_state_change(zeroconf, service_type, name, state_change):
     nonlocal services, handler_done
     if state_change is Added:
         info = zeroconf.get_service_info(service_type, name)
         if name.startswith(service_name):
             address = socket.inet_ntoa(info.address)
             port = info.port
             if get_many or len(services)==0:
                 services.append([name, address, port])
             if not get_many:
                 handler_done.set()
예제 #7
0
 def add_service(self, zeroconf, type, uuid):
     service_info = zeroconf.get_service_info(type, uuid)
     print("Service %s added, service info: %s" % (uuid, service_info))
     # print("Found mDNS service for cast name {}".format(name))
     device_friendly_name = self.get_device_friendly_name_of_uuid(uuid)
     if device_friendly_name and device_friendly_name in self.devices:
         cast = self.devices[device_friendly_name]
     else:
         cast = Kodi(service_info)
         with self.lock:
             self.devices[cast.device_friendly_name] = cast
     cast.online = True
     self.list_devices()
예제 #8
0
def on_service_state_change(zeroconf, service_type, name, state_change):
    global selected_server
    if state_change is ServiceStateChange.Added:
        info = zeroconf.get_service_info(service_type, name)

        #Don't sync to servers that are lower priority than us,
        #we'll just continue using our local time.
        try:
            if info and sortkey(name)<sortkey(localserver_name):
                #No excessive cache sizes
                if len(local_ntp_servers)>8192:
                    local_ntp_servers.popitem(False)
                local_ntp_servers[name]=(info.address,info.port,name)
                s = local_ntp_servers[sorted(local_ntp_servers.keys(),key=sortkey)[0]]
                selected_server = s
        except:
            logging.exception("")
예제 #9
0
    def add_service(self, zeroconf, service_type, name):
        """Add a Mbed Linux Zeroconf service to a list of services.

        Called when a new zeroconf service is discovered.
        Ensure it's an 'mbed linux device', notify listeners if it is.
        """
        info = zeroconf.get_service_info(service_type, name)
        try:
            info.properties[MBL_ID]
        except KeyError:
            return
        else:
            try:
                inet_addr = socket.inet_ntoa(info.address)
            except (OSError, TypeError):
                inet_addr = info.address
            new_dev = device.create_device(name, inet_addr)
            if new_dev not in self.devices:
                self.devices.append(new_dev)
                name = name.split(".{}".format(service_type))
                self.notify("{}: {}".format(name[0], new_dev.address))
예제 #10
0
 def add_service(self, zeroconf, type, name):
     info = zeroconf.get_service_info(type, name)
     addr = ipaddress.ip_address(info.address)
     print_info(f"{addr} {info.name} {info.server}")
예제 #11
0
    def add_service(self, zeroconf, _type, name):
        info = zeroconf.get_service_info(_type, name)

        if info:
            ident = name.partition(".")[0]

            try:
                remote_hostname = info.properties[b"hostname"].decode()
            except KeyError:
                logging.critical(
                    ">>> Discovery: no hostname in service info properties.  Is this an old version?"
                )
                return

            remote_ips = util.IPAddresses.new_from_binary_list(info.addresses)

            try:
                # Check if this is a flush registration to reset the remote server's presence.
                if info.properties[b"type"].decode() == "flush":
                    logging.debug(
                        ">>> Discovery: received flush service info (ignoring): %s (%s:%d)"
                        % (remote_hostname, remote_ips, info.port))
                    return
            except KeyError:
                logging.warning(
                    "No type in service info properties, assuming this is a real connect attempt"
                )

            if ident == self.service_ident:
                return

            try:
                api_version = info.properties[b"api-version"].decode()
                auth_port = int(info.properties[b"auth-port"].decode())
            except KeyError:
                api_version = "1"
                auth_port = 0

            # FIXME: I'm not sure why we still get discovered by other networks in some cases -
            # The Zeroconf object has a specific ip it is set to, what more do I need to do?
            if not self.netmon.same_subnet(remote_ips):
                if remote_ips != self.ips:
                    logging.debug(
                        ">>> Discovery: service is not on this subnet, ignoring: %s (%s)"
                        % (remote_hostname, remote_ips))
                return

            try:
                machine = self.remote_machines[ident]
                machine.has_zc_presence = True
                logging.debug(
                    ">>> Discovery: existing remote: %s (%s:%d)" %
                    (machine.display_hostname, remote_ips, info.port))

                # If the remote truly is the same one (our service info just dropped out
                # momentarily), this will end up just retrieving the current cert again.
                # If this was a real disconnect we didn't notice, we'll have the new cert
                # which we'll need when our supposedly existing connection tries to continue
                # pinging. It will fail out and restart the connection loop, and will need
                # this updated one.
                if not self.remote_registrar.register(
                        ident, remote_hostname, remote_ips, info.port,
                        auth_port,
                        api_version) or self.server_thread_keepalive.is_set():
                    logging.warning(
                        "Register failed, or the server was shutting down during registration, ignoring remote %s (%s:%d) auth port: %d"
                        % (remote_hostname, remote_ips, info.port, auth_port))
                    return

                if machine.status == RemoteStatus.ONLINE:
                    logging.debug(
                        ">>> Discovery: rejoining existing connect with %s (%s:%d)"
                        % (machine.display_hostname, remote_ips, info.port))
                    return

                # Update our connect info if it changed.
                machine.hostname = remote_hostname
                machine.ips = remote_ips
                machine.port = info.port
                machine.api_version = api_version
            except KeyError:
                display_hostname = remote_hostname
                i = 1

                while True:
                    found = False

                    for key in self.remote_machines.keys():
                        remote_machine = self.remote_machines[key]

                        if remote_machine.display_hostname == display_hostname:
                            display_hostname = "%s[%d]" % (remote_hostname, i)
                            found = True
                            break

                    i += 1

                    if not found:
                        break

                logging.debug(">>> Discovery: new remote: %s (%s:%d)" %
                              (display_hostname, remote_ips, info.port))

                machine = remote.RemoteMachine(ident, remote_hostname,
                                               display_hostname, remote_ips,
                                               info.port, self.service_ident,
                                               api_version)

                if not self.remote_registrar.register(
                        ident, remote_hostname, remote_ips, info.port,
                        auth_port,
                        api_version) or self.server_thread_keepalive.is_set():
                    logging.warning(
                        "Register failed, or the server was shutting down during registration, ignoring remote %s (%s:%d) auth port: %d"
                        % (remote_hostname, remote_ips, info.port, auth_port))
                    return

                self.remote_machines[ident] = machine
                machine.connect("ops-changed", self.remote_ops_changed)
                machine.connect("remote-status-changed",
                                self.remote_status_changed)
                self.idle_emit("remote-machine-added", machine)

            machine.has_zc_presence = True

            machine.shutdown(
            )  # This does nothing if run more than once.  It's here to make sure
            # the previous start thread is complete before starting a new one.
            # This is needed in the corner case where the remote has gone offline,
            # and returns before our Ping loop times out and closes the thread
            # itself.

            machine.start_remote_thread()
예제 #12
0
 def add_service(self, zeroconf, type, name):
     zeroconf.get_service_info(type, name)
     service_added.set()
예제 #13
0
 def add_service(self, zeroconf, type, name):
     if self.service_info is None:
         self.service_info = zeroconf.get_service_info(type, name)
         print("service found", self.service_info.name)
예제 #14
0
 def remove_service(self, zeroconf, type, name):
     info = zeroconf.get_service_info(type, name)
     if self.server.cb_on_device_removed:
         self.server.cb_on_device_removed(info)
예제 #15
0
 def add_service(self, zeroconf, type, name):
     info = zeroconf.get_service_info(type, name)
     addr = str(ipaddress.ip_address(info.address))
     if addr not in self.found_devices:
         dev = self.check_and_create_device(info, addr)
         self.found_devices[addr] = dev
 def on_state_change(self, zeroconf, service_type, name, state_change):
     info = zeroconf.get_service_info(service_type, name)
     if info:
         self.deviceDiscovered.emit(name, socket.inet_ntoa(info.address), info)
예제 #17
0
 def on_state_change(self, zeroconf, service_type, name, state_change):
     info = zeroconf.get_service_info(service_type, name)
     if info:
         for addr in info.parsed_addresses():
          print("  Addresses: %s" % (addr))
          self.deviceDiscovered.emit(name, addr, info)
예제 #18
0
 def add_service(self, zeroconf, type, device):
   info = zeroconf.get_service_info(type, device)
   device = device.replace('._http._tcp.local.', '')
   if info:
     logger.trace(f"[Device Scan] found device: {device} added, IP address: {socket.inet_ntoa(info.addresses[0])}")
     self.queue.put(Device(device, socket.inet_ntoa(info.addresses[0])))
예제 #19
0
 def add_service(self, zeroconf, type, name):
     info = zeroconf.get_service_info(type, name)
     if info:
       addresses = [("%s" % socket.inet_ntoa(a)) for a in info.addresses]
       nm = name.split(info.type)[0].rstrip(".")
       self.myservices[f"{nm}"] = {"addresses": addresses, "port": info.port, "server": info.server.rstrip("."), "type": info.type}
예제 #20
0
 def update_service(self, zeroconf, type, name):
     info = zeroconf.get_service_info(type, name)
     print(f"ZeroConf: Update SERVICE {info}")
     addresses = [("%s" % socket.inet_ntoa(a)) for a in info.addresses]
     nm = name.split(info.type)[0].rstrip(".")
     self.myservices[f"{nm}"] = {"addresses": addresses, "port": info.port, "server": info.server.rstrip("."), "type": info.type}
예제 #21
0
 def add_service(self, zeroconf, type, name):
     info = zeroconf.get_service_info(type, name)
     if self.server.cb_on_device_added:
         self.server.cb_on_device_added(info)
예제 #22
0
 def update_record(self, zeroconf, now, record):
   print("ZeroConf: Update DNS RECORD")
   info = zeroconf.get_service_info(type, record.name)
   addresses = [("%s" % socket.inet_ntoa(a)) for a in info.addresses]
   nm = info.name.split(info.type)[0].rstrip(".")
   self.myservices[f"{nm}"] = {"addresses": addresses, "port": info.port, "server": info.server.rstrip("."), "type": info.type}
예제 #23
0
 def add_service(self, zeroconf, type, name):
     zeroconf.get_service_info(type, name)
     service_added.set()
예제 #24
0
    def add_service(self, zeroconf, _type, name):
        info = zeroconf.get_service_info(_type, name)

        if info:
            ident = name.partition(".")[0]

            try:
                remote_hostname = info.properties[b"hostname"].decode()
            except KeyError:
                logging.critical(
                    ">>> Discovery: no hostname in service info properties.  Is this an old version?"
                )
                return

            remote_ip = socket.inet_ntoa(info.addresses[0])

            if not util.same_subnet(remote_ip):
                if remote_ip != self.ip_address:
                    logging.debug(
                        ">>> Discovery: service is not on this subnet, ignoring: %s (%s)"
                        % (remote_hostname, remote_ip))
                return

            try:
                # Check if this is a flush registration to reset the remote servier's presence.
                if info.properties[b"type"].decode() == "flush":
                    logging.debug(
                        ">>> Discovery: received flush service info (ignoring): %s (%s:%d)"
                        % (remote_hostname, remote_ip, info.port))
                    return
            except KeyError:
                logging.warning(
                    "No type in service info properties, assuming this is a real connect attempt"
                )

            if ident == self.service_ident:
                return

            def check_cert():
                # This will block if the remote's warp udp port is closed, until either the port is unblocked
                # or we tell the auth object to shutdown, in which case the request timer will cancel and return
                # here immediately (with None)
                got_cert = auth.get_singleton().retrieve_remote_cert(
                    ident, remote_hostname, remote_ip, info.port)

                if not got_cert:
                    logging.critical(
                        ">>> Discovery: unable to authenticate with %s (%s:%d)"
                        % (remote_hostname, remote_ip, info.port))
                    return False
                return True

            try:
                machine = self.remote_machines[ident]
                machine.has_zc_presence = True
                logging.debug(">>> Discovery: existing remote: %s (%s:%d)" %
                              (machine.display_hostname, remote_ip, info.port))

                # If the remote truly is the same one (our service info just dropped out
                # momentarily), this will end up just retrieving the current cert again.
                # If this was a real disconnect we didn't notice, we'll have the new cert
                # which we'll need when our supposedly existing connection tries to continue
                # pinging. It will fail out and restart the connection loop, and will need
                # this updated one.
                if not check_cert():
                    return

                if machine.status == RemoteStatus.ONLINE:
                    logging.debug(
                        ">>> Discovery: rejoining existing connect with %s (%s:%d)"
                        % (machine.display_hostname, remote_ip, info.port))
                    return

                # Update our connect info if it changed.
                machine.hostname = remote_hostname
                machine.ip_address = remote_ip
                machine.port = info.port
            except KeyError:
                if not check_cert():
                    return

                display_hostname = remote_hostname
                i = 1

                while True:
                    found = False

                    for key in self.remote_machines.keys():
                        remote_machine = self.remote_machines[key]

                        if remote_machine.display_hostname == display_hostname:
                            display_hostname = "%s[%d]" % (remote_hostname, i)
                            found = True
                            break

                    i += 1

                    if not found:
                        break

                logging.debug(">>> Discovery: new remote: %s (%s:%d)" %
                              (display_hostname, remote_ip, info.port))

                machine = remote.RemoteMachine(ident, remote_hostname,
                                               display_hostname, remote_ip,
                                               info.port, self.service_ident)

                self.remote_machines[ident] = machine
                machine.connect("ops-changed", self.remote_ops_changed)
                machine.connect("remote-status-changed",
                                self.remote_status_changed)
                self.idle_emit("remote-machine-added", machine)

            machine.has_zc_presence = True

            logging.debug(
                ">>> Discovery: closing previous connection for %s (%s:%d)" %
                (machine.display_hostname, remote_ip, info.port))
            machine.shutdown(
            )  # This does nothing if run more than once.  It's here to make sure
            # the previous start thread is complete before starting a new one.
            # This is needed in the corner case where the remote has gone offline,
            # and returns before our Ping loop times out and closes the thread
            # itself.

            machine.start_remote_thread()