clash 透明代理

docker clash & 透明代理

termial proxy

# Define `setproxy` command to enable proxy configuration
setproxy() {
  export http_proxy="http://localhost:7890"
  export https_proxy="http://localhost:7890"
  export all_proxy="socks5://localhost:7890"
  echo "Proxy on"
}

# Define `unsetproxy` command to disable proxy configuration
unsetproxy() {
  unset http_proxy
  unset https_proxy
  unset all_proxy
  echo "Proxy off"
}

透明代理

  1. 转发
    为了让系统有基本的路由转发功能,需要开一下 IP 转发。编辑 /etc/sysctl.conf 文件,将 net.ipv4.ip_forward 和 net.ipv6.conf.all.forwarding 都改为 1,然后执行 sysctl -p 使配置生效。
cat /proc/sys/net/ipv4/ip_forward # 检查是否已生效
  1. 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
  1. iptables
    首先 clash 需要配置 redir-port ,以下为例子:

参照 clash-udp-tproxy-support: https://lancellc.gitbook.io/clash/start-clash/clash-udp-tproxy-support

以下例子,7892为 redir port,5353 为 clash dns port

##### TCP #####

# Bypass private IP address ranges
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

# Redirect all TCP traffic to redir port, where Clash listens
iptables -t nat -A CLASH -p tcp -j REDIRECT --to-ports 7892
iptables -t nat -A PREROUTING -p tcp -j CLASH

##### UDP #####

# IP rules
ip rule add fwmark 1 table 100
ip route add local default dev lo table 100

# Bypass private IP address ranges
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

# Redirect
iptables -t mangle -A CLASH -p udp -j TPROXY --on-port 7892 --tproxy-mark 1
iptables -t mangle -A PREROUTING -p udp -j CLASH

##### DNS #####

# Redirect 53 to 5353 
iptables -t nat -I PREROUTING -p udp --dport 53 -d 192.168.0.0/16 -j REDIRECT --to 5353

将上面保存为 set-iptables.sh 文件执行。

持久化
由于 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。

IP 规则添加在 /etc/network/interfaces,https://unix.stackexchange.com/questions/84552/set-persistent-routing-table-on-debian

post-up ip rule add fwmark 1 table 100
post-up ip route add local default dev lo table 100

clash Fake IP mode

# ip range: 192.18.0.0/16
iptables -t nat -A OUTPUT -p tcp -d 198.18.0.0/16 -j REDIRECT --to-port 7892
iptables -t mangle -A OUTPUT -p udp -d 198.18.0.0/16 -j MARK --set-mark 1