Skip to content

Instantly share code, notes, and snippets.

@Lackoftactics
Created May 5, 2025 11:48
Show Gist options
  • Save Lackoftactics/3916b6ab75e91c38feb8ed7363ee00ce to your computer and use it in GitHub Desktop.
Save Lackoftactics/3916b6ab75e91c38feb8ed7363ee00ce to your computer and use it in GitHub Desktop.
Update youtube routes
#!/bin/sh
# Script to update YouTube IP ranges and set up routing
# Create YouTube domains file if it doesn't exist
if [ ! -f /config/youtube_domains.txt ]; then
cat > /config/youtube_domains.txt << EOF
youtube.com
youtu.be
ytimg.com
yt3.ggpht.com
googlevideo.com
yt.be
youtubei.googleapis.com
youtube-nocookie.com
EOF
fi
# Get the VPN Gateway IP from gluetun API
VPN_GATEWAY=$(curl -s http://localhost:8023/v1/openvpn/status | grep -oP '(?<="gateway":")[^"]*')
if [ -z "$VPN_GATEWAY" ]; then
echo "Failed to get VPN gateway from gluetun API"
exit 1
fi
echo "VPN Gateway: $VPN_GATEWAY"
# Create the routing script for iptables
cat > /config/routing.sh << EOF
#!/bin/sh
# Set up iptables for YouTube domains
iptables -t nat -F YOUTUBE_PREROUTING 2>/dev/null || iptables -t nat -N YOUTUBE_PREROUTING
iptables -t nat -F YOUTUBE_OUTPUT 2>/dev/null || iptables -t nat -N YOUTUBE_OUTPUT
# Delete any existing references to our chains
iptables -t nat -D PREROUTING -j YOUTUBE_PREROUTING 2>/dev/null
iptables -t nat -D OUTPUT -j YOUTUBE_OUTPUT 2>/dev/null
# Add our chains to the main tables
iptables -t nat -A PREROUTING -j YOUTUBE_PREROUTING
iptables -t nat -A OUTPUT -j YOUTUBE_OUTPUT
# Add specific YouTube domains - IP mapping from resolved domains
EOF
# Resolve each domain and add to routing script
for domain in $(cat /config/youtube_domains.txt); do
echo "Resolving domain: $domain"
IPs=$(getent hosts $domain | awk '{ print $1 }')
if [ -z "$IPs" ]; then
# Try with nslookup as fallback
IPs=$(nslookup $domain | grep Address | grep -v '#' | awk '{ print $2 }')
fi
if [ -n "$IPs" ]; then
for ip in $IPs; do
echo "iptables -t nat -A YOUTUBE_PREROUTING -d $ip -j DNAT --to-destination $VPN_GATEWAY" >> /config/routing.sh
echo "iptables -t nat -A YOUTUBE_OUTPUT -d $ip -j DNAT --to-destination $VPN_GATEWAY" >> /config/routing.sh
done
else
echo "WARNING: Could not resolve IPs for $domain"
fi
done
# Make the script executable
chmod +x /config/routing.sh
# Run the routing script
/config/routing.sh
echo "YouTube routing setup complete"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment