$ show oldj/SwitchHosts
SwitchHosts is a cross-platform desktop application for managing the system hosts file (`/etc/hosts`). Built with Tauri (Rust backend) and React (TypeScript frontend), it provides a rich GUI for creating, editing, toggling, and organizing hosts entries into groups, folders, and remote subscriptions. The application supports multiple write modes (append/overwrite), syntax-highlighted editing via CodeMirror, DNS resolution for domain-based entries, import/export with preview and conflict detection, a local HTTP API for third-party integration (e.g., Alfred, Raycast), and a system tray with a mini-window. It also includes a privileged helper daemon on macOS for passwordless writes, atomic file operations with transaction rollback, and crash recovery mechanisms. The architecture follows a clear separation: the Rust backend handles storage (atomic writes, manifest, entries, trashcan, history), hosts application (aggregation, elevation, writing, recovery), DNS resolution, HTTP API server, and system tray lifecycle. The React frontend manages UI state with Jotai, uses Mantine for components, and communicates with the backend via Tauri IPC commands. The codebase is well-tested with unit tests (Vitest, Rust `#[test]`), E2E tests (Playwright), and a shared fixture for append-mode behavior between Rust and JS. Internationalization covers 16 languages with a proxy-based lazy translation system. Key strengths include a robust storage layer with transactional safety, a generic tree component with drag-and-drop, a reusable DNS provider registry, and a find/replace engine with UTF-16 offset handling. The project demonstrates high craftsmanship in data durability (atomic writes, crash recovery tests) and cross-platform considerations (elevation strategies, tray behavior). However, the codebase has some complexity in recovery logic and platform-specific code that may require careful maintenance.
The LLM identified 6 files worth highlighting for reuse:
Implements a multi-file atomic transaction with an undo journal. The `run` function accepts a list of target files and a closure that performs mutations. On failure, it rolls back all changes using the journal. This pattern is self-contained, reusable for any file-based storage system requiring crash safety, and demonstrates careful handling of temporary files, rollback markers, and recovery from interrupted transactions.
Provides DNS-over-HTTPS resolution with a registry of built-in providers (Ali, DNSPod, Cloudflare, Google) and support for custom templates. The `resolve_domains` function batches requests with concurrency limits and caches results. The provider lookup pattern (`builtin_providers()`, `provider_by_id()`) is extensible and could be reused in any project needing configurable DNS resolution.
Implements platform-specific elevation strategies for writing to `/etc/hosts`: macOS helper daemon, AEWP, Linux pkexec, and Windows UAC. The `choose_elevation_strategy` function selects the appropriate method based on OS and availability. Each strategy is encapsulated in an enum variant with its own execution logic. This pattern is reusable for any application needing privileged file writes across platforms.
Implements a proxy-based internationalization system. The `I18N` class uses a JavaScript Proxy to lazily resolve translation keys from nested language dictionaries. It supports interpolation placeholders and fallback to English. This pattern is self-contained, requires no framework, and can be extracted into any TypeScript project needing lightweight i18n.
A generic tree component with drag-and-drop reordering, multi-select, collapse/expand, and custom node rendering. It uses memoization and handles keyboard navigation. The component is decoupled from the hosts data model via a `nodeAttr` prop for per-node permissions and styling. This is a high-quality reusable UI component that could be adapted for any hierarchical data display.
Implements a find/replace engine for hosts file content. It handles UTF-16 offset conversion for correct cursor positioning, supports regex and case-insensitive search, and returns match positions with line numbers. The `find_positions_in_content` function and `LineIndex`/`Utf16Cursor` utilities are reusable for any text search task requiring accurate offset handling in mixed encoding contexts.