Network Optimization Strategies
Relevant source files
The following files were used as context for generating this wiki page:
Network Optimization Strategies in Zephyr refer to a multi-layered system designed to improve connection performance through Mihomo configuration defaults, Operating System (OS) TCP tuning, and DNS optimization. The system provides a standalone UI for users to apply, revert, and view the status of these optimizations across Windows, macOS, and Linux platforms.
Sources: README_en.md, FEATURES.md
Optimization Architecture
The optimization system follows a three-layer architecture to ensure high-performance traffic routing. It coordinates frontend user interactions with backend system-level commands to modify networking parameters.
- Mihomo Layer: Sets defaults for
tcp-concurrent,keep-alive, andfake-ippersistence. - OS Layer: Tunes TCP parameters including Fast Open, Explicit Congestion Notification (ECN), and buffer sizes.
- DNS Layer: Optimizes resolution patterns to reduce latency.
Sources: README_en.md, FEATURES.md
Data Flow for Applying Optimizations
When a user initiates optimization, the system validates the current state, backs up existing system settings, and applies platform-specific commands.
The diagram above shows the sequence of events from the UI request to system-level execution.
Sources: apps/desktop/src/ui/network-optim.js:146-166, apps/desktop/src-tauri/src/core/network_optim.rs:136-155
Platform-Specific Implementations
Zephyr implements unique optimization logic for each supported operating system to account for differing networking stacks and privilege models.
Linux Optimization
On Linux, Zephyr modifies sysctl parameters and persists them to /etc/sysctl.d/99-zephyr-tcp-tuning.conf. It uses pkexec to gain the necessary privileges for writing to system directories.
| Parameter | Purpose | Source |
|---|---|---|
net.ipv4.tcp_fastopen | Enables TCP Fast Open for faster handshakes | network_optim.rs:205 |
net.ipv4.tcp_ecn | Enables Explicit Congestion Notification | network_optim.rs:206 |
net.core.rmem_max | Increases maximum TCP receive buffer | network_optim.rs:214 |
net.core.wmem_max | Increases maximum TCP send buffer | network_optim.rs:219 |
macOS Optimization
macOS optimizations utilize osascript to execute sysctl commands with administrator privileges. The primary focus is on reducing the Maximum Segment Lifetime (MSL) and enabling Fast Open.
Sources: apps/desktop/src-tauri/src/core/network_optim.rs:356-370
Windows Optimization
Windows tuning relies on the netsh utility and direct registry modifications in SYSTEM\CurrentControlSet\Services\Tcpip\Parameters.
| Netsh Command | Description |
|---|---|
autotuninglevel | Sets the TCP window auto-tuning level to 'normal' |
heuristics | Disables TCP heuristics to prevent interference |
initialRto | Sets the Initial Round Trip Time (RTT) |
ecncapability | Enables ECN capability |
Sources: apps/desktop/src-tauri/src/core/network_optim.rs:107-133, apps/desktop/src-tauri/src/core/network_optim.rs:493-524
Optimization Levels
The system defines three presets that determine the aggressiveness of the tuning parameters. These are controlled via the ZEPHYR_NET_OPTIM_LEVEL environment variable.
pub enum OptimLevel {
Conservative,
Balanced, // Default
Aggressive,
}
- Conservative: Focuses on stability with moderate buffer increases.
- Balanced: The default profile providing a mix of speed and compatibility.
- Aggressive: Maximum buffer sizes and optimized timeouts for high-speed connections.
Sources: apps/desktop/src-tauri/src/core/network_optim.rs:18-40
Backup and Recovery
Zephyr prioritizes system integrity by enforcing a strict backup-before-modify policy. Before any optimization is applied, the backend reads current system values and writes them to a tcp-backup.conf file located in the application's data directory.
Safety Measures
- Non-Overwriting Backups: The system checks if a backup file already exists before creating a new one to prevent overwriting original system defaults with already-optimized values.
- Validated Reversion: During a revert operation, the backend parses the backup file and validates that the values are safe (e.g., numeric-only for Linux sysctl) before applying them.
- Atomic Cleanup: The backup file is only deleted after a successful revert operation.
Sources: apps/desktop/src-tauri/src/core/network_optim.rs:141-160, apps/desktop/src-tauri/src/core/network_optim.rs:242-260
Frontend Integration
The Network Optimization UI provides real-time status feedback through a "Status Dot" and action buttons.
The initNetworkOptim function initializes the UI, while autoApplyIfNeeded ensures that optimizations persist across application restarts if the "Auto Apply" setting is enabled.
Sources: apps/desktop/src/ui/network-optim.js:186-203, apps/desktop/src/ui/network-optim.js:209-231
Conclusion
Network Optimization Strategies in Zephyr provide a robust mechanism for extracting maximum performance from the host operating system. By combining safe backup procedures with platform-specific tuning of TCP and DNS parameters, the project ensures that proxy traffic is routed with minimal latency and maximal throughput while maintaining a path to restore original system settings.