cf/README.md

120 lines
3.7 KiB
Markdown
Raw Normal View History

2024-10-07 07:48:20 +02:00
# Cloudflare DNS and Caddy Configuration Script
2024-10-07 07:31:37 +02:00
2024-10-07 07:48:20 +02:00
## 1. Installation and Setup
2024-10-07 08:09:59 +02:00
- Ensure you have Anaconda or Miniconda installed on your system.
2024-10-07 07:48:20 +02:00
2024-10-07 08:09:59 +02:00
- Clone the repository:
2024-10-07 07:48:20 +02:00
```bash
2024-10-07 08:09:59 +02:00
git clone https://sij.ai/sij/cf.git
cd cf
2024-10-07 07:48:20 +02:00
```
2024-10-07 08:09:59 +02:00
- Run the setup script to create the conda environment and install dependencies:
2024-10-07 07:48:20 +02:00
```bash
2024-10-07 08:09:59 +02:00
chmod +x setup.sh
./setup.sh
2024-10-07 07:48:20 +02:00
```
2024-10-07 08:09:59 +02:00
- Create `.env` file with your Cloudflare API token:
2024-10-07 07:48:20 +02:00
```bash
echo "CLOUDFLARE_API_TOKEN=your_token_here" > .env
```
- Set up `cf_domains.yaml` with your domains and zone IDs:
```bash
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:
```yaml
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.
2024-10-07 08:09:59 +02:00
- (Optional) Create a symlink to use the script from anywhere:
```bash
sudo ln -sf "$(pwd)/cf" /usr/local/bin/cf
```
- Ensure Caddy is installed and `/etc/caddy/Caddyfile` is writable (Ubuntu/Debian example):
2024-10-07 07:48:20 +02:00
```bash
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
```
2024-10-07 08:09:59 +02:00
./cf ddns [--force]
2024-10-07 07:48:20 +02:00
```
Updates all domains with current IP. Use `--force` to update regardless of IP change.
### 2.2 Adding/Updating Domain Configuration
```
2024-10-07 08:09:59 +02:00
./cf <full-domain> [--ip <ip address>] --port <port>
2024-10-07 07:48:20 +02:00
```
Adds or updates domain in Cloudflare and Caddyfile. Default IP is localhost.
### 2.3 Updating All Domains
```
2024-10-07 08:09:59 +02:00
./cf all [--force]
2024-10-07 07:48:20 +02:00
```
Updates all domains and Caddyfile configurations.
2024-10-07 08:09:59 +02:00
Note: If you created the symlink, you can use `cf` instead of `./cf` from any directory.
2024-10-07 07:48:20 +02:00
## 3. File and Environment Structure
2024-10-07 08:28:51 +02:00
- `cf`: Wrapper script to activate conda environment and run the Python script
- `cf.py`: Main Python script
- `setup.sh`: Shell script to set up your Conda environment
- `environment.yaml`: Conda environment specification
2024-10-07 07:48:20 +02:00
- `cf_domains.yaml`: Stores domain info and DNS record IDs
- `.env`: Contains `CLOUDFLARE_API_TOKEN` and `CURRENT_IP`
- `cf_script.log`: Logs script actions
## 4. Key Functions and Error Handling
- `ddns()`: Handles DDNS updates
- `update_caddyfile()`: Modifies Caddy configuration
- `update_or_create_record()`: Manages Cloudflare DNS records
Common errors:
2024-10-07 08:28:51 +02:00
- API authentication failures: Check Cloudflare API token in `.env` file
2024-10-07 07:48:20 +02:00
- Permission issues: Run with sudo for Caddyfile changes
- Domain not found: Ensure domain is in `cf_domains.yaml`
2024-10-07 08:28:51 +02:00
- Caddy failing to obtain HTTPS certificates: Ensure the Caddy service environment contains your CLOUDFLARE_API_TOKEN, and your Caddyfile contains your email address; see Caddy documentation if you have further trouble
2024-10-07 07:48:20 +02:00
2024-10-07 08:09:59 +02:00
Logs are in `cf_script.log` for troubleshooting.
## 5. Updating
To update the script and its dependencies:
1. Pull the latest changes:
```bash
git pull
```
2. Update the conda environment:
```bash
2024-10-07 08:11:26 +02:00
conda env update -f environment.yaml -p ./cf-env
2024-10-07 08:09:59 +02:00
```