Exemplo n.º 1
0
    async def _spawn_protocol(self, message):
        if message.unresolved_remote is None:
            host = message.opt.uri_host
            port = message.opt.uri_port or self._default_port
            if host is None:
                raise ValueError(
                    "No location found to send message to (neither in .opt.uri_host nor in .remote)"
                )
        else:
            host, port = util.hostportsplit(message.unresolved_remote)
            port = port or self._default_port

        if (host, port) in self._pool:
            return self._pool[(host, port)]

        _, protocol = await self.loop.create_connection(lambda: TcpConnection(
            self, self.log, self.loop, hostinfo=util.hostportjoin(host, port)),
                                                        host,
                                                        port,
                                                        ssl=self.
                                                        _ssl_context_factory())

        self._pool[(host, port)] = protocol

        return protocol
Exemplo n.º 2
0
    async def _spawn_protocol(self, message):
        if message.unresolved_remote is None:
            host = message.opt.uri_host
            port = message.opt.uri_port or self._default_port
            if host is None:
                raise ValueError(
                    "No location found to send message to (neither in .opt.uri_host nor in .remote)"
                )
        else:
            host, port = util.hostportsplit(message.unresolved_remote)
            port = port or self._default_port

        if (host, port) in self._pool:
            return self._pool[(host, port)]

        try:
            _, protocol = await self.loop.create_connection(
                lambda: TcpConnection(self,
                                      self.log,
                                      self.loop,
                                      hostinfo=util.hostportjoin(host, port)),
                host,
                port,
                ssl=self._ssl_context_factory())
        except socket.gaierror:
            raise error.ResolutionError(
                "No address information found for requests to %r" % host)
        except OSError:
            raise error.NetworkError("Connection failed to %r" % host)

        self._pool[(host, port)] = protocol

        return protocol
Exemplo n.º 3
0
    async def _connect_task(self, key: PoolKey):
        try:
            if key.scheme == 'coaps+ws':
                ssl_context = self._client_credentials.ssl_client_context(key.scheme, key.hostinfo)
            else:
                # websockets library would not appreciate the extra info when connecting to ws://
                ssl_context = None

            hostinfo_split = util.hostportsplit(key.hostinfo)

            websocket = await websockets.connect("%s://%s/.well-known/coap" % (
                {'coap+ws': 'ws', 'coaps+ws': 'wss'}[key.scheme], key.hostinfo),
                subprotocols=['coap'],
                ping_interval=None,
                ssl=ssl_context,
                )

            remote = WSRemote(self, websocket, self.loop, self.log, scheme=key.scheme, remote_hostinfo=hostinfo_split)
            self._pool[remote] = remote

            self.loop.create_task(self._run_recv_loop(remote))

            return remote
        finally:
            del self._outgoing_starting[key]
Exemplo n.º 4
0
    async def determine_remote(self, request):
        if request.requested_scheme not in ('coap', None):
            return None

        if request.unresolved_remote is not None:
            host, port = util.hostportsplit(request.unresolved_remote)
            port = port or COAP_PORT
        elif request.opt.uri_host:
            host = request.opt.uri_host
            port = request.opt.uri_port or COAP_PORT
        else:
            raise ValueError("No location found to send message to (neither in .opt.uri_host nor in .remote)")

        return await self._pool.connect((host, port))
Exemplo n.º 5
0
    async def _new_connection(self, websocket, path, *, scheme):
        # ignoring path: Already checked in _process_request

        hostheader = websocket.request_headers['Host']
        if hostheader.count(':') > 1 and '[' not in hostheader:
            # Workaround for websockets version before
            # https://github.com/aaugustin/websockets/issues/802
            #
            # To be removed once a websockets version with this fix can be
            # depended on
            hostheader = '[' + hostheader[:hostheader.rfind(':')] + ']' + hostheader[hostheader.rfind(':'):]
        local_hostinfo = util.hostportsplit(hostheader)

        remote = WSRemote(self, websocket, self.loop, self.log, scheme=scheme, local_hostinfo=local_hostinfo)

        await self._run_recv_loop(remote)
Exemplo n.º 6
0
    async def connection(self, message):

        if message.unresolved_remote is None:
            host = message.opt.uri_host
            port = message.opt.uri_port or self.default_port
            if host is None:
                raise ValueError(
                    "No location found to send message to (neither in .opt.uri_host nor in .remote)"
                )
        else:
            host, port = util.hostportsplit(message.unresolved_remote)
            port = port or self.default_port

        try:
            ipaddress.ip_address(host)
            server_name = None
        except ValueError as ve:
            server_name = host

        infos = await self.loop.getaddrinfo(host, port, type=socket.SOCK_DGRAM)
        self.addr = infos[0][4]

        config = QuicConfiguration(is_client=True,
                                   alpn_protocols='coap',
                                   idle_timeout=864000,
                                   server_name=server_name)
        config.verify_mode = ssl.CERT_NONE

        if config.server_name is None:
            config.server_name = server_name

        connection = QuicConnection(configuration=config)

        self.quic = Quic(connection)
        self.quic.ctx = self

        try:
            transport, protocol = await self.loop.create_datagram_endpoint(
                lambda: self.quic, remote_addr=(host, port))
            protocol.connect(self.addr)
            await protocol.wait_connected()
            self.con = True
        except OSError:
            raise error.NetworkError("Connection failed to %r" % host)

        return protocol
Exemplo n.º 7
0
    async def _spawn_protocol(self, message):
        if message.unresolved_remote is None:
            host = message.opt.uri_host
            port = message.opt.uri_port or self._default_port
            if host is None:
                raise ValueError("No location found to send message to (neither in .opt.uri_host nor in .remote)")
        else:
            host, port = util.hostportsplit(message.unresolved_remote)
            port = port or self._default_port

        if (host, port) in self._pool:
            return self._pool[(host, port)]

        _, protocol = await self.loop.create_connection(
                lambda: TcpConnection(self, self.log, self.loop,
                    hostinfo=util.hostportjoin(host, port)),
                host, port,
                ssl=self._ssl_context_factory())

        self._pool[(host, port)] = protocol

        return protocol