Пример #1
0
 def __init__(self, msg, timestamp=None):
     """
     @type msg: str
     @type timestamp: float
     """
     self.msg = msg
     self.timestamp = timestamp or utils.timestamp()
Пример #2
0
 def __init__(self, msg, timestamp=None):
     """
     @type msg: str
     @type timestamp: float
     """
     self.msg = msg
     self.timestamp = timestamp or utils.timestamp()
Пример #3
0
    def establish_ssl(self, clientcerts, sni, **kwargs):
        clientcert = None
        if clientcerts:
            if os.path.isfile(clientcerts):
                clientcert = clientcerts
            else:
                path = os.path.join(clientcerts,
                                    self.address.host.encode("idna")) + ".pem"
                if os.path.exists(path):
                    clientcert = path

        self.convert_to_ssl(cert=clientcert, sni=sni, **kwargs)
        self.sni = sni
        self.timestamp_ssl_setup = utils.timestamp()
Пример #4
0
 def send_response_to_client(self, flow):
     if not flow.response.stream:
         # no streaming:
         # we already received the full response from the server and can
         # send it to the client straight away.
         self.send_response(flow.response)
     else:
         # streaming:
         # First send the headers and then transfer the response incrementally
         self.send_response_headers(flow.response)
         chunks = self.read_response_body(flow.request, flow.response)
         if callable(flow.response.stream):
             chunks = flow.response.stream(chunks)
         self.send_response_body(flow.response, chunks)
         flow.response.timestamp_end = utils.timestamp()
Пример #5
0
    def establish_ssl(self, clientcerts, sni, **kwargs):
        clientcert = None
        if clientcerts:
            if os.path.isfile(clientcerts):
                clientcert = clientcerts
            else:
                path = os.path.join(
                    clientcerts,
                    self.address.host.encode("idna")) + ".pem"
                if os.path.exists(path):
                    clientcert = path

        self.convert_to_ssl(cert=clientcert, sni=sni, **kwargs)
        self.sni = sni
        self.timestamp_ssl_setup = utils.timestamp()
Пример #6
0
    def get_response_from_server(self, flow):
        def get_response():
            self.send_request(flow.request)
            flow.response = self.read_response_headers()

        try:
            get_response()
        except netlib.exceptions.NetlibException as e:
            self.log(
                "server communication error: %s" % repr(e),
                level="debug"
            )
            # In any case, we try to reconnect at least once. This is
            # necessary because it might be possible that we already
            # initiated an upstream connection after clientconnect that
            # has already been expired, e.g consider the following event
            # log:
            # > clientconnect (transparent mode destination known)
            # > serverconnect (required for client tls handshake)
            # > read n% of large request
            # > server detects timeout, disconnects
            # > read (100-n)% of large request
            # > send large request upstream

            if isinstance(e, exceptions.Http2ProtocolException):
                # do not try to reconnect for HTTP2
                raise exceptions.ProtocolException("First and only attempt to get response via HTTP2 failed.")

            self.disconnect()
            self.connect()
            get_response()

        # call the appropriate script hook - this is an opportunity for an
        # inline script to set flow.stream = True
        flow = self.channel.ask("responseheaders", flow)

        if flow.response.stream:
            flow.response.data.content = None
        else:
            flow.response.data.content = b"".join(self.read_response_body(
                flow.request,
                flow.response
            ))
        flow.response.timestamp_end = utils.timestamp()

        # no further manipulation of self.server_conn beyond this point
        # we can safely set it as the final attribute value here.
        flow.server_conn = self.server_conn
Пример #7
0
    def __init__(self, client_connection, address, server):
        # Eventually, this object is restored from state. We don't have a
        # connection then.
        if client_connection:
            super(ClientConnection, self).__init__(client_connection, address, server)
        else:
            self.connection = None
            self.server = None
            self.wfile = None
            self.rfile = None
            self.address = None
            self.clientcert = None
            self.ssl_established = None

        self.timestamp_start = utils.timestamp()
        self.timestamp_end = None
        self.timestamp_ssl_setup = None
        self.protocol = None
Пример #8
0
 def send_response_to_client(self, flow):
     if not flow.response.stream:
         # no streaming:
         # we already received the full response from the server and can
         # send it to the client straight away.
         self.send_response(flow.response)
     else:
         # streaming:
         # First send the headers and then transfer the response incrementally
         self.send_response_headers(flow.response)
         chunks = self.read_response_body(
             flow.request,
             flow.response
         )
         if callable(flow.response.stream):
             chunks = flow.response.stream(chunks)
         self.send_response_body(flow.response, chunks)
         flow.response.timestamp_end = utils.timestamp()
Пример #9
0
    def get_response_from_server(self, flow):
        def get_response():
            self.send_request(flow.request)
            flow.response = self.read_response_headers()

        try:
            get_response()
        except netlib.exceptions.NetlibException as e:
            self.log("server communication error: %s" % repr(e), level="debug")
            # In any case, we try to reconnect at least once. This is
            # necessary because it might be possible that we already
            # initiated an upstream connection after clientconnect that
            # has already been expired, e.g consider the following event
            # log:
            # > clientconnect (transparent mode destination known)
            # > serverconnect (required for client tls handshake)
            # > read n% of large request
            # > server detects timeout, disconnects
            # > read (100-n)% of large request
            # > send large request upstream

            if isinstance(e, exceptions.Http2ProtocolException):
                # do not try to reconnect for HTTP2
                raise exceptions.ProtocolException(
                    "First and only attempt to get response via HTTP2 failed.")

            self.disconnect()
            self.connect()
            get_response()

        # call the appropriate script hook - this is an opportunity for an
        # inline script to set flow.stream = True
        flow = self.channel.ask("responseheaders", flow)

        if flow.response.stream:
            flow.response.data.content = None
        else:
            flow.response.data.content = b"".join(
                self.read_response_body(flow.request, flow.response))
        flow.response.timestamp_end = utils.timestamp()

        # no further manipulation of self.server_conn beyond this point
        # we can safely set it as the final attribute value here.
        flow.server_conn = self.server_conn
Пример #10
0
    def __init__(self, client_connection, address, server):
        # Eventually, this object is restored from state. We don't have a
        # connection then.
        if client_connection:
            super(ClientConnection, self).__init__(client_connection, address,
                                                   server)
        else:
            self.connection = None
            self.server = None
            self.wfile = None
            self.rfile = None
            self.address = None
            self.clientcert = None
            self.ssl_established = None

        self.timestamp_start = utils.timestamp()
        self.timestamp_end = None
        self.timestamp_ssl_setup = None
        self.protocol = None
Пример #11
0
 def convert_to_ssl(self, *args, **kwargs):
     super(ClientConnection, self).convert_to_ssl(*args, **kwargs)
     self.timestamp_ssl_setup = utils.timestamp()
Пример #12
0
 def finish(self):
     tcp.TCPClient.finish(self)
     self.timestamp_end = utils.timestamp()
Пример #13
0
 def connect(self):
     self.timestamp_start = utils.timestamp()
     tcp.TCPClient.connect(self)
     self.timestamp_tcp_setup = utils.timestamp()
Пример #14
0
 def finish(self):
     super(ClientConnection, self).finish()
     self.timestamp_end = utils.timestamp()
Пример #15
0
def test_format_timestamp():
    assert utils.format_timestamp(utils.timestamp())
Пример #16
0
def test_format_timestamp_with_milli():
    assert utils.format_timestamp_with_milli(utils.timestamp())
Пример #17
0
 def connect(self):
     self.timestamp_start = utils.timestamp()
     tcp.TCPClient.connect(self)
     self.timestamp_tcp_setup = utils.timestamp()
Пример #18
0
 def finish(self):
     super(ClientConnection, self).finish()
     self.timestamp_end = utils.timestamp()
Пример #19
0
 def convert_to_ssl(self, *args, **kwargs):
     super(ClientConnection, self).convert_to_ssl(*args, **kwargs)
     self.timestamp_ssl_setup = utils.timestamp()
Пример #20
0
 def finish(self):
     tcp.TCPClient.finish(self)
     self.timestamp_end = utils.timestamp()