113 lines
2.6 KiB
Python
113 lines
2.6 KiB
Python
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
from typing import Any
|
|
|
|
|
|
class SearchContextsRepository:
|
|
def __init__(self, conn: sqlite3.Connection) -> None:
|
|
self._conn = conn
|
|
|
|
def create_context(
|
|
self,
|
|
token_hash: str,
|
|
criteria_json: str,
|
|
matched_access_point_count: int,
|
|
handshake_count: int,
|
|
created_at: str,
|
|
expires_at: str,
|
|
) -> None:
|
|
self._conn.execute(
|
|
"""
|
|
INSERT INTO crack_search_contexts (
|
|
token_hash,
|
|
criteria_json,
|
|
matched_access_point_count,
|
|
handshake_count,
|
|
created_at,
|
|
expires_at
|
|
)
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
|
""",
|
|
(
|
|
token_hash,
|
|
criteria_json,
|
|
matched_access_point_count,
|
|
handshake_count,
|
|
created_at,
|
|
expires_at,
|
|
),
|
|
)
|
|
|
|
def add_handshakes(
|
|
self,
|
|
token_hash: str,
|
|
handshake_ids: list[int],
|
|
) -> None:
|
|
self._conn.executemany(
|
|
"""
|
|
INSERT INTO crack_search_context_handshakes (
|
|
token_hash,
|
|
handshake_id
|
|
)
|
|
VALUES (?, ?)
|
|
""",
|
|
[
|
|
(token_hash, handshake_id)
|
|
for handshake_id in handshake_ids
|
|
],
|
|
)
|
|
|
|
def get_context(
|
|
self,
|
|
token_hash: str,
|
|
) -> sqlite3.Row | None:
|
|
return self._conn.execute(
|
|
"""
|
|
SELECT
|
|
id,
|
|
token_hash,
|
|
criteria_json,
|
|
matched_access_point_count,
|
|
handshake_count,
|
|
created_at,
|
|
expires_at
|
|
FROM crack_search_contexts
|
|
WHERE token_hash = ?
|
|
""",
|
|
(token_hash,),
|
|
).fetchone()
|
|
|
|
def list_handshake_ids(
|
|
self,
|
|
token_hash: str,
|
|
) -> list[int]:
|
|
rows = self._conn.execute(
|
|
"""
|
|
SELECT handshake_id
|
|
FROM crack_search_context_handshakes
|
|
WHERE token_hash = ?
|
|
ORDER BY handshake_id
|
|
""",
|
|
(token_hash,),
|
|
).fetchall()
|
|
|
|
return [
|
|
int(row["handshake_id"])
|
|
for row in rows
|
|
]
|
|
|
|
def delete_expired(
|
|
self,
|
|
now: str,
|
|
) -> int:
|
|
cursor = self._conn.execute(
|
|
"""
|
|
DELETE FROM crack_search_contexts
|
|
WHERE expires_at <= ?
|
|
""",
|
|
(now,),
|
|
)
|
|
|
|
return cursor.rowcount
|