| Crates.io | mihomo-rs |
| lib.rs | mihomo-rs |
| version | 1.3.0 |
| created_at | 2025-09-04 05:10:44.269301+00 |
| updated_at | 2026-01-15 05:28:29.523729+00 |
| description | A Rust SDK and CLI tool for mihomo proxy management with service lifecycle management, configuration handling, and real-time monitoring |
| homepage | https://github.com/DINGDANGMAOUP/mihomo-rs |
| repository | https://github.com/DINGDANGMAOUP/mihomo-rs |
| max_upload_size | |
| id | 1823703 |
| size | 388,653 |
English | įŽäŊ䏿
A Rust SDK and CLI tool for mihomo proxy management with service lifecycle management, configuration handling, and real-time monitoring.
Add to your Cargo.toml:
[dependencies]
mihomo-rs = "*"
cargo install mihomo-rs
use mihomo_rs::{Channel, ConfigManager, MihomoClient, ProxyManager, ServiceManager, VersionManager, ConnectionManager, Result};
#[tokio::main]
async fn main() -> Result<()> {
// 1. Install mihomo
let vm = VersionManager::new()?;
vm.install_channel(Channel::Stable).await?;
// 2. Setup configuration
let cm = ConfigManager::new()?;
cm.ensure_default_config().await?;
let controller_url = cm.ensure_external_controller().await?;
// 3. Start service
let binary = vm.get_binary_path(None).await?;
let config = cm.get_current_path().await?;
let sm = ServiceManager::new(binary, config);
sm.start().await?;
// 4. Use proxy manager
let client = MihomoClient::new(&controller_url, None)?;
let pm = ProxyManager::new(client.clone());
// List proxy groups
let groups = pm.list_groups().await?;
for group in groups {
println!("{}: {} ({})", group.name, group.now, group.group_type);
}
// Switch proxy
pm.switch("GLOBAL", "proxy-name").await?;
// 5. Monitor connections
let conn_mgr = ConnectionManager::new(client.clone());
// List active connections
let connections = conn_mgr.list().await?;
println!("Active connections: {}", connections.len());
// Filter connections by host
let filtered = conn_mgr.filter_by_host("example.com").await?;
// Close specific connection
if let Some(conn) = connections.first() {
conn_mgr.close(&conn.id).await?;
}
// 6. Stream real-time traffic
let mut traffic_rx = client.stream_traffic().await?;
while let Some(traffic) = traffic_rx.recv().await {
println!("Upload: {} KB/s, Download: {} KB/s",
traffic.up / 1024, traffic.down / 1024);
}
Ok(())
}
# Install mihomo
mihomo-rs install stable
# Start service
mihomo-rs start
# List proxies
mihomo-rs proxy list
# Switch proxy
mihomo-rs proxy switch GLOBAL proxy-name
# Stream logs (with level filter)
mihomo-rs logs --level info
# Stream traffic statistics
mihomo-rs traffic
# Show memory usage
mihomo-rs memory
# List active connections
mihomo-rs connection list
# Show connection statistics
mihomo-rs connection stats
# Stream connections in real-time
mihomo-rs connection stream
# Close specific connection
mihomo-rs connection close <connection-id>
# Close all connections
mihomo-rs connection close-all --force
The examples/ directory includes comprehensive examples:
Run any example with:
cargo run --example hello_mihomo
See examples/README.md for detailed documentation.
mihomo-rs/
âââ src/
â âââ core/ # Core HTTP/WebSocket client and types
â â âââ client.rs # MihomoClient (HTTP + WebSocket)
â â âââ types.rs # Data structures
â â âââ error.rs # Error types
â â âââ port.rs # Port utilities
â â âââ home.rs # Home directory management
â âââ version/ # Version management
â â âââ manager.rs # VersionManager
â â âââ channel.rs # Channel (Stable/Beta/Nightly)
â â âââ download.rs # Binary downloader
â âââ config/ # Configuration management
â â âââ manager.rs # ConfigManager
â â âââ profile.rs # Profile struct
â âââ service/ # Service lifecycle
â â âââ manager.rs # ServiceManager
â â âââ process.rs # Process utilities
â âââ proxy/ # Proxy operations
â â âââ manager.rs # ProxyManager
â â âââ test.rs # Delay testing
â âââ connection/ # Connection management
â â âââ manager.rs # ConnectionManager
â âââ cli/ # CLI application
âââ examples/ # 31 comprehensive examples
âââ tests/ # Integration tests
| Module | Description |
|---|---|
MihomoClient |
HTTP/WebSocket client for mihomo API |
VersionManager |
Install and manage mihomo versions |
ConfigManager |
Manage configuration profiles |
ServiceManager |
Control service lifecycle |
ProxyManager |
High-level proxy operations |
ConnectionManager |
Monitor and manage active connections |
| Type | Description |
|---|---|
Version |
Mihomo version information |
ProxyNode |
Individual proxy node |
ProxyGroup |
Proxy group (Selector, URLTest, etc.) |
TrafficData |
Upload/download statistics |
MemoryData |
Memory usage information |
Channel |
Release channel (Stable/Beta/Nightly) |
Connection |
Active connection information |
ConnectionSnapshot |
Real-time connections snapshot |
ConnectionMetadata |
Connection metadata (source, destination, process, etc.) |
// Convenience functions for common operations
use mihomo_rs::{install_mihomo, start_service, stop_service, switch_proxy};
// Install mihomo
install_mihomo(None).await?; // Latest stable
// Service management
start_service(&config_path).await?;
stop_service(&config_path).await?;
// Proxy switching
switch_proxy("GLOBAL", "proxy-name").await?;
mihomo-rs stores data in ~/.config/mihomo-rs/ (or $MIHOMO_HOME):
~/.config/mihomo-rs/
âââ versions/ # Installed mihomo binaries
â âââ v1.18.0/
â âââ v1.18.9/
âââ configs/ # Configuration profiles
â âââ default.yaml
â âââ custom.yaml
âââ config.toml # mihomo-rs settings
âââ mihomo.pid # Service PID file
Set via environment variable:
export MIHOMO_HOME=/custom/path
Or programmatically:
use mihomo_rs::{VersionManager, ConfigManager};
use std::path::PathBuf;
let home = PathBuf::from("/custom/path");
let vm = VersionManager::with_home(home.clone())?;
let cm = ConfigManager::with_home(home)?;
# ~/.config/mihomo-rs/configs/default.yaml
port: 7890
socks-port: 7891
allow-lan: false
mode: rule
log-level: info
external-controller: 127.0.0.1:9090
proxies:
- name: "proxy1"
type: ss
server: server.example.com
port: 443
cipher: aes-256-gcm
password: password
proxy-groups:
- name: "GLOBAL"
type: select
proxies:
- proxy1
git clone https://github.com/DINGDANGMAOUP/mihomo-rs
cd mihomo-rs
cargo build --release
cargo test
# Enable logging for debugging
RUST_LOG=debug cargo run --example basic_workflow
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
cargo testcargo clippycargo fmtMIT License - see LICENSE for details.