fixed DHCP calculations i hope
This commit is contained in:
83
deploy.sh
83
deploy.sh
@@ -586,7 +586,6 @@ fi
|
||||
# Enable DHCP on PiHole for options 2 and 3
|
||||
if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; then
|
||||
echo "Configuring DHCP on PiHole..."
|
||||
|
||||
# Get network information
|
||||
gateway=$(ip route | awk '/default/ {print $3}')
|
||||
interface=$(ip route | awk '/default/ {print $5}')
|
||||
@@ -611,6 +610,19 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t
|
||||
echo "$(echo "ibase=2; $octet1" | bc).$(echo "ibase=2; $octet2" | bc).$(echo "ibase=2; $octet3" | bc).$(echo "ibase=2; $octet4" | bc)"
|
||||
}
|
||||
|
||||
# Function to add a number to a binary IP string
|
||||
add_to_bin_ip() {
|
||||
local bin_ip=$1
|
||||
local num=$2
|
||||
# Convert binary IP to decimal
|
||||
local dec_ip=$((2#${bin_ip:0:8}00000000 + 2#00000000${bin_ip:8:8}0000000 + 2#0000000000000000${bin_ip:16:8}0000000 + 2#000000000000000000000000${bin_ip:24:8}))
|
||||
# Add the number
|
||||
dec_ip=$((dec_ip + num))
|
||||
# Convert back to binary IP
|
||||
local new_bin_ip=$(printf "%032d" $(echo "obase=2; $dec_ip" | bc))
|
||||
echo "$new_bin_ip"
|
||||
}
|
||||
|
||||
# Calculate network and broadcast addresses using binary operations
|
||||
gateway_bin=$(ip_to_bin "$gateway")
|
||||
netmask_bin=$(ip_to_bin "$netmask")
|
||||
@@ -639,62 +651,51 @@ if [ "$external_access_method" -eq 2 ] || [ "$external_access_method" -eq 3 ]; t
|
||||
network_address=$(bin_to_ip "$network_bin")
|
||||
broadcast_address=$(bin_to_ip "$broadcast_bin")
|
||||
|
||||
# Calculate DHCP range
|
||||
# Convert network address to decimal for arithmetic
|
||||
IFS=. read -r n1 n2 n3 n4 <<< "$network_address"
|
||||
network_dec=$((n1 * 256**3 + n2 * 256**2 + n3 * 256 + n4))
|
||||
|
||||
# Convert broadcast address to decimal
|
||||
IFS=. read -r b1 b2 b3 b4 <<< "$broadcast_address"
|
||||
broadcast_dec=$((b1 * 256**3 + b2 * 256**2 + b3 * 256 + b4))
|
||||
|
||||
# Convert gateway to decimal
|
||||
IFS=. read -r g1 g2 g3 g4 <<< "$gateway"
|
||||
gateway_dec=$((g1 * 256**3 + g2 * 256**2 + g3 * 256 + g4))
|
||||
|
||||
# Convert local IP to decimal
|
||||
IFS=. read -r l1 l2 l3 l4 <<< "$localip"
|
||||
localip_dec=$((l1 * 256**3 + l2 * 256**2 + l3 * 256 + l4))
|
||||
|
||||
# Calculate DHCP range using binary operations
|
||||
# Start DHCP 10 IPs after the gateway (but not before network+1)
|
||||
dhcp_start_dec=$((gateway_dec + 10))
|
||||
if [ $dhcp_start_dec -le $network_dec ]; then
|
||||
dhcp_start_dec=$((network_dec + 1))
|
||||
dhcp_start_bin=$(add_to_bin_ip "$gateway_bin" 10)
|
||||
network_dec=$(printf "%d" "$(echo "ibase=2; ${network_bin}" | bc)")
|
||||
dhcp_start_dec=$(printf "%d" "$(echo "ibase=2; ${dhcp_start_bin}" | bc)")
|
||||
|
||||
if [ "$dhcp_start_dec" -le "$network_dec" ]; then
|
||||
dhcp_start_bin=$(add_to_bin_ip "$network_bin" 1)
|
||||
fi
|
||||
|
||||
# End DHCP 100 IPs after start (but not after broadcast-1)
|
||||
dhcp_end_dec=$((dhcp_start_dec + 100))
|
||||
if [ $dhcp_end_dec -ge $broadcast_dec ]; then
|
||||
dhcp_end_dec=$((broadcast_dec - 1))
|
||||
dhcp_end_bin=$(add_to_bin_ip "$dhcp_start_bin" 100)
|
||||
broadcast_dec=$(printf "%d" "$(echo "ibase=2; ${broadcast_bin}" | bc)")
|
||||
dhcp_end_dec=$(printf "%d" "$(echo "ibase=2; ${dhcp_end_bin}" | bc)")
|
||||
|
||||
if [ "$dhcp_end_dec" -ge "$broadcast_dec" ]; then
|
||||
dhcp_end_bin=$(add_to_bin_ip "$broadcast_bin" -1)
|
||||
fi
|
||||
|
||||
# Ensure we don't include the local IP in the DHCP range
|
||||
if [ $localip_dec -ge $dhcp_start_dec ] && [ $localip_dec -le $dhcp_end_dec ]; then
|
||||
localip_dec=$(printf "%d" "$(echo "ibase=2; ${localip_bin}" | bc)")
|
||||
dhcp_start_dec=$(printf "%d" "$(echo "ibase=2; ${dhcp_start_bin}" | bc)")
|
||||
dhcp_end_dec=$(printf "%d" "$(echo "ibase=2; ${dhcp_end_bin}" | bc)")
|
||||
|
||||
if [ "$localip_dec" -ge "$dhcp_start_dec" ] && [ "$localip_dec" -le "$dhcp_end_dec" ]; then
|
||||
# If local IP is in the range, adjust the range
|
||||
if [ $((localip_dec - dhcp_start_dec)) -lt $((dhcp_end_dec - localip_dec)) ]; then
|
||||
dhcp_start_dec=$((localip_dec + 1))
|
||||
dhcp_start_bin=$(add_to_bin_ip "$localip_bin" 1)
|
||||
else
|
||||
dhcp_end_dec=$((localip_dec - 1))
|
||||
dhcp_end_bin=$(add_to_bin_ip "$localip_bin" -1)
|
||||
fi
|
||||
fi
|
||||
|
||||
# Convert back to IP format
|
||||
dhcp_start=$(bin_to_ip "$(ip_to_bin "$(dec_to_ip $dhcp_start_dec)")")
|
||||
dhcp_end=$(bin_to_ip "$(ip_to_bin "$(dec_to_ip $dhcp_end_dec)")")
|
||||
|
||||
# Helper function to convert decimal to IP (used above)
|
||||
dec_to_ip() {
|
||||
local dec=$1
|
||||
echo "$(( (dec >> 24) & 0xFF )).$(( (dec >> 16) & 0xFF )).$(( (dec >> 8) & 0xFF )).$(( dec & 0xFF ))"
|
||||
}
|
||||
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
|
||||
|
||||
dhcp_start=$(bin_to_ip "$dhcp_start_bin")
|
||||
dhcp_end=$(bin_to_ip "$dhcp_end_bin")
|
||||
|
||||
# Apply the 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"
|
||||
|
||||
# Restart PiHole to apply changes
|
||||
docker restart pihole
|
||||
|
||||
echo "DHCP configuration applied:"
|
||||
|
||||
Reference in New Issue
Block a user