使用nftables作为网关 转发流量留学
开启转发功能
# 为1就是打开的
cat /proc/sys/net/ipv4/ip_forward
# 任何打开,编辑
vim /etc/sysctl.conf
net.ipv4.ip_forward=1
# apply
sysctl -p
文件 nftables.conf
define private_list = {
0.0.0.0/8,
10.0.0.0/8,
127.0.0.0/8,
169.254.0.0/16,
172.16.0.0/12,
192.168.0.0/16,
224.0.0.0/4,
240.0.0.0/4,
172.17.0.0/16,
172.18.0.0/16,
172.17.0.1/16
}
table ip myclash {
chain proxy {
ip daddr $private_list return
ip protocol tcp redirect to :7890
}
chain prerouting {
type nat hook prerouting priority 0; policy accept;
jump proxy
}
}
# 查看 nftalbes 当前规则
sudo nft list ruleset
# 运行, 之后docker无法访问外网
sudo nft -f ./nftables.conf
# 查看创建的table
sudo nft -a list table myclash
# 删除
sudo nft delete table myclash
# 清空 nftalbes 规则
# sudo nft flush ruleset
使用
# 服务端
./shadowsocks2-linux -s 'ss://AEAD_CHACHA20_POLY1305:123456@:8488' -verbose
# 客户端 使用-redir指定本地端口
./shadowsocks2-linux -c 'ss://AES-128-GCM:123456@外网ip:8481' -verbose -redir :7890 -u -udptun :8053=8.8.8.8:53,:8054=8.8.4.4:53 \
-tcptun :8053=8.8.8.8:53,:8054=8.8.4.4:53
# 内网客户端网关设置为服务端ip即可
使用 iptables 实现
sudo iptables -t nat -L -n
# 新增myproxy规则
sudo iptables -t nat -N myproxy
sudo iptables -t nat -A myproxy -m addrtype --dst-type LOCAL -j RETURN
sudo iptables -t nat -A myproxy -p tcp -j REDIRECT --to-port 7890
# 增加到 PREROUTING
sudo iptables -t nat -A PREROUTING -j myproxy
sudo iptables -t nat -L -n|grep myproxy -A 100
sudo iptables -t nat -L -n|grep PREROUTING -A 10
# 删除, 通过 --line-numbers 显示数值,通过数值进行删除
sudo iptables -t nat -D PREROUTING 3
# 或者一条命令一条命令增加到 PREROUTING
# iptables -t nat -A PREROUTING -p tcp -j REDIRECT --to-port 7890