Skip to content

Instantly share code, notes, and snippets.

@moeinrahimi1
Last active July 21, 2025 18:53
Show Gist options
  • Select an option

  • Save moeinrahimi1/0dabfcb37d78a4e56264614e6224b164 to your computer and use it in GitHub Desktop.

Select an option

Save moeinrahimi1/0dabfcb37d78a4e56264614e6224b164 to your computer and use it in GitHub Desktop.
k
#!/bin/bash
# Your network interface
INTERFACE="end0"
# Clear existing rules
iptables -F
iptables -t nat -F
# Enable IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
# Create REDSOCKS chain
iptables -t nat -N REDSOCKS 2>/dev/null || iptables -t nat -F REDSOCKS
# Don't redirect local traffic and private networks
iptables -t nat -A REDSOCKS -d 0.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 169.254.0.0/16 -j RETURN
iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN
iptables -t nat -A REDSOCKS -d 192.168.1.0/24 -j RETURN
iptables -t nat -A REDSOCKS -d 224.0.0.0/4 -j RETURN
iptables -t nat -A REDSOCKS -d 240.0.0.0/4 -j RETURN
# Redirect remaining TCP traffic to redsocks
iptables -t nat -A REDSOCKS -p tcp -j REDIRECT --to-ports 12345
# IMPORTANT: Don't proxy redsocks' own traffic to avoid loops
iptables -t nat -A OUTPUT -p tcp -m owner --uid-owner redsocks -j RETURN
# Apply REDSOCKS only to traffic from other devices (not local)
iptables -t nat -A PREROUTING -p tcp --dport 1:65535 -j REDSOCKS
# Enable NAT/masquerading
iptables -t nat -A POSTROUTING -o $INTERFACE -j MASQUERADE
# Allow forwarding
iptables -A FORWARD -j ACCEPT
echo "Safe gateway rules applied"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment