Refactor
This commit is contained in:
24
lib/features/connection/data/connection_data_providers.dart
Normal file
24
lib/features/connection/data/connection_data_providers.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
import 'package:hiddify/features/config_option/data/config_option_data_providers.dart';
|
||||
import 'package:hiddify/features/connection/data/connection_platform_source.dart';
|
||||
import 'package:hiddify/features/connection/data/connection_repository.dart';
|
||||
import 'package:hiddify/features/geo_asset/data/geo_asset_data_providers.dart';
|
||||
import 'package:hiddify/features/profile/data/profile_data_providers.dart';
|
||||
import 'package:hiddify/services/service_providers.dart';
|
||||
import 'package:hiddify/singbox/service/singbox_service_provider.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'connection_data_providers.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
ConnectionRepository connectionRepository(
|
||||
ConnectionRepositoryRef ref,
|
||||
) {
|
||||
return ConnectionRepositoryImpl(
|
||||
directories: ref.watch(filesEditorServiceProvider).dirs,
|
||||
configOptionRepository: ref.watch(configOptionRepositoryProvider),
|
||||
singbox: ref.watch(singboxServiceProvider),
|
||||
platformSource: ConnectionPlatformSourceImpl(),
|
||||
profilePathResolver: ref.watch(profilePathResolverProvider),
|
||||
geoAssetPathResolver: ref.watch(geoAssetPathResolverProvider),
|
||||
);
|
||||
}
|
||||
67
lib/features/connection/data/connection_platform_source.dart
Normal file
67
lib/features/connection/data/connection_platform_source.dart
Normal file
@@ -0,0 +1,67 @@
|
||||
import 'dart:ffi';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:hiddify/core/utils/ffi_utils.dart';
|
||||
import 'package:hiddify/utils/custom_loggers.dart';
|
||||
import 'package:posix/posix.dart';
|
||||
import 'package:win32/win32.dart';
|
||||
|
||||
abstract interface class ConnectionPlatformSource {
|
||||
Future<bool> checkPrivilege();
|
||||
}
|
||||
|
||||
class ConnectionPlatformSourceImpl
|
||||
with InfraLogger
|
||||
implements ConnectionPlatformSource {
|
||||
@override
|
||||
Future<bool> checkPrivilege() async {
|
||||
try {
|
||||
if (Platform.isWindows) {
|
||||
bool isElevated = false;
|
||||
withMemory<void, Uint32>(sizeOf<Uint32>(), (phToken) {
|
||||
withMemory<void, Uint32>(sizeOf<Uint32>(), (pReturnedSize) {
|
||||
withMemory<void, _TokenElevation>(sizeOf<_TokenElevation>(),
|
||||
(pElevation) {
|
||||
if (OpenProcessToken(
|
||||
GetCurrentProcess(),
|
||||
TOKEN_QUERY,
|
||||
phToken.cast(),
|
||||
) ==
|
||||
1) {
|
||||
if (GetTokenInformation(
|
||||
phToken.value,
|
||||
TOKEN_INFORMATION_CLASS.TokenElevation,
|
||||
pElevation,
|
||||
sizeOf<_TokenElevation>(),
|
||||
pReturnedSize,
|
||||
) ==
|
||||
1) {
|
||||
isElevated = pElevation.ref.tokenIsElevated != 0;
|
||||
}
|
||||
}
|
||||
if (phToken.value != 0) {
|
||||
CloseHandle(phToken.value);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
return isElevated;
|
||||
} else if (Platform.isLinux || Platform.isMacOS) {
|
||||
final euid = geteuid();
|
||||
return euid == 0;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} catch (e) {
|
||||
loggy.warning("error checking privilege", e);
|
||||
return true; // return true so core handles it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sealed class _TokenElevation extends Struct {
|
||||
/// A nonzero value if the token has elevated privileges;
|
||||
/// otherwise, a zero value.
|
||||
@Int32()
|
||||
external int tokenIsElevated;
|
||||
}
|
||||
214
lib/features/connection/data/connection_repository.dart
Normal file
214
lib/features/connection/data/connection_repository.dart
Normal file
@@ -0,0 +1,214 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:hiddify/core/model/directories.dart';
|
||||
import 'package:hiddify/core/utils/exception_handler.dart';
|
||||
import 'package:hiddify/features/config_option/data/config_option_repository.dart';
|
||||
import 'package:hiddify/features/connection/data/connection_platform_source.dart';
|
||||
import 'package:hiddify/features/connection/model/connection_failure.dart';
|
||||
import 'package:hiddify/features/connection/model/connection_status.dart';
|
||||
import 'package:hiddify/features/geo_asset/data/geo_asset_path_resolver.dart';
|
||||
import 'package:hiddify/features/profile/data/profile_path_resolver.dart';
|
||||
import 'package:hiddify/singbox/model/singbox_config_option.dart';
|
||||
import 'package:hiddify/singbox/model/singbox_status.dart';
|
||||
import 'package:hiddify/singbox/service/singbox_service.dart';
|
||||
import 'package:hiddify/utils/utils.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
abstract interface class ConnectionRepository {
|
||||
Stream<ConnectionStatus> watchConnectionStatus();
|
||||
TaskEither<ConnectionFailure, Unit> connect(
|
||||
String fileName,
|
||||
bool disableMemoryLimit,
|
||||
);
|
||||
TaskEither<ConnectionFailure, Unit> disconnect();
|
||||
TaskEither<ConnectionFailure, Unit> reconnect(
|
||||
String fileName,
|
||||
bool disableMemoryLimit,
|
||||
);
|
||||
}
|
||||
|
||||
class ConnectionRepositoryImpl
|
||||
with ExceptionHandler, InfraLogger
|
||||
implements ConnectionRepository {
|
||||
ConnectionRepositoryImpl({
|
||||
required this.directories,
|
||||
required this.singbox,
|
||||
required this.platformSource,
|
||||
required this.configOptionRepository,
|
||||
required this.profilePathResolver,
|
||||
required this.geoAssetPathResolver,
|
||||
});
|
||||
|
||||
final Directories directories;
|
||||
final SingboxService singbox;
|
||||
final ConnectionPlatformSource platformSource;
|
||||
final ConfigOptionRepository configOptionRepository;
|
||||
final ProfilePathResolver profilePathResolver;
|
||||
final GeoAssetPathResolver geoAssetPathResolver;
|
||||
|
||||
bool _initialized = false;
|
||||
|
||||
@override
|
||||
Stream<ConnectionStatus> watchConnectionStatus() {
|
||||
return singbox.watchStatus().map(
|
||||
(event) => switch (event) {
|
||||
SingboxStopped(:final alert?, :final message) => Disconnected(
|
||||
switch (alert) {
|
||||
SingboxAlert.emptyConfiguration =>
|
||||
ConnectionFailure.invalidConfig(message),
|
||||
SingboxAlert.requestNotificationPermission =>
|
||||
ConnectionFailure.missingNotificationPermission(message),
|
||||
SingboxAlert.requestVPNPermission =>
|
||||
ConnectionFailure.missingVpnPermission(message),
|
||||
SingboxAlert.startCommandServer ||
|
||||
SingboxAlert.createService ||
|
||||
SingboxAlert.startService =>
|
||||
ConnectionFailure.unexpected(message),
|
||||
},
|
||||
),
|
||||
SingboxStopped() => const Disconnected(),
|
||||
SingboxStarting() => const Connecting(),
|
||||
SingboxStarted() => const Connected(),
|
||||
SingboxStopping() => const Disconnecting(),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
TaskEither<ConnectionFailure, SingboxConfigOption> getConfigOption() {
|
||||
return TaskEither<ConnectionFailure, SingboxConfigOption>.Do(
|
||||
($) async {
|
||||
final options = await $(
|
||||
configOptionRepository
|
||||
.getFullSingboxConfigOption()
|
||||
.mapLeft((l) => const InvalidConfigOption()),
|
||||
);
|
||||
|
||||
return $(
|
||||
TaskEither(
|
||||
() async {
|
||||
final geoip = geoAssetPathResolver.resolvePath(options.geoipPath);
|
||||
final geosite =
|
||||
geoAssetPathResolver.resolvePath(options.geositePath);
|
||||
if (!await File(geoip).exists() ||
|
||||
!await File(geosite).exists()) {
|
||||
return left(const ConnectionFailure.missingGeoAssets());
|
||||
}
|
||||
return right(options);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
).handleExceptions(UnexpectedConnectionFailure.new);
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
TaskEither<ConnectionFailure, Unit> applyConfigOption(
|
||||
SingboxConfigOption options,
|
||||
) {
|
||||
return exceptionHandler(
|
||||
() {
|
||||
return singbox
|
||||
.changeOptions(options)
|
||||
.mapLeft(InvalidConfigOption.new)
|
||||
.run();
|
||||
},
|
||||
UnexpectedConnectionFailure.new,
|
||||
);
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
TaskEither<ConnectionFailure, Unit> setup() {
|
||||
if (_initialized) return TaskEither.of(unit);
|
||||
return exceptionHandler(
|
||||
() {
|
||||
loggy.debug("setting up singbox");
|
||||
return singbox
|
||||
.setup(
|
||||
directories,
|
||||
false,
|
||||
)
|
||||
.map((r) {
|
||||
loggy.debug("setup complete");
|
||||
_initialized = true;
|
||||
return r;
|
||||
})
|
||||
.mapLeft(UnexpectedConnectionFailure.new)
|
||||
.run();
|
||||
},
|
||||
UnexpectedConnectionFailure.new,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ConnectionFailure, Unit> connect(
|
||||
String fileName,
|
||||
bool disableMemoryLimit,
|
||||
) {
|
||||
return TaskEither<ConnectionFailure, Unit>.Do(
|
||||
($) async {
|
||||
final options = await $(getConfigOption());
|
||||
loggy.info(
|
||||
"config options: ${options.format()}\nMemory Limit: ${!disableMemoryLimit}",
|
||||
);
|
||||
|
||||
await $(
|
||||
TaskEither(() async {
|
||||
if (options.enableTun) {
|
||||
final hasPrivilege = await platformSource.checkPrivilege();
|
||||
if (!hasPrivilege) {
|
||||
loggy.warning("missing privileges for tun mode");
|
||||
return left(const MissingPrivilege());
|
||||
}
|
||||
}
|
||||
return right(unit);
|
||||
}),
|
||||
);
|
||||
await $(setup());
|
||||
loggy.debug("after setup");
|
||||
await $(applyConfigOption(options));
|
||||
loggy.debug("after apply");
|
||||
return await $(
|
||||
singbox
|
||||
.start(
|
||||
profilePathResolver.file(fileName).path,
|
||||
disableMemoryLimit,
|
||||
)
|
||||
.mapLeft(UnexpectedConnectionFailure.new),
|
||||
);
|
||||
},
|
||||
).handleExceptions(UnexpectedConnectionFailure.new);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ConnectionFailure, Unit> disconnect() {
|
||||
return exceptionHandler(
|
||||
() => singbox.stop().mapLeft(UnexpectedConnectionFailure.new).run(),
|
||||
UnexpectedConnectionFailure.new,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
TaskEither<ConnectionFailure, Unit> reconnect(
|
||||
String fileName,
|
||||
bool disableMemoryLimit,
|
||||
) {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
return getConfigOption()
|
||||
.flatMap((options) => applyConfigOption(options))
|
||||
.andThen(
|
||||
() => singbox
|
||||
.restart(
|
||||
profilePathResolver.file(fileName).path,
|
||||
disableMemoryLimit,
|
||||
)
|
||||
.mapLeft(UnexpectedConnectionFailure.new),
|
||||
)
|
||||
.run();
|
||||
},
|
||||
UnexpectedConnectionFailure.new,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user