mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-18 23:14:22 +01:00
Two gaps stopped the unrooted path working end to end. The phone had no way to read the focus policy, and the blocklist was only reachable as a hosts file or over plain UDP/53 on the LAN -- while the rooted enforcer actively rejected every DoT connection, so a self-hosted resolver could not have worked even if one existed. focus_owner now loads a generated policy.json instead of hardcoding anything, so config.sh stays the single source of truth across Python, shell and Kotlin. The committed asset is rendered with --redact-home and a test fails if home coordinates ever appear in it. An unknown schema_version is rejected outright: silently accepting a newer schema means enforcing a policy the app has misread, and a misread allowlist is one that blocks the dialer. dns_enforcer.sh rejected ALL port 853 traffic and pinned Private DNS off, every 20 seconds. That is right when DoT is a bypass channel and exactly wrong when DoT is the enforcement channel, which is what an unrooted phone needs -- it has no hosts file to fall back to. With DNS_TRUSTED_DOT_HOST set it now ACCEPTs the trusted resolver before the blanket REJECT, and pins Private DNS to that hostname so the user cannot pick an unfiltered one. Rule order is load-bearing: iptables takes the first match, so an ACCEPT appended after the REJECT would never be reached. expected_rule_count() counts the new rules, because a mismatch there makes chain_intact() rebuild the chain every 20s -- a silent fork storm that also drops the connection each time. With the variable empty the behaviour is byte-identical, so the rooted phone is untouched until this is deployed. setup_dot_resolver.sh terminates DoT with stunnel rather than Caddy: DoT is a raw TLS stream and stock caddy:2.8 has no layer4 module, so using Caddy would have meant an xcaddy rebuild of a container serving five live sites. It reuses the certificate Caddy already renews, so nothing competes for port 80. It binds to the WireGuard address, not 0.0.0.0 -- a public DoT endpoint is an open resolver, and over the existing tunnel it needs no port-forward, no firewall rule and no rate limiting. Verified: example.com resolves, pornhub.com returns 0.0.0.0, TLS 1.3 against the LE cert, and both the LAN and public addresses refuse. The Device Owner checklist is planning only. Checking the device rather than the config settled the one open risk: Google Wallet is not installed and no NFC payment default is set, so the unconfirmable tap-to-pay question does not apply here. The same check found that Aegis, MS Authenticator, Oracle IDM and PKO IKO are absent -- they are in config.sh's whitelist, which describes the rooted phone, not this one. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AwepyL5Lxs5QKbKkMpxxxL
202 lines
6.6 KiB
Dart
202 lines
6.6 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/services.dart' show AssetBundle, rootBundle;
|
|
|
|
/// Schema version this build understands.
|
|
///
|
|
/// The asset is generated by `python3 -m python_pkg.focus_policy`, which is the
|
|
/// single source of truth. Refusing an unknown version is deliberate: silently
|
|
/// accepting a newer schema would mean enforcing a policy this code has
|
|
/// misread, and a misread allowlist is an allowlist that blocks the dialer.
|
|
const int kSupportedSchemaVersion = 1;
|
|
|
|
/// Raised when the bundled policy cannot be trusted.
|
|
class PolicyFormatException implements Exception {
|
|
const PolicyFormatException(this.message);
|
|
|
|
final String message;
|
|
|
|
@override
|
|
String toString() => 'PolicyFormatException: $message';
|
|
}
|
|
|
|
/// A nightly window that may wrap midnight (23:00 -> 05:00).
|
|
class CurfewWindow {
|
|
const CurfewWindow({required this.startMinutes, required this.endMinutes});
|
|
|
|
/// Parses an `{"start": "23:00", "end": "05:00"}` object.
|
|
factory CurfewWindow.fromJson(Map<String, Object?> json) {
|
|
return CurfewWindow(
|
|
startMinutes: _parseHhMm(json['start'], 'curfew.start'),
|
|
endMinutes: _parseHhMm(json['end'], 'curfew.end'),
|
|
);
|
|
}
|
|
|
|
/// Minutes since midnight.
|
|
final int startMinutes;
|
|
final int endMinutes;
|
|
|
|
/// Whether [minutesSinceMidnight] falls inside the window.
|
|
///
|
|
/// Wrapping windows are the normal case here, so "inside" means at or after
|
|
/// the start *or* before the end, not both.
|
|
bool contains(int minutesSinceMidnight) {
|
|
if (startMinutes <= endMinutes) {
|
|
return minutesSinceMidnight >= startMinutes &&
|
|
minutesSinceMidnight < endMinutes;
|
|
}
|
|
return minutesSinceMidnight >= startMinutes ||
|
|
minutesSinceMidnight < endMinutes;
|
|
}
|
|
}
|
|
|
|
/// The home anchor. Coordinates are null in a redacted policy.
|
|
class HomeLocation {
|
|
const HomeLocation({
|
|
required this.latitude,
|
|
required this.longitude,
|
|
required this.radiusM,
|
|
required this.hysteresisM,
|
|
});
|
|
|
|
/// Parses the `home` object.
|
|
factory HomeLocation.fromJson(Map<String, Object?> json) {
|
|
return HomeLocation(
|
|
latitude: (json['latitude'] as num?)?.toDouble(),
|
|
longitude: (json['longitude'] as num?)?.toDouble(),
|
|
radiusM: _requireNum(json['radius_m'], 'home.radius_m'),
|
|
hysteresisM: _requireNum(json['hysteresis_m'], 'home.hysteresis_m'),
|
|
);
|
|
}
|
|
|
|
final double? latitude;
|
|
final double? longitude;
|
|
final double radiusM;
|
|
final double hysteresisM;
|
|
|
|
/// Whether this policy carries real coordinates.
|
|
///
|
|
/// The committed asset is redacted, so the location gate cannot run against
|
|
/// it. Enforcement must check this rather than treating a missing coordinate
|
|
/// as (0, 0), which would put "home" in the Atlantic.
|
|
bool get hasCoordinates => latitude != null && longitude != null;
|
|
}
|
|
|
|
/// The device-independent focus policy, as generated from `config.sh`.
|
|
class FocusPolicy {
|
|
const FocusPolicy({
|
|
required this.home,
|
|
required this.allowedPackages,
|
|
required this.nightAllowedPackages,
|
|
required this.neverDisablePrefixes,
|
|
required this.workoutUnblockDomains,
|
|
required this.curfew,
|
|
required this.launcherPackage,
|
|
});
|
|
|
|
/// Parses a rendered policy document.
|
|
factory FocusPolicy.fromJson(Map<String, Object?> json) {
|
|
final version = json['schema_version'];
|
|
if (version != kSupportedSchemaVersion) {
|
|
throw PolicyFormatException(
|
|
'unsupported schema_version $version '
|
|
'(this build understands $kSupportedSchemaVersion)',
|
|
);
|
|
}
|
|
final home = json['home'];
|
|
if (home is! Map<String, Object?>) {
|
|
throw const PolicyFormatException('missing "home" object');
|
|
}
|
|
final curfew = json['curfew'];
|
|
return FocusPolicy(
|
|
home: HomeLocation.fromJson(home),
|
|
allowedPackages: _stringSet(json['allowed_packages'], 'allowed_packages'),
|
|
nightAllowedPackages:
|
|
_stringSet(json['night_allowed_packages'], 'night_allowed_packages'),
|
|
neverDisablePrefixes: _stringSet(
|
|
json['never_disable_prefixes'],
|
|
'never_disable_prefixes',
|
|
),
|
|
workoutUnblockDomains: _stringSet(
|
|
json['workout_unblock_domains'],
|
|
'workout_unblock_domains',
|
|
),
|
|
curfew: curfew == null
|
|
? null
|
|
: CurfewWindow.fromJson(curfew as Map<String, Object?>),
|
|
launcherPackage: json['launcher_package'] as String?,
|
|
);
|
|
}
|
|
|
|
/// Loads the policy bundled with the app.
|
|
static Future<FocusPolicy> load({
|
|
AssetBundle? bundle,
|
|
String assetKey = 'assets/policy.json',
|
|
}) async {
|
|
final raw = await (bundle ?? rootBundle).loadString(assetKey);
|
|
final decoded = jsonDecode(raw);
|
|
if (decoded is! Map<String, Object?>) {
|
|
throw const PolicyFormatException('policy asset is not a JSON object');
|
|
}
|
|
return FocusPolicy.fromJson(decoded);
|
|
}
|
|
|
|
final HomeLocation home;
|
|
final Set<String> allowedPackages;
|
|
final Set<String> nightAllowedPackages;
|
|
final Set<String> neverDisablePrefixes;
|
|
final Set<String> workoutUnblockDomains;
|
|
final CurfewWindow? curfew;
|
|
final String? launcherPackage;
|
|
|
|
/// Whether a package must never be disabled.
|
|
///
|
|
/// Prefix-matched on whole labels, so `com.android.providers` covers
|
|
/// `com.android.providers.telephony` but not `com.android.providersomething`.
|
|
bool isProtected(String package) => neverDisablePrefixes.any(
|
|
(prefix) => package == prefix || package.startsWith('$prefix.'),
|
|
);
|
|
|
|
/// Whether a package may run under the given conditions.
|
|
bool isAllowed(String package, {bool duringCurfew = false}) {
|
|
if (isProtected(package)) return true;
|
|
return duringCurfew
|
|
? nightAllowedPackages.contains(package)
|
|
: allowedPackages.contains(package);
|
|
}
|
|
|
|
/// Whether the curfew is in force at [minutesSinceMidnight].
|
|
bool isCurfewActive(int minutesSinceMidnight) =>
|
|
curfew?.contains(minutesSinceMidnight) ?? false;
|
|
}
|
|
|
|
Set<String> _stringSet(Object? value, String field) {
|
|
if (value is! List) {
|
|
throw PolicyFormatException('"$field" must be a list');
|
|
}
|
|
return value.map((e) => e! as String).toSet();
|
|
}
|
|
|
|
double _requireNum(Object? value, String field) {
|
|
if (value is! num) {
|
|
throw PolicyFormatException('"$field" must be a number');
|
|
}
|
|
return value.toDouble();
|
|
}
|
|
|
|
int _parseHhMm(Object? value, String field) {
|
|
if (value is! String) {
|
|
throw PolicyFormatException('"$field" must be a "HH:MM" string');
|
|
}
|
|
final parts = value.split(':');
|
|
if (parts.length != 2) {
|
|
throw PolicyFormatException('"$field" must be "HH:MM", got "$value"');
|
|
}
|
|
final hour = int.tryParse(parts[0]);
|
|
final minute = int.tryParse(parts[1]);
|
|
if (hour == null || minute == null || hour > 23 || minute > 59) {
|
|
throw PolicyFormatException('"$field" is not a valid time: "$value"');
|
|
}
|
|
return hour * 60 + minute;
|
|
}
|