From 0f228f2b3c6b829fd679efa88fc301f2aadf7fce Mon Sep 17 00:00:00 2001 From: git-elliot Date: Sun, 17 Mar 2024 14:56:46 +0530 Subject: [PATCH 1/4] Scan different subnet other than local one --- android/app/build.gradle | 4 +- lib/helper/app_settings.dart | 17 +++++ lib/main.dart | 2 +- .../host_scna_bloc/host_scan_bloc.dart | 71 ++++++++++--------- .../widgets/host_scan_widget.dart | 13 ++-- .../network_troubleshoot/port_scan_page.dart | 1 + lib/pages/settings_page.dart | 22 ++++++ .../settings_dialog/custom_subnet_dialog.dart | 45 ++++++++++++ lib/values/strings.dart | 5 ++ 9 files changed, 138 insertions(+), 42 deletions(-) create mode 100644 lib/ui/settings_dialog/custom_subnet_dialog.dart diff --git a/android/app/build.gradle b/android/app/build.gradle index c4c9b491..1e42c596 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -31,7 +31,7 @@ if (keystorePropertiesFile.exists()) { } android { - compileSdkVersion 33 + compileSdkVersion 34 sourceSets { main.java.srcDirs += 'src/main/kotlin' @@ -41,7 +41,7 @@ android { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "org.fsociety.vernet" minSdkVersion 19 - targetSdkVersion 33 + targetSdkVersion 34 versionCode flutterVersionCode.toInteger() versionName flutterVersionName } diff --git a/lib/helper/app_settings.dart b/lib/helper/app_settings.dart index f5221713..caa717e8 100644 --- a/lib/helper/app_settings.dart +++ b/lib/helper/app_settings.dart @@ -9,11 +9,13 @@ class AppSettings { static const String _socketTimeoutKey = 'AppSettings-SOCKET_TIMEOUT'; static const String _pingCountKey = 'AppSettings-PING_COUNT'; static const String _inAppInternetKey = 'AppSettings-IN-APP-INTERNET'; + static const String _customSubnetKey = 'AppSettings-CUSTOM-SUBNET'; int _firstSubnet = 1; int _lastSubnet = 254; int _socketTimeout = 500; int _pingCount = 5; bool _inAppInternet = false; + String _customSubnet = ''; static final AppSettings _instance = AppSettings._(); @@ -23,6 +25,10 @@ class AppSettings { int get socketTimeout => _socketTimeout; int get pingCount => _pingCount; bool get inAppInternet => _inAppInternet; + String get customSubnet => _customSubnet; + String get gatewayIP => _customSubnet.isNotEmpty + ? _customSubnet.substring(0, _customSubnet.lastIndexOf('.')) + : _customSubnet; Future setFirstSubnet(int firstSubnet) async { _firstSubnet = firstSubnet; @@ -54,6 +60,12 @@ class AppSettings { .setBool(_inAppInternetKey, _inAppInternet); } + Future setCustomSubnet(String customSubnet) async { + _customSubnet = customSubnet; + return (await SharedPreferences.getInstance()) + .setString(_customSubnetKey, _customSubnet); + } + Future load() async { debugPrint("Fetching all app settings"); _firstSubnet = @@ -80,5 +92,10 @@ class AppSettings { (await SharedPreferences.getInstance()).getBool(_inAppInternetKey) ?? _inAppInternet; debugPrint("In-App Internet : $_inAppInternet"); + + _customSubnet = + (await SharedPreferences.getInstance()).getString(_customSubnetKey) ?? + _customSubnet; + debugPrint("Custom Subnet : $_customSubnet"); } } diff --git a/lib/main.dart b/lib/main.dart index 687cac74..90ee73aa 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -16,7 +16,7 @@ Future main() async { configureDependencies(Env.prod); WidgetsFlutterBinding.ensureInitialized(); final appDocDirectory = await getApplicationDocumentsDirectory(); - await configureNetworkTools(appDocDirectory.path); + await configureNetworkToolsFlutter(appDocDirectory.path); final bool allowed = await ConsentLoader.isConsentPageShown(); // load app settings diff --git a/lib/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart b/lib/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart index 63a0ebda..99db55ee 100644 --- a/lib/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart +++ b/lib/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart @@ -39,8 +39,13 @@ class HostScanBloc extends Bloc { ) async { emit(const HostScanState.loadInProgress()); ip = await NetworkInfo().getWifiIP(); - subnet = ip!.substring(0, ip!.lastIndexOf('.')); - gatewayIp = await NetworkInfo().getWifiGatewayIP(); + gatewayIp = appSettings.customSubnet.isNotEmpty + ? appSettings.customSubnet + : await NetworkInfo().getWifiGatewayIP(); + subnet = gatewayIp!.substring(0, gatewayIp!.lastIndexOf('.')); + print('IP : $ip'); + print('Subnet : $subnet'); + print('GT IP : $gatewayIp'); add(const HostScanEvent.startNewScan()); } @@ -49,37 +54,37 @@ class HostScanBloc extends Bloc { StartNewScan event, Emitter emit, ) async { - MdnsScannerService.instance - .searchMdnsDevices() - .then((List activeHostList) async { - for (final ActiveHost activeHost in activeHostList) { - final int index = indexOfActiveHost(activeHost.address); - final MdnsInfo? mDns = await activeHost.mdnsInfo; - if (mDns == null) { - continue; - } - - if (index == -1) { - deviceInTheNetworkList.add( - DeviceInTheNetwork.createFromActiveHost( - activeHost: activeHost, - currentDeviceIp: ip!, - gatewayIp: gatewayIp!, - mdns: mDns, - mac: (await activeHost.arpData)?.macAddress, - ), - ); - } else { - deviceInTheNetworkList[index] = deviceInTheNetworkList[index] - ..mdns = mDns; - } - - deviceInTheNetworkList.sort(sort); - - emit(const HostScanState.loadInProgress()); - emit(HostScanState.foundNewDevice(deviceInTheNetworkList)); - } - }); + // MdnsScannerService.instance + // .searchMdnsDevices() + // .then((List activeHostList) async { + // for (final ActiveHost activeHost in activeHostList) { + // final int index = indexOfActiveHost(activeHost.address); + // final MdnsInfo? mDns = await activeHost.mdnsInfo; + // if (mDns == null) { + // continue; + // } + + // if (index == -1) { + // deviceInTheNetworkList.add( + // DeviceInTheNetwork.createFromActiveHost( + // activeHost: activeHost, + // currentDeviceIp: ip!, + // gatewayIp: gatewayIp!, + // mdns: mDns, + // mac: (await activeHost.arpData)?.macAddress, + // ), + // ); + // } else { + // deviceInTheNetworkList[index] = deviceInTheNetworkList[index] + // ..mdns = mDns; + // } + + // deviceInTheNetworkList.sort(sort); + + // emit(const HostScanState.loadInProgress()); + // emit(HostScanState.foundNewDevice(deviceInTheNetworkList)); + // } + // }); final streamController = HostScannerService.instance.getAllPingableDevices( subnet!, diff --git a/lib/pages/host_scan_page/widgets/host_scan_widget.dart b/lib/pages/host_scan_page/widgets/host_scan_widget.dart index 49ed3d30..118279b8 100644 --- a/lib/pages/host_scan_page/widgets/host_scan_widget.dart +++ b/lib/pages/host_scan_page/widgets/host_scan_widget.dart @@ -1,10 +1,10 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:vernet/main.dart'; import 'package:vernet/pages/host_scan_page/device_in_the_network.dart'; import 'package:vernet/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart'; import 'package:vernet/pages/network_troubleshoot/port_scan_page.dart'; -// import 'package:vernet/pages/port_scan_page/port_scan_page.dart'; class HostScanWidget extends StatelessWidget { @override @@ -17,16 +17,17 @@ class HostScanWidget extends StatelessWidget { return Center( child: Container( margin: const EdgeInsets.all(30), - child: const Column( + child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - CircularProgressIndicator(), - SizedBox( + const CircularProgressIndicator(), + const SizedBox( height: 30, ), Text( - 'Searching for devices in your local network', - style: TextStyle( + 'Searching for devices in your local network \n ${appSettings.gatewayIP}', + textAlign: TextAlign.center, + style: const TextStyle( fontSize: 18, color: Colors.blue, ), diff --git a/lib/pages/network_troubleshoot/port_scan_page.dart b/lib/pages/network_troubleshoot/port_scan_page.dart index 3cbd3ba3..cdd583eb 100644 --- a/lib/pages/network_troubleshoot/port_scan_page.dart +++ b/lib/pages/network_troubleshoot/port_scan_page.dart @@ -92,6 +92,7 @@ class _PortScanPageState extends State _targetIPEditingController.text, timeout: Duration(milliseconds: appSettings.socketTimeout), progressCallback: _handleProgress, + async: true, ) .listen(_handleEvent, onDone: _handleOnDone); } else { diff --git a/lib/pages/settings_page.dart b/lib/pages/settings_page.dart index 8ff9de18..fe55062d 100644 --- a/lib/pages/settings_page.dart +++ b/lib/pages/settings_page.dart @@ -5,6 +5,7 @@ import 'package:vernet/api/update_checker.dart'; import 'package:vernet/helper/utils_helper.dart'; import 'package:vernet/main.dart'; import 'package:vernet/models/dark_theme_provider.dart'; +import 'package:vernet/ui/settings_dialog/custom_subnet_dialog.dart'; import 'package:vernet/ui/settings_dialog/first_subnet_dialog.dart'; import 'package:vernet/ui/settings_dialog/last_subnet_dialog.dart'; import 'package:vernet/ui/settings_dialog/ping_count_dialog.dart'; @@ -137,6 +138,27 @@ class _SettingsPageState extends State { }, ), ), + Card( + child: ListTile( + title: const Text(StringValue.customSubnet), + subtitle: const Text(StringValue.customSubnetDesc), + trailing: Text( + appSettings.customSubnet, + style: Theme.of(context) + .textTheme + .titleSmall + ?.copyWith(color: Theme.of(context).colorScheme.secondary), + ), + onTap: () async { + await showDialog( + context: context, + builder: (context) => const CustomSubnetDialog(), + ); + await appSettings.load(); + setState(() {}); + }, + ), + ), Card( child: ListTile( title: const Text('Check for Updates'), diff --git a/lib/ui/settings_dialog/custom_subnet_dialog.dart b/lib/ui/settings_dialog/custom_subnet_dialog.dart new file mode 100644 index 00000000..ff9b45a1 --- /dev/null +++ b/lib/ui/settings_dialog/custom_subnet_dialog.dart @@ -0,0 +1,45 @@ +import 'package:flutter/material.dart'; +import 'package:vernet/main.dart'; +import 'package:vernet/ui/base_settings_dialog.dart'; +import 'package:vernet/values/strings.dart'; + +class CustomSubnetDialog extends StatefulWidget { + const CustomSubnetDialog({super.key}); + + @override + State createState() => _CustomSubnetDialogState(); +} + +class _CustomSubnetDialogState extends BaseSettingsDialog { + @override + String getDialogTitle() { + return StringValue.customSubnet; + } + + @override + String getHintText() { + return StringValue.customSubnetHint; + } + + @override + String getInitialValue() { + return appSettings.customSubnet; + } + + @override + TextInputType getKeyBoardType() { + return TextInputType.number; + } + + @override + void onSubmit(String value) { + if (value != appSettings.customSubnet) { + appSettings.setCustomSubnet(value); + } + } + + @override + String? validate(String? value) { + return null; + } +} diff --git a/lib/values/strings.dart b/lib/values/strings.dart index 11d41756..d8607c3c 100644 --- a/lib/values/strings.dart +++ b/lib/values/strings.dart @@ -14,4 +14,9 @@ class StringValue { static const String pingCount = 'Ping Count'; static const String pingCountDesc = 'Number of times ping request should be sent'; + + static const String customSubnet = 'Custom Subnet'; + static const String customSubnetDesc = + 'Scan a custom subnet instead of local one.'; + static const String customSubnetHint = 'Enter Gateway IP e.g., 10.102.200.1'; } From 5a9295033b6d72568cf65e7ae87d001b7a3cdb40 Mon Sep 17 00:00:00 2001 From: git-elliot Date: Sun, 17 Mar 2024 14:59:51 +0530 Subject: [PATCH 2/4] remove comments --- .../host_scna_bloc/host_scan_bloc.dart | 66 ++++++++++--------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/lib/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart b/lib/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart index 99db55ee..2c729551 100644 --- a/lib/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart +++ b/lib/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:bloc/bloc.dart'; +import 'package:flutter/foundation.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:injectable/injectable.dart'; import 'package:network_info_plus/network_info_plus.dart'; @@ -54,37 +55,40 @@ class HostScanBloc extends Bloc { StartNewScan event, Emitter emit, ) async { - // MdnsScannerService.instance - // .searchMdnsDevices() - // .then((List activeHostList) async { - // for (final ActiveHost activeHost in activeHostList) { - // final int index = indexOfActiveHost(activeHost.address); - // final MdnsInfo? mDns = await activeHost.mdnsInfo; - // if (mDns == null) { - // continue; - // } - - // if (index == -1) { - // deviceInTheNetworkList.add( - // DeviceInTheNetwork.createFromActiveHost( - // activeHost: activeHost, - // currentDeviceIp: ip!, - // gatewayIp: gatewayIp!, - // mdns: mDns, - // mac: (await activeHost.arpData)?.macAddress, - // ), - // ); - // } else { - // deviceInTheNetworkList[index] = deviceInTheNetworkList[index] - // ..mdns = mDns; - // } - - // deviceInTheNetworkList.sort(sort); - - // emit(const HostScanState.loadInProgress()); - // emit(HostScanState.foundNewDevice(deviceInTheNetworkList)); - // } - // }); + // mdns scanner causes crash on macos while running app. + if (!kDebugMode) { + MdnsScannerService.instance + .searchMdnsDevices() + .then((List activeHostList) async { + for (final ActiveHost activeHost in activeHostList) { + final int index = indexOfActiveHost(activeHost.address); + final MdnsInfo? mDns = await activeHost.mdnsInfo; + if (mDns == null) { + continue; + } + + if (index == -1) { + deviceInTheNetworkList.add( + DeviceInTheNetwork.createFromActiveHost( + activeHost: activeHost, + currentDeviceIp: ip!, + gatewayIp: gatewayIp!, + mdns: mDns, + mac: (await activeHost.arpData)?.macAddress, + ), + ); + } else { + deviceInTheNetworkList[index] = deviceInTheNetworkList[index] + ..mdns = mDns; + } + + deviceInTheNetworkList.sort(sort); + + emit(const HostScanState.loadInProgress()); + emit(HostScanState.foundNewDevice(deviceInTheNetworkList)); + } + }); + } final streamController = HostScannerService.instance.getAllPingableDevices( subnet!, From 8ad0593f6d31c0bc64e2617dc6b4b1c14aa6e1bc Mon Sep 17 00:00:00 2001 From: git-elliot Date: Sun, 17 Mar 2024 15:04:51 +0530 Subject: [PATCH 3/4] string update --- lib/pages/host_scan_page/widgets/host_scan_widget.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/pages/host_scan_page/widgets/host_scan_widget.dart b/lib/pages/host_scan_page/widgets/host_scan_widget.dart index 118279b8..2182bbc9 100644 --- a/lib/pages/host_scan_page/widgets/host_scan_widget.dart +++ b/lib/pages/host_scan_page/widgets/host_scan_widget.dart @@ -25,7 +25,9 @@ class HostScanWidget extends StatelessWidget { height: 30, ), Text( - 'Searching for devices in your local network \n ${appSettings.gatewayIP}', + appSettings.gatewayIP.isNotEmpty + ? 'Searching for devices in ${appSettings.gatewayIP} network' + : 'Searching for devices in your local network', textAlign: TextAlign.center, style: const TextStyle( fontSize: 18, From d302049ffaa770ab89faae478ec145d2b13690fd Mon Sep 17 00:00:00 2001 From: git-elliot Date: Sun, 17 Mar 2024 15:05:20 +0530 Subject: [PATCH 4/4] remove print --- lib/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart b/lib/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart index 2c729551..65ecc66e 100644 --- a/lib/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart +++ b/lib/pages/host_scan_page/host_scna_bloc/host_scan_bloc.dart @@ -44,10 +44,6 @@ class HostScanBloc extends Bloc { ? appSettings.customSubnet : await NetworkInfo().getWifiGatewayIP(); subnet = gatewayIp!.substring(0, gatewayIp!.lastIndexOf('.')); - print('IP : $ip'); - print('Subnet : $subnet'); - print('GT IP : $gatewayIp'); - add(const HostScanEvent.startNewScan()); }