Skip to content

Instantly share code, notes, and snippets.

@celeron633
Created April 1, 2023 03:57
Show Gist options
  • Save celeron633/f156f1d9d0a1bdaec783f04d668dd172 to your computer and use it in GitHub Desktop.
Save celeron633/f156f1d9d0a1bdaec783f04d668dd172 to your computer and use it in GitHub Desktop.
iptables script for v2ray transparent proxy
#!/bin/bash
INTERFACE_NAME='ens18'
REDIR_PORT='1088'
# 开启代理
start()
{
echo "start tproxy begin"
# 设置策略路由
ip rule add fwmark 1 table 100
ip route add local 0.0.0.0/0 dev lo table 100
# 代理局域网设备
iptables -t mangle -N V2RAY
iptables -t mangle -A V2RAY -d 127.0.0.1/32 -j RETURN
iptables -t mangle -A V2RAY -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A V2RAY -d 255.255.255.255/32 -j RETURN
iptables -t mangle -A V2RAY -d 192.168.0.0/16 -p tcp -j RETURN # 直连局域网,避免 V2Ray 无法启动时无法连网关的 SSH,如果你配置的是其他网段(如 10.x.x.x 等),则修改成自己的
iptables -t mangle -A V2RAY -d 192.168.0.0/16 -p udp ! --dport 53 -j RETURN # 直连局域网,53 端口除外(因为要使用 V2Ray 的 DNS)
iptables -t mangle -A V2RAY -j RETURN -m mark --mark 0xff # 直连 SO_MARK 为 0xff 的流量(0xff 是 16 进制数,数值上等同与上面V2Ray 配置的 255),此规则目的是解决v2ray占用大量CPU(https://github.com/v2ray/v2ray-core/issues/2621)
iptables -t mangle -A V2RAY -p udp -j TPROXY --on-ip 127.0.0.1 --on-port ${REDIR_PORT} --tproxy-mark 1 # 给 UDP 打标记 1,转发至 12345 端口
iptables -t mangle -A V2RAY -p tcp -j TPROXY --on-ip 127.0.0.1 --on-port ${REDIR_PORT} --tproxy-mark 1 # 给 TCP 打标记 1,转发至 12345 端口
iptables -t mangle -A PREROUTING -j V2RAY # 应用规则
# 代理网关本机
iptables -t mangle -N V2RAY_MASK
# skip bt process traffic
# iptables -t mangle -A V2RAY_MASK -m owner --cmd-owner qbittorrent-nox -j RETURN
# skip bt user
iptables -t mangle -A V2RAY_MASK -m owner --uid-owner 1001 -j RETURN
iptables -t mangle -A V2RAY_MASK -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A V2RAY_MASK -d 255.255.255.255/32 -j RETURN
iptables -t mangle -A V2RAY_MASK -d 192.168.0.0/16 -p tcp -j RETURN # 直连局域网
iptables -t mangle -A V2RAY_MASK -d 192.168.0.0/16 -p udp ! --dport 53 -j RETURN # 直连局域网,53 端口除外(因为要使用 V2Ray 的 DNS)
iptables -t mangle -A V2RAY_MASK -j RETURN -m mark --mark 0xff # 直连 SO_MARK 为 0xff 的流量(0xff 是 16 进制数,数值上等同与上面V2Ray 配置的 255),此规则目的是避免代理本机(网关)流量出现回环问题
iptables -t mangle -A V2RAY_MASK -p udp -j MARK --set-mark 1 # 给 UDP 打标记,重路由
iptables -t mangle -A V2RAY_MASK -p tcp -j MARK --set-mark 1 # 给 TCP 打标记,重路由
iptables -t mangle -A OUTPUT -j V2RAY_MASK # 应用规则
# 新建 DIVERT 规则,避免已有连接的包二次通过 TPROXY,理论上有一定的性能提升
iptables -t mangle -N DIVERT
iptables -t mangle -A DIVERT -j MARK --set-mark 1
iptables -t mangle -A DIVERT -j ACCEPT
iptables -t mangle -I PREROUTING -p tcp -m socket -j DIVERT
echo "start tproxy end"
}
clean_iptables()
{
sleep 0.5
iptables -t mangle -X V2RAY > /dev/null 2>&1
iptables -t mangle -X V2RAY_MASK > /dev/null 2>&1
iptables -t mangle -X DIVERT > /dev/null 2>&1
}
# 停止所有代理
stop()
{
echo "stop tproxy begin"
# reset mangle
echo "reseting mangle..."
iptables -t mangle -F V2RAY > /dev/null 2>&1
iptables -t mangle -F V2RAY_MASK > /dev/null 2>&1
iptables -t mangle -F DIVERT > /dev/null 2>&1
iptables -t mangle -D PREROUTING -p tcp -m socket -j DIVERT > /dev/null 2>&1
iptables -t mangle -D OUTPUT -j V2RAY_MASK > /dev/null 2>&1
iptables -t mangle -D PREROUTING -j V2RAY > /dev/null 2>&1
clean_iptables
# reset ip table for mangle mark
echo "reseting route table..."
ip rule del table 100 > /dev/null 2>&1
ip route flush table 100 > /dev/null 2>&1
echo "stop tproxy end"
}
# 关闭本机代理
stop_local()
{
echo "reseting OUTPUT chain..."
iptables -t mangle -D OUTPUT -j V2RAY_MASK > /dev/null 2>&1
}
main()
{
# 参数个数检查
if [ $# -eq 0 ];
then
echo "usage: $0 start|stop|..."
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