diff --git a/bind/Dockerfile b/bind/Dockerfile index f56f8f8..717b387 100644 --- a/bind/Dockerfile +++ b/bind/Dockerfile @@ -24,7 +24,7 @@ FROM alpine:3.18 WORKDIR /app -RUN apk add --no-cache curl +RUN apk add --no-cache curl jq COPY --from=builder /usr/local/bin/dnscrypt-proxy /app/dnscrypt-proxy diff --git a/bind/entrypoint.sh b/bind/entrypoint.sh index 990b5b3..a6d3edd 100755 --- a/bind/entrypoint.sh +++ b/bind/entrypoint.sh @@ -1,63 +1,45 @@ #!/bin/sh -# Function to fetch environment variables -fetch_env() { - local env_var_name=$1 - local env_var_value="" - local retries=30 # Number of retries - local wait_time=1 # Wait time between retries - - for i in $(seq 1 $retries); do - echo "Fetching $env_var_name (attempt $i)" - env_var_value=$(curl -s "my.dappnode/global-envs/$env_var_name") - - if [ -n "$env_var_value" ]; then - echo "$env_var_value" - return 0 - fi - - sleep $wait_time - done - - echo " [ERROR] Failed to fetch $env_var_name after $retries attempts." - return 1 -} - # Start DNS server in background right away /app/dnscrypt-proxy & +pid=$! + # Initialize domain and internal_ip variables domain="" internal_ip="" -pid=$! +# Fetch domain and internal_ip from Dappmanager API +fetch_envs() { + local wait_time=10 # Wait time between retries -# Fetch required environment variables + while true; do + response=$(curl -s http://dappmanager.dappnode/global-envs) -if [ -n "${_DAPPNODE_GLOBAL_DOMAIN}" ]; then - domain=${_DAPPNODE_GLOBAL_DOMAIN} - echo "Using existing domain: $domain" -else - fetched_domain=$(fetch_env "DOMAIN") + if [ $? -eq 0 ]; then + domain=$(echo $response | jq -r '.DOMAIN') + internal_ip=$(echo $response | jq -r '.INTERNAL_IP') - if [ $? -eq 0 ]; then - domain=$fetched_domain - else - echo "[ERROR] Failed to fetch DOMAIN" - fi -fi + if [ -n "$domain" ] && [ -n "$internal_ip" ]; then + break + fi + else + echo "[ERROR] Failed to fetch data. Retrying in $wait_time seconds..." + fi -if [ -n "${_DAPPNODE_GLOBAL_INTERNAL_IP}" ]; then + sleep $wait_time + done +} + +# Check if both domain and internal_ip are available as global environment variables +if [ -n "${_DAPPNODE_GLOBAL_DOMAIN}" ] && [ -n "${_DAPPNODE_GLOBAL_INTERNAL_IP}" ]; then + domain=${_DAPPNODE_GLOBAL_DOMAIN} internal_ip=${_DAPPNODE_GLOBAL_INTERNAL_IP} - echo "Using existing domain: $domain" -else - fetched_internal_ip=$(fetch_env "INTERNAL_IP") - if [ $? -eq 0 ]; then - internal_ip=$fetched_internal_ip - else - echo "[ERROR] Failed to fetch INTERNAL_IP" - fi + echo "[INFO] Using domain($domain) and internal ip ($internal_ip) from global envs." +else + echo "[INFO] Fetching domain and internal ip from Dappmanager API..." + fetch_envs fi # Only write to cloaking-rules.txt if both domain and internal_ip are available @@ -69,5 +51,6 @@ if [ -n "$domain" ] && [ -n "$internal_ip" ]; then /app/dnscrypt-proxy else + touch cloaking-rules.txt echo "[ERROR] Missing domain or internal IP. Cloaking rules not updated. Dyndns domain will not be forwarded to internal IP." fi