Last active
January 8, 2025 02:24
-
-
Save zfl9/d52482118f38ce2c16195583dffc44d2 to your computer and use it in GitHub Desktop.
pure TPROXY transparent proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
start_ssredir() { | |
# ss-libev 3.3.5+ support `-T` option | |
(ss-redir -s SS_IP -p SS_PORT -m SS_METHOD -k SS_PASSWD -b 127.0.0.1 -l 60080 --no-delay -u -T -v </dev/null &>>/var/log/ss-redir.log &) | |
} | |
stop_ssredir() { | |
kill -9 $(pidof ss-redir) &>/dev/null | |
} | |
start_iptables() { | |
##################### SSREDIR ##################### | |
iptables -t mangle -N SSREDIR | |
# connection-mark -> packet-mark | |
iptables -t mangle -A SSREDIR -j CONNMARK --restore-mark | |
iptables -t mangle -A SSREDIR -m mark --mark 0x2333 -j RETURN | |
# ignore traffic to ss-server | |
iptables -t mangle -A SSREDIR -p tcp -d SS_IP --dport SS_PORT -j RETURN | |
iptables -t mangle -A SSREDIR -p udp -d SS_IP --dport SS_PORT -j RETURN | |
# ignore traffic to reserved addresses | |
# can use ipset to reduce the rule count | |
iptables -t mangle -A SSREDIR -d 0.0.0.0/8 -j RETURN | |
iptables -t mangle -A SSREDIR -d 10.0.0.0/8 -j RETURN | |
iptables -t mangle -A SSREDIR -d 100.64.0.0/10 -j RETURN | |
iptables -t mangle -A SSREDIR -d 127.0.0.0/8 -j RETURN | |
iptables -t mangle -A SSREDIR -d 169.254.0.0/16 -j RETURN | |
iptables -t mangle -A SSREDIR -d 172.16.0.0/12 -j RETURN | |
iptables -t mangle -A SSREDIR -d 192.0.0.0/24 -j RETURN | |
iptables -t mangle -A SSREDIR -d 192.0.2.0/24 -j RETURN | |
iptables -t mangle -A SSREDIR -d 192.88.99.0/24 -j RETURN | |
iptables -t mangle -A SSREDIR -d 192.168.0.0/16 -j RETURN | |
iptables -t mangle -A SSREDIR -d 198.18.0.0/15 -j RETURN | |
iptables -t mangle -A SSREDIR -d 198.51.100.0/24 -j RETURN | |
iptables -t mangle -A SSREDIR -d 203.0.113.0/24 -j RETURN | |
iptables -t mangle -A SSREDIR -d 224.0.0.0/4 -j RETURN | |
iptables -t mangle -A SSREDIR -d 240.0.0.0/4 -j RETURN | |
iptables -t mangle -A SSREDIR -d 255.255.255.255/32 -j RETURN | |
# mark the first packet of the connection | |
iptables -t mangle -A SSREDIR -p tcp --syn -j MARK --set-mark 0x2333 | |
iptables -t mangle -A SSREDIR -p udp -m conntrack --ctstate NEW -j MARK --set-mark 0x2333 | |
# packet-mark -> connection-mark | |
iptables -t mangle -A SSREDIR -j CONNMARK --save-mark | |
##################### OUTPUT ##################### | |
# proxy outbound traffic from local host | |
iptables -t mangle -A OUTPUT -p tcp -m addrtype --src-type LOCAL ! --dst-type LOCAL -j SSREDIR | |
iptables -t mangle -A OUTPUT -p udp -m addrtype --src-type LOCAL ! --dst-type LOCAL -j SSREDIR | |
##################### PREROUTING ##################### | |
# proxy outbound traffic from other hosts (as gateway) | |
iptables -t mangle -A PREROUTING -p tcp -m addrtype ! --src-type LOCAL ! --dst-type LOCAL -j SSREDIR | |
iptables -t mangle -A PREROUTING -p udp -m addrtype ! --src-type LOCAL ! --dst-type LOCAL -j SSREDIR | |
# marked packet -> ss-redir:60080 (via TPROXY) | |
iptables -t mangle -A PREROUTING -p tcp -m mark --mark 0x2333 -j TPROXY --on-ip 127.0.0.1 --on-port 60080 | |
iptables -t mangle -A PREROUTING -p udp -m mark --mark 0x2333 -j TPROXY --on-ip 127.0.0.1 --on-port 60080 | |
} | |
stop_iptables() { | |
##################### PREROUTING ##################### | |
iptables -t mangle -D PREROUTING -p tcp -m mark --mark 0x2333 -j TPROXY --on-ip 127.0.0.1 --on-port 60080 &>/dev/null | |
iptables -t mangle -D PREROUTING -p udp -m mark --mark 0x2333 -j TPROXY --on-ip 127.0.0.1 --on-port 60080 &>/dev/null | |
iptables -t mangle -D PREROUTING -p tcp -m addrtype ! --src-type LOCAL ! --dst-type LOCAL -j SSREDIR &>/dev/null | |
iptables -t mangle -D PREROUTING -p udp -m addrtype ! --src-type LOCAL ! --dst-type LOCAL -j SSREDIR &>/dev/null | |
##################### OUTPUT ##################### | |
iptables -t mangle -D OUTPUT -p tcp -m addrtype --src-type LOCAL ! --dst-type LOCAL -j SSREDIR &>/dev/null | |
iptables -t mangle -D OUTPUT -p udp -m addrtype --src-type LOCAL ! --dst-type LOCAL -j SSREDIR &>/dev/null | |
##################### SSREDIR ##################### | |
iptables -t mangle -F SSREDIR &>/dev/null | |
iptables -t mangle -X SSREDIR &>/dev/null | |
} | |
start_iproute2() { | |
ip route add local default dev lo table 100 | |
ip rule add fwmark 0x2333 table 100 | |
} | |
stop_iproute2() { | |
ip rule del table 100 &>/dev/null | |
ip route flush table 100 &>/dev/null | |
} | |
start_resolvconf() { | |
echo "nameserver 1.1.1.1" >/etc/resolv.conf | |
} | |
stop_resolvconf() { | |
echo "nameserver 223.5.5.5" >/etc/resolv.conf | |
} | |
start() { | |
echo "start ..." | |
start_ssredir | |
start_iptables | |
start_iproute2 | |
start_resolvconf | |
echo "start end" | |
} | |
stop() { | |
echo "stop ..." | |
stop_resolvconf | |
stop_iproute2 | |
stop_iptables | |
stop_ssredir | |
echo "stop end" | |
} | |
restart() { | |
stop | |
sleep 1 | |
start | |
} | |
main() { | |
if [ $# -eq 0 ]; then | |
echo "usage: $0 start|stop|restart ..." | |
return 1 | |
fi | |
for funcname in "$@"; do | |
if [ "$(type -t $funcname)" != 'function' ]; then | |
echo "'$funcname' not a shell function" | |
return 1 | |
fi | |
done | |
for funcname in "$@"; do | |
$funcname | |
done | |
return 0 | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Executing this script on the Linux host can proxy all outbound traffic of this machine (except the traffic sent to the reserved address). Other hosts under the same LAN can also change their default gateway to the IP of this Linux host (at the same time change the DNS server to 1.1.1.1 or 8.8.8.8, etc.) to proxy their outbound traffic.
IPv6 proxy is similar, just change
iptables
toip6tables
,ip
toip -6
,127.0.0.1
to::1
and so on.