clash在树莓派

clash: 上网软件,不多做介绍

主路由:openwrt

树莓派系统:archlinux

为什么是树莓派?

主路由性能太差,开着openclash wifi就各种断连带宽跑不满,上树莓派后家里带宽能跑满配合着telegram开关主机、以后想上电智能设备,聊天控制如果路由性能够请出门左转openwrt,openclash香的多

连接图: -----> 光猫 ---> 主路由---> 树莓派 | | 有线无线 拓扑(大概): ------ 光猫 ---- 主路由---- 树莓派 | | 有线无线 也就是树莓派作为局域网的网关和DNS服务器

到此默认树莓派已经搞定。

1、更改拓扑结构

树莓派部分:

开启ipv4转发:

1、永久:不同系统可能不用,该方法 systemd 应该是通用的 echo net.ipv4.ip_forward=1 > /etc/sysctl.d/30-ipforward.conf 重启后生效(没找到对应的服务,暂定重启树莓派) 2、暂时: echo 1 > /proc/sys/net/ipv4/ip_forward 立即生效

静态IP:

如果不设置静态IP,树莓派会将自己的网关设置成自己。需要根据自己系统修改 vim /etc/systemd/network/eth0.network [Match] Name=eth0 [Network] Address=192.168.xxx.xxx/24 # 树莓派IP地址 Gateway=192.168.xxx.xxx # 主路由器IP DNS=192.168.xxx.xxx # 主路由器IP

路由器部分:

通过修改dhcp服务达成目的

openwrt为例:

网络 -> 接口 -> 编辑局域网 ->DHCP服务器 -> 高级设置 -> DHCP选项(DHCP Option) 添加2条:

3,192.168.xxx.xxx(树莓派IP) 6,192.168.xxx.xxx 3 - 网关 6 - DNS服务器 或在 /etc/config/dhcp 直接修改 ``` config dhcp lan option interface lan option start 100 option limit 150 option leasetime 12h list dhcp_option 3,192.168.xxx.xxx list dhcp_option 6,192.168.xxx.xxx ```

如果路由上没办法可暂时尝试下手动设置网关和DNS服务器

手机的 wifi 选项可手动设置,电脑类似。

(网关DNS设为树莓派IP后)此时,电脑已经可以通过树莓派上网了

2、clash

Dreamacro/clash​github.com/Dreamacro/clash/wiki

clash目录:

ls -l /etc/clash/ drr-xr-x 4 rootroot 4096 Oct 28 05:17 clash-dashboard #ui,作者仓库里有 -rw-r--r-- 1 rootrootOct 30 07:27 config.yaml # 配置文件 -rw-r--r-- 1 rootroot Oct 27 13:59 Country.mmdb# 自动下载

关闭系统自带DNS:

不关掉无法会占用CLASH的DNS端口 archlinux 自带两个DNS服务,都要关掉 systemctl stop systemd-resolved systemctl disable systemd-resolved systemctl stop dnsmasq systemctl disable dnsmasq

2.1 非tun版本 —— redir-host (只能代理 TCP)

配置文件设置部分:

来自openclash port: 7890 socks-port: 7891 allow-lan: true bind-address: "ipv4 地址" #默认*会绑定ipv6,bug嫌疑点 ipv6: false mode: rule log-level: debug external-controller: 0.0.0.0:9090 redir-port: 7892 secret: "" external-ui: "/home/alarm/yacd/public" # 控制面板文件夹路径, IP:9090/ui 查看 hosts: ##Custom HOSTS## #experimental hosts, support wildcard (e.g. *.clash.dev Even *.foo.*.example.com) #static domain has a higher priority than wildcard domain (foo.example.com > *.example.com) #NOTE: hosts dont work with `fake-ip` #*.clash.dev: 127.0.0.1 #alpha.clash.dev: ::1 ##Custom HOSTS END## dns: use-hosts: true listen: 0.0.0.0:53 #listen: 127.0.0.1:7874 enable: true ipv6: false enhanced-mode: redir-host nameserver: - 114.114.114.114 - 119.29.29.29 fallback: - - - - tls://8.8.8.8:853 fallback-filter: geoip: true ipcidr: - 0.0.0.0/8 - 10.0.0.0/8 - 100.64.0.0/10 - 127.0.0.0/8 - 169.254.0.0/16 - 172.16.0.0/12 - 192.0.0.0/24 - 192.0.2.0/24 - 192.88.99.0/24 - 192.168.0.0/16 - 198.18.0.0/15 - 198.51.100.0/24 - 203.0.113.0/24 - 224.0.0.0/4 - 240.0.0.0/4 - 255.255.255.255/32

防火墙转发:

iptables -t nat -N CLASH 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-ports 7892 iptables -t nat -A PREROUTING -p tcp -j CLASH 保存防火墙的配置文件 iptables-save -f /etc/iptables/iptables.rules systemctl enable iptables

自启动脚本:

cat /etc/systemd/system/clash.service ``` [Unit] Description=Clash Daemon [Service] ExecStart=/usr/local/bin/clash -d /etc/clash [Install] WantedBy=multi-user.target

2.1 tun版本 —— redir-host

内核:

Release Premium 2020.10.08 · Dreamacro/clash​github.com/Dreamacro/clash/releases/tag/premium

配置文件设置部分:

在非tun的基础上增加: experimental: interface-name: eth0# 网卡 tun: enable: true stack: system dns-hijack: - tcp://8.8.8.8:53

防火墙:

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 -j MARK --set-xmark 100 iptables -t mangle -A PREROUTING -j CLASH iptables -t mangle -A OUTPUT -m owner --uid-owner 0 -j RETURN iptables -t mangle -A OUTPUT -j CLASH 保存防火墙的配置文件 iptables-save -f /etc/iptables/iptables.rules systemctl enable iptables

自启动:

cat /etc/systemd/system/clash.service ``` [Unit] Description=Clash Daemon [Service] ExecStart=/usr/local/bin/clash -d /etc/clash ExecStartPost=/usr/local/bin/clash_tun [Install] WantedBy=multi-user.target ``` cat /usr/local/bin/clash_tun ``` #!/bin/bash clash -d /etc/clash & sleep 5 /sbin/ip route add default dev utun table 100 /sbin/ip rule add fwmark 100lookup 100

注释:

来自: clash tun 启动,下列指令完成后如果可以进行代理就算配置成功了。 因为日志等级为 debug,所以已经能看到各种连接信息了。 # 内网内部数据放过 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 # 除了放过的数据,其它数据都打上标记,标记为100 iptables -t mangle -A CLASH -j MARK --set-xmark 100 # 应用于 mangle 的 PREROUTING 项 iptables -t mangle -A PREROUTING -j CLASH # root 数据放过, 避免回环 iptables -t mangle -A OUTPUT -m owner --uid-owner 0 -j RETURN # 应用于 mangle 的 OUTPUT 项 iptables -t mangle -A OUTPUT -j CLASH # utun 添加策略路由表 ip route add default dev utun table 100 # 标记为100的包由策略路由表100处理 ip rule add fwmark 100lookup 100

bug:

本机除了root能用代理,root需要通过环境变量进行设置。

步骤需要更改,由于没什么影响暂且搁置

22年更新:tun配置文件有些问题,可以直接用机场给的配置文件,然后修改