From ba3a1426c212d961f2940e83f6918aa593cf1d76 Mon Sep 17 00:00:00 2001 From: SDGDen Date: Mon, 9 Mar 2026 11:14:31 +0100 Subject: [PATCH] temporarily disabled downloader to speed up debugging, added debugging to pihole DHCP config. --- deploy.sh | 197 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 157 insertions(+), 40 deletions(-) diff --git a/deploy.sh b/deploy.sh index a0adec2..4851dba 100644 --- a/deploy.sh +++ b/deploy.sh @@ -496,8 +496,8 @@ echo -e "${cyan}Dockge${nc} has been installed, configured and launched from htt echo -e "${cyan}pihole${nc} has been launched from http://dns.$domain, to use pihole as your DNS provider, set your DNS to $localip in your router for DHCP and on your device for any device with a static ip. Currently, the DNS is configured to use the joindns4.eu DNS, which *also* has built-in adblocking." echo "" echo "you may have to go to settings > additional in owncloud and click "save" for the onlyoffice server settings." -docker compose up -d > /dev/null -echo -e "${cyan}downloader${nc} has been launched from http://download.$domain, verify it is online" +# docker compose up -d > /dev/null +# echo -e "${cyan}downloader${nc} has been launched from http://download.$domain, verify it is online" #bookstack @@ -587,40 +587,93 @@ fi if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; then echo "Configuring DHCP on PiHole..." echo "Gathering network information..." - # Get network information - gateway=$(ip route | awk '/default/ {print $3}') - interface=$(ip route | awk '/default/ {print $5}') - netmask=$(ifconfig $interface | awk '/netmask/ {print $4}') - localip=$(hostname -I | awk '{print $1}') + + # Get network information with error checking + gateway=$(ip route | awk '/default/ {print $3}' || echo "ERROR") + interface=$(ip route | awk '/default/ {print $5}' || echo "ERROR") + netmask=$(ifconfig $interface | awk '/netmask/ {print $4}' 2>/dev/null || echo "ERROR") + localip=$(hostname -I | awk '{print $1}' || echo "ERROR") + + if [[ "$gateway" == "ERROR" || "$interface" == "ERROR" || "$netmask" == "ERROR" || "$localip" == "ERROR" ]]; then + echo "Error: Failed to get network information" + exit 1 + fi + echo " Gateway: $gateway" echo " Interface: $interface" echo " Netmask: $netmask" echo " Local IP: $localip" - # Function to convert IP to binary string + # Function to convert IP to binary string with error checking ip_to_bin() { local ip=$1 local a b c d + + # Validate IP format + if ! [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then + echo "Error: Invalid IP format '$ip'" >&2 + return 1 + fi + IFS=. read -r a b c d <<< "$ip" - printf "%08d%08d%08d%08d" "$(echo "obase=2; $a" | bc)" "$(echo "obase=2; $b" | bc)" "$(echo "obase=2; $c" | bc)" "$(echo "obase=2; $d" | bc)" + + # Convert each octet to binary with error checking + local bin_a=$(echo "obase=2; $a" | bc 2>/dev/null || echo "ERROR") + local bin_b=$(echo "obase=2; $b" | bc 2>/dev/null || echo "ERROR") + local bin_c=$(echo "obase=2; $c" | bc 2>/dev/null || echo "ERROR") + local bin_d=$(echo "obase=2; $d" | bc 2>/dev/null || echo "ERROR") + + if [[ "$bin_a" == "ERROR" || "$bin_b" == "ERROR" || "$bin_c" == "ERROR" || "$bin_d" == "ERROR" ]]; then + echo "Error: Failed to convert IP '$ip' to binary" >&2 + return 1 + fi + + # Pad each octet to 8 bits + printf "%08d%08d%08d%08d" "$bin_a" "$bin_b" "$bin_c" "$bin_d" } - # Function to convert binary string to IP + # Function to convert binary string to IP with error checking bin_to_ip() { local bin=$1 + + # Validate binary string length + if [ ${#bin} -ne 32 ]; then + echo "Error: Invalid binary string length (expected 32, got ${#bin})" >&2 + return 1 + fi + local octet1=${bin:0:8} local octet2=${bin:8:8} local octet3=${bin:16:8} local octet4=${bin:24:8} - echo "$(echo "ibase=2; $octet1" | bc).$(echo "ibase=2; $octet2" | bc).$(echo "ibase=2; $octet3" | bc).$(echo "ibase=2; $octet4" | bc)" + + # Convert each octet with error checking + local ip1=$(echo "ibase=2; $octet1" | bc 2>/dev/null || echo "ERROR") + local ip2=$(echo "ibase=2; $octet2" | bc 2>/dev/null || echo "ERROR") + local ip3=$(echo "ibase=2; $octet3" | bc 2>/dev/null || echo "ERROR") + local ip4=$(echo "ibase=2; $octet4" | bc 2>/dev/null || echo "ERROR") + + if [[ "$ip1" == "ERROR" || "$ip2" == "ERROR" || "$ip3" == "ERROR" || "$ip4" == "ERROR" ]]; then + echo "Error: Failed to convert binary '$bin' to IP" >&2 + return 1 + fi + + echo "$ip1.$ip2.$ip3.$ip4" } # Function to add a number to an IP address (returns IP string) add_to_ip() { local ip=$1 local num=$2 - local bin_ip=$(ip_to_bin "$ip") - local bin_add=$(printf "%08d" "$(echo "obase=2; $num" | bc)") + + # Convert IP to binary + local bin_ip=$(ip_to_bin "$ip" || return 1) + local bin_add=$(printf "%08d" "$(echo "obase=2; $num" | bc 2>/dev/null || echo "ERROR")") + + if [ "$bin_add" = "ERROR" ]; then + echo "Error: Failed to convert number $num to binary" >&2 + return 1 + fi # Split the IP into 4 octets (8 bits each) local octet1=${bin_ip:0:8} @@ -635,22 +688,39 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t if [ "$new_octet4" -gt 255 ]; then new_octet4=$((new_octet4 - 256)) octet3=$((2#$octet3 + 1)) # Carry over to the third octet - octet3=$(printf "%08d" "$(echo "obase=2; $octet3" | bc)") # Convert back to 8-bit binary + octet3=$(printf "%08d" "$(echo "obase=2; $octet3" | bc 2>/dev/null || echo "ERROR")") + + if [ "$octet3" = "ERROR" ]; then + echo "Error: Failed to process octet3 after carry" >&2 + return 1 + fi fi # Update the last octet - octet4=$(printf "%08d" "$(echo "obase=2; $new_octet4" | bc)") + octet4=$(printf "%08d" "$(echo "obase=2; $new_octet4" | bc 2>/dev/null || echo "ERROR")") + + if [ "$octet4" = "ERROR" ]; then + echo "Error: Failed to process octet4 after addition" >&2 + return 1 + fi # Reconstruct the binary IP string and convert to IP - bin_to_ip "${octet1}${octet2}${octet3}${octet4}" + bin_to_ip "${octet1}${octet2}${octet3}${octet4}" || return 1 } # Function to subtract a number from an IP address (returns IP string) subtract_from_ip() { local ip=$1 local num=$2 - local bin_ip=$(ip_to_bin "$ip") - local bin_sub=$(printf "%08d" "$(echo "obase=2; $num" | bc)") + + # Convert IP to binary + local bin_ip=$(ip_to_bin "$ip" || return 1) + local bin_sub=$(printf "%08d" "$(echo "obase=2; $num" | bc 2>/dev/null || echo "ERROR")") + + if [ "$bin_sub" = "ERROR" ]; then + echo "Error: Failed to convert number $num to binary" >&2 + return 1 + fi # Split the IP into 4 octets (8 bits each) local octet1=${bin_ip:0:8} @@ -665,22 +735,39 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t if [ "$new_octet4" -lt 0 ]; then new_octet4=$((new_octet4 + 256)) octet3=$((2#$octet3 - 1)) # Borrow from the third octet - octet3=$(printf "%08d" "$(echo "obase=2; $octet3" | bc)") # Convert back to 8-bit binary + octet3=$(printf "%08d" "$(echo "obase=2; $octet3" | bc 2>/dev/null || echo "ERROR")") + + if [ "$octet3" = "ERROR" ]; then + echo "Error: Failed to process octet3 after borrow" >&2 + return 1 + fi fi # Update the last octet - octet4=$(printf "%08d" "$(echo "obase=2; $new_octet4" | bc)") + octet4=$(printf "%08d" "$(echo "obase=2; $new_octet4" | bc 2>/dev/null || echo "ERROR")") + + if [ "$octet4" = "ERROR" ]; then + echo "Error: Failed to process octet4 after subtraction" >&2 + return 1 + fi # Reconstruct the binary IP string and convert to IP - bin_to_ip "${octet1}${octet2}${octet3}${octet4}" + bin_to_ip "${octet1}${octet2}${octet3}${octet4}" || return 1 } # Function to compare two IP addresses (returns 0 if ip1 <= ip2, 1 if ip1 > ip2) compare_ips() { local ip1=$1 local ip2=$2 - local bin1=$(ip_to_bin "$ip1") - local bin2=$(ip_to_bin "$ip2") + + # Convert IPs to binary + local bin1=$(ip_to_bin "$ip1" 2>/dev/null || echo "ERROR") + local bin2=$(ip_to_bin "$ip2" 2>/dev/null || echo "ERROR") + + if [[ "$bin1" == "ERROR" || "$bin2" == "ERROR" ]]; then + echo "Error: Failed to convert IPs to binary for comparison" >&2 + return 1 + fi if [ "$bin1" = "$bin2" ]; then return 0 # equal @@ -721,9 +808,10 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t } # Calculate network and broadcast addresses using binary operations - gateway_bin=$(ip_to_bin "$gateway") - netmask_bin=$(ip_to_bin "$netmask") - localip_bin=$(ip_to_bin "$localip") + echo "Calculating network and broadcast addresses..." + gateway_bin=$(ip_to_bin "$gateway" || exit 1) + netmask_bin=$(ip_to_bin "$netmask" || exit 1) + localip_bin=$(ip_to_bin "$localip" || exit 1) # Calculate network address (bitwise AND of gateway and netmask) network_bin="" @@ -745,37 +833,48 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t fi done - network_address=$(bin_to_ip "$network_bin") - broadcast_address=$(bin_to_ip "$broadcast_bin") + network_address=$(bin_to_ip "$network_bin" || exit 1) + broadcast_address=$(bin_to_ip "$broadcast_bin" || exit 1) echo " Network Address: $network_address" echo " Broadcast Address: $broadcast_address" # Calculate DHCP start: network_address + 10 - dhcp_start=$(add_to_ip "$network_address" 10) + echo "Calculating DHCP range..." + dhcp_start=$(add_to_ip "$network_address" 10 || exit 1) + echo " Initial DHCP start: $dhcp_start" # Ensure DHCP start is not before network+1 - network_plus_1=$(add_to_ip "$network_address" 1) + network_plus_1=$(add_to_ip "$network_address" 1 || exit 1) compare_ips "$dhcp_start" "$network_plus_1" if [ $? -eq 1 ]; then # if dhcp_start < network_plus_1 + echo " Adjusting DHCP start to network+1: $network_plus_1" dhcp_start="$network_plus_1" fi # Calculate DHCP end: dhcp_start + 100 - dhcp_end=$(add_to_ip "$dhcp_start" 100) + dhcp_end=$(add_to_ip "$dhcp_start" 100 || exit 1) + echo " Initial DHCP end: $dhcp_end" # Ensure DHCP end is not after broadcast-1 - broadcast_minus_1=$(subtract_from_ip "$broadcast_address" 1) + broadcast_minus_1=$(subtract_from_ip "$broadcast_address" 1 || exit 1) compare_ips "$dhcp_end" "$broadcast_minus_1" if [ $? -eq 1 ]; then # if dhcp_end > broadcast_minus_1 + echo " Adjusting DHCP end to broadcast-1: $broadcast_minus_1" dhcp_end="$broadcast_minus_1" fi # Exclude local IP from range is_ip_in_range "$localip" "$dhcp_start" "$dhcp_end" if [ $? -eq 0 ]; then - echo "Local IP is in DHCP range. Adjusting..." + echo "Local IP $localip is in DHCP range. Adjusting..." # For simplicity, move the start up by 1 - dhcp_start=$(add_to_ip "$localip" 1) + new_dhcp_start=$(add_to_ip "$localip" 1 || exit 1) + if compare_ips "$new_dhcp_start" "$dhcp_end" && [ $? -eq 0 ]; then + echo " Adjusting DHCP start to $new_dhcp_start" + dhcp_start="$new_dhcp_start" + else + echo " Cannot adjust DHCP start without making it exceed end" + fi fi # Validate DHCP range @@ -792,15 +891,33 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t # Apply the DHCP configuration echo "Applying DHCP configuration..." - docker exec pihole pihole-FTL --config dhcp.active true - docker exec pihole pihole-FTL --config dhcp.start "$dhcp_start" - docker exec pihole pihole-FTL --config dhcp.end "$dhcp_end" - docker exec pihole pihole-FTL --config dhcp.router "$gateway" - docker exec pihole pihole-FTL --config dhcp.netmask "$netmask" + docker exec pihole pihole-FTL --config dhcp.active true || { + echo "Error: Failed to enable DHCP" + exit 1 + } + docker exec pihole pihole-FTL --config dhcp.start "$dhcp_start" || { + echo "Error: Failed to set DHCP start" + exit 1 + } + docker exec pihole pihole-FTL --config dhcp.end "$dhcp_end" || { + echo "Error: Failed to set DHCP end" + exit 1 + } + docker exec pihole pihole-FTL --config dhcp.router "$gateway" || { + echo "Error: Failed to set DHCP router" + exit 1 + } + docker exec pihole pihole-FTL --config dhcp.netmask "$netmask" || { + echo "Error: Failed to set DHCP netmask" + exit 1 + } # Restart PiHole to apply changes echo "Restarting PiHole to apply changes..." - docker restart pihole + docker restart pihole || { + echo "Error: Failed to restart PiHole" + exit 1 + } echo "DHCP configuration applied successfully." fi