mihomo-rs
English | 简体中文
A Rust SDK and CLI tool for mihomo proxy management with service lifecycle management, configuration handling, and real-time monitoring.
Features
- 🔧 Version Management - Install, update, and switch between mihomo versions (rustup-like experience)
- ⚙️ Configuration Management - Manage multiple configuration profiles with validation
- 🚀 Service Lifecycle - Start, stop, restart mihomo service with PID management
- 🔄 Proxy Operations - List, switch, and test proxy nodes and groups
- 📊 Real-time Monitoring - Stream logs, traffic statistics, and memory usage
- 🔌 Connection Management - Monitor, filter, and close active connections in real-time
- 📦 SDK Library - Use as a library in your Rust applications
- 🖥️ CLI Tool - Command-line interface for easy management
Installation
As a Library
Add to your Cargo.toml:
[]
= "*"
As a CLI Tool
Quick Start
SDK Usage
use ;
async
CLI Usage
# Install mihomo
# Start service
# List proxies
# Switch proxy
# Stream logs (with level filter)
# Stream traffic statistics
# Show memory usage
# List active connections
# Show connection statistics
# Stream connections in real-time
# Close specific connection
# Close all connections
Examples
The examples/ directory includes comprehensive examples:
Quick Start
- hello_mihomo.rs - Minimal example
- basic_workflow.rs - Complete beginner workflow
Version Management
- install_version.rs - Install specific version
- install_by_channel.rs - Install from channel
- list_versions.rs - List installed versions
- manage_versions.rs - Version lifecycle
Configuration
- manage_profiles.rs - Profile management
- custom_config.rs - Custom configuration
- external_controller.rs - Controller setup
Service Management
- service_lifecycle.rs - Start/stop/restart
- service_status.rs - Check status
- auto_restart.rs - Auto-restart logic
Proxy Operations
- list_proxies.rs - List all proxies
- list_groups.rs - List proxy groups
- switch_proxy.rs - Switch proxy
- test_delay.rs - Test latency
- current_proxy.rs - Current selections
Monitoring
- stream_logs.rs - Real-time logs
- stream_logs_filtered.rs - Filtered logs
- stream_traffic.rs - Traffic monitoring
- memory_usage.rs - Memory usage
Connection Management
- list_connections.rs - List active connections with filtering
- close_connections.rs - Close connections by ID, host, or process
- stream_connections.rs - Real-time connection monitoring
Advanced
- custom_home_dir.rs - Custom home directory
- complete_workflow.rs - Full application
- error_handling.rs - Error patterns
- concurrent_operations.rs - Parallel ops
Integration
- first_time_setup.rs - First-time setup
- migration_helper.rs - Migration guide
Run any example with:
See examples/README.md for detailed documentation.
Architecture
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
API Overview
Main Modules
| 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 |
Key Types
| 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.) |
Top-level Functions
// Convenience functions for common operations
use ;
// Install mihomo
install_mihomo.await?; // Latest stable
// Service management
start_service.await?;
stop_service.await?;
// Proxy switching
switch_proxy.await?;
Configuration
Default Locations
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
Custom Home Directory
Set via environment variable:
Or programmatically:
use ;
use PathBuf;
let home = from;
let vm = with_home?;
let cm = with_home?;
Example Configuration
# ~/.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
Development
Building from Source
Running Tests
Running Examples
# Enable logging for debugging
RUST_LOG=debug
Use Cases
1. System Administrators
- Automate mihomo deployment and updates
- Monitor multiple mihomo instances
- Centralized configuration management
2. Application Developers
- Integrate proxy management into applications
- Real-time traffic monitoring
- Programmatic proxy switching
3. Power Users
- Manage multiple mihomo versions
- Quick proxy testing and switching
- Custom automation scripts
4. CI/CD Pipelines
- Automated testing with proxies
- Isolated test environments
- Version-specific testing
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
Development Setup
- Install Rust (1.70+)
- Clone the repository
- Run tests:
cargo test - Run clippy:
cargo clippy - Format code:
cargo fmt
License
MIT License - see LICENSE for details.
Related Projects
- mihomo - Mihomo