ubuntu clash + adguard 透明代理
约定
设备主机IP:a.b.c.d
主机系统已安装docker以及docker-compose.
配置adguard
DNS解析
Ubuntu 以及其他的一些 Linux 发行版 默认使用了 systemd-resolved 提供域名解析服务,默认会占用服务器的53端口。由于我们需要使用 clash 提供的 DNS 服务,则需要禁止 system-resolved 监听端口或者直接将其停止。
# 停止服务
sudo systemctl stop systemd-resolved && systemctl disable systemd-resolved
or
# 或者取消监听端口(二者选其一)
vi /etc/systemd/resolved.conf
# 将 DNSStubListener 改为 no
然后 sudo systemctl restart systemd-resolved
通过docker-compose启动adguard
docker-compose
version: "3"
services:
adguard:
image: adguard/adguardhome
ports:
- "53:53/tcp"
- "53:53/udp"
- "67:67/udp"
- "68:68/tcp"
- "68:68/udp"
- "9080:9080/tcp"
- "9443:9443/tcp"
- "853:853/tcp"
- "3000:3000/tcp"
- "8000:8000/tcp"
volumes:
- ./conf/adguardhome/work:/opt/adguardhome/work
- ./conf/adguardhome/confdir:/opt/adguardhome/conf
network_mode: "host"
restart: unless-stopped
配置adguard
修改adguard的前端bind端口
adguard的管理地址:a.b.c.d:9080
设置adguard上游 DNS 服务器为:
a.b.c.d:5656
配置clash
配置ubuntu透明代理
- 为了让系统有基本的路由转发功能,需要开一下 IP 转发。编辑
/etc/sysctl.conf
文件,将net.ipv4.ip_forward
和net.ipv6.conf.all.forwarding
都改为 1,然后执行sysctl -p
使配置生效。
cat /proc/sys/net/ipv4/ip_forward # 检查是否已生效
- 设置iptables
#tcp
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 192.168.123.10 -j RETURN
iptables -t nat -A clash -p tcp -j REDIRECT --to-port 7892
iptables -t nat -I PREROUTING -p tcp -d 8.8.8.8 -j REDIRECT --to-port 7892
iptables -t nat -I PREROUTING -p tcp -d 8.8.4.4 -j REDIRECT --to-port 7892
iptables -t nat -A PREROUTING -p tcp -j clash
#udp
ip rule add fwmark 1 table 100
ip route add local default dev lo table 100
iptables -t mangle -N clash
iptables -t mangle -A clash -d 0.0.0.0/8 -j RETURN
iptables -t mangle -A clash -d 10.0.0.0/8 -j RETURN
iptables -t mangle -A clash -d 127.0.0.0/8 -j RETURN
iptables -t mangle -A clash -d 169.254.0.0/16 -j RETURN
iptables -t mangle -A clash -d 172.16.0.0/12 -j RETURN
iptables -t mangle -A clash -d 192.168.0.0/16 -j RETURN
iptables -t mangle -A clash -d 224.0.0.0/4 -j RETURN
iptables -t mangle -A clash -d 240.0.0.0/4 -j RETURN
iptables -t mangle -A clash -d 192.168.123.10 -j RETURN
iptables -t mangle -A clash -p udp -j TPROXY --on-port 7892 --tproxy-mark 1
iptables -t mangle -A PREROUTING -p udp -j clash
持久化
由于 iptables 规则会在重新启动后清空,因此需要 iptables-persistent 实现持久化:
sudo apt install iptables-persistent netfilter-persistent
netfilter-persistent save
netfilter-persistent start
iptables-save > /etc/iptables/rules.v4
保存后 iptables 规则会在重新启动后自动加载,也可以使用netfilter-persistent reload
命令手动加载到 iptables。
启动clash
Docker-compose
Tips:
替换下文中的provider_url
version: "3"
services:
clash:
image: abcdelf/clash_pro:latest
environment:
- provider_url=https://xxxxxx/clashx/xxxxx
volumes:
- ./conf/clash/config/:/root/.config/clash/
ports:
- "7890:7890"
- "7891:7891"
- "7892:7892"
- "5656:5656/tcp"
- "5656:5656/udp"
- "9090:9090"
restart: unless-stopped
network_mode: "host"
clash ui
http://主机地址:9090/ui/
如上图可点击⚡️图标进行测速,选择延时最低的节点即可。
透明代理使用
目标设备设置 网关和DNS 为 a.b.c.d
发表回复