note/tech/iptables代理 国内外流量.md
2025-11-19 10:16:05 +08:00

42 lines
1.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 以下为脚本:
```bash
#新建一个名为 V2RAY 的链
iptables -t nat -N V2RAY
#内部流量不转发给V2R
## AY直通
iptables -t nat -A V2RAY -d 0.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 10.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 127.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 169.254.0.0/16 -j RETURN
iptables -t nat -A V2RAY -d 172.16.0.0/12 -j RETURN
iptables -t nat -A V2RAY -d 192.168.0.0/16 -j RETURN
iptables -t nat -A V2RAY -d 224.0.0.0/4 -j RETURN
iptables -t nat -A V2RAY -d 240.0.0.0/4 -j RETURN
#直连中国的IP
####ipset脚本开始#####
#安装一个ipset
apt install ipset 或 yum install ipset 或 pacman -S ipset 或 等等
创建一个名字为china 的
ipset destroy china
#创建规则
ipset -N china hash:net
#清空旧的规则文件
rm cn.zone
#下载中国的IP文件
wget -P . http://www.ipdeny.com/ipblocks/data/countries/cn.zone
# 把IP文件的每个IP添加到IPSET规则里
for i in $(cat ./cn.zone ); do ipset -A china $i; done
#直连中国的IP
iptables -t nat -A V2RAY -m set --match-set china dst -j RETURN
iptables -t nat -A V2RAY -p tcp -j RETURN -m mark --mark 0xff
# 直连 SO_MARK为 0xff 的流量(0xff 是 16 进制数,数值上等同与上面的 255),此规则目的是避免代理本机(网关)流量出现回环问题
iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 12345
# 其余流量转发到 12345 端口(即 V2Ray
iptables -t nat -A PREROUTING -p tcp -j V2RAY
# 对局域网其他设备进行透明代理
iptables -t nat -A OUTPUT -p tcp -j V2RAY
# 对本机进行透明代理
```