使用clash搭建透明网关
转载自:https://little-star.love/posts/5d083060/
安装 clash
-
下载 clash
wget https://github.com/Dreamacro/clash/releases/download/v1.8.0/clash-linux-amd64-v1.8.0.gz
-
解压并安装 clash 二进制文件
gzip -d clash-linux-amd64-v1.8.0.gz install clash-linux-amd64-v1.8.0 /usr/local/bin/clash
-
创建 clash 的
systemd
配置文件/etc/systemd/system/clash.service [Unit] Description=Clash daemon, A rule-based proxy in Go. After=network.target [Service] Type=simple Restart=always ExecStart=/usr/local/bin/clash -d /etc/clash [Install] WantedBy=multi-user.target
-
设置
clash
为开机自动启,并启动该服务systemctl enable clash systemctl start clash
-
查看
clash
服务状态systemctl status clash
-
导入 clash web 管理界面
git clone -b gh-pages --depth 1 https://github.com/Dreamacro/clash-dashboard /opt/clash-dashboard
流量转发
开启 Linux 内核的转发功能
注意:如果不开启该功能则可能存在内网设备无法访问问题
-
编辑配置文件
/etc/sysctl.conf
并向其中添加如下内容/etc/sysctl.conf net.ipv4.ip_forward=1
-
保存退出后,执行以下命令使修改生效
sysctl -p
-
查看
/proc/sys/net/ipv4/ip_forward
的内容,如果是1
表示设置成功生效
nftables
使用 nftables
替代 iptables
做流量转发,如果不想用 nftables
也可以使用添加如下 iptables
规则
iptables -t nat -N clash
iptables -t nat -A CLASH -d 0.0.0.0/8 -j RETURN
iptables -t nat -A CLASH -d 10.0.0.0/8 -j RETURN
iptables -t nat -A CLASH -d 127.0.0.0/8 -j RETURN
iptables -t nat -A CLASH -d 169.254.0.0/16 -j RETURN
iptables -t nat -A CLASH -d 172.16.0.0/12 -j RETURN
iptables -t nat -A CLASH -d 192.168.0.0/16 -j RETURN
iptables -t nat -A CLASH -d 224.0.0.0/4 -j RETURN
iptables -t nat -A CLASH -d 240.0.0.0/4 -j RETURN
iptables -t nat -A clash -d 0.0.0.0/8 -j RETURN
iptables -t nat -A clash -d 10.0.0.0/8 -j RETURN
iptables -t nat -A clash -d 127.0.0.0/8 -j RETURN
iptables -t nat -A clash -d 169.254.0.0/16 -j RETURN
iptables -t nat -A clash -d 172.16.0.0/12 -j RETURN
iptables -t nat -A clash -d 192.168.0.0/16 -j RETURN
iptables -t nat -A clash -d 224.0.0.0/4 -j RETURN
iptables -t nat -A clash -d 240.0.0.0/4 -j RETURN
iptables -t nat -A clash -p tcp -j REDIRECT --to-port 7892
iptables -t nat -A PREROUTING -p tcp -j clash
-
安装 nftables
apt -y install nftables
-
创建 nftables 配置文件扩展目录
mkdir /etc/nftables.conf.d
-
创建私有地址的定义文件
/etc/nftables.conf.d/private.nft 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 }
-
修改 nftalbes 配置文件,内容如下
/etc/nftables.conf #!/usr/sbin/nft -f include "/etc/nftables.conf.d/private.nft" table ip nat { chain proxy { ip daddr $private_list return ip protocol tcp redirect to :7892 } chain prerouting { type nat hook prerouting priority 0; policy accept; jump proxy } }
-
清空 nftalbes 规则,并使新规则生效
nft flush ruleset nft -f /etc/nftables.conf
-
查看 nftalbes 当前规则
nft list ruleset
-
设置
nftalbes
开机自启动systemctl enable nftables
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。