release: v0.1.3 — correctif VPN, alerte pare-feu inactif, contrôle parental (FamilyControl), anti-désinstallation
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c63cc45c57
commit
82d47fcd72
25 files changed
+996
-16
No files matched your search
@@ -0,0 +1,177 @@
|
||||
package com.jttools.datacontrol.remote
|
||||
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import com.google.firebase.firestore.DocumentReference
|
||||
import com.google.firebase.firestore.SetOptions
|
||||
import com.jttools.datacontrol.data.AppSettings
|
||||
import com.jttools.datacontrol.data.InstalledAppsRepository
|
||||
import com.jttools.datacontrol.data.SettingsRepository
|
||||
import com.jttools.datacontrol.data.TrafficDay
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.tasks.await
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* Liaison avec FamilyControl via Firestore.
|
||||
*
|
||||
* Modèle (voir FamilyControl/firebase/firestore.rules) :
|
||||
* - pairings/{code} : créé par le parent, lu une fois par l'enfant
|
||||
* - families/{fid}/devices/{did} : état, batterie, position (écrit par l'appareil)
|
||||
* - families/{fid}/devices/{did}/config/remote : consignes du parent (lues par l'appareil)
|
||||
* - families/{fid}/devices/{did}/traffic/{yyyy-MM-dd}: historique de trafic
|
||||
* - families/{fid}/devices/{did}/events/{auto} : journal d'évènements
|
||||
*
|
||||
* L'appareil ne s'auto-bloque jamais : l'app DataControl est toujours exclue du tunnel VPN
|
||||
* (cf. FirewallVpnService), donc ces échanges passent même quand le réseau est coupé.
|
||||
*/
|
||||
@Singleton
|
||||
class RemoteSync @Inject constructor(
|
||||
private val gateway: FirebaseGateway,
|
||||
private val settingsRepository: SettingsRepository,
|
||||
private val installedApps: InstalledAppsRepository
|
||||
) {
|
||||
val available: Boolean get() = gateway.enabled
|
||||
|
||||
private fun deviceDoc(s: AppSettings): DocumentReference? {
|
||||
val db = gateway.db ?: return null
|
||||
val fid = s.familyId ?: return null
|
||||
val did = s.deviceId ?: return null
|
||||
return db.collection("families").document(fid).collection("devices").document(did)
|
||||
}
|
||||
|
||||
/** Associe cet appareil à la famille correspondant au [code] affiché dans FamilyControl. */
|
||||
suspend fun pair(code: String, deviceName: String): Result<Unit> = runCatching {
|
||||
check(gateway.enabled) { "Firebase non configuré dans cette version" }
|
||||
val db = gateway.db!!
|
||||
val uid = gateway.ensureSignedIn() ?: error("Connexion Firebase impossible (réseau ?)")
|
||||
val clean = code.trim().uppercase()
|
||||
val pairing = db.collection("pairings").document(clean).get().await()
|
||||
check(pairing.exists()) { "Code inconnu ou expiré" }
|
||||
val familyId = pairing.getString("familyId") ?: error("Code invalide")
|
||||
check((pairing.getLong("expiresAt") ?: 0L) > System.currentTimeMillis()) { "Code expiré" }
|
||||
val deviceId = UUID.randomUUID().toString()
|
||||
db.collection("families").document(familyId).collection("devices").document(deviceId).set(
|
||||
mapOf(
|
||||
"deviceUid" to uid,
|
||||
"name" to deviceName.ifBlank { Build.MODEL },
|
||||
"model" to "${Build.MANUFACTURER} ${Build.MODEL}",
|
||||
"pairingCode" to clean,
|
||||
"pairedAt" to System.currentTimeMillis()
|
||||
)
|
||||
).await()
|
||||
settingsRepository.setPairing(familyId, deviceId)
|
||||
}
|
||||
|
||||
suspend fun unpair() {
|
||||
val s = settingsRepository.settingsFlow.first()
|
||||
pushEvent("unpaired", "Appareil dissocié depuis DataControl")
|
||||
deviceDoc(s)?.let { runCatching { it.update("unpairedAt", System.currentTimeMillis()) } }
|
||||
settingsRepository.setPairing(null, null)
|
||||
}
|
||||
|
||||
/** Écoute les consignes du parent et les applique aux réglages locaux. */
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
fun start(scope: CoroutineScope) {
|
||||
if (!gateway.enabled) return
|
||||
scope.launch {
|
||||
settingsRepository.settingsFlow
|
||||
.map { it.familyId to it.deviceId }
|
||||
.distinctUntilChanged()
|
||||
.flatMapLatest { (fid, did) ->
|
||||
if (fid == null || did == null) flowOf(null) else configFlow(fid, did)
|
||||
}
|
||||
.collect { cfg -> if (cfg != null) apply(cfg) }
|
||||
}
|
||||
scope.launch {
|
||||
// Liste des applications installées, pour que le parent puisse choisir lesquelles autoriser.
|
||||
settingsRepository.settingsFlow.map { it.isPaired }.distinctUntilChanged().collect { paired ->
|
||||
if (paired) pushApps()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class Config(val map: Map<String, Any?>)
|
||||
|
||||
private fun configFlow(fid: String, did: String) = callbackFlow<Config?> {
|
||||
val db = gateway.db
|
||||
if (db == null || gateway.ensureSignedIn() == null) { trySend(null); close(); return@callbackFlow }
|
||||
val reg = db.collection("families").document(fid).collection("devices").document(did)
|
||||
.collection("config").document("remote")
|
||||
.addSnapshotListener { snap, err ->
|
||||
if (err != null) { Log.w(TAG, "config listener", err); return@addSnapshotListener }
|
||||
if (snap != null && snap.exists()) trySend(Config(snap.data.orEmpty()))
|
||||
}
|
||||
awaitClose { reg.remove() }
|
||||
}
|
||||
|
||||
private suspend fun apply(cfg: Config) {
|
||||
val m = cfg.map
|
||||
val version = (m["updatedAt"] as? Number)?.toLong() ?: return
|
||||
fun strings(key: String) = (m[key] as? List<*>)?.filterIsInstance<String>()?.toSet()
|
||||
settingsRepository.applyRemoteConfig(
|
||||
version = version,
|
||||
controlEnabled = m["controlEnabled"] as? Boolean,
|
||||
whitelistedPackages = strings("whitelistedPackages"),
|
||||
whitelistedPackagesWifi = strings("whitelistedPackagesWifi"),
|
||||
wifiFilterEnabled = m["wifiFilterEnabled"] as? Boolean,
|
||||
planLimitMo = (m["planLimitMo"] as? Number)?.toLong(),
|
||||
cutoffIntervalMinutes = (m["cutoffIntervalMinutes"] as? Number)?.toInt()
|
||||
)
|
||||
}
|
||||
|
||||
private suspend fun pushApps() {
|
||||
val s = settingsRepository.settingsFlow.first()
|
||||
val ref = deviceDoc(s) ?: return
|
||||
val apps = installedApps.listInstalledApps(withIcons = false).map {
|
||||
mapOf("pkg" to it.packageName, "label" to it.label, "system" to it.isSystemApp)
|
||||
}
|
||||
write(ref.collection("meta").document("apps"), mapOf("apps" to apps, "at" to System.currentTimeMillis()))
|
||||
}
|
||||
|
||||
/** Fusionne [data] dans le document de l'appareil (état, batterie, position…). */
|
||||
suspend fun pushDevice(data: Map<String, Any?>) {
|
||||
val s = settingsRepository.settingsFlow.first()
|
||||
val ref = deviceDoc(s) ?: return
|
||||
write(ref, data + ("lastSeen" to System.currentTimeMillis()))
|
||||
}
|
||||
|
||||
suspend fun pushTraffic(day: TrafficDay) {
|
||||
val s = settingsRepository.settingsFlow.first()
|
||||
val ref = deviceDoc(s) ?: return
|
||||
write(
|
||||
ref.collection("traffic").document(day.date),
|
||||
mapOf("date" to day.date, "mobileBytes" to day.mobileBytes, "wifiBytes" to day.wifiBytes,
|
||||
"at" to System.currentTimeMillis())
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun pushEvent(type: String, detail: String) {
|
||||
val s = settingsRepository.settingsFlow.first()
|
||||
val ref = deviceDoc(s) ?: return
|
||||
runCatching {
|
||||
ref.collection("events").add(
|
||||
mapOf("type" to type, "detail" to detail, "at" to System.currentTimeMillis())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun write(ref: DocumentReference, data: Map<String, Any?>) {
|
||||
if (gateway.ensureSignedIn() == null) return
|
||||
// Les écritures sont mises en file par le SDK hors-ligne : pas d'attente bloquante du réseau.
|
||||
ref.set(data, SetOptions.merge())
|
||||
}
|
||||
|
||||
companion object { private const val TAG = "RemoteSync" }
|
||||
}
|
||||
Reference in new issue
Block a user