コード例 #1
0
 async def _wrap_create_socks_connection(self, *args, req, **kwargs):
     try:
         return await create_connection(*args, **kwargs)
     except certificate_errors as exc:
         raise aiohttp.ClientConnectorCertificateError(
             req.connection_key, exc) from exc
     except ssl_errors as exc:
         raise aiohttp.ClientConnectorSSLError(req.connection_key,
                                               exc) from exc
     except (OSError, SocksConnectionError) as exc:
         raise aiohttp.ClientProxyConnectionError(req.connection_key,
                                                  exc) from exc
コード例 #2
0
    async def _create_socks_connection(self, req):
        if req.ssl:
            sslcontext = self.ssl_context
        else:
            sslcontext = None

        if not self._remote_resolve:
            dst_hosts = list(await self._resolve_host(req.host, req.port))
            dst = dst_hosts[0]['host'], dst_hosts[0]['port']
        else:
            dst = req.host, req.port

        proxy_hosts = await self._resolve_host(req.proxy.host, req.proxy.port)
        exc = None

        for hinfo in proxy_hosts:
            if req.proxy.scheme == 'socks4':
                proxy = Socks4Addr(hinfo['host'], hinfo['port'])
            else:
                proxy = Socks5Addr(hinfo['host'], hinfo['port'])

            try:
                transp, proto = await create_connection(
                    self._factory,
                    proxy,
                    req.proxy_auth,
                    dst,
                    loop=self._loop,
                    remote_resolve=self._remote_resolve,
                    ssl=sslcontext,
                    family=hinfo['family'],
                    proto=hinfo['proto'],
                    flags=hinfo['flags'],
                    local_addr=self._local_addr,
                    server_hostname=req.host if sslcontext else None)

                self._validate_ssl_fingerprint(transp, req.host, req.port)
                return transp, proto
            except (OSError, SocksError, SocksConnectionError) as e:
                exc = e
        else:
            if isinstance(exc, SocksConnectionError):
                raise aiohttp.ClientProxyConnectionError(*exc.args)
            if isinstance(exc, SocksError):
                raise exc
            else:
                raise aiohttp.ClientOSError(
                    exc.errno, 'Can not connect to %s:%s [%s]' %
                    (req.host, req.port, exc.strerror)) from exc