This commit is contained in:
Meghdad committed 2026-08-25 02:18:50 +03:30
commit 341e9e433e
65 files changed
+5782

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
# Architecture
## Shape
```text
Compose UI → GatewayViewModel → repositories → Room / server controller / Android SMS
↑
HTTP transport → ApiRouter → security policy + repositories + SmsSender
SMS broadcasts ────────────────────────────────→ repositories
```
The project deliberately remains a single Gradle module. Packages provide the boundaries that are currently useful without paying the maintenance cost of premature multi-module architecture.
## Responsibilities
- `ui`: immutable screen state and user actions. It never invokes `SmsManager` or NanoHTTPD directly.
- `data.local`: Room entities, DAO, and database. Database schema version 1 is exported under `app/schemas`.
- `data`: repositories exposing persistence operations and flows.
- `sms`: `SmsSender` abstraction, Android sender, sent-result receiver, and incoming receiver.
- `security`: API-key hashing/constant-time comparison, exact-IP validation, and access decisions.
- `server`: server lifecycle, foreground service, transport adapter, API routing, JSON models, network address detection, and reboot receiver.
- `di`: a small application-scoped constructor-injection container.
Hilt was evaluated, but the current Hilt/AGP dependency chain required an unavailable Fragment artifact despite this being a Compose-only app. The small `AppContainer` keeps dependencies explicit and replaceable without introducing an artifact mirror or unused Fragment runtime. Repositories and controllers use constructor injection, so migration to Hilt remains mechanical.
## Server lifecycle
`GatewayServerService` owns the foreground-service lifetime. `GatewayServerController` serializes start/stop/restart operations with a mutex and isolates server exceptions from the UI process. A state flow exposes stopped, starting, running, stopping, and error states. NanoHTTPD uses a 10-second socket timeout and handles requests on worker threads; suspend repository work is isolated on `Dispatchers.IO`.
The desired enabled state is persisted. When reboot startup is enabled, `BootReceiver` restores the foreground service after `BOOT_COMPLETED`.
## API pipeline
Every `/api/v1` request follows one centralized path:
1. Restrict routes to `/api/v1` and the locally bundled `/docs` resources, then capture safe request metadata.
2. Load the current security settings.
3. Enforce exact-IP allowlisting when enabled.
4. Enforce Bearer or `X-API-Key` authentication when enabled.
5. Validate method, route, pagination, content length, JSON, phone number, and message size.
6. Invoke repositories or the `SmsSender` interface.
7. Emit a stable JSON response.
8. Persist sanitized method/path/status/client IP/duration metadata in `finally`.
Documentation resources are intentionally public so a normal browser can load Swagger UI and its OpenAPI document. They expose only the published contract; API operations still pass through authentication and allowlisting.
`ApiRouter` does not depend on NanoHTTPD implementation details beyond the request session at its transport boundary. Authentication decisions are separate in `SecurityPolicy`, leaving a clear insertion point for HMAC, timestamp/nonce checks, replay storage, and rate limiting.
## Persistence
Room stores four tables:
- `incoming_sms`
- `outgoing_sms`
- `request_logs`
- `app_settings`
Messages use UUID strings so IDs remain stable across API, database, pending intents, and future central management. Request logs are capped at the newest 10,000 records. API keys are stored as SHA-256 hashes, never plaintext.
## Reliability boundaries
- Server lifecycle transitions are mutex-protected.
- Each API request has a top-level exception boundary and a stable internal error response.
- Broadcast receivers use `goAsync`, defensive parsing, IO coroutines, and guaranteed `finish()` calls.
- Outgoing rows are persisted before Android submission and updated on immediate or asynchronous failure.
- Room provides serialized transactions and concurrency safety.
- Server configuration changes trigger a safe restart only when currently running.
## Future extensions
- Replace the key-only policy with a chain supporting HMAC, nonce persistence, and timestamp windows.
- Add a queue/work scheduler between API submission and `SmsSender`.
- Add multipart and delivery pending intents without changing API routing.
- Introduce a SIM-selection strategy behind `SmsSender`.
- Add CIDR parsing behind the allowlist policy.
- Terminate TLS in an Android-capable transport or a controlled reverse proxy.