139

Clash Transparent Proxy 透明代理/网关 在 Linux 设备上的实践和分享

 2 years ago
source link: https://hellodk.cn/post/844
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Clash Transparent Proxy 透明代理/网关 在 Linux 设备上的实践和分享

最近折腾 Clash 透明代理/网关,记录一些心路历程,也分享一些我收集到的互联网上关于透明代理写得很好的博客文章。

我的目标是在家里局域网中某台 7x24h working 的 Linux 设备上安装 Clash Core(或者 Clash Premium),然后主路由 Openwrt 修改 lan dhcp config,将 dhcp default gateway 和 default dns 发 offer 给请求 wifi 连接的局域网设备,这样就不需要设备主动修改网关和 DNS(指向透明代理设备的 IP)。为什么不直接使用 OpenWRT 的 Passwall、OpenClash、Shadowsocks R Plus + 这些?因为我用起来总有些问题……

注意:其他普通路由器固件可能不支持修改 dhcp 默认的 gateway 和 dns

本文大概分四个部分:如何安装部署、openwrt 指定客户端获得的默认网关和 dns、透明代理设备的静态 ip 问题、文章分享

一、如何安装部署 Clash 透明代理

关于如何安装部署 clash 透明代理,网上文章很多,后面我也会贴上很多链接。在此非常推荐这个:Clash-Linux-折腾笔记

二、openwrt 的 dhcp 指定客户端获得的网关和 dns 地址

我分享一下 openwrt 如何设置 dhcp 下发的默认 gateway 和 dns, luci 访问到 http://YOUR_ROUTER_LAN_IP:PORT/cgi-bin/luci/admin/network/network/lan,将高级设置里的 dhcp 选项设置如下

20211107111450.png

或者 ssh 到 openwrt 设备,通过修改 /etc/config/dhcp 也能达到效果

config dhcp 'lan'
option interface 'lan'
option start '100'
option limit '150'
option leasetime '12h'
option ra_slaac '1'
list ra_flags 'managed-config'
list ra_flags 'other-config'
list dhcp_option '3,10.10.10.4'
list dhcp_option '6,10.10.10.4'

在 config dhcp 'lan' 下新增如下两行即可。这样指定后重启网络 /etc/init.d/network restart 然后让设备 renew dhcp lease 或者“忘记网络”重新连接。重新连接上之后设备获得的网关和 dns 就是透明代理设备的 IP 10.10.10.4 了。否则会是 openwrt lan 接口的 ip(比如我的是 10.10.10.1

list dhcp_option '3,10.10.10.4'
list dhcp_option '6,10.10.10.4'

关于 dhcp option 请参考 此帖

option 号 含义 3 设置网关地址选项 6 设置DNS服务器地址选项

四、透明代理设备的静态 ip 问题

可以看到 Clash-Linux-折腾笔记 中是强制指定了客户端使用 static ip,但是通过 openwrt 的 /etc/config/dhcp 也可以做到。

config host
option name 't400'
option dns '1'
option mac '00:1c:25:a2:54:c6'
option ip '10.10.10.3'
option leasetime 'infinite'
config host
option name 'n1-armbian'
option dns '1'
option mac 'fc:7c:02:4e:69:f4'
option ip '10.10.10.4'
option leasetime 'infinite'

leasetime 指定为 infinite 无限期

上面的设备 n1-armbian 就是我的透明代理设备。

然后看看 n1-armbian 的网络配置,文件 /etc/network/interfaces

source /etc/network/interfaces.d/*
#auto eth0
allow-hotplug eth0
no-auto-down eth0
iface eth0 inet dhcp
hwaddress fc:7c:02:4e:69:f4
# Local loopback
auto lo
iface lo inet loopback

四、文章分享

感谢各位大佬的无私分享!!! 这里有很多写得深度的文章,读来受益匪浅。

文章最后:Clash 是个优秀的项目,衍生出来众多平台的版本,感谢开发者们的辛勤付出。

至于 clash tun mode,以及结合其他 dns 转发器之类的实践,暂时不做了,因为现在我的网络已满足当下的需求。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK