Exemple #1
0
def patched_connect(self):
    # Add certificate verification
    try:
        sock = socket.create_connection(address=(self.host, self.port),
                                        timeout=self.timeout)
    except SocketTimeout:
        raise ConnectTimeoutError(
            self, "Connection to %s timed out. (connect timeout=%s)" %
            (self.host, self.timeout))

    resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs)
    resolved_ssl_version = resolve_ssl_version(self.ssl_version)

    if self._tunnel_host:
        self.sock = sock
        # Calls self._set_hostport(), so self.host is
        # self._tunnel_host below.
        self._tunnel()

    # Wrap socket using verification with the root certs in
    # trusted_root_certs
    self.sock = ssl_wrap_socket(sock,
                                self.key_file,
                                self.cert_file,
                                cert_reqs=resolved_cert_reqs,
                                ca_certs=self.ca_certs,
                                server_hostname=self.host,
                                ssl_version=resolved_ssl_version)

    if self.assert_fingerprint:
        assert_fingerprint(self.sock.getpeercert(binary_form=True),
                           self.assert_fingerprint)
    elif resolved_cert_reqs != ssl.CERT_NONE and self.assert_hostname is not False:
        match_hostname(self.sock.getpeercert(), self.assert_hostname
                       or self.host)
Exemple #2
0
def _validating_new_conn(self):
    """ Establish a socket connection and set nodelay settings on it.

    :return: New socket connection.
    """
    extra_kw = {}
    if self.source_address:
        extra_kw['source_address'] = self.source_address

    if self.socket_options:
        extra_kw['socket_options'] = self.socket_options

    try:
        # Hack around HTTPretty's patched sockets
        # TODO: some better method of hacking around it that checks if we
        # _would have_ connected to a private addr?
        conn_func = validating_create_connection
        if socket.getaddrinfo.__module__.startswith("httpretty"):
            conn_func = old_create_connection
        else:
            extra_kw["validator"] = self._validator

        conn = conn_func((self.host, self.port), self.timeout, **extra_kw)

    except SocketTimeout:
        raise ConnectTimeoutError(
            self, "Connection to %s timed out. (connect timeout=%s)" %
            (self.host, self.timeout))

    return conn
Exemple #3
0
def patched_new_conn(self):
    """ Establish a socket connection and set nodelay settings on it

    :return: a new socket connection
    """
    # resolve hostname to an ip address; use your own
    # resolver here, as otherwise the system resolver will be used.
    hostname = get_a_record(self.host)
    print "==========121=============:%s,type: %s, %s, :%d" % (hostname, type(hostname), self.host, self.port)

    extra_kw = {}
    if self.source_address:
        extra_kw['source_address'] = self.source_address

    if self.socket_options:
        extra_kw['socket_options'] = self.socket_options

    try:
        conn = connection.create_connection(
            (hostname, self.port), self.timeout, **extra_kw)

    except SocketTimeout:
        raise ConnectTimeoutError(
            self, "Connection to %s timed out. (connect timeout=%s)" %
                  (self.host, self.timeout))

    return conn
Exemple #4
0
 def _new_conn(self):
     logger.debug('[MyHTTPSConnection] new conn %s:%i', self.host, self.port)
     try:
         self._tor_stream = self._circuit.create_stream((self.host, self.port))
         logger.debug('[MyHTTPSConnection] tor_stream create_socket')
         return self._tor_stream.create_socket()
     except TimeoutError:
         logger.error('TimeoutError')
         raise ConnectTimeoutError(
             self, 'Connection to %s timed out. (connect timeout=%s)' % (self.host, self.timeout)
         )
     except Exception as e:
         logger.error('NewConnectionError')
         raise NewConnectionError(self, 'Failed to establish a new connection: %s' % e)
Exemple #5
0
 def connect(self):
     logger.debug('[MyHTTPConnection] connect %s:%i', self.host, self.port)
     try:
         self._tor_stream = self._circuit.create_stream_(
             (self.host, self.port))
         logger.debug('[MyHTTPConnection] tor_stream create_socket')
         self.sock = self._tor_stream.create_socket()
         if self._tunnel_host:
             self._tunnel()
     except TimeoutError as e:
         logger.error("TimeoutError")
         raise ConnectTimeoutError(
             self, "Connection to %s timed out. (connect timeout=%s)" %
             (self.host, self.timeout))
     except Exception as e:
         logger.error("NewConnectionError")
         raise NewConnectionError(
             self, "Failed to establish a new connection: %s" % e)
    def _new_conn(self):
        extra_kw = {}
        if self.source_address:
            extra_kw['source_address'] = self.source_address

        if getattr(self, 'socket_options', None):
            extra_kw['socket_options'] = self.socket_options

        dest_host = self.dest_ip if self.dest_ip else self.host

        try:
            conn = connection.create_connection(
                (dest_host, self.port), self.timeout, **extra_kw)

        except SocketTimeout as e:
            raise ConnectTimeoutError(
                self, "Connection to %s timed out. (connect timeout=%s)" %
                (self.host, self.timeout))

        except SocketError as e:
            raise NewConnectionError(
                self, "Failed to establish a new connection: %s" % e)

        return conn