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.
This commit is contained in:
commit
a568cd2669
5 changed files with 143 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
.venv/
|
||||||
|
venv/
|
||||||
|
config.ini
|
||||||
|
filter
|
5
config.dist.ini
Normal file
5
config.dist.ini
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
[database]
|
||||||
|
host = yourdb.local
|
||||||
|
user = onionator
|
||||||
|
password = s3cr3t
|
||||||
|
database = onionator
|
3
filter.dist
Normal file
3
filter.dist
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
one
|
||||||
|
two
|
||||||
|
three
|
55
onion_importer.py
Normal file
55
onion_importer.py
Normal file
|
@ -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()
|
76
onionator.py
Normal file
76
onionator.py
Normal file
|
@ -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()
|
Loading…
Reference in a new issue