Add proxy routing and sing-box support
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user