Skip to content

Commit

Permalink
Hashes
Browse files Browse the repository at this point in the history
removed redundant hash checking in task runner

added remove key route, searches database for matching hash to remove
  • Loading branch information
LucifersCircle committed Dec 7, 2024
1 parent 60986c1 commit 7eb3647
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
46 changes: 42 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import sqlite3
import hashlib

from flask import Flask, request, jsonify, render_template_string
from cryptography.fernet import Fernet

Expand Down Expand Up @@ -52,7 +53,7 @@ def initialize_db():
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Key</title>
<title>Key Management</title>
<style>
body {
font-family: Arial, sans-serif;
Expand Down Expand Up @@ -89,20 +90,57 @@ def initialize_db():
button:hover {
background-color: #3f4c8c;
}
form {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>Add a Key</h1>
<h1>Key Management</h1>
<form action="/add_key" method="post">
<input type="text" name="key" placeholder="Enter your key" required>
<button type="submit">Submit</button>
<input type="text" name="key" placeholder="Enter your key to add" required>
<button type="submit">Add Key</button>
</form>
<form action="/remove_key" method="post">
<input type="text" name="key" placeholder="Enter your key to remove" required>
<button type="submit">Remove Key</button>
</form>
</div>
</body>
</html>
"""

# New route to handle key removal
@app.route('/remove_key', methods=['POST'])
def remove_key():
key = request.form.get('key')
if not key:
return jsonify({'error': 'Key is required'}), 400

# Validate the key using a regular expression
if not re.fullmatch(r'^[a-fA-F0-9]{64}$', key):
return jsonify({'error': 'Invalid key format. Only 64-character alphanumeric keys are allowed.'}), 400

try:
key_hash = hashlib.sha256(key.encode()).hexdigest()
conn = sqlite3.connect(DB_FILE)

# Check if the key exists
cursor = conn.execute("SELECT COUNT(*) FROM keys WHERE key_hash = ?", (key_hash,))
if cursor.fetchone()[0] == 0:
conn.close()
return jsonify({'error': 'Key not found'}), 404

# Remove the key from the database
conn.execute("DELETE FROM keys WHERE key_hash = ?", (key_hash,))
conn.commit()
conn.close()
return jsonify({'message': 'Key removed successfully'}), 200
except Exception as e:
print(f"Error removing key: {e}")
return jsonify({'error': str(e)}), 500

# Landing page route
@app.route('/')
def index():
Expand Down
32 changes: 1 addition & 31 deletions task_runner.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import time
import sqlite3

import requests
from cryptography.fernet import Fernet

Expand All @@ -19,31 +20,6 @@ def mask_token(token):
"""Masks the token, showing only the last 4 characters."""
return f"***{token[-4:]}"

def remove_duplicates():
"""Remove duplicate keys from the database."""
try:
conn = sqlite3.connect(db_file)
cursor = conn.cursor()

# Remove duplicates based on key_hash
cursor.execute("""
DELETE FROM keys
WHERE rowid NOT IN (
SELECT MIN(rowid)
FROM keys
GROUP BY key_hash
)
""")

removed_count = conn.total_changes # Number of rows deleted
conn.commit()
conn.close()
print(f"Removed {removed_count} duplicate keys from the database.", flush=True)
except Exception as e:
print(f"Error removing duplicates: {e}", flush=True)



def fetch_keys():
"""Fetch and decrypt all keys from the database."""
try:
Expand Down Expand Up @@ -71,16 +47,10 @@ def send_request(token):
if __name__ == "__main__":
print("Starting task_runner...", flush=True)
while True:
# Remove duplicates before processing
remove_duplicates()

# Fetch keys and process them
keys = fetch_keys()
if not keys:
print("No keys found in the database.", flush=True)
for key in keys:
send_request(key)

# Wait before the next iteration
print(f"Sleeping for {interval} seconds.", flush=True)
time.sleep(interval)

0 comments on commit 7eb3647

Please sign in to comment.