【每日阅读】2020年10月3日-树莓派充当透明代理

透明代理

透明代理的含义是,具体设备没有配置代理,但是其他网络数据转发设备在背后默默充当了代理的角色,所以叫透明代理。本文的实现手段是使用iptables+clash。iptables作底层流量分发,判断是否该发送到clash。clash作高层的规则判断。

本文参考资料:https://linux.die.net/man/8/iptables

也可以在服务器man iptables获取(不过有一些不一样)

iptables概念

iptables分为表(table)、处理链(chain)、规则(rule)三个层级。我们配置就是配置rule。linux内置了很多table,有filter、nat、mangle、raw。每个table下面都内置了多种chain。我们本次配置是在nat表的CLASH链(本次会新建CLASH链)。

使树莓派支持数据包转发

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

# 使之生效
sysctl -p

iptables参数

透明代理之所以叫“透明”,重点就是这一步。使用iptables使流量被默默的转发到clash上,设备根本感知不到自己的流量被clash处理了。没有配置代理的一步,所以叫“透明代理”。

--table     -t table    table to manipulate (default: `filter')
--new       -N chain   Create a new user-defined chain
--append   -A chain   Append to chain
--destination -d address[/mask][...]
                      destination specification
--jump     -j target   target for rule (may load target extension)
--proto     -p proto   protocol: by number or name, eg. `tcp'
--list     -L [chain] 显示已配置的规则
# 常用命令
# 查看已有规则
sudo iptables -t nat -L
【每日阅读】2020年10月3日-树莓派充当透明代理

-j RETURN

RETURN means stop traversing this chain and resume at the next rule in the previous (calling) chain.

翻译:
RETURN表示停止遍历此链,并在上一个(调用)链中的下一个规则处恢复。

-j REDIRECT

REDIRECT

This target is only valid in the nat table, in the PREROUTING and OUTPUT chains, and user-defined chains which are only called from those chains. It redirects the packet to the machine itself by changing the destination IP to the primary address of the incoming interface (locally-generated packets are mapped to the 127.0.0.1 address). It takes one option:
--to-ports port[-port]
This specifies a destination port or range of ports to use: without this, the destination port is never altered. This is only valid if the rule also specifies -p tcp or -p udp.

大致翻译:
只在nat表的PREROUTING和OUTPUT链以及用户自定义链可用。这个选项将数据包指向本机,可选参数只有--to-ports,可以配置一个端口,也可以配置一个端口区间。并且只在配置了-p tcp或者-p udp时有用。

树莓派这么配

# 依次执行下面这些命令

# 创建一个名为CLASH的处理链
sudo iptables -t nat -N CLASH

# 内部流量不转发给 CLASH 直通
sudo iptables -t nat -A CLASH -d 192.168.0.0/16 -j RETURN
# 下面这些以及上面这一行都是内网IP,家用一般路由器都是192.168.x.x,所以一般只需要配置上面这一个即可。让内网(猫下、路由器下)流量不经过CLASH的判断,因为没有必要。
sudo iptables -t nat -A CLASH -d 0.0.0.0/8 -j RETURN
sudo iptables -t nat -A CLASH -d 10.0.0.0/8 -j RETURN
sudo iptables -t nat -A CLASH -d 127.0.0.0/8 -j RETURN
sudo iptables -t nat -A CLASH -d 169.254.0.0/16 -j RETURN
sudo iptables -t nat -A CLASH -d 172.16.0.0/12 -j RETURN
sudo iptables -t nat -A CLASH -d 224.0.0.0/4 -j RETURN
sudo iptables -t nat -A CLASH -d 240.0.0.0/4 -j RETURN

# 代理所有tcp流量
sudo iptables -t nat -A CLASH -p tcp -j REDIRECT --to-ports 7892
sudo iptables -t nat -A PREROUTING -p tcp -j CLASH

持久保存iptables规则

经过我的实测,上面配置的规则在树莓派重启之后就丢失了。所以需要经过下面的这些操作来实现规则的永远可用。

iptables自带工具

这种方式需要再配置开机自动执行iptables-restore命令。

# iptables自带命令
# 保存规则到文件
sudo iptables-save -t nat -f iptables_rule.output
# 从文件恢复规则
sudo iptables-restore iptables_rule.output

其他工具

这种方式全自动,安装上就已经实现开机自动配置规则了。

# 安装其他工具,自动实现开启自动配置(ipv4的规则默认保存在/etc/iptables/rules.v4)
sudo apt-get install iptables-persistent

# 日后修改规则之后更新文件(也可自己使用iptables-save覆盖默认文件)
sudo dpkg-reconfigure iptables-persistent
【每日阅读】2020年10月3日-树莓派充当透明代理

使用

树莓派经过上面的步骤就已经安装好了clash,且支持作为网关被使用。只需要在其他设备上配置即可。下面用windows电脑做演示。

【每日阅读】2020年10月3日-树莓派充当透明代理

很简单对吧。网关设置为树莓派的IP,就可以实现所有流量被树莓派接管。DNS也配置树莓派是因为我的树莓派安装了pihole,如果你没有安装pihole,则将dns设置为家里路由器的地址。也可以在开启clash的dns解析的情况下,将dns解析地址配置为树莓派的地址。其他手机、ipad之类的都同理设置即可。

还有啊,如果你的路由器支持改变网关,则可以只在路由器设置一次,其他所有设备就都设置好啦。但这种要记得把树莓派的网关设置为路由器哦。

速度如何

我用的使树莓派4B 8G,从监控看是可以跑满带宽的,毕竟是千兆网卡。我的带宽峰值大概300多兆。

【每日阅读】2020年10月3日-树莓派充当透明代理

总结

昨天我只是安装了clash,直接使用http(s)端口走代理,那不叫透明代理,因为还是配置了代理。今天这个叫透明代理了,因为在具体的设备使用时根本没有配置代理这一步对不对。只是把网关和DNS解析服务器的地址改了一下就可以了。而且我感觉这种方式性能更好一些。

原创文章,作者:geekgao,如若转载,请注明出处:https://www.geekgao.cn/archives/2515

(3)
geekgaogeekgao博主
上一篇 2020年10月2日 下午11:21
下一篇 2020年10月5日 上午1:05

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

GitHub
分享本页
返回顶部

Warning: error_log(/usr/local/lighthouse/softwares/wordpress/wp-content/plugins/spider-analyser/#log/log-2508.txt): failed to open stream: No such file or directory in /usr/local/lighthouse/softwares/wordpress/wp-content/plugins/spider-analyser/spider.class.php on line 2900