前言
简介
Clash
是一款用 Go
开发的支持 Linux
/MacOS
/Windows
等多平台的代理工具,支持 ss
/v2ray
(不支持 ssr
),支持规则分流(类似于 Surge 的配置)。
项目地址:
- 原项目:Clash
- Clash for macOS:ClashX
- Clash for Windows:Clash for Windows
本文测试用ip和端口
- 例子中使用到的本机ip为:192.168.1.230
- clash中利用到的端口:http:7890 socks: 7891 redir:7892
- 测试结果:群晖,vm ubuntu18,pve ubuntu18,树莓派3b/3b+ 通过
安装clash
1. 设置dns
#安装需要用的组件
apt -y install iptables-persistent net-tools curl vim
#编辑dns文件,取消53端口被占用的情况
vim /etc/systemd/resolved.conf
#最后一行找到DNSStubListener 取消注释并且修为DNSStubListener=no
DNSStubListener=no
2. 下载安装clash
#创建目录
mkdir clash
#进去目录
cd clash
#命令下载
wget https://github.com/Dreamacro/clash/releases/download/v0.15.0/clash-linux-amd64-v0.15.0.gz
#解压
gunzip clash-linux-amd64-v0.15.0.gz
#修改名称
mv clash-linux-amd64-v0.15.0 clash
#赋予clash权限
chmod +x clash
#第一次启动,并且在指定目录下运行
./clash -d .
#他会提示没有config.yml文件,我们从本地上传到服务器上,下面是用的本地命令上传。你也可以使用sftp工具
scp /sandyliao/Desktop/config.yml root@10.10.10.10:/root/clash
3. 编辑config.yml文件
port: 7890
socks-port: 7891
redir-port: 7892
allow-lan: true
mode: Rule
log-level: info
external-controller: '0.0.0.0:6170'
secret: ''
dns:
enable: true
ipv6: false
listen: 0.0.0.0:53
#enhanced-mode: fake-ip
enhanced-mode: redir-host
nameserver:
- 114.114.114.114
- 223.5.5.5
- tls://dns.rubyfish.cn:853
fallback:
- 114.114.114.114
- tls://dns.rubyfish.cn:853
- 8.8.8.8
Proxy:
局域网转发:这里面有几个注意事项:
1.做透明代理,必须启用redir-port,也就是clash透明代理的端口。
2.做网关设备,你就必须得让其他局域网的机器能够连接到clash,所以allow-lan必须打开。
3.必须打开clash的DNS功能,并且使用redir-host模式,最重要的是你还需要在fallback:后面加上一个支持DoT的DNS。比如我这边是使用的dns.rubyfish.cn:853,如果你用普通的DNS无非解决域名被污染的问题。
4.设置端口转发
配置文件弄好了之后,现在我们需要把机器所有的流量都转发到clash的透明代理端口上面,当然192.168.0.0这个网段的流量我们不做转发:
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 7892
iptables -t nat -A PREROUTING -p tcp -j Clash
iptables -t nat -A PREROUTING -p tcp -j REDIRECT --to-ports 7892
永久保存iptables规则:
netfilter-persistent save
5.开机运行
1. 利用screen来后台运行
首先安装screen
apt install screen
screen -S clash
./clash -d .
2. 设置rc-local
设置开机启用,仅限ubuntu18版本
#首先检查rc-local是不是启动了,如果启动最后完全还要restart,这里的坑,sandy踩了半个小时
systemctl status rc-local.service
vim /lib/systemd/system/rc-local.service
#最下加入
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
3. 编辑rc.local
vim /etc/rc.local
#加入下面代码
#!/bin/bash
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
bash /root/start.sh
exit 0
4. 编辑start.sh文件
vim /root/start.sh
#!/bin/bash
screen_name="clash"
screen -dmS $screen_name
screen -x -S $screen_name -p 0 -X stuff $'/root/clash/clash -d /root/clash &\n'
给予权限
chmod +x /etc/rc.local
5. 开启服务
systemctl enable rc-local
启动服务并检查状态
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
重新启动服务器
reboot
检测服务器是不是已经启动
sudo systemctl status rc-local.service
输出文字,字样就成功启动
Active: active (running) since Fri 2019-06-07 03:25:59 UTC; 19s ago
screen -ls
输出文字,字样就成功启动
There is a screen on:
168.clash (06/08/19 07:14:04) (Detached)
1 Socket in /run/screen/S-root.
安装WEBUI:yacd
配置yacd
我们来配置yacd需要的组件,直接在这台旁路由上安装Node.js/Yarn:
apt -y install git build-essential
curl -sL https://deb.nodesource.com/setup_12.x | bash -
apt -y install nodejs
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
apt -y update && apt -y install yarn
安装nginx
apt -y install nginx unzip
nginx安装完成之后把nginx的这个引用配置文件改个名:
mv /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/default.bak
新建一个nginx的conf:
vim /etc/nginx/conf.d/yacd.conf
配置如下:
server {
listen 80;
server_name 192.168.0.233;
root /usr/share/nginx/html/yacd;
index index.html;
}
安装yacd
git clone https://github.com/haishanh/yacd.git
cd yacd
yarn
yarn build
cp -r public/. /usr/share/nginx/html/yacd
最后启动nginx:
systemctl restart nginx
systemctl start nginx
systemctl enable nginx
客户端设置
1. 手机和wifi在局域网的情况下设置
#例子
ip:192.168.1.211
掩码:255.255.255.0
网关:192.168.1.230
dns:192.168.1.230
无wifi,使用4g的情况下!使用frpc端
在frpc下增加下面一段http的透明代理
[clash]
type = tcp
local_ip =192.168.1.230
local_port = 7890
remote_port = 7890