# Automation and Intents Nightzuku exposes an authenticated control plane for starting, stopping, restarting, querying, and configuring the service from automation tools and third-party apps. It is disabled by default and configured in **Settings → Automation & Intents**. ## Transports Nightzuku provides two control transports: 1. **Control ContentProvider (recommended for apps)** — `content://kerneldroid.nightzuku.control` - Caller UID and package are verified by the Android kernel (`Binder.getCallingUid()` cross-checked against `PackageManager.getPackagesForUid()`). - App grants are pinned to the APK signing certificate SHA-256 digest. - Returns synchronous, structured `Bundle` responses. - Supports optional single-use challenge-response HMAC-SHA256 verification. 2. **Control BroadcastReceiver (for Tasker, MacroDroid, Automate, ADB)** — `kerneldroid.nightzuku.action.CONTROL` - Component `moe.shizuku.manager.control.NightControlReceiver` is disabled in the manifest by default and dynamically enabled/disabled from Settings. - Enforces replay-protected HMAC-SHA256 signatures by default (`±60 s` timestamp window, bounded nonce cache, constant-time comparison). - Supports optional per-profile scoped keys when **Require HMAC-SHA256** is disabled globally and on the individual key. - On Android 14+, package-bound keys enforce sender identity (`sentFromPackage` + signing certificate match) when **Strict sender check** is enabled. - Supports ordered broadcasts (`sendOrderedBroadcast`), returning `resultCode` and structured `resultExtras`. ## Security model | Control | Behavior | | :--- | :--- | | Key storage | Per-client 256-bit secrets are encrypted at rest with Android Keystore AES-256-GCM and paired with SHA-256 verifiers. Raw secrets are shown once at creation/rotation. | | Scopes | Every grant/key is restricted to explicit scopes: `start`, `stop`, `restart`, `status`, `nightdog`. | | Constant-time checks | All HMAC digests, key verifiers, and signing-cert digests use `MessageDigest.isEqual`. | | Replay protection | Provider challenges expire after `30 s` and are single-use. Intent nonces (min 8 chars) are tracked in a replay cache across the `±60 s` timestamp window. | | Brute-force lockout | `5` consecutive authentication failures within `5 min` lock the caller/key bucket with exponential backoff (`60 s` up to `15 min`). | | Reload | Tapping **Reload** in Settings re-reads policy and keys, re-syncs the receiver component state, and clears transient nonces and lockouts. | ## Commands and scopes | Command (`command`) | Required scope(s) | Optional extras | Description | | :--- | :--- | :--- | :--- | | `start` | `start` | `mode` (`auto` \| `root` \| `adb`) | Starts the server if stopped. | | `stop` | `stop` | `pause_nightdog` (`Boolean`, default `true`) | Stops the server; optionally disables NightDog first. | | `restart` | `restart` | `mode`, `pause_nightdog` | Stops (if running) and starts the server. | | `toggle` | `start`, `stop` | `mode`, `pause_nightdog` | Stops if running; starts if stopped. | | `status` | `status` | — | Returns running state, UID, version, and NightDog state. | | `nightdog_enable` | `nightdog` | — | Enables NightDog watchdog. | | `nightdog_disable` | `nightdog` | — | Disables NightDog watchdog. | ## App integration ### 1. Request authorization Launch `kerneldroid.nightzuku.intent.action.REQUEST_CONTROL_ACCESS` with `startActivityForResult` / Activity Result API. Nightzuku resolves the caller from `callingActivity` / `launchedFromPackage` (never from extras) and displays an approval dialog showing the package name, signing-certificate fingerprint, and scopes. Request extras: - `scopes` (`ArrayList`): requested scopes (defaults to `start`, `stop`, `status`). - `request_key` (`Boolean`, default `false`): also issue a one-time 256-bit per-app HMAC key. - `reason` (`String`, optional): short explanation shown in the consent dialog. Result extras (`RESULT_OK`): - `granted` (`Boolean`) - `key_id` (`String`) - `scopes` (`ArrayList`) - `provider_authority` (`String`: `kerneldroid.nightzuku.control`) - `action_control` (`String`: `kerneldroid.nightzuku.action.CONTROL`) - `client_key` (`String`, present only when `request_key` was approved) ### 2. Call the ContentProvider ```kotlin val uri = Uri.parse("content://kerneldroid.nightzuku.control") // Check authorization state val desc = contentResolver.call(uri, "describe", null, null) // Execute a command (UID + signing cert verified by kernel) val args = Bundle().apply { putString("command", "start") putString("mode", "auto") } val reply = contentResolver.call(uri, "execute", null, args) val ok = reply?.getBoolean("ok") == true val code = reply?.getString("code") ``` For challenge-response HMAC via Provider: 1. Call method `"challenge"` → returns `nonce` and `expires_at`. 2. Compute `signature = hex(HMAC-SHA256(client_key, "|||"))`. 3. Call method `"execute"` with `command`, `mode`, `key_id`, `timestamp`, `nonce`, and `signature`. ### 3. Send a Broadcast Intent Action: `kerneldroid.nightzuku.action.CONTROL` Component: `kerneldroid.nightzuku/moe.shizuku.manager.control.NightControlReceiver` **HMAC-SHA256 mode (default):** - Extras: `command`, `mode` (optional, default `auto`), `key_id`, `timestamp` (epoch seconds), `nonce` (unique, `≥ 8` chars), `signature`. - Canonical payload: `"${command.lowercase()}|$timestamp|$nonce|${mode.lowercase()}"`. **Scoped key mode (for simple automation tools):** - Disable **Require HMAC-SHA256** in Settings and create a key with **HMAC-SHA256 only** unchecked. - Extras: `command`, `key_id`, `key` (and optional `mode`, `pause_nightdog`). ## Response fields and codes Both Provider replies and ordered broadcast `resultExtras` return: - `ok` (`Boolean`), `code` (`String`), `message` (`String`), `command` (`String`) - `running` (`Boolean`), `uid` (`Int`), `version` (`Int`), `nightdog_enabled` (`Boolean`), `timestamp` (`Long`) Common `code` values: `OK`, `STARTING`, `ALREADY_RUNNING`, `ALREADY_STOPPED`, `DISABLED`, `INTENTS_DISABLED`, `UNAUTHORIZED`, `KEY_NOT_FOUND`, `KEY_DISABLED`, `SIGNATURE_MISMATCH`, `PACKAGE_MISMATCH`, `INVALID_SIGNATURE`, `INVALID_CHALLENGE`, `REPLAY_DETECTED`, `TIMESTAMP_SKEW`, `HMAC_REQUIRED`, `SCOPE_DENIED`, `RATE_LIMITED`, `INVALID_COMMAND`, `ROOT_UNAVAILABLE`. ## Source files - `manager/src/main/java/moe/shizuku/manager/control/ControlProtocol.kt` - `manager/src/main/java/moe/shizuku/manager/control/ControlCrypto.kt` - `manager/src/main/java/moe/shizuku/manager/control/ControlPolicyStore.kt` - `manager/src/main/java/moe/shizuku/manager/control/ControlSecurityEngine.kt` - `manager/src/main/java/moe/shizuku/manager/control/ControlCommandExecutor.kt` - `manager/src/main/java/moe/shizuku/manager/control/NightControlProvider.kt` - `manager/src/main/java/moe/shizuku/manager/control/NightControlReceiver.kt` - `manager/src/main/java/moe/shizuku/manager/control/ControlAccessRequestActivity.kt` - `manager/src/main/java/moe/shizuku/manager/control/ControlSettingsContent.kt`