diff --git a/Dockerfile b/Dockerfile index d84bb26..05db38b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,10 @@ FROM alpine:latest -RUN apk add --no-cache curl bash busybox-cron +RUN apk add --no-cache curl bash busybox tzdata COPY update-blocklist.sh /usr/local/bin/update-blocklist.sh COPY entrypoint.sh /entrypoint.sh RUN chmod +x /usr/local/bin/update-blocklist.sh /entrypoint.sh -ENTRYPOINT ["/entrypoint.sh"] +ENTRYPOINT ["/entrypoint.sh"] \ No newline at end of file diff --git a/README.md b/README.md index bccdba2..a8278a4 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,21 @@
-🤖 **Adguard CIDRE Sync** - A bot to synchronize adguard clients disallow list with countries CIDR list of your choices. +**Adguard CIDRE Sync** - A bot to synchronize adguard clients disallow list with countries CIDR list of your choices. *The code is partially generated by AI* -## 📌 Sommaire +## Sommaire - [Features](#features) -- [Install with Docker and our image](#install-with-docker) -- [Install with git and build (développeur)](#install-with-git-and-build) +- [Environment Variables](#environment-variables) +- [File Structure](#file-structure) +- [Installation and Usage](#nstallation-and-usage) -## ✨ Features +## Features - Automatically downloads IP CIDR blocks for specified countries to block. - Supports additional manually blocked IPs from a configurable file. @@ -26,9 +28,12 @@ | Variable | Description | Default | | ------------------- | ---------------------------------------------------------- | --------------------------------- | +| `TZ` | Your Time Zone | (required) | | `BLOCK_COUNTRIES` | Comma-separated country codes to block (e.g., `CN,RU,IR`) | (required) | | `BLOCKLIST_CRON` | Cron expression for update frequency (e.g., `0 6 * * *`) | `0 6 * * *` (at 6:00 everydays) | -| `DOCKER_API_URL` | URL of Docker socket proxy to restart AdGuard container | `http://docker-socket-proxy:2375` | +| `DOCKER_API_URL` | URL of Docker socket proxy to restart AdGuard container | `http://socket-proxy-adguard:2375` | +| `ADGUARD_CONTAINER_NAME` | Name of your adguard container | `adguardhome` | + ## File Structure @@ -43,8 +48,8 @@ 1. **Clone the repository:** ```bash - git clone https://github.com/your-username/adguard-blocklist-updater.git - cd adguard-blocklist-updater + git clone https://git.djeex.fr/Djeex/adguard-cidre + cd adguard-cidre ``` 2. **Modify docker-compose.yml** diff --git a/docker-compose.yml b/docker-compose.yml index 9eeab34..f56559c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,10 +2,13 @@ services: adguard-cidre: build: . + container_name: adguard-cidre environment: - - BLOCK_COUNTRIES=CN,RU,IR # choose countries listed IP to block. Full lists here https://github.com/vulnebify/cidre/tree/main/output/cidr/ipv4 + - TZ=Europe/Paris # change to your timezone + - BLOCK_COUNTRIES=cn,ru,ir # choose countries listed IP to block. Full lists here https://github.com/vulnebify/cidre/tree/main/output/cidr/ipv4 - BLOCKLIST_CRON=0 6 * * * # at 6:00 every days - - DOCKER_API_URL=http://socket-proxy-adguard:2375 + - DOCKER_API_URL=http://socket-proxy-adguard:2375 # docker socket proxy + - ADGUARD_CONTAINER_NAME=adguardhome # adguard container name volumes: - /path/to/adguard/confdir:/adguard @@ -16,6 +19,7 @@ services: - no-new-privileges:true environment: - CONTAINERS=1 + - ALLOW_RESTARTS=1 volumes: - /var/run/docker.sock:/var/run/docker.sock:ro restart: unless-stopped diff --git a/entrypoint.sh b/entrypoint.sh index 249a0ce..a4a93fa 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,13 +1,14 @@ #!/bin/sh - set -e -CRON_EXPR="${BLOCKLIST_CRON:-"0 6 * * *"}" # default: every hour -SCRIPT_PATH="/usr/local/bin/update-blocklist.sh" +if [ -n "$TZ" ]; then + if [ -f "/usr/share/zoneinfo/$TZ" ]; then + cp "/usr/share/zoneinfo/$TZ" /etc/localtime + echo "$TZ" > /etc/timezone + fi +fi -echo "Installing cron job with expression: $CRON_EXPR" +CRON_EXPR="${BLOCKLIST_CRON:-"0 6 * * *"}" +echo "$CRON_EXPR /usr/local/bin/update-blocklist.sh" > /etc/crontabs/root -echo "$CRON_EXPR root $SCRIPT_PATH" > /etc/crontabs/root - -echo "Starting cron..." -crond -f -L /dev/stdout +exec crond -f -c /etc/crontabs \ No newline at end of file diff --git a/update-blocklist.sh b/update-blocklist.sh index 33a4ff8..cdddeb1 100644 --- a/update-blocklist.sh +++ b/update-blocklist.sh @@ -7,7 +7,8 @@ TMP_YAML="/tmp/AdGuardHome.yaml" MANUAL_IPS_FILE="/adguard/manually_blocked_ips.conf" CIDR_BASE_URL="https://raw.githubusercontent.com/vulnebify/cidre/main/output/cidr/ipv4" COUNTRIES=${BLOCK_COUNTRIES:-""} -DOCKER_API_URL=${DOCKER_API_URL:-"http://docker-socket-proxy:2375"} +DOCKER_API_URL=${DOCKER_API_URL:-"http://socket-proxy-adguard:2375"} +CONTAINER_NAME=${ADGUARD_CONTAINER_NAME:-"adguard-home"} if [ -z "$COUNTRIES" ]; then echo "No countries specified in BLOCK_COUNTRIES." @@ -20,8 +21,8 @@ mkdir -p /tmp/cidr IFS=',' read -ra CODES <<< "$COUNTRIES" for CODE in "${CODES[@]}"; do echo "Downloading CIDR list for $CODE..." - curl -sf "$CIDR_BASE_URL/${CODE^^}.txt" -o "/tmp/cidr/${CODE}.txt" || continue - cat "/tmp/cidr/${CODE}.txt" >> /tmp/cidr/all.txt + curl -sf "$CIDR_BASE_URL/${CODE,,}.cidr" -o "/tmp/cidr/${CODE}.cidr" || continue + cat "/tmp/cidr/${CODE}.cidr" >> /tmp/cidr/all.txt done if [ -f "$MANUAL_IPS_FILE" ]; then @@ -29,19 +30,46 @@ if [ -f "$MANUAL_IPS_FILE" ]; then grep -E '^([0-9]{1,3}\.){3}[0-9]{1,3}(/[0-9]{1,2})?$' "$MANUAL_IPS_FILE" >> /tmp/cidr/all.txt fi -IPS_FORMATTED=$(sed 's/^/ - /' /tmp/cidr/all.txt) +# Format IPs as YAML list items +sed 's/^/ - /' /tmp/cidr/all.txt > /tmp/cidr/ips_formatted.txt -awk -v ips="$IPS_FORMATTED" ' -BEGIN { inside=0 } -/^ disallowed_clients:/ { print; inside=1; next } -/^ [^ ]/ && inside==1 { print ips; inside=0 } -{ if (!inside) print } -END { if (inside==1) print ips } +awk ' +BEGIN { + # Read formatted IPs into array + while ((getline line < "/tmp/cidr/ips_formatted.txt") > 0) { + ips[++count] = line + } + close("/tmp/cidr/ips_formatted.txt") + inside=0 +} + +/^ disallowed_clients:/ { + print + inside=1 + next +} + +/^ [^ ]/ && inside==1 { + # Insert all IPs here + for (i=1; i<=count; i++) print ips[i] + inside=0 +} + +{ + if (!inside) print +} + +END { + # If file ended while still inside disallowed_clients section + if (inside==1) { + for (i=1; i<=count; i++) print ips[i] + } +} ' "$ADGUARD_YAML" > "$TMP_YAML" mv "$TMP_YAML" "$ADGUARD_YAML" -echo "Restarting adguard-home container..." -curl -s -X POST "$DOCKER_API_URL/containers/adguard-home/restart" -o /dev/null +echo "Restarting $CONTAINER_NAME container..." +curl -s -X POST "$DOCKER_API_URL/containers/$CONTAINER_NAME/restart" -o /dev/null echo "Done."