Add proxy routing and sing-box support

This commit is contained in:
Fabian Freund
2026-05-22 18:16:31 +02:00
parent 51289f1266
commit a5974617aa
262 changed files with 32003 additions and 3962 deletions
@@ -49,13 +49,20 @@ class ProfileBackupScreen extends HookConsumerWidget {
final backupFuture = useState<Future<bool>?>(null);
final backupState = useFuture(backupFuture.value);
// One-shot: success navigates away; rebuilds before the route swap
// completes would otherwise re-fire navigation and the snackbar.
final successHandled = useRef(false);
useEffect(() {
if (backupState.hasError) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!context.mounted) return;
showErrorMessage(context, backupState.error!.toString());
});
} else if (backupState.hasData) {
} else if (backupState.hasData && !successHandled.value) {
successHandled.value = true;
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!context.mounted) return;
showInfoMessage(context, 'Backup created successfully');
ProfileListRoute().go(context);
});
@@ -55,13 +55,20 @@ class ProfileRestoreScreen extends HookConsumerWidget {
final restoreTarget = useState(forcedTarget ?? RestoreTarget.createNew);
// One-shot: a successful restore navigates away, but rebuilds before the
// route swap completes can otherwise re-fire the success branch.
final successHandled = useRef(false);
useEffect(() {
if (restoreState.hasError) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!context.mounted) return;
showErrorMessage(context, restoreState.error!.toString());
});
} else if (restoreState.hasData) {
} else if (restoreState.hasData && !successHandled.value) {
successHandled.value = true;
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!context.mounted) return;
showInfoMessage(context, 'Backup restored successfully');
if (onRestoreSuccess != null) {
onRestoreSuccess!(context, restoreState.data);
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:drift/drift.dart';
import 'package:riverpod/riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:weblibre/features/user/data/models/proxy_routing_settings.dart';
import 'package:weblibre/features/user/data/providers.dart';
part 'proxy_routing_settings.g.dart';
typedef UpdateProxyRoutingSettingsFunc =
ProxyRoutingSettings Function(ProxyRoutingSettings currentSettings);
@Riverpod(keepAlive: true)
class ProxyRoutingSettingsRepository extends _$ProxyRoutingSettingsRepository {
final _partitionKey = 'proxy_routing';
ProxyRoutingSettings _deserializeSettings(
List<MapEntry<String, DriftAny?>> entries,
) {
final settings = Map.fromEntries(entries);
final db = ref.read(userDatabaseProvider);
return ProxyRoutingSettings.fromJson({
'regularTabsMode': settings['regularTabsMode']?.readAs(
DriftSqlType.string,
db.typeMapping,
),
'regularTabsProxyConnectionId': settings['regularTabsProxyConnectionId']
?.readAs(DriftSqlType.string, db.typeMapping),
'privateTabsProxyConnectionId': settings['privateTabsProxyConnectionId']
?.readAs(DriftSqlType.string, db.typeMapping),
});
}
//Eager fetch, when up to date settings are required
Future<ProxyRoutingSettings> fetchSettings() {
return ref
.read(userDatabaseProvider)
.settingDao
.getAllSettingsOfPartitionKey(_partitionKey)
.get()
.then(_deserializeSettings);
}
Future<void> updateSettings(
UpdateProxyRoutingSettingsFunc updateWithCurrent,
) async {
final db = ref.read(userDatabaseProvider);
final current = await fetchSettings();
final oldJson = current.toJson();
final newJson = updateWithCurrent(current).toJson();
return db.transaction(() async {
for (final MapEntry(:key, :value) in newJson.entries) {
if (oldJson[key] != value) {
await db.settingDao.updateSetting(key, _partitionKey, value);
}
}
});
}
@override
Stream<ProxyRoutingSettings> build() {
final db = ref.watch(userDatabaseProvider);
return db.settingDao
.getAllSettingsOfPartitionKey(_partitionKey)
.watch()
.map(_deserializeSettings);
}
}
@Riverpod(keepAlive: true)
ProxyRoutingSettings proxyRoutingSettingsWithDefaults(Ref ref) {
return ref.watch(
proxyRoutingSettingsRepositoryProvider.select(
(value) => value.value ?? ProxyRoutingSettings.withDefaults(),
),
);
}
@@ -0,0 +1,115 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'proxy_routing_settings.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, type=warning
@ProviderFor(ProxyRoutingSettingsRepository)
final proxyRoutingSettingsRepositoryProvider =
ProxyRoutingSettingsRepositoryProvider._();
final class ProxyRoutingSettingsRepositoryProvider
extends
$StreamNotifierProvider<
ProxyRoutingSettingsRepository,
ProxyRoutingSettings
> {
ProxyRoutingSettingsRepositoryProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'proxyRoutingSettingsRepositoryProvider',
isAutoDispose: false,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$proxyRoutingSettingsRepositoryHash();
@$internal
@override
ProxyRoutingSettingsRepository create() => ProxyRoutingSettingsRepository();
}
String _$proxyRoutingSettingsRepositoryHash() =>
r'10c46172bad7426bb639cf3bc769ba150bdb30c3';
abstract class _$ProxyRoutingSettingsRepository
extends $StreamNotifier<ProxyRoutingSettings> {
Stream<ProxyRoutingSettings> build();
@$mustCallSuper
@override
void runBuild() {
final ref =
this.ref
as $Ref<AsyncValue<ProxyRoutingSettings>, ProxyRoutingSettings>;
final element =
ref.element
as $ClassProviderElement<
AnyNotifier<
AsyncValue<ProxyRoutingSettings>,
ProxyRoutingSettings
>,
AsyncValue<ProxyRoutingSettings>,
Object?,
Object?
>;
element.handleCreate(ref, build);
}
}
@ProviderFor(proxyRoutingSettingsWithDefaults)
final proxyRoutingSettingsWithDefaultsProvider =
ProxyRoutingSettingsWithDefaultsProvider._();
final class ProxyRoutingSettingsWithDefaultsProvider
extends
$FunctionalProvider<
ProxyRoutingSettings,
ProxyRoutingSettings,
ProxyRoutingSettings
>
with $Provider<ProxyRoutingSettings> {
ProxyRoutingSettingsWithDefaultsProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'proxyRoutingSettingsWithDefaultsProvider',
isAutoDispose: false,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$proxyRoutingSettingsWithDefaultsHash();
@$internal
@override
$ProviderElement<ProxyRoutingSettings> $createElement(
$ProviderPointer pointer,
) => $ProviderElement(pointer);
@override
ProxyRoutingSettings create(Ref ref) {
return proxyRoutingSettingsWithDefaults(ref);
}
/// {@macro riverpod.override_with_value}
Override overrideWithValue(ProxyRoutingSettings value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<ProxyRoutingSettings>(value),
);
}
}
String _$proxyRoutingSettingsWithDefaultsHash() =>
r'899f545188633843d5224bf86d4611d80543cf5c';
@@ -38,14 +38,6 @@ class TorSettingsRepository extends _$TorSettingsRepository {
final db = ref.read(userDatabaseProvider);
return TorSettings.fromJson({
'proxyRegularTabsMode': settings['proxyRegularTabsMode']?.readAs(
DriftSqlType.string,
db.typeMapping,
),
'proxyPrivateTabsTor': settings['proxyPrivateTabsTor']?.readAs(
DriftSqlType.bool,
db.typeMapping,
),
'config': settings['config']?.readAs(DriftSqlType.string, db.typeMapping),
'requireBridge': settings['requireBridge']?.readAs(
DriftSqlType.bool,
@@ -34,7 +34,7 @@ final class TorSettingsRepositoryProvider
}
String _$torSettingsRepositoryHash() =>
r'f771f23f17903bd192b24604bab522fb20571ffd';
r'10fa5ad15138e3df24940c3462d62a9df9fe5eef';
abstract class _$TorSettingsRepository extends $StreamNotifier<TorSettings> {
Stream<TorSettings> build();