使用 Raspberry Pi 和 Clash 打造即插即用的透明网关/代理。
使用 Raspberry Pi (Rapbian 10) 和 Clash (v0.15.0) 打造即插即用的透明网关/代理。
编译和准备 Clash
准备配置文件
Clash 配置文件的前半部分修改如下:port: HTTP代理端口
socks-port: SOCKS代理端口
redir-port: 转发端口
allow-lan: true
mode: Rule
log-level: error
external-controller: 0.0.0.0:配置端口
secret: ""
dns:
enable: true
ipv6: false
listen: 0.0.0.0:53
enhanced-mode: fake-ip
nameserver:
- 223.5.5.5
- tls://dns.rubyfish.cn:853
- tls://1.1.1.1:853
- tls://dns.google
- tcp://1.1.1.1:53
- tcp://208.67.222.222:443
交叉编译 Clash
Mac 或 Linux 上可以:go get -u -v github.com/Dreamacro/clash
cd ~/go/src/github.com/Dreamacro/clash
GOARCH=arm GOOS=linux GOARM=7 CGO_ENABLED=0 go build -ldflags '-w -s'
编译的时候有个小陷阱,编译到 go-shadowsocks2
时会报 undefined ... chacha20poly1305.NewX
。找到那个源文件把
X
删掉就好了。准备 Clash
上传 Clash 和你的配置文件到 Raspberry Pi:scp clash pi@raspberrypi.local:
scp config.yaml pi@raspberrypi.local:
登录 Raspberry Pi,把 Clash 和配置文件放到 /opt/clash
目录sudo -i
cd /home/pi
chown root:root clash
mkdir -p /opt/clash
mv clash config.yaml /opt/clash/
网络配置
编辑/etc/dhcpcd.conf
,固定 Raspberry Pi 的 IP:interface eth0
static ip_address=固定IP/24
static routers=路由器IP
打开转发:echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf && sysctl -p
echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.conf && sysctl -p
设置并保存转发规则:iptables -t nat -N Clash
iptables -t nat -A Clash -d 192.168.0.0/16 -j RETURN
iptables -t nat -A Clash -p tcp -j REDIRECT --to-ports 转发端口
iptables -t nat -A PREROUTING -p tcp -j Clash
netfilter-persistent save
使用 Supervisor 监护 Clash 进程
创建/etc/supervisor/conf.d/clash.conf
:[supervisord]
nodaemon=false
[program:clash]
priority=1
directory=/opt/clash
command=/opt/clash/clash -d .
autorestart=true
启动 Supervisor:systemctl restart supervisor
systemctl enable supervisor
使用 yacd 作为控制前端
安装 git 和 build 工具:apt install git build-essential
安装 nodejs:curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
apt install nodejs
安装 yarn:curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
apt update && sudo apt install yarn
安装 nginx:apt -y install nginx unzip
mv /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/default.bak
下载 yacd:git clone https://github.com/haishanh/yacd.git
cd yacd
可以编辑 src/ducks/app.js
,把登录页面上缺省的服务器和端口改为你习惯的:const defaultState = {
clashAPIConfig: {
hostname: '固定IP’,
port: ‘配置端口’,
secret: ''
},
使用 yarn 编译安装:yarn
yarn build
cp -r public/. /usr/share/nginx/html/yacd
创建 /etc/nginx/conf.d/yacd.conf
:server {
listen 80;
server_name 固定IP;
root /usr/share/nginx/html/yacd;
index index.html;
}
启动 nginx:systemctl start nginx
systemctl enable nginx
使用 Cron 自动更新和重启 Clash
执行crontab -e
,加入以下两行(每天 3:45 更新配置,3:50 重启 Clash):45 3 * * 1 /usr/bin/wget 你的配置链接 -O /opt/clash/config.yaml
50 3 * * * /usr/bin/supervisorctl -c /etc/supervisor/supervisord.conf restart clash
使用方法
- 作为网关
IP:随便,不冲突就行
网关:固定IP
DNS:固定IP
-
作为代理
-
作为 HTTP 代理
固定IP:HTTP端口
-
作为 SOCKS 代理
固定IP:SOCKS端口
-
作为 HTTP 代理
-
访问配置界面
在任何浏览器中打开:
http://固定IP
Extra
使用清华镜像
在/etc/apt/sources.list
中注释掉原有的 repo,加入:deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi
# deb http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi
在/etc/apt/sources.list.d/raspi.list
中注释掉原有的 repo,加入:deb https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ buster main ui
# deb http://archive.raspberrypi.org/debian/ buster main ui
关闭蓝牙和 Wi-Fi
如果只作为一个网关运行,可以关闭蓝牙和 Wi-Fi,省电的同时纯净无线空间。编辑
/boot/config.txt
中加入以下两行:dtoverlay=pi3-disable-wifi
dtoverlay=pi3-disable-bt
感谢
from https://github.com/cykor/RaspGateway----------
Running Clash on OpenWrt as a transparent proxy
I will revamp this post soon as Clash is going to have major changes.
Download the tools
$ mkdir /etc/clash
$ cd /etc/clash
$ wget https://github.com/Dreamacro/clash/releases/download/v0.13.0/clash-linux-amd64.tar.gz
$ tar -xzvf clash-linux-amd64.tar.gz
$ mv clash-linux-amd64 clash
$ mkdir /etc/unbound
$ opkg update && opkg install unbound luci-app-unbound
Make them into services
/etc/init.d/clash
:#!/bin/sh /etc/rc.common
START=90
USE_PROCD=1
start_service() {
procd_open_instance
procd_set_param command /etc/clash/clash -d /etc/clash
procd_set_param respawn 300 0 5 # threshold, timeout, retry
procd_set_param file /etc/clash/config.yml
procd_set_param stdout 1
procd_set_param stderr 1
procd_set_param pidfile /var/run/clash.pid
procd_close_instance
}
$ chmod +x /etc/init.d/clash
/var/log/messages
.Write Clash Configuration
redir-port: 9090
allow-lan: true
external-controller: 0.0.0.0:6170
dns:
enable: true
ipv6: false
listen: 0.0.0.0:53
enhanced-mode: redir-host
nameserver:
- 127.0.0.1:5353
9090
is the redir port, allow-lan
allows other devices in LAN to access the proxy and external-controller
is the API that we’re gonna use later to control Clash.:53
to unbound (:5353
), which forwards DNS requests to DNSCrypt-proxy (:5678
). DNSCrypt-proxy will then securely get the correct DNS responses using DoH.Configure dnsmasq
# Disable dnsmasq DNS server
$ uci set 'dhcp.@dnsmasq[0].port=0'
# Configure dnsmasq to send a DNS Server DHCP option with its LAN IP
# since it does not do this by default when port is configured.
$ lan_address=$(uci get network.lan.ipaddr)
$ uci add_list "dhcp.lan.dhcp_option=option:dns-server,$lan_address"
$ uci commit
$ service dnsmasq restart
Configure DNSCrypt-proxy
server_names = ['cloudflare', 'google']
listen_addresses = ['0.0.0.0:5678']
fallback_resolver = '119.29.29.29:53'
ignore_system_dns = true
forwarding_rules = 'forwarding-rules.txt'
Configure Unbound
$ wget ftp://FTP.INTERNIC.NET/domain/named.cache -O/etc/unbound/root.hints
$ uci set 'unbound.@unbound[0].manual_conf=1'
119.29.29.29
instead of foreign DNSCrypt-proxy, we’re using https://github.com/felixonmars/dnsmasq-china-list.$ cd
$ git clone https://github.com/felixonmars/dnsmasq-china-list.git
$ cd dnsmasq-china-list
$ make SERVER=119.29.29.29 unbound
$ mkdir /etc/unbound/unbound.conf.d
$ cp accelerated-domains.china.unbound.conf /etc/unbound/unbound.conf.d
include: "/etc/unbound/unbound.conf.d/accelerated-domains.china.unbound.conf"
server:
verbosity: 1
directory: "/etc/unbound"
num-threads: 2
msg-cache-slabs: 1
rrset-cache-slabs: 1
infra-cache-slabs: 1
key-cache-slabs: 1
interface: 127.0.0.1
access-control: 127.0.0.0/8 allow
outgoing-num-tcp: 256
incoming-num-tcp: 1024
outgoing-port-permit: "10240-65335"
outgoing-range: 60
num-queries-per-thread: 30
msg-buffer-size: 8192
infra-cache-numhosts: 200
key-cache-size: 100k
neg-cache-size: 10k
target-fetch-policy: "2 1 0 0 0 0"
harden-large-queries: yes
harden-short-bufsize: yes
port: 5353
so-rcvbuf: 4m
so-sndbuf: 4m
so-reuseport: yes
msg-cache-size: 64m
rrset-cache-size: 128m
cache-max-ttl: 3600
do-ip4: yes
do-ip6: yes
do-udp: yes
do-tcp: yes
tcp-upstream: no
use-syslog: yes
log-queries: no
root-hints: "/etc/unbound/root.hints"
hide-identity: yes
hide-version: yes
identity: ""
version: ""
harden-glue: yes
private-address: 10.0.0.0/8
private-address: 172.16.0.0/12
private-address: 192.168.0.0/16
private-address: 169.254.0.0/16
private-address: fd00::/8
private-address: fe80::/10
private-address: ::ffff:0:0/96
unwanted-reply-threshold: 10000000
do-not-query-localhost: no
prefetch: yes
minimal-responses: no
module-config: "iterator"
forward-zone:
name: "."
forward-addr: 127.0.0.1@5678
Launch the services
$ service clash enable
$ service dnscrypt-proxy enable
$ service unbound enable
$ service dnscrypt-proxy restart
$ service unbound restart
$ service clash restart
Redirect the traffic to Clash
https://ROUTER_IP/cgi-bin/luci/admin/network/firewall/custom
, append the following to the end of rules. Be aware that you need to change YOUR_SSH_PORT
.iptables -t nat -N clash_lan
iptables -t nat -A clash_lan -d 0.0.0.0/8 -j RETURN
iptables -t nat -A clash_lan -d 10.0.0.0/8 -j RETURN
iptables -t nat -A clash_lan -d 127.0.0.0/8 -j RETURN
iptables -t nat -A clash_lan -d 169.254.0.0/16 -j RETURN
iptables -t nat -A clash_lan -d 172.16.0.0/12 -j RETURN
iptables -t nat -A clash_lan -d 192.168.0.0/16 -j RETURN
iptables -t nat -A clash_lan -d 224.0.0.0/4 -j RETURN
iptables -t nat -A clash_lan -d 240.0.0.0/4 -j RETURN
# Disable the proxy for 10.0.0.123
# iptables -t nat -A clash_lan -s 10.0.0.123 -j RETURN
iptables -t nat -A clash_lan -p tcp --dport YOUR_SSH_PORT -j ACCEPT
iptables -t nat -A clash_lan -p tcp --dport 80 -j REDIRECT --to-ports 9090
iptables -t nat -A clash_lan -p tcp --dport 443 -j REDIRECT --to-ports 9090
iptables -t nat -A clash_lan -p tcp --dport 53 -j REDIRECT --to-ports 53
iptables -t nat -A PREROUTING -p tcp -j clash_lan
# Chromecast
iptables -t nat -A PREROUTING -s IP_CIDR_OF_CHROMECAST_IF_YOU_HAVE_ANY -p udp --dport 53 -j REDIRECT --to-ports 53
iptables -t nat -A PREROUTING -s IP_CIDR_OF_CHROMECAST_IF_YOU_HAVE_ANY -p tcp --dport 53 -j REDIRECT --to-ports 53
https://ipinfo.io
to see if it works!Control Clash
external-controller
? We’re gonna make use of it… right now.6170
.Check the logs
$ logread -e clash -f
-------------------------------------------------------
Luci interface for Clash Openwrt.
Luci For Clash
Install
- Upload ipk file to tmp folder
- cd /tmp
- opkg update
- opkg install luci-app-clash_1.6.8_all.ipk
- opkg install luci-app-clash_1.6.8_all.ipk --force-depends
Features
- Support Manually config upload
- GeoIP Database Update
- Iptables udp redirect
- IP Query / Website Access Check
- DNS Forwarding
- Support Trojan
- Support SSR
- Ping Custom proxy servers
- Create v2ray & ssr clash config from subscription url
- Create Custom Clash Config
- Tun Support
- Support Proxy Provider,Game rules & Restore Config Thanks to @vernesong
Dependency
- bash
- coreutils
- coreutils-nohup
- coreutils-base64
- ipset
- iptables
- luci
- luci-base
- wget
- libustream-openssl
- libopenssl
- openssl-util
- curl
- jsonfilter
- ca-certificates
Clash on Other Platforms
- Clash for Windows : A Windows GUI based on Clash
- clashX : A rule based custom proxy with GUI for Mac base on clash
- ClashA : An Android GUI for Clash
- ClashForAndroid : Another Android GUI for Clash
- KoolClash OpenWrt/LEDE : A rule based custom proxy for Koolshare OpenWrt/LEDE based on Clash
OpenClash
使用手册
下载地址
- IPK 前往下载
依赖
- luci
- luci-base
- iptables
- dnsmasq-full
- coreutils
- coreutils-nohup
- bash
- curl
- jsonfilter
- ca-certificates
- ipset
- ip-full
- iptables-mod-tproxy
- kmod-tun(TUN模式)
- luci-compat(Luci-19.07)
编译
# 解压下载好的 SDK
tar xjf OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2
cd OpenWrt-SDK-ar71xx-*
# Clone 项目
mkdir package/luci-app-openclash
cd package/luci-app-openclash
git init
git remote add -f origin https://github.com/vernesong/OpenClash.git
git config core.sparsecheckout true
echo "luci-app-openclash" >> .git/info/sparse-checkout
git pull origin master
git branch --set-upstream-to=origin/master master
# 编译 po2lmo (如果有po2lmo可跳过)
pushd package/luci-app-openclash/luci-app-openclash/tools/po2lmo
make && sudo make install
popd
# 选择要编译的包 LuCI -> Applications -> luci-app-openclash
make menuconfig
# 开始编译
make package/luci-app-openclash/luci-app-openclash/compile V=99
# 您也可以直接拷贝 `luci-app-openclash` 文件夹至其他 `OpenWrt` 项目的 `Package` 目录下随固件编译
许可
- MIT License
- 内核 clash by Dreamacro
- 本项目代码基于 Luci For Clash by frainzy1477
- GEOIP数据库 GeoLite2 by MaxMind
- IP检查 MyIP by SukkaW
- 控制面板 clash-dashboard by Dreamacro
- 控制面板 yacd by haishanh
- lhie1规则 lhie1-Rules by lhie1
- ConnersHua规则 ConnersHua-Rules by ConnersHua
- 游戏规则 SSTap-Rule by FQrabbit
- 订阅转换API Api_Constructor by Fndroid
仅针对 Linux
setup-clash-tun.sh
简单配置 Clash 的 tun 代理
clean-clash-tun.sh
清理 clash 的 tun 代理
setup-clash-cgroup.sh
配置 绕过 Clash 的 应用的 cgroup
clash.service
Clash 的 systemd 服务单元
bypass-proxy
利用 cgroup 使部分进程绕过 Clash
用法 bypass-proxy 命令...
bypass-proxy-pid
同上
用法 bypass-proxy-pid <PID>
from https://github.com/Kr328/kr328-clash-setup-scripts
------
Clash Premiun Installer
Simple clash premiun core installer with full tun support for Linux.
Usage
Install dependencies git, nftables, iproute2
Clone repository
git clone https://github.com/Kr328/clash-premium-installer cd clash-premium-installer
Download clash core link
Extract core and rename it to
./clash
Run Installer
./installer.sh install
Clash Tun Scripts for Linux
Usage
Clone Repo
git clone https://github.com/Kr328/clash-tun-for-linux cd clash-tun-for-linux
Build Clash Binary
Build by install.sh (upstream comzyh/clash)./install.sh build
or just copy one
cp /path/to/clash ./clash
Install
sudo ./install.sh install
Clash的功能强大,除了最基本的分流,屏蔽广告功能,可以通过加载TUN驱动,实现全局代理,有点类似与Netch的功能。
TUN 模式 | Clash for Windows编写,主要针对Windows平台,其他平台请自行研究
操作步骤
Wintun网站下载Wintun的软件包,解压,将文件路径为wintun\bin\amd64\wintun.dll(应该没人是32位系统了吧)复制到Clash的Home Directory目录中。dns:
enable: true
enhanced-mode: redir-host
nameserver:
- 8.8.8.8 # 真实请求DNS,可多设置几个
- 114.114.114.114
# interface-name: WLAN # 出口网卡名称,或者使用下方的自动检测
tun:
enable: true
stack: gvisor # 使用 system 需要 Clash Premium 2021.05.08 及更高版本
dns-hijack:
- 198.18.0.2:53 # 请勿更改
auto-route: true
auto-detect-interface: true # 自动检测出口网卡
复制以上内容填入。
使用
回到主界面点击Mixin,即可启动Tun模式。