示例#1
0
    async def _parse_cmd(self, cmd: ReactorCommand):
        """
        Parses a cmd for execution, by extracting/preparing the necessary kwargs for execution.
        :param cmd: an instance of ReactorCommand
        :return: A tuple of 3 elements (executor_name, executor_func, kwargs)
        """
        executor_name = cmd.class_name
        executor_func = cmd.func_name
        kwargs = cmd.kwargs

        # Remove class_name and func_name from kwargs. We just need these to lookup the function to call
        del (kwargs['class_name'])
        del (kwargs['func_name'])

        # Add envelope to kwargs if its in the reactor command
        if cmd.envelope_binary:
            kwargs['envelope'] = cmd.envelope_binary

        # Replace VK with IP address if necessary
        if 'url' in kwargs:
            url = kwargs['url']

            # Check if URL has a VK inside
            vk = IPUtils.get_vk(url)
            if vk:
                ip = await self.dht.network.lookup_ip(vk)
                new_url = IPUtils.interpolate_url(url, ip)
                kwargs['url'] = new_url

        return executor_name, executor_func, kwargs
示例#2
0
文件: daemon.py 项目: Mat001/cilantro
    async def _lookup_ip(self, cmd, url, vk, *args, **kwargs):
        ip, node = None, None
        try:
            node = await self.dht.network.lookup_ip(vk)
        except Exception as e:
            delim_line = '!' * 64
            err_msg = '\n\n' + delim_line + '\n' + delim_line
            err_msg += '\n ERROR CAUGHT IN LOOKUP FUNCTION {}\ncalled \w args={}\nand kwargs={}\n'\
                        .format(args, kwargs)
            err_msg += '\nError Message: '
            err_msg += '\n\n{}'.format(traceback.format_exc())
            err_msg += '\n' + delim_line + '\n' + delim_line
            self.log.error(err_msg)

        if node is None:

            kwargs = cmd.kwargs
            callback = ReactorCommand.create_callback(
                callback=StateInput.LOOKUP_FAILED, **kwargs)
            self.log.debug(
                "Sending callback failure to mainthread {}".format(callback))
            self.socket.send(callback.serialize())
            # TODO -- send callback to SM saying hey i couldnt lookup this vk

            return

        # Send interpolated command back through pipeline
        ip = node.ip if type(node) == Node else node
        new_url = IPUtils.interpolate_url(url, ip)
        kwargs = cmd.kwargs
        kwargs['url'] = new_url
        new_cmd = ReactorCommand.create_cmd(envelope=cmd.envelope, **kwargs)

        self._execute_cmd(new_cmd)
示例#3
0
    def test_get_vk_valid(self):
        vk = 'E532F434E348749B97F371212CDEF69F436AB8D342618AF67D1443808E36DAEF'
        vk_url = 'tcp://{}:7020'.format(vk)

        fetched_vk = IPUtils.get_vk(vk_url)

        self.assertEqual(vk, fetched_vk)
示例#4
0
    def _parse_cmd(self, cmd: ReactorCommand):
        """
        Parses a cmd for execution, by extracting/preparing the necessary kwargs for execution.
        :param cmd: an instance of ReactorCommand
        :return: A tuple of 3 elements (executor_name, executor_func, kwargs)
        """
        executor_name = cmd.class_name
        executor_func = cmd.func_name
        kwargs = cmd.kwargs

        # Remove class_name and func_name from kwargs. We just need these to lookup the function to call
        del kwargs['class_name']
        del kwargs['func_name']
        #timer.sleep(2)

        # Add envelope to kwargs if its in the reactor command
        if cmd.envelope_binary:
            kwargs['envelope'] = cmd.envelope_binary

        # Replace VK with IP address if necessary
        if 'url' in kwargs:
            self.log.debug("Processing command with url {}".format(
                kwargs['url']))
            url = kwargs['url']

            # Check if URL has a VK inside
            vk = IPUtils.get_vk(url)
            self.log.info(
                "raghu ** _parse_cmd: exe_nm {} exe_fn {} url {} vk {}".format(
                    executor_name, executor_func, url, vk))
            if vk:
                if vk == self.dht.network.ironhouse.vk:
                    ip = self.dht.ip
                else:
                    ip = self.dht.network.lookup_ip_in_cache(vk)
                if not ip:
                    self.log.info(
                        "Could not find ip for vk {} in cache. Performing lookup in DHT."
                        .format(vk))

                    asyncio.ensure_future(self._lookup_ip(cmd, url, vk))
                    return

                new_url = IPUtils.interpolate_url(url, ip)
                kwargs['url'] = new_url

        return executor_name, executor_func, kwargs
示例#5
0
    def test_interpolate_url(self):
        ip = "127.0.0.1"
        url = "tcp://E532F434E348749B97F371212CDEF69F436AB8D342618AF67D1443808E36DAEF:8080"

        actual_url = IPUtils.interpolate_url(url, ip)
        correct_url = 'tcp://{}:8080'.format(ip)

        self.assertEqual(actual_url, correct_url)
示例#6
0
    async def _lookup_ip(self, cmd, url, vk, *args, **kwargs):
        ip, node = None, None
        try:
            node, cached = await self.dht.network.lookup_ip(vk)
            # NOTE while secure, this is a more loose connection policy
            self.log.fatal('{} resolves for {}'.format(os.getenv('HOST_IP'),
                                                       node))
            if node and not cached:
                ip = node.ip if type(node) == Node else node.split(':')[0]
                public_key = self.dht.network.ironhouse.vk2pk(vk)
                authorization = await self.dht.network.ironhouse.authenticate(
                    public_key, ip)
                self.log.fatal('{} -> {} is {}'.format(os.getenv('HOST_IP'),
                                                       node, authorization))
                if authorization != 'authorized':
                    node = None
                else:
                    n = Node(node_id=digest(vk),
                             public_key=public_key,
                             ip=ip,
                             port=self.dht.network.network_port)
                    self.dht.network.protocol.router.addContact(n)
                    self.dht.network.connect_to_neighbor(n)

                self.log.fatal([
                    item[0]
                    for item in self.dht.network.bootstrappableNeighbors()
                ])
        except Exception as e:
            delim_line = '!' * 64
            err_msg = '\n\n' + delim_line + '\n' + delim_line
            err_msg += '\n ERROR CAUGHT IN LOOKUP FUNCTION {}\ncalled \w args={}\nand kwargs={}\n'\
                        .format(args, kwargs)
            err_msg += '\nError Message: '
            err_msg += '\n\n{}'.format(traceback.format_exc())
            err_msg += '\n' + delim_line + '\n' + delim_line
            self.log.error(err_msg)

        if node is None:

            kwargs = cmd.kwargs
            callback = ReactorCommand.create_callback(
                callback=StateInput.LOOKUP_FAILED, **kwargs)
            self.log.debug(
                "Sending callback failure to mainthread {}".format(callback))
            self.socket.send(callback.serialize())
            # TODO -- send callback to SM saying hey i couldnt lookup this vk

            return

        # Send interpolated command back through pipeline
        ip = node.ip if type(node) == Node else node
        new_url = IPUtils.interpolate_url(url, ip)
        kwargs = cmd.kwargs
        kwargs['url'] = new_url
        new_cmd = ReactorCommand.create_cmd(envelope=cmd.envelope, **kwargs)

        self._execute_cmd(new_cmd)
示例#7
0
    async def _lookup_ip(self, cmd, url, vk, *args, **kwargs):
        ip, node = None, None
        try:
            node, cached = await self.dht.network.lookup_ip(vk)
            # NOTE while secure, this is a more loose connection policy
            self.log.debugv('IP {} resolves {} into {}'.format(
                os.getenv('HOST_IP', '127.0.0.1'), vk, node))
            self.log.debugv(
                '... but is {} authorized? Until next episode!'.format(node))
            if node:
                if not self.dht.network.ironhouse.authorized_nodes.get(
                        node.id):
                    authorization = await self.dht.network.authenticate(node)
                    if not authorization:
                        node = None
            else:
                node = None

        except Exception as e:
            delim_line = '!' * 64
            err_msg = '\n\n' + delim_line + '\n' + delim_line
            err_msg += '\n ERROR CAUGHT IN LOOKUP FOR VK {}\ncalled \w args={}\nand kwargs={}\n'\
                       .format(vk, args, kwargs)
            err_msg += '\nError Message: '
            err_msg += '\n\n{}'.format(traceback.format_exc())
            err_msg += '\n' + delim_line + '\n' + delim_line
            self.log.fatal(err_msg)

        if node is None:
            kwargs = cmd.kwargs
            callback = ReactorCommand.create_callback(
                callback=StateInput.LOOKUP_FAILED, **kwargs)
            self.log.debug(
                "Sending callback failure to mainthread {}".format(callback))
            self.socket.send(callback.serialize())
            # TODO -- send callback to SM saying hey i couldnt lookup this vk

            return

        # Send interpolated command back through pipeline
        ip = node.ip if type(node) == Node else node
        new_url = IPUtils.interpolate_url(url, ip)
        kwargs = cmd.kwargs
        kwargs['url'] = new_url
        new_cmd = ReactorCommand.create_cmd(envelope=cmd.envelope, **kwargs)

        self._execute_cmd(new_cmd)
示例#8
0
    def test_get_vk_invalid(self):
        fetched_vk = IPUtils.get_vk("tcp://127.0.0.1:8080")

        self.assertEqual(False, fetched_vk)