取消sudo权限要求

This commit is contained in:
Lihatoo 2026-02-11 16:19:50 +08:00
parent 2f13317e6e
commit 6feea6391e
2 changed files with 409 additions and 384 deletions

211
README.md
View file

@ -1,189 +1,74 @@
# mh (mihomo wrapper)
基于 mihomo 的纯终端控制梯子工具,适合简单全局控制与分享。
A terminal-only proxy wrapper based on mihomo, designed for simple global control and sharing.
>基于 mihomo 的纯ubuntu终端控制梯子工具适合简单全局控制与。
A small Bash wrapper around `mihomo` to manage profiles, start/stop the service, switch nodes, and toggle TUN.
mihomo: https://github.com/mihomo/mihomo
重要:必须使用 `sudo` 启动。
Important: You must run this script with `sudo`.
# 部署
Repo / 仓库:
将目录下的`mh`复制到`/usr/local/bin`下,并添加执行权限。
```bash
sudo chmod +x mh # 给予执行权限
sudo setcap cap_net_admin,cap_net_raw+ep /home/lht/bfile/mihomo/mihomo # 给予网络tun权限
```
https://github.com/Lihatoo/mh
然后在任何终端都可以使用`mh`命令
# 使用
## 导入
> 支持两种导入方法
```bash
mh add name path
mh add name url
mh select name # 选择一个配置
```
分别是订阅链接或者是某个已配置好的`yaml`文件
得到如下的项目结构
```json
tnt
├── cache.db
├── config.yaml # 这是mh生成的配置文件用于启动mihomo
├── geoip.metadb # 这是运行中生成的文件
├── mihomo.log # 这是日志可以用config.yaml 的log_file 指定error,warning,info,debug
└── providers
└── tnt.yaml # 这是提供节点的文件,来自你的订阅链接,tnt为你输入的名称
current.profile # 保存是否需要tun配置
run.pid # 运行时保存的进程id目前只能支持起一个mihomo
```
## What it does / 功能
## 换节点
- Creates per-profile directories under `MIHOMO_ROOT/<name>/` with generated `config.yaml`
- Starts/stops `mihomo` and records PID in `run.pid`
- Switches nodes by keyword and optionally ranks by delay
- Toggles TUN in the current profile and restarts
- Exports/unsets HTTP proxy environment variables
- 在 `MIHOMO_ROOT/<name>/` 下创建 profile 目录并生成 `config.yaml`
- 启动/停止 `mihomo`PID 记录在 `run.pid`
- 通过关键词切换节点,并可按延迟排序
- 切换 TUN 并重启
- 导出/取消 HTTP 代理环境变量
## Requirements / 依赖
- Linux with `bash`
- `mihomo` binary in `MIHOMO_ROOT/mihomo`
- Tools: `ss`, `yq`, `jq`, `curl`, `find`, `ip`
- Linux + `bash`
- `mihomo` 二进制放在 `MIHOMO_ROOT/mihomo`
- 工具:`ss`, `yq`, `jq`, `curl`, `find`, `ip`
## Quick start / 快速开始
1. Set the root path / 设置根路径
Open `mh` and update this line to the absolute path of this repo:
打开 `mh`,把下面这行改成仓库的绝对路径:
```
MIHOMO_ROOT=/path/to/this/repo
```bash
mh -l # 列出搜索可选择节点
mh name # 搜索并列表选择节点
```
2. Make the script executable / 授权可执行
## 其他操作
```
chmod +x mh
```bash
mh tun on/off # 开启/关闭tun
mh -h # 帮助
mh # 快速预览信息
mh db # 一些测试debug专用
```
3. Put `mh` in `/usr/local/bin` for direct use / 放到 `/usr/local/bin` 以便直接使用
# 错误
> 主要错误都可以看看`mihomo.log`文件,从其中分析
## 网络dns解析失败
> 节点正常搜索,测速正常,可是无法使用(浏览器,网站)
> 查看日志,前面下载正常,就是网络解析不了
```
sudo cp mh /usr/local/bin/mh
```
`mh db`选择7 ,看看有几个dns
4. Start with `sudo` (required) / 必须用 `sudo` 启动
```
sudo mh
```
## Usage / 用法
Create a profile from a subscription URL (quote the URL is recommended):
从订阅 URL 创建 profile建议加双引号
```
sudo mh add <name> "<url>"
```
Create a profile from a local file:
从本地文件创建 profile
```
sudo mh add <name> /path/to/subscription.yaml
```
Start/select a profile:
启动/切换 profile
```
sudo mh select
sudo mh select <name>
```
Stop:
停止:
```
sudo mh end
```
Toggle proxy env vars (current shell only):
导出/取消代理环境变量(只影响当前 shell
```
sudo -E mh on
sudo -E mh off
```
List nodes in the main group:
列出主组节点:
```
sudo mh -l
```
Switch node by keyword and rank by delay:
按关键词切换并按延迟排序:
```
sudo mh 美国
sudo mh HK
```
Toggle TUN (requires sudo):
切换 TUN需要 sudo
```
sudo mh tnu on
sudo mh tnu off
sudo mh tnu toggle
```
Run connectivity test:
网络连通性测试:
```
sudo mh test
```
Show help:
查看帮助:
```
sudo mh --help
```
## Notes / 说明
- Default ports: `mixed-port=7890`, API `external-controller=127.0.0.1:9090`.
- If you use `sudo` and want `HTTP_PROXY/HTTPS_PROXY` to persist, use `sudo -E`.
- When you run `mh` with no arguments, it checks ports, environment, TUN status, and current node delay.
- 默认端口:`mixed-port=7890`API `external-controller=127.0.0.1:9090`
- 若想保留 `HTTP_PROXY/HTTPS_PROXY`,请用 `sudo -E`
- 不带参数运行 `mh` 会检查端口、环境、TUN 状态和当前节点延迟。
## Template / 模板
Use `profile_template/` as a sanitized example of the basic profile format.
可参考 `profile_template/` 作为去隐私后的基础配置格式示例。
问我这里显示有两个dns也就是说错误的ip解析可能来自另一个干扰的`tailscale0`(我这里是这个)
## Files created / 生成文件
- `current.profile`: name of the active profile / 当前 profile 名称
- `run.pid`: PID of the running `mihomo` process / 运行中的 PID
- `<profile>/mihomo.log`: log output for each profile / 日志文件
## Example session / 示例
```
# add profile
sudo mh add test "https://example.com/sub.yaml"
# start/select
sudo mh select test
# switch node by keyword
sudo mh 日本
```
## Links / 相关链接
Official mihomo repo:
官方 mihomo 仓库:
```
https://github.com/MetaCubeX/mihomo
```