From f5bb815a0e78618155579b95bba11ea24309bcca Mon Sep 17 00:00:00 2001 From: WildSoft Date: Fri, 9 Oct 2026 04:57:25 +0300 Subject: [PATCH] Update public source --- cracklab/server/repositories/handshakes.py | 80 +++++++ cracklab/server/services/job_issuance.py | 28 +++ cracklab/server/web/routes.py | 242 ++++++++++++++++++++- cracklab/server/web/templates/jobs.html | 200 +++++++++++++++++ 4 files changed, 544 insertions(+), 6 deletions(-) create mode 100644 cracklab/server/web/templates/jobs.html diff --git a/cracklab/server/repositories/handshakes.py b/cracklab/server/repositories/handshakes.py index ae9c7c4..ddff7df 100644 --- a/cracklab/server/repositories/handshakes.py +++ b/cracklab/server/repositories/handshakes.py @@ -56,3 +56,83 @@ class HandshakesRepository: limit, ), ).fetchall() + + def get_method_statistics( + self, + method_id: str, + method_version: int, + ) -> sqlite3.Row: + return self._conn.execute( + """ + SELECT + COUNT(*) AS total_count, + SUM( + CASE + WHEN EXISTS ( + SELECT 1 + FROM credentials AS c + WHERE c.handshake_id = h.id + ) + THEN 1 + ELSE 0 + END + ) AS found_count, + SUM( + CASE + WHEN NOT EXISTS ( + SELECT 1 + FROM credentials AS c + WHERE c.handshake_id = h.id + ) + AND EXISTS ( + SELECT 1 + FROM crack_attempts AS a + WHERE a.handshake_id = h.id + AND a.method_id = ? + AND a.method_version = ? + AND a.status = 'COMPLETED' + ) + THEN 1 + ELSE 0 + END + ) AS covered_count, + SUM( + CASE + WHEN NOT EXISTS ( + SELECT 1 + FROM credentials AS c + WHERE c.handshake_id = h.id + ) + AND NOT EXISTS ( + SELECT 1 + FROM crack_attempts AS a + WHERE a.handshake_id = h.id + AND a.method_id = ? + AND a.method_version = ? + AND a.status = 'COMPLETED' + ) + AND EXISTS ( + SELECT 1 + FROM crack_reservations AS r + WHERE r.handshake_id = h.id + AND r.method_id = ? + AND r.method_version = ? + AND r.status = 'ACTIVE' + ) + THEN 1 + ELSE 0 + END + ) AS reserved_count + FROM handshakes AS h + WHERE h.hash22000 IS NOT NULL + AND TRIM(h.hash22000) <> '' + """, + ( + method_id, + method_version, + method_id, + method_version, + method_id, + method_version, + ), + ).fetchone() diff --git a/cracklab/server/services/job_issuance.py b/cracklab/server/services/job_issuance.py index 4cf9331..fb1354e 100644 --- a/cracklab/server/services/job_issuance.py +++ b/cracklab/server/services/job_issuance.py @@ -48,6 +48,34 @@ class JobIssuanceService: self._reservations = ReservationsRepository(conn) self._audit = AuditRepository(conn) + def get_method_statistics( + self, + method_id: str, + method_version: int, + ) -> dict[str, int]: + row = self._handshakes.get_method_statistics( + method_id, + method_version, + ) + + total_count = int(row["total_count"] or 0) + found_count = int(row["found_count"] or 0) + covered_count = int(row["covered_count"] or 0) + reserved_count = int(row["reserved_count"] or 0) + + return { + "total_count": total_count, + "found_count": found_count, + "covered_count": covered_count, + "reserved_count": reserved_count, + "available_count": ( + total_count + - found_count + - covered_count + - reserved_count + ), + } + def issue_job( self, method_id: str, diff --git a/cracklab/server/web/routes.py b/cracklab/server/web/routes.py index ad35c0e..6d74a5b 100644 --- a/cracklab/server/web/routes.py +++ b/cracklab/server/web/routes.py @@ -1,9 +1,13 @@ from __future__ import annotations +import shutil +import tempfile +from pathlib import Path + import json from collections import Counter -from flask import redirect, render_template, request, url_for +from flask import after_this_request, redirect, render_template, request, send_file, url_for from cracklab.server.db.connection import connect_database from cracklab.server.repositories.clients import ClientsRepository @@ -12,6 +16,12 @@ from cracklab.server.repositories.methods import MethodsRepository from cracklab.server.repositories.results import ResultsRepository from cracklab.server.services.methods import MethodService, MethodServiceError +from cracklab.server.services.job_export import JobExportService +from cracklab.server.services.job_issuance import ( + JobIssuanceError, + JobIssuanceService, +) + from cracklab.server.web import cracklab_bp @@ -67,14 +77,234 @@ def dashboard(): ) -@cracklab_bp.route("/jobs/") +@cracklab_bp.route("/jobs/", methods=["GET", "POST"]) def jobs_page(): - return _placeholder( - "Jobs", - "Job management will be implemented here.", - "jobs", + error = None + created_job_id = request.args.get("created") + + conn = connect_database() + + try: + method_service = MethodService(conn) + issuance_service = JobIssuanceService(conn) + jobs_repository = JobsRepository(conn) + + methods = method_service.list_methods() + + method_versions = {} + + for method in methods: + method_versions[method["id"]] = ( + method_service.list_method_versions( + method["id"], + ) + ) + + selected_method_id = request.values.get( + "method_id", + "", + ).strip() + + selected_version_raw = request.values.get( + "method_version", + "", + ).strip() + + if not selected_method_id and methods: + selected_method_id = methods[0]["id"] + + selected_versions = method_versions.get( + selected_method_id, + [], + ) + + if not selected_version_raw and selected_versions: + selected_version_raw = str( + selected_versions[-1]["version"] + ) + + selected_version = None + + if selected_version_raw: + try: + selected_version = int(selected_version_raw) + except ValueError: + selected_version = None + + if request.method == "POST": + if not selected_method_id: + raise JobIssuanceError( + "Выберите метод." + ) + + if selected_version is None: + raise JobIssuanceError( + "Выберите версию метода." + ) + + limit_raw = request.form.get( + "limit", + "100", + ).strip() + + try: + limit = int(limit_raw) + except ValueError as exc: + raise JobIssuanceError( + "Количество handshakes должно быть целым числом." + ) from exc + + issued_job = issuance_service.issue_job( + method_id=selected_method_id, + method_version=selected_version, + limit=limit, + ) + + if issued_job is None: + raise JobIssuanceError( + "Нет handshakes, доступных для нового job." + ) + + return redirect( + url_for( + "cracklab.jobs_page", + method_id=selected_method_id, + method_version=selected_version, + created=issued_job.job_id, + ) + ) + + statistics = None + + if ( + selected_method_id + and selected_version is not None + ): + statistics = issuance_service.get_method_statistics( + selected_method_id, + selected_version, + ) + + active_jobs = jobs_repository.list_jobs() + + active_jobs = [ + job + for job in active_jobs + if job["status"] in { + "CREATED", + "QUEUED", + "RUNNING", + "PARTIAL", + } + ] + + method_names = { + method["id"]: method["name"] + for method in methods + } + + return render_template( + "jobs.html", + active_page="jobs", + methods=methods, + method_versions=method_versions, + selected_method_id=selected_method_id, + selected_version=selected_version, + statistics=statistics, + active_jobs=active_jobs, + method_names=method_names, + created_job_id=created_job_id, + error=error, + ) + + except (JobIssuanceError, ValueError) as exc: + error = str(exc) + + statistics = None + + if ( + selected_method_id + and selected_version is not None + ): + statistics = issuance_service.get_method_statistics( + selected_method_id, + selected_version, + ) + + active_jobs = jobs_repository.list_jobs() + + active_jobs = [ + job + for job in active_jobs + if job["status"] in { + "CREATED", + "QUEUED", + "RUNNING", + "PARTIAL", + } + ] + + method_names = { + method["id"]: method["name"] + for method in methods + } + + return render_template( + "jobs.html", + active_page="jobs", + methods=methods, + method_versions=method_versions, + selected_method_id=selected_method_id, + selected_version=selected_version, + statistics=statistics, + active_jobs=active_jobs, + method_names=method_names, + created_job_id=created_job_id, + error=error, + ) + + finally: + conn.close() + +@cracklab_bp.route("/jobs//download/") +def job_download(job_id: str): + conn = connect_database() + temporary_dir = Path( + tempfile.mkdtemp( + prefix="cracklab-job-", + ) ) + try: + export_service = JobExportService(conn) + output_path = export_service.export_job( + job_id, + temporary_dir, + ) + except Exception: + shutil.rmtree( + temporary_dir, + ignore_errors=True, + ) + conn.close() + raise + + conn.close() + + @after_this_request + def cleanup(response): + shutil.rmtree( + temporary_dir, + ignore_errors=True, + ) + return response + + return send_file( + output_path, + as_attachment=True, + download_name=output_path.name, + mimetype="application/zip", + ) @cracklab_bp.route("/methods/") def methods_page(): diff --git a/cracklab/server/web/templates/jobs.html b/cracklab/server/web/templates/jobs.html new file mode 100644 index 0000000..85563c2 --- /dev/null +++ b/cracklab/server/web/templates/jobs.html @@ -0,0 +1,200 @@ +{% extends "base.html" %} + +{% block title %}CrackLab — Jobs{% endblock %} + +{% block content %} +
+ +
+
+

Jobs

+

+ Формирование заданий для CrackLab-клиентов. +

+
+
+ + {% if error %} +
+ {{ error }} +
+ {% endif %} + + {% if created_job_id %} +
+ Job {{ created_job_id }} сформирован. +
+ {% endif %} + +
+

New Job

+ +
+ +
+ + + + + +
+ + {% if statistics %} +
+
+
+ {{ statistics["total_count"] }} +
+
+ Handshakes +
+
+ +
+
+ {{ statistics["found_count"] }} +
+
+ Password Found +
+
+ +
+
+ {{ statistics["covered_count"] }} +
+
+ Covered +
+
+ +
+
+ {{ statistics["reserved_count"] }} +
+
+ Reserved +
+
+ +
+
+ {{ statistics["available_count"] }} +
+
+ Available +
+
+
+ {% endif %} + +
+ + + + + +
+ +
+
+ +
+

Issued Jobs

+ + {% if active_jobs %} +
+ + + + + + + + + + + + + {% for job in active_jobs %} + + + + + + + + + {% endfor %} + +
JobMethodHashesCreatedStatus
+ {{ job["id"] }} + + {{ method_names.get(job["method_id"], job["method_id"]) }} + v{{ job["method_version"] }} + {{ job["hash_count"] }}{{ job["created_at"] }} + + {{ job["status"] }} + + + + Download ZIP + +
+
+ {% else %} +
+ No active jobs. +
+ {% endif %} +
+ +
+ + +{% endblock %}