|
1 | 1 | # SingBox for Magisk |
2 | 2 |
|
3 | | -**使用方式** |
| 3 | +A Magisk module that provides transparent proxy functionality for Android devices using sing-box. |
4 | 4 |
|
5 | | -sing-box 配置文件 config.json 放在 `/data/adb/singbox/` 目录, 重启手机模块生效 |
| 5 | +## 快速开始 |
6 | 6 |
|
7 | | -**黑白名单** |
| 7 | +### 1. 安装模块 |
8 | 8 |
|
9 | | -- `/data/adb/singbox/include.list` 文件中的应用包名走代理 |
| 9 | +在 Magisk Manager 中安装本模块,重启设备。 |
10 | 10 |
|
11 | | -- `/data/adb/singbox/exclude.list` 文件中的应用包名不走代理 |
| 11 | +### 2. 配置文件 |
12 | 12 |
|
13 | | -`exclude.list` 优先级高于 `include.list`: |
| 13 | +将 sing-box 配置文件放置到: |
| 14 | +``` |
| 15 | +/data/adb/singbox/config.json |
| 16 | +``` |
| 17 | + |
| 18 | +### 3. 启动服务 |
| 19 | + |
| 20 | +模块会在开机时自动启动,也可以手动管理: |
| 21 | + |
| 22 | +```bash |
| 23 | +# 启动服务 |
| 24 | +/data/adb/singbox/scripts/service.sh start |
| 25 | + |
| 26 | +# 停止服务 |
| 27 | +/data/adb/singbox/scripts/service.sh stop |
| 28 | + |
| 29 | +# 重启服务 |
| 30 | +/data/adb/singbox/scripts/service.sh restart |
| 31 | + |
| 32 | +# 查看状态 |
| 33 | +/data/adb/singbox/scripts/service.sh status |
| 34 | + |
| 35 | +# 健康检查 |
| 36 | +/data/adb/singbox/scripts/service.sh health |
| 37 | + |
| 38 | +# 强制停止 |
| 39 | +/data/adb/singbox/scripts/service.sh force-stop |
| 40 | +``` |
| 41 | + |
| 42 | +## 配置说明 |
| 43 | + |
| 44 | +### 网络模式 |
| 45 | + |
| 46 | +编辑 `/data/adb/singbox/settings.ini`: |
| 47 | + |
| 48 | +```ini |
| 49 | +# 启用/禁用 IPv6 |
| 50 | +ipv6="false" |
| 51 | + |
| 52 | +# 网络模式: redirect / tproxy / tun |
| 53 | +network_mode="tproxy" |
| 54 | + |
| 55 | +# 热点接口(支持无线热点代理) |
| 56 | +ap_list=("ap+" "wlan+" "rndis+" "swlan+" "ncm+" "rmnet+") |
| 57 | +``` |
| 58 | + |
| 59 | +**网络模式说明:** |
| 60 | +- **tproxy** (推荐): TCP + UDP 透明代理,性能最佳 |
| 61 | +- **redirect**: TCP + UDP(直连) 重定向代理,兼容性最好 |
| 62 | +- **tun**: TCP + UDP 虚拟网卡模式,自动路由 |
| 63 | + |
| 64 | +**重要:** 确保 `settings.ini` 中的 `network_mode` 与 `config.json` 中的 inbound 类型匹配: |
| 65 | +- 如果 config.json 中有 `"type": "tun"` 的 inbound,使用 `network_mode="tun"` |
| 66 | +- 如果 config.json 中有 `"type": "tproxy"` 的 inbound,使用 `network_mode="tproxy"` |
| 67 | +- 如果 config.json 中有 `"type": "redirect"` 的 inbound,使用 `network_mode="redirect"` |
| 68 | + |
| 69 | +### 应用过滤(黑白名单) |
| 70 | + |
| 71 | +#### 白名单模式(仅代理指定应用) |
| 72 | + |
| 73 | +编辑 `/data/adb/singbox/include.list`,添加需要代理的应用包名: |
| 74 | +``` |
| 75 | +com.android.chrome |
| 76 | +com.google.android.youtube |
| 77 | +``` |
| 78 | + |
| 79 | +#### 黑名单模式(排除指定应用) |
| 80 | + |
| 81 | +编辑 `/data/adb/singbox/exclude.list`,添加不需要代理的应用包名: |
| 82 | +``` |
| 83 | +com.tencent.mm # 微信 |
| 84 | +com.tencent.mobileqq # QQ |
| 85 | +com.eg.android.AlipayGphone # 支付宝 |
| 86 | +``` |
| 87 | + |
| 88 | +**优先级:** `exclude.list` > `include.list` |
14 | 89 |
|
15 | 90 | 如果一个应用的包名同时出现在 `exclude.list` 和 `include.list` 中,这个应用不走代理。 |
16 | 91 |
|
| 92 | +## 高级功能 |
| 93 | + |
| 94 | +### 1. 日志管理 |
| 95 | + |
| 96 | +日志文件位置: |
| 97 | +``` |
| 98 | +/data/adb/singbox/logs/run.log # 运行日志 |
| 99 | +/data/adb/singbox/logs/box.log # sing-box 日志 |
| 100 | +``` |
| 101 | + |
| 102 | +日志会自动轮转(超过 10MB 时),保留最近 3 个备份。 |
| 103 | + |
| 104 | +实时查看日志: |
| 105 | +```bash |
| 106 | +tail -f /data/adb/singbox/logs/box.log |
| 107 | +``` |
| 108 | + |
| 109 | +### 2. 健康检查 |
| 110 | + |
| 111 | +```bash |
| 112 | +/data/adb/singbox/scripts/service.sh health |
| 113 | +``` |
| 114 | + |
| 115 | +检查内容: |
| 116 | +- 进程运行状态 |
| 117 | +- 配置文件完整性 |
| 118 | +- 日志错误统计 |
| 119 | +- 网络连通性 |
| 120 | + |
| 121 | +### 3. 手动 iptables 管理 |
| 122 | + |
| 123 | +```bash |
| 124 | +# 应用 tproxy 规则 |
| 125 | +/data/adb/singbox/scripts/iptables.sh tproxy |
| 126 | + |
| 127 | +# 应用 redirect 规则 |
| 128 | +/data/adb/singbox/scripts/iptables.sh redirect |
| 129 | + |
| 130 | +# 应用 tun 规则 |
| 131 | +/data/adb/singbox/scripts/iptables.sh tun |
| 132 | + |
| 133 | +# 清理所有规则 |
| 134 | +/data/adb/singbox/scripts/iptables.sh clear |
| 135 | +``` |
| 136 | + |
| 137 | +### 4. 验证代码结构 |
| 138 | + |
| 139 | +```bash |
| 140 | +/data/adb/singbox/scripts/validate.sh |
| 141 | +``` |
| 142 | + |
| 143 | +检查所有脚本文件和函数是否完整。 |
| 144 | + |
| 145 | +## 常见问题 |
| 146 | + |
| 147 | +### Q1: 服务无法启动 |
| 148 | + |
| 149 | +**检查步骤:** |
| 150 | + |
| 151 | +1. 验证配置文件: |
| 152 | +```bash |
| 153 | +/data/adb/singbox/bin/sing-box check -D /data/adb/singbox/ -C /data/adb/singbox |
| 154 | +``` |
| 155 | + |
| 156 | +2. 查看日志: |
| 157 | +```bash |
| 158 | +cat /data/adb/singbox/logs/box.log |
| 159 | +``` |
| 160 | + |
| 161 | +3. 检查 network_mode 是否与 config.json 的 inbound 类型匹配 |
| 162 | +4. 检查 TUN 设备(如果使用 tun 模式): |
| 163 | +```bash |
| 164 | +ls -l /dev/net/tun |
| 165 | +``` |
| 166 | + |
| 167 | +### Q2: 某些应用无法联网 |
| 168 | + |
| 169 | +1. 检查应用是否在 exclude.list 中 |
| 170 | +2. 验证 iptables 规则: |
| 171 | +```bash |
| 172 | +iptables -t mangle -L -n -v |
| 173 | +``` |
| 174 | + |
| 175 | +### Q3: IPv6 不工作 |
| 176 | + |
| 177 | +1. 确认 settings.ini 中 `ipv6="true"` |
| 178 | +2. 检查 IPv6 路由规则: |
| 179 | +```bash |
| 180 | +ip -6 rule list |
| 181 | +ip -6 route show table 2024 |
| 182 | +``` |
| 183 | + |
| 184 | +### Q4: 代理速度慢 |
| 185 | + |
| 186 | +1. 检查内存使用: |
| 187 | +```bash |
| 188 | +/data/adb/singbox/scripts/service.sh status |
| 189 | +``` |
| 190 | + |
| 191 | +2. 检查日志中的错误: |
| 192 | +```bash |
| 193 | +grep ERROR /data/adb/singbox/logs/box.log |
| 194 | +``` |
| 195 | + |
| 196 | +3. 尝试切换网络模式(tproxy 性能最佳) |
| 197 | + |
| 198 | +### Q5: 模块更新后配置丢失 |
| 199 | + |
| 200 | +模块会自动备份 `config.json`,但建议手动备份: |
| 201 | +```bash |
| 202 | +cp /data/adb/singbox/config.json /sdcard/backup/ |
| 203 | +``` |
| 204 | + |
| 205 | +## 故障排查 |
| 206 | + |
| 207 | +### 收集诊断信息 |
| 208 | + |
| 209 | +```bash |
| 210 | +# 1. 服务状态 |
| 211 | +/data/adb/singbox/scripts/service.sh status |
| 212 | + |
| 213 | +# 2. 健康检查 |
| 214 | +/data/adb/singbox/scripts/service.sh health |
| 215 | + |
| 216 | +# 3. 最近日志 |
| 217 | +tail -50 /data/adb/singbox/logs/box.log |
| 218 | + |
| 219 | +# 4. iptables 规则 |
| 220 | +iptables -t mangle -L -n -v | head -50 |
| 221 | + |
| 222 | +# 5. 路由规则 |
| 223 | +ip rule list |
| 224 | +ip route show table 2024 |
| 225 | + |
| 226 | +# 6. 进程信息 |
| 227 | +ps | grep sing-box |
| 228 | +``` |
| 229 | + |
| 230 | +### 完全重置 |
| 231 | + |
| 232 | +如果遇到无法解决的问题: |
| 233 | + |
| 234 | +```bash |
| 235 | +# 1. 停止服务 |
| 236 | +/data/adb/singbox/scripts/service.sh force-stop |
| 237 | + |
| 238 | +# 2. 清理规则 |
| 239 | +/data/adb/singbox/scripts/iptables.sh clear |
| 240 | + |
| 241 | +# 3. 备份配置 |
| 242 | +cp /data/adb/singbox/config.json /sdcard/backup/ |
| 243 | + |
| 244 | +# 4. 在 Magisk Manager 中重新安装模块 |
| 245 | +``` |
| 246 | + |
| 247 | +## 性能优化建议 |
| 248 | + |
| 249 | +### 1. 网络模式选择 |
| 250 | +- 优先使用 **tproxy** 模式(性能最佳) |
| 251 | +- 如果遇到兼容性问题,使用 redirect 模式 |
| 252 | +- tun 模式适合需要自动路由的场景 |
| 253 | + |
| 254 | +### 2. 应用过滤 |
| 255 | +- 使用黑名单模式(exclude.list)性能更好 |
| 256 | +- 排除不需要代理的国内应用 |
| 257 | +- 减少规则数量 |
| 258 | + |
| 259 | +### 3. DNS 优化 |
| 260 | +- 在 config.json 中配置 FakeIP 模式 |
| 261 | +- 使用快速的 DNS 服务器(如阿里 DNS: 223.5.5.5) |
| 262 | + |
| 263 | +### 4. 资源监控 |
| 264 | +- 定期检查内存使用 |
| 265 | +- 清理旧日志文件(自动轮转) |
| 266 | +- 监控磁盘空间 |
| 267 | + |
| 268 | +## 进阶配置 |
| 269 | + |
| 270 | +### 自定义常量 |
| 271 | + |
| 272 | +编辑 `/data/adb/singbox/scripts/constants.sh`: |
| 273 | + |
| 274 | +```bash |
| 275 | +# 日志轮转配置 |
| 276 | +LOG_MAX_SIZE=10485760 # 10MB |
| 277 | +LOG_MAX_BACKUPS=3 |
| 278 | + |
| 279 | +# 进程检查配置 |
| 280 | +MAX_RETRIES=10 |
| 281 | +RETRY_INTERVAL=0.5 |
| 282 | + |
| 283 | +# 文件描述符限制 |
| 284 | +FILE_DESCRIPTOR_LIMIT=1000000 |
| 285 | +``` |
| 286 | + |
| 287 | +## 安全建议 |
| 288 | + |
| 289 | +1. **定期备份配置文件** |
| 290 | +2. **不要在 config.json 中存储明文密码**(使用环境变量或密钥文件) |
| 291 | +3. **定期更新 sing-box 版本** |
| 292 | +4. **监控日志中的异常活动** |
| 293 | +5. **使用强密码保护订阅链接** |
| 294 | + |
| 295 | +## 贡献与反馈 |
| 296 | + |
| 297 | +提交问题时请附带: |
| 298 | +- 设备信息(型号、Android 版本) |
| 299 | +- Magisk 版本 |
| 300 | +- 日志文件 |
| 301 | +- 重现步骤 |
| 302 | + |
| 303 | +## 更新日志 |
| 304 | + |
| 305 | +### v1.3.0 |
| 306 | +- ✨ 重构代码结构,模块化设计 |
| 307 | +- ✨ 新增健康检查功能 |
| 308 | +- ✨ 新增日志轮转功能 |
| 309 | +- ✨ 完善 IPv6 支持 |
| 310 | +- ✨ 改进错误处理和资源清理 |
| 311 | +- ✨ 新增配置验证功能 |
| 312 | +- ✨ 自动修复文件权限 |
| 313 | +- ✨ 友好的配置错误提示 |
| 314 | +- 🐛 修复策略路由清理不完整的问题 |
| 315 | +- 🐛 修复 IPv6 规则缺失的问题 |
| 316 | +- 🐛 修复 Android shell 兼容性问题 |
| 317 | +- ⚡ 优化连接跟踪性能 |
| 318 | +- 📝 完善文档和注释 |
| 319 | + |
| 320 | +## 许可证 |
| 321 | + |
| 322 | +[查看 LICENSE 文件] |
| 323 | + |
| 324 | +## 致谢 |
| 325 | + |
| 326 | +- [sing-box](https://sing-box.sagernet.org/) 项目 |
| 327 | +- [Magisk](https://github.com/topjohnwu/Magisk) 项目 |
| 328 | +- 所有贡献者 |
0 commit comments