MCPcopy Index your code
hub / github.com/djkcyl/mihomo-connections-tracker

github.com/djkcyl/mihomo-connections-tracker @v0.1.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.1.1 ↗ · + Follow
12 symbols 14 edges 1 files 5 documented · 42%

Browse by type

Functions 7 Types & classes 5
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

mihomo-connections-tracker

一个用于实时监控并记录 mihomo(原Clash)网络连接信息的工具,将连接数据持久化存储到SQLite数据库。

功能特性

  • 实时监控mihomo API的connections数据
  • 自动维护连接生命周期(新建/关闭检测)
  • 流量变化追踪与持久化存储
  • 支持以下连接元数据记录:
  • 网络类型(TCP/UDP)
  • 源/目的IP和端口
  • GEOIP信息
  • 代理链路径
  • 匹配规则
  • 进程信息
  • 入站配置
  • DNS信息等
  • 支持Token认证
  • 自动重连机制
  • 优雅停机处理

安装使用

前置要求

  • Rust 1.70+ 环境
  • mihomo API服务已启用

安装步骤

  1. 克隆仓库:
git clone https://github.com/djkcyl/mihomo-connections-tracker.git
cd mihomo-connections-tracker
  1. 编译项目:
cargo build --release

运行命令

./target/release/mihomo-connections-tracker \
    --host 127.0.0.1 \
    --port 9090 \
    --token your_api_token

命令行参数

参数 描述 默认值
--host API服务器地址 127.0.0.1
--port API端口号 9090
--token 认证令牌 空字符串

数据库结构

数据存储在connections.db文件中,表结构如下:

CREATE TABLE connections (
    id TEXT PRIMARY KEY,             -- 连接唯一标识
    conn_download INTEGER,           -- 累计下载字节
    conn_upload INTEGER,             -- 累计上传字节
    last_updated TEXT,               -- 最后更新时间(ISO8601)
    start TEXT,                      -- 连接开始时间(ISO8601)
    network TEXT,                    -- 网络类型(tcp/udp)
    type TEXT,                       -- 连接类型
    source_ip TEXT,                  -- 源IP地址
    destination_ip TEXT,             -- 目的IP地址
    source_geoip TEXT,               -- 源IP地理信息(JSON)
    destination_geoip TEXT,          -- 目的IP地理信息(JSON)
    -- 其余字段详见metadata结构...
);

实现细节

  1. 连接管理
  2. 使用WebSocket长连接接收实时数据
  3. 通过HashSet比较检测连接变化
  4. 流量变化检测使用缓存机制

  5. 数据持久化

  6. 使用SQLx进行异步数据库操作
  7. Upsert操作保证数据更新原子性
  8. 每200ms批量提交变更

  9. 异常处理

  10. 自动重连机制
  11. Ctrl+C信号处理
  12. 连接超时控制

  13. 日志输出

  14. 新连接建立时打印详细信息
  15. 连接关闭时显示流量统计
  16. 每次更新显示变更摘要

Core symbols most depended-on inside this repo

Shape

Class 5
Method 4
Function 3

Languages

Rust100%

Modules by API surface

src/main.rs12 symbols

For agents

$ claude mcp add mihomo-connections-tracker \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page