def send_error_response(self, code, message): try: response = models.make_error_response(code, message) self.send_response(response) except (netlib.exceptions.NetlibException, h2.exceptions.H2Error, exceptions.Http2ProtocolException): self.log(traceback.format_exc(), "debug")
def authenticate(self, request): if self.config.authenticator: if self.config.authenticator.authenticate(request.headers): self.config.authenticator.clean(request.headers) else: if self.mode == "transparent": self.send_response(models.make_error_response( 401, "Authentication Required", http.Headers(**self.config.authenticator.auth_challenge_headers()) )) else: self.send_response(models.make_error_response( 407, "Proxy Authentication Required", http.Headers(**self.config.authenticator.auth_challenge_headers()) )) return False return True
def authenticate(self, request): if self.config.authenticator: if self.config.authenticator.authenticate(request.headers): self.config.authenticator.clean(request.headers) else: if self.mode == "transparent": self.send_response( models.make_error_response( 401, "Authentication Required", http.Headers(**self.config.authenticator. auth_challenge_headers()))) else: self.send_response( models.make_error_response( 407, "Proxy Authentication Required", http.Headers(**self.config.authenticator. auth_challenge_headers()))) return False return True
def handle(self): self.log("clientconnect", "info") root_layer = self._create_root_layer() try: root_layer = self.channel.ask("clientconnect", root_layer) root_layer() except exceptions.Kill: self.log("Connection killed", "info") except exceptions.ProtocolException as e: if isinstance(e, exceptions.ClientHandshakeException): self.log( "Client Handshake failed. " "The client may not trust the proxy's certificate for {}.". format(e.server), "warn") self.log(repr(e), "debug") elif isinstance(e, exceptions.InvalidServerCertificate): self.log(str(e), "warn") self.log( "Invalid certificate, closing connection. Pass --insecure to disable validation.", "warn") else: self.log(str(e), "warn") self.log(traceback.format_exc(), "debug") # If an error propagates to the topmost level, # we send an HTTP error response, which is both # understandable by HTTP clients and humans. try: error_response = models.make_error_response(502, repr(e)) self.client_conn.send(http1.assemble_response(error_response)) except netlib.exceptions.TcpException: pass except Exception: self.log(traceback.format_exc(), "error") print(traceback.format_exc(), file=sys.stderr) print("mitmproxy has crashed!", file=sys.stderr) print( "Please lodge a bug report at: https://github.com/mitmproxy/mitmproxy", file=sys.stderr) self.log("clientdisconnect", "info") self.channel.tell("clientdisconnect", root_layer) self.client_conn.finish()
def handle(self): self.log("clientconnect", "info") root_layer = self._create_root_layer() try: root_layer = self.channel.ask("clientconnect", root_layer) root_layer() except exceptions.Kill: self.log("Connection killed", "info") except exceptions.ProtocolException as e: if isinstance(e, exceptions.ClientHandshakeException): self.log( "Client Handshake failed. " "The client may not trust the proxy's certificate for {}.".format(e.server), "warn" ) self.log(repr(e), "debug") elif isinstance(e, exceptions.InvalidServerCertificate): self.log(str(e), "warn") self.log("Invalid certificate, closing connection. Pass --insecure to disable validation.", "warn") else: self.log(repr(e), "warn") self.log(traceback.format_exc(), "debug") # If an error propagates to the topmost level, # we send an HTTP error response, which is both # understandable by HTTP clients and humans. try: error_response = models.make_error_response(502, repr(e)) self.client_conn.send(http1.assemble_response(error_response)) except netlib.exceptions.TcpException: pass except Exception: self.log(traceback.format_exc(), "error") print(traceback.format_exc(), file=sys.stderr) print("mitmproxy has crashed!", file=sys.stderr) print("Please lodge a bug report at: https://github.com/mitmproxy/mitmproxy", file=sys.stderr) self.log("clientdisconnect", "info") self.channel.tell("clientdisconnect", root_layer) self.client_conn.finish()
def send_error_response(self, code, message, headers=None): try: response = models.make_error_response(code, message, headers) self.send_response(response) except (netlib.exceptions.NetlibException, h2.exceptions.H2Error, exceptions.Http2ProtocolException): self.log(traceback.format_exc(), "debug")