目的:

在Linux主机安装Clash,并配置透明代理。

实现客户端网关配置为Linux主机IP,能够正常访问公网及外网的目的。

环境:

操作系统:Ubuntu 20.04

Clash版本:v1.18.0

这里没有涉及到调整网络设备的配置,仅通过在Linux主机部署clash并配置透明代理,然后通过手动设置同层主机IP及网关,测试透明代理的正常使用。

一.配置示例

1.网络环境

用户网段 192.168.20.0/255.255.254.0,默认网关 192.168.21.254,开启DHCP

用户A IP: 192.168.20.10

Linux配置透明代理主机: 192.168.20.100

同层网络

2.安装clash

linux下安装 ,参考 https://docs.gtk.pw/contents/linux/clash-cli.html#%E5%AE%89%E8%A3%85-installation

下载地址

https://github.com/Dreamacro/clash/releases

存储库已经删除

通过快照获取到地址

https://web.archive.org/web/20231102080815/https://github.com/Dreamacro/clash/releases/tag/v1.18.0

下载地址

https://web.archive.org/web/20231102080815mp_/https://github.com/Dreamacro/clash/releases/download/v1.18.0/clash-linux-amd64-v1.18.0.gz

下载并安装

wget https://web.archive.org/web/20231102080815mp_/https://github.com/Dreamacro/clash/releases/download/v1.18.0/clash-linux-amd64-v1.18.0.gz

gunzip clash-linux-amd64-v1.18.0.gz

mkdir /opt/clash

mv clash-linux-amd64-v1.18.0 /opt/clash/clash

chmod +x /opt/clash/clash

# 将配置文件上传到 /opt/clash/config.yaml

# 创建service
cat > /etc/systemd/system/clash.service <<EOF
[Unit]
Description=Clash Proxy
After=network.target

[Service]
Type=simple
User=root
ExecStart=/opt/clash/clash -d /opt/clash
Restart=always
LimitNOFILE=1048576

[Install]
WantedBy=multi-user.target

EOF


# reload
systemctl daemon-reload

# 启动
systemctl start clash

# 开机自启
systemctl enable clash

3.配置clash

调整clash的配置文件config.yaml

1.开启tproxy端口

tproxy-port: 7893

2.配置监听地址

allow-lan: true
bind-address: '*'

3.配置DNS

dns:
  enable: true
  listen: 0.0.0.0:1053
tproxy-port: 7893

dns:
  enable: true
  listen: 0.0.0.0:1053
  enhanced-mode: fake-ip
  fake-ip-range: 198.18.0.1/16

4.日志级别

# 日志级别: info / warning / error / debug / silent
# 调试
log-level: debug

根据自身需求,可以调整日志级别

5.选择服务器节点

这个好像是默认自动选择,无需处理

配置文件示例

port: 7890
socks-port: 7891
redir-port: 7892
tproxy-port: 7893
#mixed-port: 7893
allow-lan: true
bind-address: '*'
mode: Rule
#log-level: info
log-level: debug
ipv6: false
hosts:

external-controller: 0.0.0.0:9090
clash-for-android:
  append-system-dns: false
profile:
  tracing: true
dns:
  enable: true
  listen: 0.0.0.0:1053
  default-nameserver:
    - 223.5.5.5
    - 119.29.29.29
    - 144.114.115.115
  ipv6: false
  enhanced-mode: fake-ip
  fake-ip-filter:
    - "*.lan"
    - stun.*.*.*
    - stun.*.*
    - time.windows.com
    - time.nist.gov
    - time.apple.com
    - time.asia.apple.com
    - "*.ntp.org.cn"
    - "*.openwrt.pool.ntp.org"
    - time1.cloud.tencent.com
    - time.ustc.edu.cn
    - pool.ntp.org
    - ntp.ubuntu.com
    - ntp.aliyun.com
    - ntp1.aliyun.com
    - ntp2.aliyun.com
    - ntp3.aliyun.com
    - ntp4.aliyun.com
    - ntp5.aliyun.com
    - ntp6.aliyun.com
    - ntp7.aliyun.com
    - time1.aliyun.com
    - time2.aliyun.com
    - time3.aliyun.com
    - time4.aliyun.com
    - time5.aliyun.com
    - time6.aliyun.com
    - time7.aliyun.com
    - "*.time.edu.cn"
    - time1.apple.com
    - time2.apple.com
    - time3.apple.com
    - time4.apple.com
    - time5.apple.com
    - time6.apple.com
    - time7.apple.com
    - time1.google.com
    - time2.google.com
    - time3.google.com
    - time4.google.com
    - music.163.com
    - "*.music.163.com"
    - "*.126.net"
    - musicapi.taihe.com
    - music.taihe.com
    - songsearch.kugou.com
    - trackercdn.kugou.com
    - "*.kuwo.cn"
    - api-jooxtt.sanook.com
    - api.joox.com
    - joox.com
    - y.qq.com
    - "*.y.qq.com"
    - streamoc.music.tc.qq.com
    - mobileoc.music.tc.qq.com
    - isure.stream.qqmusic.qq.com
    - dl.stream.qqmusic.qq.com
    - aqqmusic.tc.qq.com
    - amobile.music.tc.qq.com
    - "*.xiami.com"
    - "*.music.migu.cn"
    - music.migu.cn
    - "*.msftconnecttest.com"
    - "*.msftncsi.com"
    - localhost.ptlogin2.qq.com
    - "*.*.*.srv.nintendo.net"
    - "*.*.stun.playstation.net"
    - xbox.*.*.microsoft.com
    - "*.ipv6.microsoft.com"
    - "*.*.xboxlive.com"
    - speedtest.cros.wr.pvp.net
  nameserver:
    - https://dns.alidns.com/dns-query
    - https://dns.pub/dns-query
  fallback:
    - https://doh.dns.sb/dns-query
    - https://dns.cloudflare.com/dns-query
    - tls://8.8.4.4
    - tls://1.1.1.1:853
    - tls://1.0.0.1:853
  fallback-filter:
    geoip: true
    ipcidr:
      - 240.0.0.0/4
      - 0.0.0.0/32
      - 127.0.0.1/32
    domain:
      - +.google.com
      - +.facebook.com
      - +.twitter.com
      - +.youtube.com
      - +.xn--ngstr-lra8j.com
      - +.google.cn
      - +.googleapis.cn
      - +.googleapis.com
      - +.gvt1.com

# 节点配置
proxies:
- name: test B07
  type: ss
  server: test.example.com
  port: 15001
  udp: true
  cipher: rc4-md5
  password: xxxxxxxx
  plugin: obfs
  plugin-opts:
    mode: http
    host: test.example.com

# 组配置
proxy-groups:
  -
    name: 节点列表
    type: select
    proxies:
      - 'test B07'

# 规则
rules:
#Other,节点列表
- DOMAIN-SUFFIX,fyzhuji.com,节点列表
- DOMAIN-SUFFIX,fenghost.io,节点列表
- DOMAIN-SUFFIX,google.com,节点列表

4.配置路由

# 把被打 fwmark 1 的包,交给自定义路由表 100 处理。
ip rule add fwmark 1 table 100
# 自定义路由表里,所有流量直接丢到本地 lo,让本机应用接管。
ip route add local 0.0.0.0/0 dev lo table 100

5.配置iptables

# 配置DNS
iptables -t nat -I PREROUTING -p udp --dport 53 -j REDIRECT --to 1053

# 配置代理
iptables -t mangle -N CLASH
iptables -t mangle -A CLASH -d 0.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 192.168.20.0/23 -j RETURN
iptables -t mangle -A CLASH -p tcp -j TPROXY --tproxy-mark 1 --on-port 7893
iptables -t mangle -A CLASH -p udp -j TPROXY --tproxy-mark 1 --on-port 7893
iptables -t mangle -A PREROUTING -j CLASH

6.测试

通过curl测试代理是否可用

curl -x http://127.0.0.1:7890 -L https://www.google.com

wifi客户端修改网关,测试透明代理是否可用

wifi客户端,手动配置IPv4,指定网关为 192.168.20.100

目前网络

ip地址:  192.168.20.10
子网掩码: 255.255.254.0
网关:    192.168.21.254
DNS地址: 223.5.5.5

调整为

ip地址:  192.168.20.10
子网掩码: 255.255.254.0
网关:    192.168.20.100
DNS地址: 223.5.5.5

然后通过浏览器测试是否能正常访问互联网

国内: https://www.baidu.com

国外: https://www.google.com

如果访问国外地址失败,可以查看dns解析的地址

比如:

chrome下通过访问chrome://net-internals/?#dns,在Domain中输入 www.google.com ,点击Lookup查询对应的IP,是否是 198.18.0.0/16 网段IP。

如果不是,则没有通过clash解析dns,因为配置了dns fake-ip,fake-ip 池的默认 CIDR 是 198.18.0.1/16 (一个保留的 IPv4 地址空间, 可以在 dns.fake-ip-range 中进行更改).

查看chrome是不是开启了 对您访问的网站的名称进行加密 (chrome->设置 -> 隐私和安全 -> 安全->对您访问的网站的名称进行加密),如果开启,进行关闭,然后在刚才查询域名dns地址的页面,点击 Clear host cache ,清除dns缓存,然后再进行访问测试

fake-ip过程可以参考: fake-ip

过程分析:

当 DNS 请求被发送到 Clash DNS 时, Clash 内核会通过管理内部的域名和其 fake-ip 地址的映射, 从池中分配一个 空闲 的 fake-ip 地址.

以使用浏览器访问 http://google.com 为例.

  1. 浏览器向 Clash DNS 请求 google.com 的 IP 地址
  2. Clash 检查内部映射并返回 198.18.1.5
  3. 浏览器向 198.18.1.580/tcp 端口发送 HTTP 请求
  4. 当收到 198.18.1.5 的入站数据包时, Clash 查询内部映射, 发现客户端实际上是在向 google.com 发送数据包
  5. 根据规则的不同:

    1. Clash 可能仅将域名发送到 SOCKS5 或 shadowsocks 等出站代理, 并与代理服务器建立连接
    2. 或者 Clash 可能会基于 SCRIPTGEOIPIP-CIDR 规则或者使用 DIRECT 直连出口查询 google.com 的真实 IP 地址

7.配置持久化

统一管理配置

一个脚本管理 /opt/clash/network-setup.sh

#!/bin/bash

set -e

case "$1" in
start)
    echo "[Network Setup] Applying ip rules and iptables..."

    # IP rules
    ip rule add fwmark 1 table 100 || true
    ip route add local 0.0.0.0/0 dev lo table 100 || true

    # IPTABLES
    iptables -t mangle -N CLASH || true
    iptables -t mangle -A CLASH -d 0.0.0.0/8 -j RETURN || true
    iptables -t mangle -A CLASH -d 127.0.0.0/8 -j RETURN || true
    iptables -t mangle -A CLASH -d 192.168.20.0/23 -j RETURN || true
    iptables -t mangle -A CLASH -p tcp -j TPROXY --tproxy-mark 1 --on-port 7893 || true
    iptables -t mangle -A CLASH -p udp -j TPROXY --tproxy-mark 1 --on-port 7893 || true
    iptables -t mangle -A PREROUTING -j CLASH || true

    ;;
stop)
    echo "[Network Setup] Cleaning up ip rules and iptables..."

    # IP rules
    ip rule del fwmark 1 table 100 || true
    ip route del local 0.0.0.0/0 dev lo table 100 || true

    # IPTABLES
    iptables -t mangle -D PREROUTING -j CLASH || true
    iptables -t mangle -F CLASH || true
    iptables -t mangle -X CLASH || true

    ;;
*)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac

添加可执行权限

chmod +x network-setup.sh

调整service配置,添加启动前和启动后的执行命令

/etc/systemd/system/clash.service

[Unit]
Description=Clash Proxy
After=network.target

[Service]
Type=simple
User=root

ExecStartPre=/opt/clash/network-setup.sh start
ExecStart=/opt/clash/clash -d /opt/clash
ExecStopPost=/opt/clash/network-setup.sh stop

Restart=always
LimitNOFILE=1048576

[Install]
WantedBy=multi-user.target

更新配置

sudo systemctl daemon-reload

这样后面机器重启,clash启动,就会把需要的iptables和route自定添加,关闭的时候自动清理。

后续,也可以在脚本中添加一些日志输出的东西。网络变动,也便于更新。

标签: none

添加新评论