From f9d2ccb2ad91fdf3e1a39f37de07c4224a4eca59 Mon Sep 17 00:00:00 2001 From: Timothy Miller Date: Tue, 15 Oct 2019 23:13:57 -0400 Subject: [PATCH] CNAME records - Recommended way to configure multiple subdomains per IP address is now via CNAME records. Notes added in README. - Updated config.json subdomain examples to reflect how base domain is appended in code. --- .gitignore | 125 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 3 ++ cloudflare-ddns.py | 6 ++- config.json | 4 +- 4 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c8128d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,125 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ \ No newline at end of file diff --git a/README.md b/README.md index 25a2dfd..3799099 100755 --- a/README.md +++ b/README.md @@ -20,6 +20,9 @@ Values explained: "proxied": false (defaults to false. Make it true if you want CDN/SSL benefits from cloudflare. This usually disables SSH) ``` +## :fax: Hosting multiple domains on the same IP? +You can save yourself some trouble when hosting multiple domains pointing to the same IP address (in the case of Traefik) by defining one A & AAAA record 'ddns.example.com' pointing to the IP of the server that will be updated by this DDNS script. For each subdomain, create a CNAME record pointing to 'ddns.example.com'. Now you don't have to manually modify the script config every time you add a new subdomain to your site! + ## :running: Running This script requires Python 3.5+, which comes preinstalled on the latest version of Raspbian. Download/clone this repo and execute `./sync`, which will set up a virtualenv, pull in any dependencies, and fire the script. diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index 2b16e2f..17fa404 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -47,7 +47,8 @@ def commitRecord(ip): "content": ip["ip"], "proxied": c["proxied"] } - list = cf_api("zones/" + c['zone_id'] + "/dns_records&per_page=100?type=" + ip["type"], "GET", c) + list = cf_api( + "zones/" + c['zone_id'] + "/dns_records&per_page=100?type=" + ip["type"], "GET", c) full_subdomain = subdomain + "." + base_domain_name dns_id = "" for r in list["result"]: @@ -71,7 +72,8 @@ def commitRecord(ip): # Delete duplicate, stale records for identifier in stale_record_ids: print("Deleting stale record " + str(identifier)) - response = cf_api("zones/" + c['zone_id'] + "/dns_records/" + identifier, "DELETE", c) + response = cf_api( + "zones/" + c['zone_id'] + "/dns_records/" + identifier, "DELETE", c) return True diff --git a/config.json b/config.json index d62a13a..e960599 100755 --- a/config.json +++ b/config.json @@ -5,8 +5,8 @@ "account_email": "your_email_here", "zone_id": "your_zone_id_here", "subdomains": [ - "one.example.com", - "two.example.com" + "", + "subdomain" ], "proxied": false }