Add proxy http adapter
This commit is contained in:
@@ -11,6 +11,16 @@ ConfigOptionRepository configOptionRepository(
|
||||
) {
|
||||
return ConfigOptionRepositoryImpl(
|
||||
preferences: ref.watch(sharedPreferencesProvider).requireValue,
|
||||
);
|
||||
}
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
SingBoxConfigOptionRepository singBoxConfigOptionRepository(
|
||||
SingBoxConfigOptionRepositoryRef ref,
|
||||
) {
|
||||
return SingBoxConfigOptionRepositoryImpl(
|
||||
preferences: ref.watch(sharedPreferencesProvider).requireValue,
|
||||
optionsRepository: ref.watch(configOptionRepositoryProvider),
|
||||
geoAssetRepository: ref.watch(geoAssetRepositoryProvider).requireValue,
|
||||
geoAssetPathResolver: ref.watch(geoAssetPathResolverProvider),
|
||||
);
|
||||
|
||||
@@ -14,25 +14,115 @@ import 'package:meta/meta.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
abstract interface class ConfigOptionRepository {
|
||||
TaskEither<ConfigOptionFailure, SingboxConfigOption>
|
||||
getFullSingboxConfigOption();
|
||||
TaskEither<ConfigOptionFailure, ConfigOptionEntity> getConfigOption();
|
||||
Either<ConfigOptionFailure, ConfigOptionEntity> getConfigOption();
|
||||
TaskEither<ConfigOptionFailure, Unit> updateConfigOption(
|
||||
ConfigOptionPatch patch,
|
||||
);
|
||||
TaskEither<ConfigOptionFailure, Unit> resetConfigOption();
|
||||
}
|
||||
|
||||
abstract interface class SingBoxConfigOptionRepository {
|
||||
TaskEither<ConfigOptionFailure, SingboxConfigOption>
|
||||
getFullSingboxConfigOption();
|
||||
}
|
||||
|
||||
class ConfigOptionRepositoryImpl
|
||||
with ExceptionHandler, InfraLogger
|
||||
implements ConfigOptionRepository {
|
||||
ConfigOptionRepositoryImpl({
|
||||
ConfigOptionRepositoryImpl({required this.preferences});
|
||||
|
||||
final SharedPreferences preferences;
|
||||
|
||||
@override
|
||||
Either<ConfigOptionFailure, ConfigOptionEntity> getConfigOption() {
|
||||
try {
|
||||
final map = ConfigOptionEntity.initial.toJson();
|
||||
for (final key in map.keys) {
|
||||
final persisted = preferences.get(key);
|
||||
if (persisted != null) {
|
||||
final defaultValue = map[key];
|
||||
if (defaultValue != null &&
|
||||
persisted.runtimeType != defaultValue.runtimeType) {
|
||||
loggy.warning(
|
||||
"error getting preference[$key], expected type: [${defaultValue.runtimeType}] - received value: [$persisted](${persisted.runtimeType})",
|
||||
);
|
||||
continue;
|
||||
}
|
||||
map[key] = persisted;
|
||||
}
|
||||
}
|
||||
final options = ConfigOptionEntity.fromJson(map);
|
||||
return right(options);
|
||||
} catch (error, stackTrace) {
|
||||
return left(ConfigOptionUnexpectedFailure(error, stackTrace));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ConfigOptionFailure, Unit> updateConfigOption(
|
||||
ConfigOptionPatch patch,
|
||||
) {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
final map = patch.toJson();
|
||||
await updateByJson(map);
|
||||
return right(unit);
|
||||
},
|
||||
ConfigOptionUnexpectedFailure.new,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ConfigOptionFailure, Unit> resetConfigOption() {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
final map = ConfigOptionEntity.initial.toJson();
|
||||
await updateByJson(map);
|
||||
return right(unit);
|
||||
},
|
||||
ConfigOptionUnexpectedFailure.new,
|
||||
);
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
Future<void> updateByJson(
|
||||
Map<String, dynamic> options,
|
||||
) async {
|
||||
final map = ConfigOptionEntity.initial.toJson();
|
||||
for (final key in map.keys) {
|
||||
final value = options[key];
|
||||
if (value != null) {
|
||||
loggy.debug("updating [$key] to [$value]");
|
||||
|
||||
switch (value) {
|
||||
case bool _:
|
||||
await preferences.setBool(key, value);
|
||||
case String _:
|
||||
await preferences.setString(key, value);
|
||||
case int _:
|
||||
await preferences.setInt(key, value);
|
||||
case double _:
|
||||
await preferences.setDouble(key, value);
|
||||
default:
|
||||
loggy.warning("unexpected type");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SingBoxConfigOptionRepositoryImpl
|
||||
with ExceptionHandler, InfraLogger
|
||||
implements SingBoxConfigOptionRepository {
|
||||
SingBoxConfigOptionRepositoryImpl({
|
||||
required this.preferences,
|
||||
required this.optionsRepository,
|
||||
required this.geoAssetRepository,
|
||||
required this.geoAssetPathResolver,
|
||||
});
|
||||
|
||||
final SharedPreferences preferences;
|
||||
final ConfigOptionRepository optionsRepository;
|
||||
final GeoAssetRepository geoAssetRepository;
|
||||
final GeoAssetPathResolver geoAssetPathResolver;
|
||||
|
||||
@@ -81,7 +171,7 @@ class ConfigOptionRepositoryImpl
|
||||
.run();
|
||||
|
||||
final persisted =
|
||||
await getConfigOption().getOrElse((l) => throw l).run();
|
||||
optionsRepository.getConfigOption().getOrElse((l) => throw l);
|
||||
final singboxConfigOption = SingboxConfigOption(
|
||||
executeConfigAsIs: false,
|
||||
logLevel: persisted.logLevel,
|
||||
@@ -138,82 +228,4 @@ class ConfigOptionRepositoryImpl
|
||||
ConfigOptionUnexpectedFailure.new,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ConfigOptionFailure, ConfigOptionEntity> getConfigOption() {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
final map = ConfigOptionEntity.initial.toJson();
|
||||
for (final key in map.keys) {
|
||||
final persisted = preferences.get(key);
|
||||
if (persisted != null) {
|
||||
final defaultValue = map[key];
|
||||
if (defaultValue != null &&
|
||||
persisted.runtimeType != defaultValue.runtimeType) {
|
||||
loggy.warning(
|
||||
"error getting preference[$key], expected type: [${defaultValue.runtimeType}] - received value: [$persisted](${persisted.runtimeType})",
|
||||
);
|
||||
continue;
|
||||
}
|
||||
map[key] = persisted;
|
||||
}
|
||||
}
|
||||
final options = ConfigOptionEntity.fromJson(map);
|
||||
return right(options);
|
||||
},
|
||||
ConfigOptionUnexpectedFailure.new,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ConfigOptionFailure, Unit> updateConfigOption(
|
||||
ConfigOptionPatch patch,
|
||||
) {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
final map = patch.toJson();
|
||||
await updateByJson(map);
|
||||
return right(unit);
|
||||
},
|
||||
ConfigOptionUnexpectedFailure.new,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ConfigOptionFailure, Unit> resetConfigOption() {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
final map = ConfigOptionEntity.initial.toJson();
|
||||
await updateByJson(map);
|
||||
return right(unit);
|
||||
},
|
||||
ConfigOptionUnexpectedFailure.new,
|
||||
);
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
Future<void> updateByJson(
|
||||
Map<String, dynamic> options,
|
||||
) async {
|
||||
final map = ConfigOptionEntity.initial.toJson();
|
||||
for (final key in map.keys) {
|
||||
final value = options[key];
|
||||
if (value != null) {
|
||||
loggy.debug("updating [$key] to [$value]");
|
||||
|
||||
switch (value) {
|
||||
case bool _:
|
||||
await preferences.setBool(key, value);
|
||||
case String _:
|
||||
await preferences.setString(key, value);
|
||||
case int _:
|
||||
await preferences.setInt(key, value);
|
||||
case double _:
|
||||
await preferences.setDouble(key, value);
|
||||
default:
|
||||
loggy.warning("unexpected type");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,14 +9,14 @@ part 'config_option_notifier.g.dart';
|
||||
@Riverpod(keepAlive: true)
|
||||
class ConfigOptionNotifier extends _$ConfigOptionNotifier with AppLogger {
|
||||
@override
|
||||
Future<ConfigOptionEntity> build() {
|
||||
Future<ConfigOptionEntity> build() async {
|
||||
return ref
|
||||
.watch(configOptionRepositoryProvider)
|
||||
.getConfigOption()
|
||||
.getOrElse((l) {
|
||||
loggy.error("error getting persisted options $l", l);
|
||||
throw l;
|
||||
}).run();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> updateOption(ConfigOptionPatch patch) async {
|
||||
|
||||
@@ -15,7 +15,8 @@ ConnectionRepository connectionRepository(
|
||||
) {
|
||||
return ConnectionRepositoryImpl(
|
||||
directories: ref.watch(appDirectoriesProvider).requireValue,
|
||||
configOptionRepository: ref.watch(configOptionRepositoryProvider),
|
||||
singBoxConfigOptionRepository:
|
||||
ref.watch(singBoxConfigOptionRepositoryProvider),
|
||||
singbox: ref.watch(singboxServiceProvider),
|
||||
platformSource: ConnectionPlatformSourceImpl(),
|
||||
profilePathResolver: ref.watch(profilePathResolverProvider),
|
||||
|
||||
@@ -38,7 +38,7 @@ class ConnectionRepositoryImpl
|
||||
required this.directories,
|
||||
required this.singbox,
|
||||
required this.platformSource,
|
||||
required this.configOptionRepository,
|
||||
required this.singBoxConfigOptionRepository,
|
||||
required this.profilePathResolver,
|
||||
required this.geoAssetPathResolver,
|
||||
});
|
||||
@@ -46,7 +46,7 @@ class ConnectionRepositoryImpl
|
||||
final Directories directories;
|
||||
final SingboxService singbox;
|
||||
final ConnectionPlatformSource platformSource;
|
||||
final ConfigOptionRepository configOptionRepository;
|
||||
final SingBoxConfigOptionRepository singBoxConfigOptionRepository;
|
||||
final ProfilePathResolver profilePathResolver;
|
||||
final GeoAssetPathResolver geoAssetPathResolver;
|
||||
|
||||
@@ -83,7 +83,7 @@ class ConnectionRepositoryImpl
|
||||
return TaskEither<ConnectionFailure, SingboxConfigOption>.Do(
|
||||
($) async {
|
||||
final options = await $(
|
||||
configOptionRepository
|
||||
singBoxConfigOptionRepository
|
||||
.getFullSingboxConfigOption()
|
||||
.mapLeft((l) => const InvalidConfigOption()),
|
||||
);
|
||||
@@ -189,7 +189,7 @@ class ConnectionRepositoryImpl
|
||||
($) async {
|
||||
final options = await $(getConfigOption());
|
||||
|
||||
await $(
|
||||
await $(
|
||||
TaskEither(() async {
|
||||
if (options.enableTun) {
|
||||
// final hasPrivilege = await platformSource.checkPrivilege();
|
||||
@@ -202,13 +202,10 @@ class ConnectionRepositoryImpl
|
||||
}),
|
||||
);
|
||||
return await $(
|
||||
singbox.stop()
|
||||
.mapLeft(UnexpectedConnectionFailure.new),
|
||||
singbox.stop().mapLeft(UnexpectedConnectionFailure.new),
|
||||
);
|
||||
},
|
||||
).handleExceptions(UnexpectedConnectionFailure.new);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
Reference in New Issue
Block a user