Cloudflare DDNS and Caddy Configuration Manager: A Python script for automating DNS updates and Caddy reverse proxy setup. Simplifies domain management with Cloudflare API integration and dynamic Caddyfile updates.
.gitignore | ||
cf.py | ||
cf_domains.yaml-example | ||
README.md |
Cloudflare DNS and Caddy Configuration Script
1. Installation and Setup
You're right, using git clone is a better approach. Here's the revised installation section:
1. Installation and Setup
- Install Python 3.x and required libraries:
yaml
,requests
,loguru
,python-dotenv
:
pip install pyyaml requests loguru python-dotenv
-
Obtain Cloudflare API token from your account
-
Clone the repository and set up the script:
git clone https://sij.ai/sij/cf.git
cd cf
sudo ln -s "$(pwd)/cf.py" /usr/local/bin/cf
sudo chmod +x cf.py
- Create
.env
file withCLOUDFLARE_API_TOKEN=your_token_here
:
echo "CLOUDFLARE_API_TOKEN=your_token_here" > .env
- Set up
cf_domains.yaml
with your domains and zone IDs:
cp cf_domains.yaml-example cf_domains.yaml
nano cf_domains.yaml
Edit the cf_domains.yaml
file to include your domains, zone IDs, and DNS record IDs. The file structure should look like this:
domain1.com:
'@': your_root_domain1.com_dns_record_id_here
_id: your_domain1.com_zone_id_here
sub1: your_sub1.domain1.com_dns_record_id_here
sub2: your_sub2.domain1.com_dns_record_id_here
domain2.net:
'@': your_root_domain2.net_dns_record_id_here
_id: your_domain2.net_zone_id_here
sub1: sub1.domain2.net_dns_record_id_here
sub2: sub2.domain2.net_dns_record_id_here
Replace the example values with your actual Cloudflare zone IDs and DNS record IDs for each domain and subdomain you want to manage with this script.
- Ensure Caddy is installed and
/etc/caddy/Caddyfile
is writable:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
sudo chown caddy:caddy /etc/caddy/Caddyfile
sudo chmod 644 /etc/caddy/Caddyfile
2. Usage
2.1 DDNS Update
cf ddns [--force]
Updates all domains with current IP. Use --force
to update regardless of IP change.
2.2 Adding/Updating Domain Configuration
cf <full-domain> [--ip <ip address>] --port <port>
Adds or updates domain in Cloudflare and Caddyfile. Default IP is localhost.
2.3 Updating All Domains
cf all [--force]
Updates all domains and Caddyfile configurations.
3. File and Environment Structure
Caddyfile
: Caddy server configurationcf_domains.yaml
: Stores domain info and DNS record IDs.env
: ContainsCLOUDFLARE_API_TOKEN
andCURRENT_IP
cf_script.log
: Logs script actions
4. Key Functions and Error Handling
ddns()
: Handles DDNS updatesupdate_caddyfile()
: Modifies Caddy configurationupdate_or_create_record()
: Manages Cloudflare DNS records
Common errors:
- API authentication failures: Check API token
- Permission issues: Run with sudo for Caddyfile changes
- Domain not found: Ensure domain is in
cf_domains.yaml
Logs are in cf_script.log
for troubleshooting.