Skip to content

Instantly share code, notes, and snippets.

@jvanasco
Last active June 27, 2024 21:24
Show Gist options
  • Save jvanasco/253a8cd0c527cc2e4a6112602d46556f to your computer and use it in GitHub Desktop.
Save jvanasco/253a8cd0c527cc2e4a6112602d46556f to your computer and use it in GitHub Desktop.
replace acme-dns random domains with custom domains.
"""
This is deprecated.
It is now maintained as part of peter_sslers:
* https://github.com/aptise/peter_sslers/blob/main/tools/replace_domain.py
"""
from __future__ import print_function
import os
import sqlite3
import sys
import re
_args = sys.argv
try:
if len(_args) != 3:
raise ValueError("wrong number of args")
(_subdomain_old, _subdomain_new) = _args[1:3]
if not all((_subdomain_old, _subdomain_new)):
raise ValueError("Missing old or new subdomain")
# validate the domain inputs
_regex_subdomain = re.compile("^[A-Za-z0-9](?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9])?$")
if not _regex_subdomain.match(_subdomain_old):
raise ValueError("Invalid domain: old")
if not _regex_subdomain.match(_subdomain_new):
raise ValueError("Invalid domain: new")
except Exception as exc:
print("Please invoke this as `replace_domain.py {OLD_DOMAIN} {NEW_DOMAIN}`")
raise
_database_path = os.environ.get("ACMEDNS_DB", "acme-dns.db")
print("Using acme-dns database at: %s" % _database_path)
if not os.path.exists(_database_path):
raise ValueError(
"XXX Invalid Database Path. Please override with `ACMEDNS_DB=` environment variable"
)
with sqlite3.connect(_database_path) as connection:
cursor = connection.cursor()
cursor.execute("SELECT * FROM records WHERE subdomain=?", (_subdomain_old,))
row = cursor.fetchone()
if row is None:
raise ValueError("Old Subdomain not found in acme-dns")
print("updating the database...")
cursor.execute(
"UPDATE records SET subdomain=? WHERE subdomain=?",
(_subdomain_new, _subdomain_old),
)
cursor.execute(
"UPDATE txt SET subdomain=? WHERE subdomain=?", (_subdomain_new, _subdomain_old)
)
connection.commit()
print("done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment