From a568cd2669a456d6630729eda5bad95e1a49ff1c Mon Sep 17 00:00:00 2001 From: Kumi Date: Wed, 24 Jul 2024 19:40:48 +0200 Subject: [PATCH] feat: add onion key importer and service scripts Introduce 'onion_importer.py' for importing .onion keys from directories to a MariaDB database and deleting the sources. Add 'onionator.py' to interface with mkp224o and store generated keys in the database based on filtering criteria. Include sample configurations and update .gitignore to exclude virtual environments, config files, and filters. --- .gitignore | 4 +++ config.dist.ini | 5 ++++ filter.dist | 3 ++ onion_importer.py | 55 ++++++++++++++++++++++++++++++++++ onionator.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+) create mode 100644 .gitignore create mode 100644 config.dist.ini create mode 100644 filter.dist create mode 100644 onion_importer.py create mode 100644 onionator.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ce51dff --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.venv/ +venv/ +config.ini +filter \ No newline at end of file diff --git a/config.dist.ini b/config.dist.ini new file mode 100644 index 0000000..e2e237b --- /dev/null +++ b/config.dist.ini @@ -0,0 +1,5 @@ +[database] +host = yourdb.local +user = onionator +password = s3cr3t +database = onionator diff --git a/filter.dist b/filter.dist new file mode 100644 index 0000000..54d55bf --- /dev/null +++ b/filter.dist @@ -0,0 +1,3 @@ +one +two +three \ No newline at end of file diff --git a/onion_importer.py b/onion_importer.py new file mode 100644 index 0000000..4cc0bf9 --- /dev/null +++ b/onion_importer.py @@ -0,0 +1,55 @@ +import argparse +import mysql.connector +from pathlib import Path +import shutil +import configparser +import base64 + +# Set up the argument parser +parser = argparse.ArgumentParser() +parser.add_argument('base_dir', nargs='+', help='path to the base directory') +args = parser.parse_args() + +# Read the MariaDB server details from the configuration file +config = configparser.ConfigParser() +config.read('config.ini') + +# Connect to the database +conn = mysql.connector.connect( + host=config['database']['host'], + user=config['database']['user'], + password=config['database']['password'], + database=config['database']['database'] +) +cursor = conn.cursor() + +# Create the table if it doesn't already exist +cursor.execute(''' +CREATE TABLE IF NOT EXISTS onion_keys (hostname text, public_key text, secret_key text) +''') + +# Iterate over all base directories passed as arguments +for base_dir in args.base_dir: + base_dir = Path(base_dir) + # Iterate over all directories in the base directory + for dir_path in base_dir.iterdir(): + if dir_path.is_dir(): + # Read the contents of the hostname, public_key, and secret_key files + with open(dir_path / 'hostname') as f: + hostname = f.read().strip() + with open(dir_path / 'hs_ed25519_public_key', 'rb') as f: + public_key = base64.b64encode(f.read()) + with open(dir_path / 'hs_ed25519_secret_key', 'rb') as f: + secret_key = base64.b64encode(f.read()) + + # Insert the data into the database + cursor.execute(''' + INSERT INTO onion_keys (hostname, public_key, secret_key) VALUES (%s, %s, %s) + ''', (hostname, public_key, secret_key)) + conn.commit() + + # Delete the directory and its contents + shutil.rmtree(dir_path) + +# Close the connection to the database +conn.close() diff --git a/onionator.py b/onionator.py new file mode 100644 index 0000000..4b54a46 --- /dev/null +++ b/onionator.py @@ -0,0 +1,76 @@ +import argparse +import mysql.connector +import configparser +import subprocess + +def main(): + """ + Entry point of the script. + """ + # Set up the argument parser + parser = argparse.ArgumentParser() + parser.add_argument('-c', '--config', help='path to the configuration file', default="config.ini") + args = parser.parse_args() + + # Read the MariaDB server details from the configuration file + config = configparser.ConfigParser() + config.read(args.config) + + # Connect to the database + conn = mysql.connector.connect( + host=config['database']['host'], + user=config['database']['user'], + password=config['database']['password'], + database=config['database']['database'] + ) + cursor = conn.cursor() + + # Create the table if it doesn't already exist + cursor.execute(''' + CREATE TABLE IF NOT EXISTS onion_keys (hostname text, public_key text, secret_key text) + ''') + + # Start mkp224o + process = subprocess.Popen(['mkp224o', '-y', '-f', 'filter'], stdout=subprocess.PIPE) + + # Run mkp224o until the user interrupts the script + while True: + try: + # Read the output from mkp224o in blocks of four lines + hostname = None + public_key = None + secret_key = None + while True: + line = process.stdout.readline().decode().strip() + if line == '---': + break + + # Split the line into fields + fields = line.split(':', 1) + if len(fields) != 2: + continue + key, value = fields + key = key.strip() + value = value.strip() + + # Extract the hostname, public key, and secret key from the output + if key == 'hostname': + hostname = value + elif key == 'hs_ed25519_public_key': + public_key = value + elif key == 'hs_ed25519_secret_key': + secret_key = value + + if hostname: + # Insert the data into the database + cursor.execute(''' + INSERT INTO onion_keys (hostname, public_key, secret_key) VALUES (%s, %s, %s) + ''', (hostname, public_key, secret_key)) + + # Save the changes to the database + conn.commit() + except KeyboardInterrupt: + # If the user interrupts the script, break out of the loop + break + +main()