diff --git a/CHANGELOG.md b/CHANGELOG.md index b8f4f4e..dbfd05c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Change Log +## 5.0.1 +1. Bug fixes and improvements + ## 5.0.0 1. Added hostIds list parameter to restrict scan to only these ids. 2. Breaking changes on calling static methods. You have to use instance of service now. diff --git a/README.md b/README.md index a2ed0cf..1372fa3 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ Future main() async { ```dart //1. Range String target = '192.168.1.1'; - PortScanner.scanPortsForSingleDevice(target, startPort: 1, endPort: 1024, + PortScannerService.instance.scanPortsForSingleDevice(target, startPort: 1, endPort: 1024, progressCallback: (progress) { print('Progress for port discovery : $progress'); }).listen((ActiveHost event) { @@ -99,7 +99,7 @@ Future main() async { ### Mdns Scanner ```dart - for (final ActiveHost activeHost in await MdnsScanner.searchMdnsDevices()) { + for (final ActiveHost activeHost in await MdnsScannerService.instance.searchMdnsDevices()) { final MdnsInfo? mdnsInfo = await activeHost.mdnsInfo; print(''' Address: ${activeHost.address} diff --git a/example/lib/example_utils.dart b/example/lib/example_utils.dart index a2ecae2..24b0a8a 100644 --- a/example/lib/example_utils.dart +++ b/example/lib/example_utils.dart @@ -1,11 +1,11 @@ import 'package:logging/logging.dart'; -final examplesLog = Logger("network_tools_examples"); +final examplesLogger = Logger("network_tools_examples"); void enableExampleLogging() { Logger.root.level = Level.FINE; Logger.root.onRecord.listen((record) { - if (record.loggerName == examplesLog.name) { + if (record.loggerName == examplesLogger.name) { // ignore: avoid_print print( '${record.time.toLocal()}: ${record.level.name}: ${record.loggerName}: ${record.message}', diff --git a/example/lib/scan/host_scan.dart b/example/lib/scan/host_scan.dart index 9d80a90..3735ab9 100644 --- a/example/lib/scan/host_scan.dart +++ b/example/lib/scan/host_scan.dart @@ -16,7 +16,7 @@ Future main() async { // or You can also get address using network_info_plus package // final String? address = await (NetworkInfo().getWifiIP()); - examplesLog.fine("Starting scan on subnet $subnet"); + examplesLogger.fine("Starting scan on subnet $subnet"); // You can set [firstHostId] and scan will start from this host in the network. // Similarly set [lastHostId] and scan will end at this host in the network. @@ -25,7 +25,7 @@ Future main() async { // firstHostId: 1, // lastHostId: 254, progressCallback: (progress) { - examplesLog.finer('Progress for host discovery : $progress'); + examplesLogger.finer('Progress for host discovery : $progress'); }, ); @@ -33,10 +33,10 @@ Future main() async { (host) async { //Same host can be emitted multiple times //Use Set instead of List - examplesLog.fine('Found device: ${await host.toStringFull()}'); + examplesLogger.fine('Found device: ${await host.toStringFull()}'); }, onDone: () { - examplesLog.fine('Scan completed'); + examplesLogger.fine('Scan completed'); }, ); // Don't forget to cancel the stream when not in use. } diff --git a/example/lib/scan/mdns_scan.dart b/example/lib/scan/mdns_scan.dart index 5b93e96..e4caa4a 100644 --- a/example/lib/scan/mdns_scan.dart +++ b/example/lib/scan/mdns_scan.dart @@ -7,7 +7,7 @@ Future main() async { for (final ActiveHost activeHost in await MdnsScannerService.instance.searchMdnsDevices()) { final MdnsInfo? mdnsInfo = await activeHost.mdnsInfo; - examplesLog.fine( + examplesLogger.fine( 'Address: ${activeHost.address}, Port: ${mdnsInfo!.mdnsPort}, ServiceType: ${mdnsInfo.mdnsServiceType}, MdnsName: ${mdnsInfo.getOnlyTheStartOfMdnsName()}, Mdns Device Name: ${mdnsInfo.mdnsSrvTarget}\n', ); } diff --git a/example/lib/scan/port_scan.dart b/example/lib/scan/port_scan.dart index faf11db..c6b5fa8 100644 --- a/example/lib/scan/port_scan.dart +++ b/example/lib/scan/port_scan.dart @@ -12,7 +12,7 @@ Future main() async { final netId = interface?.networkId; if (netId != null) { subnet = netId; - examplesLog.fine('subnet id $subnet'); + examplesLogger.fine('subnet id $subnet'); } // [New] Scan for a single open port in a subnet @@ -22,23 +22,23 @@ Future main() async { subnet, 53, progressCallback: (progress) { - examplesLog.finer('Progress for port discovery on host : $progress'); + examplesLogger.finer('Progress for port discovery on host : $progress'); }, ); stream2.listen( (ActiveHost activeHost) { - examplesLog + examplesLogger .fine('[scanDevicesForSinglePort]: Found device : $activeHost'); final OpenPort deviceWithOpenPort = activeHost.openPorts[0]; if (deviceWithOpenPort.isOpen) { - examplesLog.fine( + examplesLogger.fine( '[scanDevicesForSinglePort]: Found open port: ${deviceWithOpenPort.port} on ${activeHost.address}', ); } }, onDone: () { - examplesLog.fine('Port Scan completed'); + examplesLogger.fine('Port Scan completed'); }, ); // Don't forget to cancel the stream when not in use. @@ -46,7 +46,7 @@ Future main() async { final addr = interface?.ipAddress; if (addr != null) { target = addr; - examplesLog.fine("Target is $target"); + examplesLogger.fine("Target is $target"); } PortScannerService.instance.scanPortsForSingleDevice( @@ -55,20 +55,20 @@ Future main() async { // startPort: 1, endPort: 9400, progressCallback: (progress) { - examplesLog.finer('Progress for port discovery : $progress'); + examplesLogger.finer('Progress for port discovery : $progress'); }, ).listen( (activeHost) { final OpenPort deviceWithOpenPort = activeHost.openPorts[0]; if (deviceWithOpenPort.isOpen) { - examplesLog.fine( + examplesLogger.fine( 'Found open port: ${deviceWithOpenPort.port} on device $target', ); } }, onDone: () { - examplesLog.fine('Port Scan from 1 to 9400 completed'); + examplesLogger.fine('Port Scan from 1 to 9400 completed'); }, ); } diff --git a/lib/src/configure_dart_native.dart b/lib/src/configure_dart_native.dart index 12e61d6..3eb8ffd 100644 --- a/lib/src/configure_dart_native.dart +++ b/lib/src/configure_dart_native.dart @@ -1,5 +1,5 @@ import 'package:logging/logging.dart'; -import 'package:network_tools/network_tools.dart' as pacakges_page; +import 'package:network_tools/network_tools.dart' as packages_page; import 'package:network_tools/src/network_tools_utils.dart'; import 'package:network_tools/src/services/arp_service.dart'; import 'package:network_tools/src/services/impls/arp_service_sembast_impl.dart'; @@ -11,13 +11,13 @@ Future configureNetworkTools( String dbDirectory, { bool enableDebugging = false, }) async { - pacakges_page.enableDebugging = enableDebugging; - pacakges_page.dbDirectory = dbDirectory; + packages_page.enableDebugging = enableDebugging; + packages_page.dbDirectory = dbDirectory; - if (pacakges_page.enableDebugging) { + if (packages_page.enableDebugging) { Logger.root.level = Level.FINE; Logger.root.onRecord.listen((record) { - if (record.loggerName == log.name) { + if (record.loggerName == logger.name) { // ignore: avoid_print print( '${record.time.toLocal()}: ${record.level.name}: ${record.loggerName}: ${record.message}', @@ -34,5 +34,5 @@ Future configureNetworkTools( final arpService = await ARPService.instance.open(); await arpService.buildTable(); - await pacakges_page.VendorTable.createVendorTableMap(); + await packages_page.VendorTable.createVendorTableMap(); } diff --git a/lib/src/device_info/arp_table_helper.dart b/lib/src/device_info/arp_table_helper.dart index 60aae5b..f8b9103 100644 --- a/lib/src/device_info/arp_table_helper.dart +++ b/lib/src/device_info/arp_table_helper.dart @@ -4,11 +4,20 @@ import 'package:logging/logging.dart'; import 'package:network_tools/src/models/arp_data.dart'; import 'package:universal_io/io.dart'; +/// Retreiving ARP packets is only supported for Desktop such as +/// Linux, Windows, and macOS. Dart native doesn't provide a way or rejects +/// call to arp command on mobile such as Android and iOS. +/// Maybe in future dart native will support sending raw packets, +/// so that time we can add implementation for mobile devices. +/// Currenlty this is achieved by process package. class ARPTableHelper { static final arpLogger = Logger("arp-table-logger"); + /// Fires arp -a command only on 3 platforms i.e., Linux, Windows, and macOS + /// and returns the result in form of ARPData after parsing each line. static Future> buildTable() async { final arpEntries = []; + // ARP is not allowed to be run for mobile devices currenlty. if (Platform.isAndroid || Platform.isIOS) return arpEntries; final result = await Process.run('arp', ['-a']); final entries = const LineSplitter().convert(result.stdout.toString()); diff --git a/lib/src/device_info/vendor_table.dart b/lib/src/device_info/vendor_table.dart index 3cffff5..1131816 100644 --- a/lib/src/device_info/vendor_table.dart +++ b/lib/src/device_info/vendor_table.dart @@ -38,7 +38,7 @@ class VendorTable { final csvPath = p.join(dbDirectory, "mac-vendors-export.csv"); final file = File(csvPath); if (!await file.exists()) { - log.fine("Downloading mac-vendors-export.csv from network_tools"); + logger.fine("Downloading mac-vendors-export.csv from network_tools"); final response = await http.get( Uri.https( "raw.githubusercontent.com", @@ -46,9 +46,9 @@ class VendorTable { ), ); file.writeAsBytesSync(response.bodyBytes); - log.fine("Downloaded mac-vendors-export.csv successfully"); + logger.fine("Downloaded mac-vendors-export.csv successfully"); } else { - log.fine("File mac-vendors-export.csv already exists"); + logger.fine("File mac-vendors-export.csv already exists"); } final input = file.openRead(); diff --git a/lib/src/mdns_scanner/get_srv_list_by_os/srv_list_linux.dart b/lib/src/mdns_scanner/get_srv_list_by_os/srv_list_linux.dart index 9b463c2..311aead 100644 --- a/lib/src/mdns_scanner/get_srv_list_by_os/srv_list_linux.dart +++ b/lib/src/mdns_scanner/get_srv_list_by_os/srv_list_linux.dart @@ -11,7 +11,7 @@ class SrvListLinux { srvList.addAll(await runAvahiBrowseCommand()); srvList.addAll(await runMdnsScanCommand()); } catch (e) { - log.severe('Error:\n$e'); + logger.severe('Error:\n$e'); } return srvList.toList(); } @@ -35,7 +35,7 @@ timeout 2s avahi-browse --all -p final String? resultStderr = error.result?.stderr.toString(); if (resultStderr != null && resultStderr.contains('No such file or directory')) { - log.fine( + logger.fine( 'You can make the mdns process better by installing `avahi-browse`', ); return []; @@ -59,7 +59,7 @@ timeout 2s avahi-browse --all -p } } } catch (e) { - log.severe('Error getting info from avahi-browse\n$e'); + logger.severe('Error getting info from avahi-browse\n$e'); } return srvListAvahi; } @@ -84,7 +84,7 @@ timeout 2s mdns-scan if (resultStderr == null || (resultStderr.contains('No such file or directory'))) { - log.fine( + logger.fine( 'You can make the mdns process better by installing `mdns-scan`', ); return []; @@ -105,7 +105,7 @@ timeout 2s mdns-scan } } } catch (e) { - log.severe('Error getting info from mdns-scan\n$e'); + logger.severe('Error getting info from mdns-scan\n$e'); } return srvListMdnsScan; } diff --git a/lib/src/models/active_host.dart b/lib/src/models/active_host.dart index 30fd92c..d3dcb3f 100644 --- a/lib/src/models/active_host.dart +++ b/lib/src/models/active_host.dart @@ -164,7 +164,7 @@ class ActiveHost { // throw exception. // We don't need to print this crash as it is by design. } else { - log.severe('Exception here: $e'); + logger.severe('Exception here: $e'); } } return null; diff --git a/lib/src/network_tools_utils.dart b/lib/src/network_tools_utils.dart index bf3c8ad..cd79a30 100644 --- a/lib/src/network_tools_utils.dart +++ b/lib/src/network_tools_utils.dart @@ -1,3 +1,3 @@ import 'package:logging/logging.dart'; -final log = Logger("network_tools"); +final logger = Logger("network_tools"); diff --git a/lib/src/services/impls/host_scanner_service_impl.dart b/lib/src/services/impls/host_scanner_service_impl.dart index 12d04fa..50d7c87 100644 --- a/lib/src/services/impls/host_scanner_service_impl.dart +++ b/lib/src/services/impls/host_scanner_service_impl.dart @@ -115,11 +115,11 @@ class HostScannerServiceImpl extends HostScannerService { if (pingError == null) { final Duration? time = response.time; if (time != null) { - log.fine("Pingable device found: $host"); + logger.fine("Pingable device found: $host"); tempSendableActivateHost = SendableActiveHost(host, pingData: pingData); } else { - log.fine("Non pingable device found: $host"); + logger.fine("Non pingable device found: $host"); } } } @@ -128,16 +128,16 @@ class HostScannerServiceImpl extends HostScannerService { final data = await (await arpServiceFuture).entryFor(host); if (data != null) { - log.fine("Successfully fetched arp entry for $host as $data"); + logger.fine("Successfully fetched arp entry for $host as $data"); tempSendableActivateHost = SendableActiveHost(host, pingData: pingData); } else { - log.fine("Problem in fetching arp entry for $host"); + logger.fine("Problem in fetching arp entry for $host"); } } if (tempSendableActivateHost != null) { - log.fine("Successfully added to result $host"); + logger.fine("Successfully added to result $host"); activeHostsController.add(tempSendableActivateHost); } } @@ -181,7 +181,7 @@ class HostScannerServiceImpl extends HostScannerService { i <= lastValidSubnet; i += scanRangeForIsolate + 1) { final limit = min(i + scanRangeForIsolate, lastValidSubnet); - log.fine('Scanning from $i to $limit'); + logger.fine('Scanning from $i to $limit'); final receivePort = ReceivePort(); final isolate = @@ -232,7 +232,8 @@ class HostScannerServiceImpl extends HostScannerService { final bool resultsInAddressAscendingOrder = message[4] == "true"; final String dbDirectory = message[5]; final bool enableDebugging = message[6] == "true"; - final List hostIds = message[7] + final String joinedIds = message[7]; + final List hostIds = joinedIds .split(',') .where((e) => e.isNotEmpty) .map(int.parse) diff --git a/lib/src/services/impls/mdns_scanner_service_impl.dart b/lib/src/services/impls/mdns_scanner_service_impl.dart index 43ef5bc..069f185 100644 --- a/lib/src/services/impls/mdns_scanner_service_impl.dart +++ b/lib/src/services/impls/mdns_scanner_service_impl.dart @@ -133,7 +133,7 @@ class MdnsScannerServiceImpl extends MdnsScannerService { listOfActiveHost.add(activeHost); } } catch (e) { - log.severe( + logger.severe( 'Error finding ip of mdns record ${ptr.name} srv target ${srv.target}, will add it with ip 0.0.0.0\n$e', ); final ActiveHost activeHost = convertSrvToHostName( diff --git a/pubspec.yaml b/pubspec.yaml index d4452b8..8c29767 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: network_tools description: Networking Tools library which can help you discover open ports, devices on subnet and many other things. -version: 5.0.0 +version: 5.0.1 issue_tracker: https://github.com/osociety/network_tools/issues repository: https://github.com/osociety/network_tools diff --git a/test/network_tools_test.dart b/test/network_tools_test.dart index 2bb0028..7e5ddeb 100644 --- a/test/network_tools_test.dart +++ b/test/network_tools_test.dart @@ -24,7 +24,7 @@ void main() { server = await ServerSocket.bind(InternetAddress.anyIPv4, port, shared: true); port = server.port; - log.fine("Opened port in this machine at $port"); + logger.fine("Opened port in this machine at $port"); final interface = await NetInterface.localInterface(); if (interface != null) { @@ -44,7 +44,7 @@ void main() { } } } - log.fine( + logger.fine( 'Fetched own host as $myOwnHost and interface address as $interfaceIp', ); }