Skip to content

Commit

Permalink
Fix cloaking rules file (#71)
Browse files Browse the repository at this point in the history
* Fix cloacking rules file input

* Infinitely try to fetch envs

* Improve script

* Use jq to retrieve envs

* Remove comment
  • Loading branch information
dappnodedev authored Jan 24, 2024
1 parent 755c74e commit faa0b21
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 46 deletions.
2 changes: 1 addition & 1 deletion bind/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
73 changes: 28 additions & 45 deletions bind/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit faa0b21

Please sign in to comment.