.agents/architecture.md
The Go proxy core in core/ operates in two modes.
Android lib mode:
libclash.so, through go build -buildmode=c-shared with CGO.service plugin.lib/core/lib.dart (CoreLib).Desktop core mode:
CGO_ENABLED=0.lib/core/service.dart (CoreService).lib/core/controller.dart (CoreController) selects the implementation based on platform. lib/core/interface.dart defines the shared CoreHandlerInterface.
Key Go core files:
core/hub.go: handler functions.core/action.go: dispatch.core/lib.go: CGO exports.core/server.go: socket server.Provider files in lib/providers/:
app.dart: runtime/UI state, logs, traffic, delays, loading, navigation.config.dart: persistent config providers, app settings, theme, VPN, proxy style.state.dart: derived/computed providers, navigation, proxy, tray, color scheme.action.dart: business logic notifiers, setup, backup, core lifecycle, proxy selection.database.dart: Drift database provider wrappers.globalState in lib/state.dart is a singleton holding app lifecycle, timers, theme, and start/stop state. Providers are generated into lib/providers/generated/.
The app uses Drift/SQLite in lib/database/. Current schema version is 2.
Tables:
ProfilesScriptsRulesProfileRuleLinks (profile_rule_mapping)ProxyGroupsIconRecords (icon_records)Rule scenes distinguish global added rules, profile added rules, profile custom rules, and disabled links. Rule and proxy-group ordering use fractional indexing.
Generated Drift output lives in lib/database/generated/database.g.dart. After schema changes, run code generation and add or update focused database tests under test/database/ when converter or migration behavior changes.
Managers are nested InheritedWidget/StatefulWidget components in lib/application.dart:
AppEnvManager > StatusManager > ThemeManager
> [Desktop: WindowManager > TrayManager > HotKeyManager > ProxyManager]
> ConnectivityManager > CoreManager > AppStateManager
> [Mobile: AndroidManager > VpnManager | Desktop: WindowHeaderContainer]
Each manager in lib/manager/ handles a specific platform concern. Desktop-only managers are conditionally inserted.
lib/core/controller.dart (CoreController) is a singleton facade over CoreHandlerInterface. Public methods delegate to the platform-specific interface, either Android FFI or desktop socket. It has an @visibleForTesting constructor and resetInstance() for test injection.
Business logic lives in Riverpod notifier classes in lib/providers/action.dart:
CommonAction: update check and common UI operations.SetupAction: config setup and TUN management.BackupAction: backup/restore with WebDAV sync.CoreAction: core lifecycle, init, connect, restart, shutdown.SystemAction: system integration, tray, exit, brightness.StoreAction: profile storage operations.ThemeAction: theme state updates.ProxiesAction: group management and proxy selection.ProfilesAction: profile CRUD, auto-update, import.Desktop:
WindowManagerTrayManagerHotKeyManagerProxyManagerMobile:
AndroidManagerTileManagerVpnManagerShared:
ConnectivityManagerCoreManagerAppStateManagerStatusManagerThemeManagersetup.dart is the release build orchestrator:
dart run build_tool windows and reads core_sha256.json.env.json (APP_ENV).--dart-define=CORE_SHA256=$val, embedded at compile time for Windows.flutter_distributor for packaging.Go core building is handled by build_tool, a standalone Dart CLI in plugins/setup/buildkit/build_tool/.
Platform build hooks inside flutter build trigger build_tool automatically:
build_pod.sh, build_tool macos.buildkit/cmake/buildkit.cmake, build_tool linux.buildkit/cmake/buildkit.cmake, build_tool windows. Debug passes --dev via CMAKE_BUILD_TYPE.buildkit/gradle/plugin.gradle, build_tool android.Windows helper auth:
flutter run works without the SHA256 flow.plugins/setup/ is an FFI plugin that exists only as a build harness. It carries no Dart API, only platform build hooks that trigger Go compilation. Windows builds also compile a Rust helper in services/helper/ through RustBuilder.
Build configuration defaults live in build_tool/lib/src/options.dart and can be overridden via build_config.yaml.
Architecture detection is automatic. The --description flag passed to flutter_distributor adds arch suffixes to artifact names, such as FlClash-0.8.93-macos-arm64.dmg.
setup: build harness FFI plugin.proxy: system proxy configuration.rust_api: Flutter Rust Bridge FFI plugin.tray_manager: system tray fork/customization.wifi_ssid: Wi-Fi SSID detection.window_ext: window extensions.flutter_distributor: app packaging/distribution.services/helper/ is a Windows-only privileged helper for starting the core as admin and managing TUN. It is built with:
cargo build --release --features windows-service
It uses token-based auth with the Flutter app.