|
| 1 | +# Caddy Domain Checker - Lightweight HTTP Service for Caddy TLS Automation |
| 2 | + |
| 3 | +This is a lightweight HTTP service designed to integrate with [Caddy](https://caddyserver.com/) for TLS automation. It provides a simple endpoint to check if a given domain is allowed for certificate issuance. |
| 4 | + |
| 5 | +No external dependencies are required for this basic setup. The service reads a list of allowed domains from a file and dynamically updates the list without restarting the server. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Query endpoint for domain validation. |
| 10 | +- Dynamic domain management through a file (`domains.txt`). |
| 11 | +- Logs requests and system activity. |
| 12 | +- Lightweight and easy to configure. |
| 13 | + |
| 14 | +## How It Works |
| 15 | + |
| 16 | +1. **Domain Validation**: The service checks whether a domain is allowed by consulting a list of domains stored in `domains.txt`. |
| 17 | +2. **Endpoint**: The service exposes an HTTP GET endpoint `/check_domain` for domain validation. |
| 18 | +3. **Dynamic Updates**: The service monitors the `domains.txt` file for changes every minute. |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +### Prerequisites |
| 23 | +- Python 3.7 or higher |
| 24 | + |
| 25 | +### Steps |
| 26 | +1. Clone this repository. |
| 27 | +2. Navigate to the project directory. |
| 28 | +3. Install any required dependencies (none required for this basic setup). |
| 29 | +4. Create an empty `domains.txt` file in the project directory: |
| 30 | + ```bash |
| 31 | + touch domains.txt |
| 32 | + ``` |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +### Running the Server |
| 37 | +1. Start the server: |
| 38 | + ```bash |
| 39 | + python3 server.py |
| 40 | + ``` |
| 41 | +2. The server runs on `http://127.0.0.1:8008` by default. |
| 42 | + |
| 43 | +### Querying the Service |
| 44 | +- Use the `/check_domain` endpoint to validate a domain. |
| 45 | +- Example request: |
| 46 | + ```bash |
| 47 | + curl "http://127.0.0.1:8008/check_domain?domain=example.com" |
| 48 | + ``` |
| 49 | +- Response: |
| 50 | + - Allowed: |
| 51 | + ```json |
| 52 | + {"domain": "example.com", "allowed": true} |
| 53 | + ``` |
| 54 | + - Not Allowed: |
| 55 | + ```json |
| 56 | + {"domain": "example.com", "allowed": false} |
| 57 | + ``` |
| 58 | + |
| 59 | +### Managing Domains |
| 60 | +- Add or remove domains in the `domains.txt` file. |
| 61 | +- Changes will be reflected within a minute without restarting the server. |
| 62 | + |
| 63 | +## Configuration |
| 64 | + |
| 65 | +| Parameter | Default Value | Description | |
| 66 | +|-------------------|------------------|---------------------------------| |
| 67 | +| `DOMAINS_FILE` | `domains.txt` | Path to the domains file. | |
| 68 | +| `LOG_FILE` | `domain_checker.log` | Path to the log file (optional). | |
| 69 | +| Server Address | `127.0.0.1:8008` | IP and port for the HTTP server.| |
| 70 | + |
| 71 | +## Logging |
| 72 | + |
| 73 | +Logs system activity and domain queries to the console. You can configure it to log to a file by uncommenting the `FileHandler` line in the `logging` setup. |
| 74 | + |
| 75 | +## Systemd Service Setup |
| 76 | + |
| 77 | +To set up the service to run automatically using `systemd`, follow these steps: |
| 78 | + |
| 79 | +1. Create a new service file: |
| 80 | + ```bash |
| 81 | + sudo nano /etc/systemd/system/caddy-domain-checker.service |
| 82 | + ``` |
| 83 | + |
| 84 | +2. Add the following content to the file: |
| 85 | + ```ini |
| 86 | + [Unit] |
| 87 | + Description=Caddy Domain Checker Service |
| 88 | + After=network.target |
| 89 | + |
| 90 | + [Service] |
| 91 | + ExecStart=/usr/bin/python3 /mnt/data/www/domain_checker/app.py |
| 92 | + WorkingDirectory=/mnt/data/www/domain_checker |
| 93 | + Restart=always |
| 94 | + RestartSec=5 |
| 95 | + User=www-data |
| 96 | + Group=www-data |
| 97 | + StandardOutput=journal |
| 98 | + StandardError=journal |
| 99 | + |
| 100 | + [Install] |
| 101 | + WantedBy=multi-user.target |
| 102 | + ``` |
| 103 | + |
| 104 | +3. Save and close the file. |
| 105 | + |
| 106 | +4. Reload the `systemd` daemon to recognize the new service: |
| 107 | + ```bash |
| 108 | + sudo systemctl daemon-reload |
| 109 | + ``` |
| 110 | + |
| 111 | +5. Enable the service to start on boot: |
| 112 | + ```bash |
| 113 | + sudo systemctl enable caddy-domain-checker |
| 114 | + ``` |
| 115 | + |
| 116 | +6. Start the service: |
| 117 | + ```bash |
| 118 | + sudo systemctl start caddy-domain-checker |
| 119 | + ``` |
| 120 | + |
| 121 | +7. Check the service status to ensure it is running: |
| 122 | + ```bash |
| 123 | + sudo systemctl status caddy-domain-checker |
| 124 | + ``` |
| 125 | + |
| 126 | +## Development |
| 127 | + |
| 128 | +### Testing |
| 129 | +- Use `curl` or any HTTP client to interact with the service. |
| 130 | +- Ensure the `domains.txt` file is updated correctly for testing. |
| 131 | + |
| 132 | +### Stopping the Server |
| 133 | +- Stop the server gracefully with `Ctrl+C`. |
| 134 | + |
| 135 | +## License |
| 136 | +This project is licensed under the MIT License. See the LICENSE file for details. |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +**Note:** This service is intended to work seamlessly with Caddy's TLS automation and is not designed for general-purpose use without modifications. |
0 commit comments