예제 #1
0
    def create_connection(self, timeout: int) -> socket.SocketType:
        """Setup HTTP tunneling with the configured proxy.
        """
        # Setup HTTP tunneling
        try:
            sock = socket.create_connection((self._tunnel_host, self._tunnel_port), timeout=timeout)
        except socket.timeout as e:
            raise ProxyError(self.ERR_PROXY_OFFLINE.format(str(e)))
        except socket.error as e:
            raise ProxyError(self.ERR_PROXY_OFFLINE.format(str(e)))

        # Send a CONNECT request with the host we want to tunnel to
        if self._tunnel_basic_auth_token is None:
            sock.send(self.HTTP_CONNECT_REQ.format(self._server_host, self._server_port).encode("utf-8"))
        else:
            sock.send(
                self.HTTP_CONNECT_REQ_PROXY_AUTH_BASIC.format(
                    self._server_host, self._server_port, self._tunnel_basic_auth_token.decode()
                ).encode("utf-8")
            )
        http_response = HttpResponseParser.parse_from_socket(sock)

        # Check if the proxy was able to connect to the host
        if http_response.status != 200:
            raise ProxyError(self.ERR_CONNECT_REJECTED)

        return sock
예제 #2
0
    def create_connection(self, timeout: int) -> socket.SocketType:
        """Setup HTTP tunneling with the configured proxy.
        """
        # Setup HTTP tunneling
        try:
            sock = socket.create_connection((self._tunnel_host, self._tunnel_port), timeout=timeout)
        except socket.timeout as e:
            raise ProxyError(self.ERR_PROXY_OFFLINE.format(str(e)))
        except socket.error as e:
            raise ProxyError(self.ERR_PROXY_OFFLINE.format(str(e)))

        # Send a CONNECT request with the host we want to tunnel to
        if self._tunnel_basic_auth_token is None:
            sock.send(self.HTTP_CONNECT_REQ.format(self._server_host, self._server_port).encode('utf-8'))
        else:
            sock.send(self.HTTP_CONNECT_REQ_PROXY_AUTH_BASIC.format(
                self._server_host, self._server_port, self._tunnel_basic_auth_token
            ).encode('utf-8'))
        http_response = HttpResponseParser.parse_from_socket(sock)

        # Check if the proxy was able to connect to the host
        if http_response.status != 200:
            raise ProxyError(self.ERR_CONNECT_REJECTED)

        return sock
예제 #3
0
    def do_pre_handshake(self, network_timeout):
        # type: (Optional[int]) -> socket.socket
        """Open a socket to the server; setup HTTP tunneling if a proxy was configured.
        """
        final_timeout = self.NETWORK_TIMEOUT if network_timeout is None else network_timeout

        if self._tunnel_host and self._tunnel_port:
            # Proxy configured; setup HTTP tunneling
            try:
                sock = socket.create_connection(
                    (self._tunnel_host, self._tunnel_port), final_timeout)
            except socket.timeout as e:
                raise ProxyError(self.ERR_PROXY_OFFLINE.format(str(e)))
            except socket.error as e:
                raise ProxyError(self.ERR_PROXY_OFFLINE.format(str(e)))

            # Send a CONNECT request with the host we want to tunnel to
            if self._tunnel_basic_auth_token is None:
                sock.send(
                    self.HTTP_CONNECT_REQ.format(self._hostname,
                                                 self._port).encode('utf-8'))
            else:
                sock.send(
                    self.HTTP_CONNECT_REQ_PROXY_AUTH_BASIC.format(
                        self._hostname, self._port,
                        self._tunnel_basic_auth_token).encode('utf-8'))
            http_response = HttpResponseParser.parse_from_socket(sock)

            # Check if the proxy was able to connect to the host
            if http_response.status != 200:
                raise ProxyError(self.ERR_CONNECT_REJECTED)
        elif self._ip_address:
            # No proxy; connect directly to the server
            sock = socket.create_connection(address=(self._ip_address,
                                                     self._port),
                                            timeout=final_timeout)
        else:
            raise RuntimeError('Should never happen')

        # Pass the connected socket to the SSL client
        self.ssl_client.set_underlying_socket(sock)
        return sock