{{ status_code }}
+ +diff --git a/gui/app.py b/gui/app.py index e8b587f..7120e4e 100644 --- a/gui/app.py +++ b/gui/app.py @@ -21,6 +21,7 @@ sys.path.insert( from flask import ( Flask, + abort, Response, jsonify, redirect, @@ -49,9 +50,6 @@ from reports.hashcat import ( build_export_filename, import_hashcat_show, ) -from reports.hashcat_page import ( - render_hashcat_page, -) from reports.trips_page import render_trips_page from reports.wifitrip import ( export_wifitrip, @@ -67,6 +65,7 @@ from vendor_service import VendorService from sudo import SudoChecker from config import Config from gps_test import GPSTestService +from logger import setup_logger app = Flask( __name__, @@ -78,10 +77,119 @@ app = Flask( ) ) +logger = setup_logger() + app.register_blueprint( cracklab_bp ) +def wants_json_error(): + return ( + request.path.startswith(( + "/api/", + "/gps-test/", + "/config/", + "/scanner/", + "/reports/vendors/update", + "/reports/search/data" + )) + or request.is_json + or request.headers.get("X-Requested-With") == "XMLHttpRequest" + or request.accept_mimetypes.best == "application/json" + ) + + +def render_http_error(status_code, title, message): + if wants_json_error(): + return jsonify({ + "error": message + }), status_code + + return render_template( + "error.html", + status_code=status_code, + title=title, + message=message + ), status_code + + +@app.errorhandler(404) +def page_not_found(error): + logger.warning( + "HTTP 404 method=%s path=%s", + request.method, + request.path + ) + + return render_http_error( + 404, + "Страница не найдена", + "Запрошенная страница не существует или была перемещена." + ) + + +@app.errorhandler(403) +def access_forbidden(error): + logger.warning( + "HTTP 403 method=%s path=%s", + request.method, + request.path + ) + + return render_http_error( + 403, + "Доступ запрещён", + "У вас нет разрешения на выполнение этого действия." + ) + + +@app.errorhandler(405) +def method_not_allowed(error): + logger.warning( + "HTTP 405 method=%s path=%s", + request.method, + request.path + ) + + return render_http_error( + 405, + "Действие недоступно", + "Этот способ обращения к странице не поддерживается." + ) + + +@app.errorhandler(500) +def internal_server_error(error): + original_exception = getattr( + error, + "original_exception", + None + ) + + if original_exception is not None: + logger.error( + "Unhandled exception method=%s path=%s", + request.method, + request.path, + exc_info=( + type(original_exception), + original_exception, + original_exception.__traceback__ + ) + ) + else: + logger.error( + "HTTP 500 method=%s path=%s", + request.method, + request.path + ) + + return render_http_error( + 500, + "Что-то пошло не так", + "Во время обработки запроса произошла ошибка. Попробуйте позже." + ) + @app.before_request def scanner_navigation_guard(): @@ -90,6 +198,9 @@ def scanner_navigation_guard(): if not status["running"]: return None + if request.endpoint is None: + return None + if ( request.endpoint is not None and request.endpoint.startswith("cracklab.") @@ -1680,249 +1791,13 @@ def reports_access_point(bssid): bssid ) -@app.route("/reports/hashcat/") -def reports_hashcat(): +@app.route("/reports/hashcat/", methods=["GET", "POST"]) +@app.route("/reports/hashcat/export", methods=["GET", "POST"]) +@app.route("/reports/hashcat/preflight", methods=["GET", "POST"]) +@app.route("/reports/hashcat/import", methods=["GET", "POST"]) +def reports_hashcat_gone(): - return render_report( - render_hashcat_page - ) - - -@app.route("/reports/hashcat/export") -def reports_hashcat_export(): - - vendor = request.args.get( - "vendor", - "" - ).strip() - - conn = connect_database() - - try: - - if vendor: - - criteria = normalize_criteria( - vendor=vendor - ) - - if not criteria: - - return jsonify( - { - "error": - "At least one search criterion " - "is required." - } - ), 400 - - content = build_hash_export( - conn, - criteria - ) - - filename = build_export_filename( - "search", - vendor - ) - - else: - - content = build_all_hash_export( - conn - ) - - filename = build_export_filename( - "all" - ) - - return Response( - content, - mimetype="text/plain", - headers={ - "Content-Disposition": - f'attachment; filename="{filename}"' - } - ) - - except Exception as error: - - return jsonify( - { - "error": str(error) - } - ), 500 - - finally: - - close_database(conn) - -@app.route( - "/reports/hashcat/preflight", - methods=["POST"] -) -def reports_hashcat_preflight(): - - uploaded_file = request.files.get( - "hashcat_file" - ) - - if uploaded_file is None: - return jsonify( - { - "valid": False, - "error": - "Hashcat result file is required." - } - ), 400 - - try: - content = uploaded_file.read().decode( - "utf-8" - ) - except UnicodeDecodeError: - return jsonify( - { - "valid": False, - "error": - "Hashcat result file must be " - "valid UTF-8 text." - } - ), 400 - - from reports.hashcat import ( - validate_hashcat_show_file, - ) - - conn = connect_database() - - try: - - result = validate_hashcat_show_file( - conn, - content.splitlines( - keepends=True - ) - ) - - result["valid"] = ( - result["total_lines"] > 0 - and result["matched_handshakes"] > 0 - and result["invalid_lines"] == 0 - ) - - return jsonify( - result - ) - - except Exception as error: - - return jsonify( - { - "valid": False, - "error": str(error) - } - ), 500 - - finally: - - close_database(conn) - -@app.route( - "/reports/hashcat/import", - methods=["POST"] -) -def reports_hashcat_import(): - - uploaded_file = request.files.get( - "hashcat_file" - ) - - if uploaded_file is None: - return jsonify( - { - "error": - "Hashcat result file is required." - } - ), 400 - - try: - content = uploaded_file.read().decode( - "utf-8" - ) - except UnicodeDecodeError: - return jsonify( - { - "error": - "Hashcat result file must be " - "valid UTF-8 text." - } - ), 400 - - conn = connect_database() - - try: - - result = import_hashcat_show( - conn, - content.splitlines( - keepends=True - ) - ) - - if result["errors"]: - - html = render_hashcat_page( - conn, - context=ReportContext( - mode="server" - ), - import_result=result, - message={ - "type": "error", - "text": ( - "Import was rejected. " - "No credentials were committed." - ), - } - ) - - return Response( - html, - mimetype="text/html", - status=400 - ) - - html = render_hashcat_page( - conn, - context=ReportContext( - mode="server" - ), - import_result=result, - message={ - "type": "success", - "text": ( - "Import completed successfully." - ), - } - ) - - return Response( - html, - mimetype="text/html" - ) - - except Exception as error: - - return jsonify( - { - "error": str(error) - } - ), 500 - - finally: - - close_database(conn) + abort(404) @app.route("/reports/search/") def reports_search(): diff --git a/gui/templates/error.html b/gui/templates/error.html new file mode 100644 index 0000000..9222f78 --- /dev/null +++ b/gui/templates/error.html @@ -0,0 +1,81 @@ + + +
+ + + +{{ status_code }}
+ +