Skip to content

Commit

Permalink
Merge pull request #181 from kengu/issue/180
Browse files Browse the repository at this point in the history
Adds support for checking list of ip addresses
  • Loading branch information
git-elliot authored Mar 14, 2024
2 parents ade8ed2 + b43f2e9 commit 4a348ac
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 13 deletions.
3 changes: 3 additions & 0 deletions lib/src/services/host_scanner_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ abstract class HostScannerService {
String subnet, {
int firstHostId = defaultFirstHostId,
int lastHostId = defaultLastHostId,
List<int> hostIds = const [],
int timeoutInSeconds = 1,
ProgressCallback? progressCallback,
bool resultsInAddressAscendingOrder = true,
Expand All @@ -31,6 +32,7 @@ abstract class HostScannerService {
String subnet, {
int firstHostId = defaultFirstHostId,
int lastHostId = defaultLastHostId,
List<int> hostIds = const [],
int timeoutInSeconds = 1,
ProgressCallback? progressCallback,
bool resultsInAddressAscendingOrder = true,
Expand All @@ -46,6 +48,7 @@ abstract class HostScannerService {
String subnet, {
int firstHostId = defaultFirstHostId,
int lastHostId = defaultLastHostId,
List<int> hostIds = const [],
int timeoutInSeconds = 1,
ProgressCallback? progressCallback,
bool resultsInAddressAscendingOrder = true,
Expand Down
41 changes: 29 additions & 12 deletions lib/src/services/impls/host_scanner_service_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ class HostScannerServiceImpl extends HostScannerService {
/// Set maxHost to higher value if you are not getting results.
/// It won't firstHostId again unless previous scan is completed due to heavy
/// resource consumption.
/// Use hostIds to limit subnet scan to hosts given.
/// [resultsInAddressAscendingOrder] = false will return results faster but not in
/// ascending order and without [progressCallback].
@override
Stream<ActiveHost> getAllPingableDevices(
String subnet, {
int firstHostId = HostScannerService.defaultFirstHostId,
int lastHostId = HostScannerService.defaultLastHostId,
List<int> hostIds = const [],
int timeoutInSeconds = 1,
ProgressCallback? progressCallback,
bool resultsInAddressAscendingOrder = true,
Expand All @@ -30,6 +32,7 @@ class HostScannerServiceImpl extends HostScannerService {
subnet,
firstHostId: firstHostId,
lastHostId: lastHostId,
hostIds: hostIds,
timeoutInSeconds: timeoutInSeconds,
progressCallback: progressCallback,
resultsInAddressAscendingOrder: resultsInAddressAscendingOrder,
Expand All @@ -51,6 +54,7 @@ class HostScannerServiceImpl extends HostScannerService {
String subnet, {
int firstHostId = HostScannerService.defaultFirstHostId,
int lastHostId = HostScannerService.defaultLastHostId,
List<int> hostIds = const [],
int timeoutInSeconds = 1,
ProgressCallback? progressCallback,
bool resultsInAddressAscendingOrder = true,
Expand All @@ -61,14 +65,18 @@ class HostScannerServiceImpl extends HostScannerService {
final StreamController<SendableActiveHost> activeHostsController =
StreamController<SendableActiveHost>();

final List<int> pinged = [];
for (int i = firstHostId; i <= lastValidSubnet; i++) {
activeHostsFuture.add(
getHostFromPing(
activeHostsController: activeHostsController,
host: '$subnet.$i',
timeoutInSeconds: timeoutInSeconds,
),
);
if (hostIds.isEmpty || hostIds.contains(i)) {
pinged.add(i);
activeHostsFuture.add(
getHostFromPing(
activeHostsController: activeHostsController,
host: '$subnet.$i',
timeoutInSeconds: timeoutInSeconds,
),
);
}
}

if (!resultsInAddressAscendingOrder) {
Expand All @@ -80,8 +88,9 @@ class HostScannerServiceImpl extends HostScannerService {
i++;
final SendableActiveHost? tempHost = await host;

progressCallback
?.call((i - firstHostId) * 100 / (lastValidSubnet - firstHostId));
progressCallback?.call(
(pinged[i] - firstHostId) * 100 / (lastValidSubnet - firstHostId),
);

if (tempHost == null) {
continue;
Expand Down Expand Up @@ -160,6 +169,7 @@ class HostScannerServiceImpl extends HostScannerService {
String subnet, {
int firstHostId = HostScannerService.defaultFirstHostId,
int lastHostId = HostScannerService.defaultLastHostId,
List<int> hostIds = const [],
int timeoutInSeconds = 1,
ProgressCallback? progressCallback,
bool resultsInAddressAscendingOrder = true,
Expand Down Expand Up @@ -188,15 +198,16 @@ class HostScannerServiceImpl extends HostScannerService {
resultsInAddressAscendingOrder.toString(),
dbDirectory,
enableDebugging.toString(),
hostIds.join(','),
],
);
} else if (message is SendableActiveHost) {
progressCallback
?.call((i - firstHostId) * 100 / (lastValidSubnet - firstHostId));
// print('Address ${message.address}');
final activeHostFound =
ActiveHost.fromSendableActiveHost(sendableActiveHost: message);
await activeHostFound.resolveInfo();
final j = int.tryParse(activeHostFound.hostId) ?? i;
progressCallback
?.call((j - firstHostId) * 100 / (lastValidSubnet - firstHostId));
yield activeHostFound;
} else if (message is String && message == 'Done') {
isolate.kill();
Expand All @@ -221,6 +232,11 @@ class HostScannerServiceImpl extends HostScannerService {
final bool resultsInAddressAscendingOrder = message[4] == "true";
final String dbDirectory = message[5];
final bool enableDebugging = message[6] == "true";
final List<int> hostIds = message[7]
.split(',')
.where((e) => e.isNotEmpty)
.map(int.parse)
.toList();
// configure again
await configureNetworkTools(
dbDirectory,
Expand All @@ -234,6 +250,7 @@ class HostScannerServiceImpl extends HostScannerService {
subnetIsolate,
firstHostId: firstSubnetIsolate,
lastHostId: lastSubnetIsolate,
hostIds: hostIds,
timeoutInSeconds: timeoutInSeconds,
resultsInAddressAscendingOrder: resultsInAddressAscendingOrder,
);
Expand Down
47 changes: 46 additions & 1 deletion test/network_tools_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:universal_io/io.dart';

void main() {
int port = 0;
int hostId = 0;
int firstHostId = 0;
int lastHostId = 0;
String myOwnHost = "0.0.0.0";
Expand All @@ -27,7 +28,7 @@ void main() {

final interface = await NetInterface.localInterface();
if (interface != null) {
final hostId = interface.hostId;
hostId = interface.hostId;
interfaceIp = interface.networkId;
myOwnHost = interface.ipAddress;
// Better to restrict to scan from hostId - 1 to hostId + 1 to prevent GHA timeouts
Expand Down Expand Up @@ -78,6 +79,28 @@ void main() {
),
emits(isA<ActiveHost>()),
);
expectLater(
//There should be at least one device pingable in network when limiting to own hostId
HostScannerService.instance.getAllPingableDevices(
interfaceIp,
timeoutInSeconds: 3,
hostIds: [hostId],
firstHostId: firstHostId,
lastHostId: lastHostId,
),
emits(isA<ActiveHost>()),
);
expectLater(
//There should be at least one device pingable in network when limiting to hostId other than own
HostScannerService.instance.getAllPingableDevices(
interfaceIp,
timeoutInSeconds: 3,
hostIds: [0],
firstHostId: firstHostId,
lastHostId: lastHostId,
),
neverEmits(isA<ActiveHost>()),
);
expectLater(
//Should emit at least our own local machine when pinging all hosts.
HostScannerService.instance.getAllPingableDevices(
Expand Down Expand Up @@ -122,6 +145,28 @@ void main() {
),
),
);
expectLater(
//There should be at least one device pingable in network when limiting to own hostId
HostScannerService.instance.getAllPingableDevicesAsync(
interfaceIp,
timeoutInSeconds: 3,
hostIds: [hostId],
firstHostId: firstHostId,
lastHostId: lastHostId,
),
emits(isA<ActiveHost>()),
);
expectLater(
//There should be at least one device pingable in network when limiting to hostId other than own
HostScannerService.instance.getAllPingableDevicesAsync(
interfaceIp,
timeoutInSeconds: 3,
hostIds: [0],
firstHostId: firstHostId,
lastHostId: lastHostId,
),
neverEmits(isA<ActiveHost>()),
);
},
);

Expand Down

0 comments on commit 4a348ac

Please sign in to comment.