Skip to content

blliu6/clash_for_server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Clash For Server

一个面向 Linux/macOS 终端环境的 Clash/Mihomo 本地代理目录,包含:

  • clash 可执行文件
  • config.yaml 代理配置
  • clash_node.py 命令行策略组/节点切换工具
  • install.sh 安装脚本

这个项目的目标很直接:把 Clash 运行目录放到 ~/.clash,然后提供几个常用命令,方便在 shell 里启停代理和切换节点。

项目结构

  • clash: Mihomo/Clash 可执行文件
  • config.yaml: 主配置文件,包含端口、REST API、节点、策略组和规则
  • clash_node.py: 交互式节点切换脚本,支持方向键选择
  • Country.mmdb: GeoIP 数据
  • logs/clash.log: Clash 日志文件
  • install.sh: 安装脚本,会写入 shell 函数到 ~/.zshrc~/.bashrc

安装

git clone https://github.com/blliu6/clash_for_server.git
# 或使用 gitee 镜像(网络无法连接 GitHub 时)
# git clone https://gitee.com/blliu0905/clash_for_server.git

cd clash_for_server
bash install.sh

如果你已经在项目目录下,也可以直接执行:

bash install.sh

安装脚本会做这些事:

  • 将当前目录中的运行文件复制到 ~/.clash
  • 确保 clashclash_node.py 可执行
  • 通过终端交互询问你要使用的本地代理端口
  • 往你的 shell 配置文件写入 clash_onclash_offclash_node

安装完成后,原始仓库目录可以删除;实际运行依赖的是 ~/.clash 下的文件。 安装完成后,请手动将你的私有 config.yaml 放到 ~/.clash/config.yaml

安装完成后执行:

source ~/.zshrc

如果你用的是 Bash,则执行:

source ~/.bashrc

使用说明

开启代理:

clash_on

关闭代理:

clash_off

切换策略组或节点:

clash_node

clash_node 支持:

  • ↑ / ↓ 移动
  • j / k 移动
  • Enter 确认
  • q 退出

配置说明

1. 代理端口

这个开源仓库默认不包含你的私有 config.yaml,因此安装脚本无法预先知道你实际使用的代理端口。

执行 bash install.sh 时,脚本会在终端里询问:

请输入代理端口号 (直接回车将使用默认端口 7890):

你输入的端口会被写入 shell 函数 clash_on,用于设置:

  • http_proxy
  • https_proxy
  • no_proxy

请确保这里填写的端口与后续放到 ~/.clash/config.yaml 中的 mixed-portport 一致。

2. 手动放置配置文件

安装完成后,请手动把你自己的 Clash/Mihomo 配置文件放到:

~/.clash/config.yaml

这是必需步骤;如果没有这个文件,clash_onclash_node 都无法按预期工作。

3. REST API

clash_node.py 依赖 Clash 的 REST API。请确保 config.yaml 中至少有:

external-controller: 127.0.0.1:11314
secret: 'myssr'

其中:

  • external-controller 的端口可以自己设置,不必固定为 11314
  • external-controller 可以写成纯端口,例如:
external-controller: 11314
  • 也可以写成完整地址,例如:
external-controller: 127.0.0.1:11314
  • clash_node.py 读取配置时会兼容这两种写法;如果读到的是纯端口,会自动按 127.0.0.1:<port> 处理
  • secret 请显式设置为:
secret: 'myssr'
  • secret 要与脚本访问 Clash API 时使用的口令保持一致

4. 策略组切换的含义

  • 切换 Proxy 会影响默认代理出口
  • 切换 TelegramOpenAINetflix 等策略组,只影响命中对应规则的流量

例如:

  • Proxy -> 美国 中继 04 - 1倍率 会改变 curl https://ifconfig.me 的出口 IP
  • Telegram -> 新加坡 不会改变默认出口,只会影响 Telegram 相关流量

验证是否生效

查看 Clash 是否启动:

ps -ef | grep clash

查看默认代理出口当前节点:

curl -s -H 'Authorization: Bearer myssr' http://127.0.0.1:11314/proxies/Proxy

查看当前出口 IP:

curl https://ifconfig.me

如果要验证某个业务策略组是否切换成功,例如 Telegram

curl -s -H 'Authorization: Bearer myssr' http://127.0.0.1:11314/proxies/Telegram

常见问题

安装时为什么会询问端口

因为仓库本身不携带你的私有订阅配置,所以安装脚本不知道你最终 config.yaml 里使用的是哪个本地代理端口,只能在安装时向你询问。

clash_node 提示连接 Clash API 失败

先确认 Clash 已启动:

clash_on

再检查 config.yaml 中的 external-controller 是否写成了:

external-controller: 127.0.0.1:11314

你也可以只写端口,例如:

external-controller: 11314

脚本会自动按 127.0.0.1:11314 处理,但为了避免不同 Clash / Mihomo 版本在监听行为上的差异,仍然建议优先写成完整地址。

切换了 Telegram / OpenAI,但 ifconfig.me 没变化

这是正常现象。ifconfig.me 走的是默认出口,一般受 Proxy 组影响,不受业务策略组直接影响。

日志

Clash 日志默认写入:

~/.clash/logs/clash.log

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors