diff --git a/lib/features/connection/data/connection_platform_source.dart b/lib/features/connection/data/connection_platform_source.dart index 3fc5dd67..e0e88a7c 100644 --- a/lib/features/connection/data/connection_platform_source.dart +++ b/lib/features/connection/data/connection_platform_source.dart @@ -12,8 +12,6 @@ import 'package:win32/win32.dart'; abstract interface class ConnectionPlatformSource { Future checkPrivilege(); - Future activateTunnel(); - Future deactivateTunnel(); } class ConnectionPlatformSourceImpl @@ -63,122 +61,6 @@ class ConnectionPlatformSourceImpl return true; // return true so core handles it } } - - @override - Future activateTunnel() async { - if (!Platform.isWindows && !Platform.isLinux) { - return await checkPrivilege(); - } - try { - final socket = await Socket.connect('127.0.0.1', 18020, - timeout: Duration(seconds: 1)); - await socket.close(); - return await startTunnelRequest(); - } catch (error) { - loggy.warning( - 'Tunnel Service is not running. Error: $error.--> Running...'); - return await runTunnelService(); - } - } - - @override - Future deactivateTunnel() async { - if (!Platform.isWindows && !Platform.isLinux && !Platform.isMacOS) { - return true; - } - try { - return await stopTunnelRequest(); - } catch (error) { - loggy.error('Tunnel Service Stop Error: $error.'); - return false; - } - } - - Future startTunnelRequest() async { - final params = { - "Ipv6": false, - "ServerPort": "2334", - "StrictRoute": false, - "EndpointIndependentNat": false, - "Stack": "gvisor", - }; - - final query = mapToQueryString(params); - - try { - final request = - await HttpClient().get('localhost', 18020, "/start?$query"); - final response = await request.close(); - final body = await response.transform(utf8.decoder).join(); - loggy.debug( - 'Status Code: ${response.statusCode} ${response.reasonPhrase}'); - loggy.debug('Response Body: ${body}'); - return true; - } catch (error) { - loggy.error('HTTP Request Error: $error'); - return false; - } - } - - Future stopTunnelRequest() async { - try { - final request = await HttpClient().get('localhost', 18020, "/stop"); - final response = await request.close(); - final body = await response.transform(utf8.decoder).join(); - loggy.debug( - 'Status Code: ${response.statusCode} ${response.reasonPhrase}'); - loggy.debug('Response Body: ${body}'); - return true; - } catch (error) { - loggy.error('HTTP Request Error: $error'); - return false; - } - } - - String mapToQueryString(Map params) { - return params.entries.map((entry) { - final key = Uri.encodeQueryComponent(entry.key); - final value = Uri.encodeQueryComponent(entry.value.toString()); - return '$key=$value'; - }).join('&'); - } - - Future runTunnelService() async { - final executablePath = getTunnelServicePath(); - - var command = [executablePath, "install"]; - if (Platform.isLinux) { - command.insert(0, 'pkexec'); - } - - try { - final result = - await Process.run(command[0], command.sublist(1), runInShell: true); - loggy.debug('Shell command executed: ${result.stdout} ${result.stderr}'); - return await startTunnelRequest(); - } catch (error) { - loggy.error('Error executing shell command: $error'); - return false; - } - } - - static String getTunnelServicePath() { - String fullPath = ""; - final binFolder = - Directory(Platform.resolvedExecutable).parent.absolute.path; - if (Platform.environment.containsKey('FLUTTER_TEST')) { - fullPath = "libcore"; - } - if (Platform.isWindows) { - fullPath = p.join(fullPath, "HiddifyService.exe"); - } else if (Platform.isMacOS) { - fullPath = p.join(fullPath, "HiddifyService"); - } else { - fullPath = p.join(fullPath, "HiddifyService"); - } - - return "$binFolder/$fullPath"; - } } sealed class _TokenElevation extends Struct { diff --git a/lib/features/connection/data/connection_repository.dart b/lib/features/connection/data/connection_repository.dart index 57eb892e..e4d55c9b 100644 --- a/lib/features/connection/data/connection_repository.dart +++ b/lib/features/connection/data/connection_repository.dart @@ -159,11 +159,6 @@ class ConnectionRepositoryImpl await $( TaskEither(() async { if (options.enableTun) { - final active = await platformSource.activateTunnel(); - if (!active) { - loggy.warning("Possiblity missing privileges for tun mode"); - return left(const MissingPrivilege()); - } // final hasPrivilege = await platformSource.checkPrivilege(); // if (!hasPrivilege) { // loggy.warning("missing privileges for tun mode"); @@ -197,11 +192,6 @@ class ConnectionRepositoryImpl await $( TaskEither(() async { if (options.enableTun) { - final active = await platformSource.deactivateTunnel(); - if (!active) { - loggy.warning("Possiblity missing privileges for tun mode"); - return left(const MissingPrivilege()); - } // final hasPrivilege = await platformSource.checkPrivilege(); // if (!hasPrivilege) { // loggy.warning("missing privileges for tun mode");