prepare for multiple apps
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:weblibre/core/logger.dart';
|
||||
import 'package:weblibre/features/tor/data/models/moat.dart';
|
||||
import 'package:weblibre/features/tor/data/services/builtin_bridges.dart';
|
||||
import 'package:weblibre/features/tor/data/services/moat_service.dart';
|
||||
|
||||
part 'builtin_bridges.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class BuiltinBridgesRepository extends _$BuiltinBridgesRepository {
|
||||
Future<void> updateIfNecessary() async {
|
||||
final lastUpdate = await ref
|
||||
.read(builtinBridgesServiceProvider.notifier)
|
||||
.lastUpdate();
|
||||
|
||||
if (lastUpdate == null ||
|
||||
DateTime.now().difference(lastUpdate) > const Duration(days: 2)) {
|
||||
BuiltInBridges? remoteBridges;
|
||||
try {
|
||||
remoteBridges = await service.getBuiltinBridges();
|
||||
} catch (e, s) {
|
||||
logger.e('Failed fetching builtin bridges', error: e, stackTrace: s);
|
||||
}
|
||||
|
||||
if (remoteBridges != null) {
|
||||
await ref
|
||||
.read(builtinBridgesServiceProvider.notifier)
|
||||
.updateStoredBuiltinBridges(remoteBridges);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<BuiltInBridges> getBridges({bool tryUpdate = true}) async {
|
||||
if (tryUpdate) {
|
||||
await updateIfNecessary();
|
||||
}
|
||||
|
||||
final storedBridges = await ref
|
||||
.read(builtinBridgesServiceProvider.notifier)
|
||||
.getStoredBuiltinBridges();
|
||||
|
||||
return storedBridges ??
|
||||
await ref
|
||||
.read(builtinBridgesServiceProvider.notifier)
|
||||
.getBundledBuiltinBridges();
|
||||
}
|
||||
|
||||
@override
|
||||
void build(MoatService service) {}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'builtin_bridges.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
|
||||
@ProviderFor(BuiltinBridgesRepository)
|
||||
final builtinBridgesRepositoryProvider = BuiltinBridgesRepositoryFamily._();
|
||||
|
||||
final class BuiltinBridgesRepositoryProvider
|
||||
extends $NotifierProvider<BuiltinBridgesRepository, void> {
|
||||
BuiltinBridgesRepositoryProvider._({
|
||||
required BuiltinBridgesRepositoryFamily super.from,
|
||||
required MoatService super.argument,
|
||||
}) : super(
|
||||
retry: null,
|
||||
name: r'builtinBridgesRepositoryProvider',
|
||||
isAutoDispose: false,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$builtinBridgesRepositoryHash();
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return r'builtinBridgesRepositoryProvider'
|
||||
''
|
||||
'($argument)';
|
||||
}
|
||||
|
||||
@$internal
|
||||
@override
|
||||
BuiltinBridgesRepository create() => BuiltinBridgesRepository();
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(void value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<void>(value),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is BuiltinBridgesRepositoryProvider &&
|
||||
other.argument == argument;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return argument.hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
String _$builtinBridgesRepositoryHash() =>
|
||||
r'b06ff04774d6d5b0aa7c4f93469ca514536ad85e';
|
||||
|
||||
final class BuiltinBridgesRepositoryFamily extends $Family
|
||||
with
|
||||
$ClassFamilyOverride<
|
||||
BuiltinBridgesRepository,
|
||||
void,
|
||||
void,
|
||||
void,
|
||||
MoatService
|
||||
> {
|
||||
BuiltinBridgesRepositoryFamily._()
|
||||
: super(
|
||||
retry: null,
|
||||
name: r'builtinBridgesRepositoryProvider',
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
isAutoDispose: false,
|
||||
);
|
||||
|
||||
BuiltinBridgesRepositoryProvider call(MoatService service) =>
|
||||
BuiltinBridgesRepositoryProvider._(argument: service, from: this);
|
||||
|
||||
@override
|
||||
String toString() => r'builtinBridgesRepositoryProvider';
|
||||
}
|
||||
|
||||
abstract class _$BuiltinBridgesRepository extends $Notifier<void> {
|
||||
late final _$args = ref.$arg as MoatService;
|
||||
MoatService get service => _$args;
|
||||
|
||||
void build(MoatService service);
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
final ref = this.ref as $Ref<void, void>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<void, void>,
|
||||
void,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleCreate(ref, () => build(_$args));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* 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 'dart:async';
|
||||
|
||||
import 'package:flutter_mozilla_components/flutter_mozilla_components.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:synchronized/synchronized.dart';
|
||||
import 'package:weblibre/features/geckoview/features/tabs/data/models/site_assignment.dart';
|
||||
|
||||
part 'tor_proxy.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class TorProxyRepository extends _$TorProxyRepository {
|
||||
final _service = GeckoContainerProxyService();
|
||||
final _serviceLock = Lock();
|
||||
|
||||
Future<void> setProxyPort(int port) {
|
||||
return _serviceLock.synchronized(() async {
|
||||
await _waitHealthcheck().timeout(const Duration(seconds: 30));
|
||||
|
||||
return _service.setProxyPort(port);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> addContainerProxy(String contextId) {
|
||||
return _serviceLock.synchronized(() async {
|
||||
await _waitHealthcheck().timeout(const Duration(seconds: 10));
|
||||
|
||||
return _service.addContainerProxy(contextId);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> removeContainerProxy(String contextId) {
|
||||
return _serviceLock.synchronized(() async {
|
||||
await _waitHealthcheck().timeout(const Duration(seconds: 10));
|
||||
|
||||
return _service.removeContainerProxy(contextId);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> setSiteAssignments(List<SiteAssignment> assignements) {
|
||||
return _serviceLock.synchronized(() async {
|
||||
await _waitHealthcheck().timeout(const Duration(seconds: 10));
|
||||
|
||||
return _service.setSiteAssignments(
|
||||
Map.fromEntries(
|
||||
assignements.map(
|
||||
(e) => MapEntry(
|
||||
e.assignedSite.origin,
|
||||
e.contextualIdentity ?? 'general',
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _waitHealthcheck({
|
||||
Duration timeout = const Duration(seconds: 15),
|
||||
}) async {
|
||||
final startTime = DateTime.now();
|
||||
|
||||
var healthy = await _service.healthcheck();
|
||||
while (!healthy) {
|
||||
if (DateTime.now().difference(startTime) > timeout) {
|
||||
throw TimeoutException('Timed out waiting for proxy service');
|
||||
}
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 25));
|
||||
healthy = await _service.healthcheck();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void build() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'tor_proxy.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
|
||||
@ProviderFor(TorProxyRepository)
|
||||
final torProxyRepositoryProvider = TorProxyRepositoryProvider._();
|
||||
|
||||
final class TorProxyRepositoryProvider
|
||||
extends $NotifierProvider<TorProxyRepository, void> {
|
||||
TorProxyRepositoryProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'torProxyRepositoryProvider',
|
||||
isAutoDispose: false,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$torProxyRepositoryHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
TorProxyRepository create() => TorProxyRepository();
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(void value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<void>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$torProxyRepositoryHash() =>
|
||||
r'83c2976750f3f7907274b1ae4f926cd1de89be83';
|
||||
|
||||
abstract class _$TorProxyRepository extends $Notifier<void> {
|
||||
void build();
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
final ref = this.ref as $Ref<void, void>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<void, void>,
|
||||
void,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleCreate(ref, build);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
* 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 'dart:async';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter_tor/flutter_tor.dart';
|
||||
import 'package:nullability/nullability.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:weblibre/core/logger.dart';
|
||||
import 'package:weblibre/features/tor/data/models/moat.dart';
|
||||
import 'package:weblibre/features/tor/data/services/moat_service.dart';
|
||||
import 'package:weblibre/features/tor/domain/repositories/builtin_bridges.dart';
|
||||
import 'package:weblibre/features/user/data/models/tor_settings.dart';
|
||||
import 'package:weblibre/features/user/domain/repositories/tor_settings.dart';
|
||||
|
||||
part 'tor_proxy.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class TorProxyService extends _$TorProxyService {
|
||||
final _tor = FlutterTor();
|
||||
late StreamController<TorStatus> _statusSyncController;
|
||||
|
||||
Future<TorStatus> startOrReconfigure({
|
||||
required bool reconfigureIfRunning,
|
||||
}) async {
|
||||
final currentStatus = await _tor.getStatus();
|
||||
|
||||
if (!currentStatus.isRunning ||
|
||||
currentStatus.socksPort == null ||
|
||||
reconfigureIfRunning) {
|
||||
state = const AsyncLoading();
|
||||
|
||||
final torSettings = await ref
|
||||
.read(torSettingsRepositoryProvider.notifier)
|
||||
.fetchSettings();
|
||||
|
||||
Setting? setting;
|
||||
if (torSettings.config == TorConnectionConfig.auto) {
|
||||
List<Setting>? config;
|
||||
|
||||
final moat = MoatService();
|
||||
try {
|
||||
await moat.initialize();
|
||||
config = await moat.autoConf(
|
||||
cannotConnectWithoutPt: torSettings.requireBridge,
|
||||
);
|
||||
|
||||
if (config == null && torSettings.requireBridge) {
|
||||
config = MoatService.convertBuiltinToSettings(
|
||||
await ref
|
||||
.read(builtinBridgesRepositoryProvider(moat).notifier)
|
||||
.getBridges(),
|
||||
);
|
||||
}
|
||||
} catch (e, s) {
|
||||
logger.e('Failed auto configure bridges', error: e, stackTrace: s);
|
||||
} finally {
|
||||
await moat.dispose();
|
||||
}
|
||||
|
||||
setting = config.mapNotNull(
|
||||
(config) =>
|
||||
config.firstWhereOrNull(
|
||||
(setting) => setting.bridge.type == MoatTransportType.obfs4,
|
||||
) ??
|
||||
config.firstWhereOrNull(
|
||||
(setting) => setting.bridge.type == MoatTransportType.snowflake,
|
||||
),
|
||||
);
|
||||
} else if (torSettings.config != TorConnectionConfig.direct) {
|
||||
List<Setting>? config;
|
||||
|
||||
final moat = MoatService();
|
||||
try {
|
||||
await moat.initialize();
|
||||
if (torSettings.fetchRemoteBridges) {
|
||||
config = await moat.getDefaultBridges();
|
||||
}
|
||||
|
||||
if (config == null &&
|
||||
(torSettings.requireBridge || !torSettings.fetchRemoteBridges)) {
|
||||
config = MoatService.convertBuiltinToSettings(
|
||||
await ref
|
||||
.read(builtinBridgesRepositoryProvider(moat).notifier)
|
||||
.getBridges(tryUpdate: torSettings.fetchRemoteBridges),
|
||||
);
|
||||
}
|
||||
|
||||
setting = config.mapNotNull(
|
||||
(config) => config.firstWhereOrNull(
|
||||
(setting) =>
|
||||
setting.bridge.type ==
|
||||
switch (torSettings.config) {
|
||||
TorConnectionConfig.auto => throw UnimplementedError(
|
||||
'TorConnectionConfig.auto bridge type not supported',
|
||||
),
|
||||
TorConnectionConfig.direct => throw UnimplementedError(
|
||||
'TorConnectionConfig.direct does not use bridges',
|
||||
),
|
||||
TorConnectionConfig.obfs4 => MoatTransportType.obfs4,
|
||||
TorConnectionConfig.snowflake =>
|
||||
MoatTransportType.snowflake,
|
||||
},
|
||||
),
|
||||
);
|
||||
} catch (e, s) {
|
||||
logger.e('Failed auto configure bridges', error: e, stackTrace: s);
|
||||
} finally {
|
||||
await moat.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
final config = TorConfiguration(
|
||||
transport: switch (setting?.bridge.type) {
|
||||
MoatTransportType.obfs4 => TransportType.obfs4,
|
||||
MoatTransportType.snowflake => TransportType.snowflake,
|
||||
MoatTransportType.meek => TransportType.meek,
|
||||
MoatTransportType.meekAzure => TransportType.meekAzure,
|
||||
MoatTransportType.webtunnel => TransportType.webtunnel,
|
||||
null => TransportType.none,
|
||||
},
|
||||
bridgeLines: setting?.bridge.bridges ?? [],
|
||||
entryNodeCountries: torSettings.entryNodeCountry?.toLowerCase(),
|
||||
exitNodeCountries: torSettings.exitNodeCountry?.toLowerCase(),
|
||||
);
|
||||
|
||||
await _tor.start(config);
|
||||
return _tor.getStatus();
|
||||
}
|
||||
|
||||
return currentStatus;
|
||||
}
|
||||
|
||||
Future<TorStatus> requestSync() async {
|
||||
final status = await _tor.getStatus();
|
||||
_statusSyncController.add(status);
|
||||
return status;
|
||||
}
|
||||
|
||||
Future<void> disconnect() async {
|
||||
await _tor.stop();
|
||||
}
|
||||
|
||||
Future<void> requestNewIdentity() async {
|
||||
await _tor.requestNewIdentity();
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<TorStatus> build() {
|
||||
_statusSyncController = StreamController();
|
||||
|
||||
ref.onDispose(() async {
|
||||
await _statusSyncController.close();
|
||||
await _tor.stop();
|
||||
});
|
||||
|
||||
return MergeStream([_tor.statusStream, _statusSyncController.stream]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'tor_proxy.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
|
||||
@ProviderFor(TorProxyService)
|
||||
final torProxyServiceProvider = TorProxyServiceProvider._();
|
||||
|
||||
final class TorProxyServiceProvider
|
||||
extends $StreamNotifierProvider<TorProxyService, TorStatus> {
|
||||
TorProxyServiceProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'torProxyServiceProvider',
|
||||
isAutoDispose: false,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$torProxyServiceHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
TorProxyService create() => TorProxyService();
|
||||
}
|
||||
|
||||
String _$torProxyServiceHash() => r'7b430ca32fbfc9ebebb1e52271c0183efdf60971';
|
||||
|
||||
abstract class _$TorProxyService extends $StreamNotifier<TorStatus> {
|
||||
Stream<TorStatus> build();
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
final ref = this.ref as $Ref<AsyncValue<TorStatus>, TorStatus>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<AsyncValue<TorStatus>, TorStatus>,
|
||||
AsyncValue<TorStatus>,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleCreate(ref, build);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user