Files
FlClash/lib/common/boot_record.dart
T
chen08209 aaf934c2d1 refactor(app): rebuild core client, providers, models, database, managers, and window handling
Rework the application layer end to end: core IPC client, providers,
models, database, managers, and shared utilities, with riverpod 3.4.2
and freezed 4.0.1. Keep a rejected profile selected, push the empty
config to Core when a profile fails to build, and fall back to the
default activity icon for package icons.

Add opt-in proxy authentication that injects credentials into the
generated profile and the runtime config, force-clears profile-provided
skip-auth-prefixes so loopback stays covered, sends the credentials with
the app's own proxied requests, and withholds the Android VPN system
proxy while credentials are set. HelperCoreLease treats an unreachable
Helper as confirmation once the OS reports the Core pid gone, and the
lifecycle reconcile retries a parked stop, so the Core comes back after
the Helper service dies. Launch arguments seed LinkManager so a clash://
link raises a silently started window, the access control app list is
fetched on every page entry, the desktop header matches a standard title
bar height, and the tray icons ship as resolution-aware assets scoped per
platform. The version moves to 0.8.97+2026090101.

Rule.parse mirrors mihomo's ParseRulePayload: the type is
case-insensitive, MATCH rules keep no content field, a two-field rule is
payload without a target except for comma-payload types whose lone field
is the target, params are matched whole, and the wildcard and
REMATCH-NAME types the core accepts get their own actions instead of
collapsing into DOMAIN. Window routes show, hide and toggle through one
serialized queue and defers the macOS accessory activation switch until
a second after the last regular switch, so hotkey bursts no longer leave
stray Dock icons. The hotkey manager registers through rust_api instead
of hotkey_manager, the window header restyles the caption buttons after
the native Windows title bar, and the proxies tab keeps its tab
controller alive while the empty state animates in. Bootstrap reads the
Android dynamic palette from the plugin channel without the deprecated
CorePalette, dynamic_color moves to its material_ui release,
navigator_resizable 3.1.0 fixes an assertion when popping
mid-transition, reorderable_grid goes, and the SDK floor rises to 3.10.

Changelog: Rework the app layer and window handling, and add proxy authentication
2026-09-10 12:43:00 +08:00

226 lines
5.5 KiB
Dart

const crashRecoveryClearThreshold = 2;
enum AppExitReason {
unknown,
exitSelf,
signaled,
lowMemory,
crash,
crashNative,
anr,
initializationFailure,
permissionChange,
excessiveResourceUsage,
userRequested,
userStopped,
dependencyDied,
other,
freezer,
packageStateChange,
packageUpdated;
static AppExitReason fromCode(Object? code) => switch (code) {
1 => exitSelf,
2 => signaled,
3 => lowMemory,
4 => crash,
5 => crashNative,
6 => anr,
7 => initializationFailure,
8 => permissionChange,
9 => excessiveResourceUsage,
10 => userRequested,
11 => userStopped,
12 => dependencyDied,
13 => other,
14 => freezer,
15 => packageStateChange,
16 => packageUpdated,
_ => unknown,
};
bool get isCrash => switch (this) {
crash || crashNative || anr || initializationFailure => true,
_ => false,
};
bool get isExternalStop => switch (this) {
exitSelf ||
signaled ||
lowMemory ||
permissionChange ||
excessiveResourceUsage ||
userRequested ||
userStopped ||
dependencyDied ||
freezer ||
packageStateChange ||
packageUpdated => true,
_ => false,
};
}
class AppExitInfo {
final AppExitReason reason;
final int timestamp;
final String? description;
const AppExitInfo({
required this.reason,
required this.timestamp,
this.description,
});
static AppExitInfo? fromJson(Object? json) {
if (json is! Map) {
return null;
}
final timestamp = json['timestamp'];
if (timestamp is! int) {
return null;
}
final description = json['description'];
return AppExitInfo(
reason: AppExitReason.fromCode(json['reason']),
timestamp: timestamp,
description: description is String ? description : null,
);
}
@override
String toString() =>
'AppExitInfo(${reason.name}, $timestamp, ${description ?? '-'})';
}
enum BootStage { starting, running }
class BootRecord {
final BootStage? stage;
final int? profileId;
final int startedAt;
final int failureCount;
final int? lastFailedProfileId;
final int handledExitAt;
const BootRecord({
this.stage,
this.profileId,
this.startedAt = 0,
this.failureCount = 0,
this.lastFailedProfileId,
this.handledExitAt = 0,
});
static BootRecord? fromJson(Object? json) {
if (json is! Map) {
return null;
}
final stage = json['stage'];
return BootRecord(
stage: BootStage.values.where((value) => value.name == stage).firstOrNull,
profileId: json['profileId'] is int ? json['profileId'] as int : null,
startedAt: json['startedAt'] is int ? json['startedAt'] as int : 0,
failureCount: json['failureCount'] is int
? json['failureCount'] as int
: 0,
lastFailedProfileId: json['lastFailedProfileId'] is int
? json['lastFailedProfileId'] as int
: null,
handledExitAt: json['handledExitAt'] is int
? json['handledExitAt'] as int
: 0,
);
}
Map<String, Object?> toJson() => {
'stage': stage?.name,
'profileId': profileId,
'startedAt': startedAt,
'failureCount': failureCount,
'lastFailedProfileId': lastFailedProfileId,
'handledExitAt': handledExitAt,
};
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is BootRecord &&
other.stage == stage &&
other.profileId == profileId &&
other.startedAt == startedAt &&
other.failureCount == failureCount &&
other.lastFailedProfileId == lastFailedProfileId &&
other.handledExitAt == handledExitAt;
@override
int get hashCode => Object.hash(
stage,
profileId,
startedAt,
failureCount,
lastFailedProfileId,
handledExitAt,
);
@override
String toString() =>
'BootRecord(${stage?.name ?? 'closed'}, profile: $profileId, '
'failures: $failureCount)';
}
enum BootRecovery { none, skipAutoSetup, clearProfile }
class BootDecision {
final BootRecovery recovery;
final int failureCount;
final int? failedProfileId;
final bool crashConfirmed;
final AppExitReason? exitReason;
const BootDecision({
this.recovery = BootRecovery.none,
this.failureCount = 0,
this.failedProfileId,
this.crashConfirmed = false,
this.exitReason,
});
bool get isDegraded => recovery != BootRecovery.none;
@override
String toString() =>
'BootDecision(${recovery.name}, failures: $failureCount, '
'profile: $failedProfileId, confirmed: $crashConfirmed, '
'exit: ${exitReason?.name ?? '-'})';
}
BootDecision resolveBootDecision({
required BootRecord? record,
required AppExitInfo? exitInfo,
required bool crashReported,
}) {
if (record == null || record.stage != BootStage.starting) {
return const BootDecision();
}
final matchesLastRun =
exitInfo != null &&
exitInfo.timestamp > record.handledExitAt &&
exitInfo.timestamp >= record.startedAt;
final reason = matchesLastRun ? exitInfo.reason : null;
if (reason != null && reason.isExternalStop) {
return BootDecision(exitReason: reason);
}
final failureCount = record.failureCount + 1;
final shouldClear =
failureCount >= crashRecoveryClearThreshold && record.profileId != null;
return BootDecision(
recovery: shouldClear
? BootRecovery.clearProfile
: BootRecovery.skipAutoSetup,
failureCount: failureCount,
failedProfileId: record.profileId,
crashConfirmed: (reason?.isCrash ?? false) || crashReported,
exitReason: reason,
);
}