Skip to content

Commit

Permalink
Merge pull request #47 from git-elliot/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
git-elliot authored Aug 6, 2022
2 parents 0d4ace3 + 7e488f0 commit 1ce6689
Show file tree
Hide file tree
Showing 15 changed files with 302 additions and 171 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import 'package:network_tools/network_tools.dart';
### Host Scanner

```dart
String ip = '192.168.1.12';
// or You can also get ip using network_info_plus package
// final String? ip = await (NetworkInfo().getWifiIP());
final String subnet = ip.substring(0, ip.lastIndexOf('.'));
String address = '192.168.1.12';
// or You can also get address using network_info_plus package
// final String? address = await (NetworkInfo().getWifiIP());
final String subnet = address.substring(0, address.lastIndexOf('.'));
final stream = HostScanner.discover(subnet, firstSubnet: 1, lastSubnet: 50,
progressCallback: (progress) {
print('Progress for host discovery : $progress');
Expand Down Expand Up @@ -72,7 +72,7 @@ import 'package:network_tools/network_tools.dart';
for (final ActiveHost activeHost in await MdnsScanner.searchMdnsDevices()) {
final MdnsInfo? mdnsInfo = activeHost.mdnsInfo;
print(
'IP: ${activeHost.ip}, Port: ${mdnsInfo!.mdnsPort}, ServiceType: ${mdnsInfo.mdnsServiceType}, MdnsName: ${mdnsInfo.getOnlyTheStartOfMdnsName()}',
'Address: ${activeHost.address}, Port: ${mdnsInfo!.mdnsPort}, ServiceType: ${mdnsInfo.mdnsServiceType}, MdnsName: ${mdnsInfo.getOnlyTheStartOfMdnsName()}',
);
}
```
Expand Down
15 changes: 14 additions & 1 deletion network_tools/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
# Change Log

## 3.0.0

**Breaking change** Change most of the methods names
* HostScanner.discover To HostScanner.getAllPingableDevices
* HostScanner.discoverPort to HostScanner.scanDevicesForSinglePort
* PortScanner.discover to PortScanner.scanPortsForSingleDevice

Ip field in ActiveHost is now InternetAddress type instead of string which improve handling of IPv6.

ActiveHost now contains host name of the address if exist.

Better naming of methods

## 2.1.0

Added partly support for searching mdns devices.

## 2.0.0

**Breaking change**. Bump minimum dart version to 2.17.0.
**Breaking change** Bump minimum dart version to 2.17.0.

Updated dart_ping package version to 7.0.1.

Expand Down
10 changes: 5 additions & 5 deletions network_tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import 'package:network_tools/network_tools.dart';
### Host Scanner

```dart
String ip = '192.168.1.12';
// or You can also get ip using network_info_plus package
// final String? ip = await (NetworkInfo().getWifiIP());
final String subnet = ip.substring(0, ip.lastIndexOf('.'));
String address = '192.168.1.12';
// or You can also get address using network_info_plus package
// final String? address = await (NetworkInfo().getWifiIP());
final String subnet = address.substring(0, address.lastIndexOf('.'));
final stream = HostScanner.discover(subnet, firstSubnet: 1, lastSubnet: 50,
progressCallback: (progress) {
print('Progress for host discovery : $progress');
Expand Down Expand Up @@ -72,7 +72,7 @@ import 'package:network_tools/network_tools.dart';
for (final ActiveHost activeHost in await MdnsScanner.searchMdnsDevices()) {
final MdnsInfo? mdnsInfo = activeHost.mdnsInfo;
print(
'IP: ${activeHost.ip}, Port: ${mdnsInfo!.mdnsPort}, ServiceType: ${mdnsInfo.mdnsServiceType}, MdnsName: ${mdnsInfo.getOnlyTheStartOfMdnsName()}',
'Address: ${activeHost.address}, Port: ${mdnsInfo!.mdnsPort}, ServiceType: ${mdnsInfo.mdnsServiceType}, MdnsName: ${mdnsInfo.getOnlyTheStartOfMdnsName()}',
);
}
```
Expand Down
20 changes: 10 additions & 10 deletions network_tools/example/host_scan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ void main() {
);
});

final _log = Logger('host_scan');
const String ip = '192.168.1.1';
// or You can also get ip using network_info_plus package
// final String? ip = await (NetworkInfo().getWifiIP());
final String subnet = ip.substring(0, ip.lastIndexOf('.'));
final log = Logger('host_scan');
const String address = '192.168.1.1';
// or You can also get address using network_info_plus package
// final String? address = await (NetworkInfo().getWifiIP());
final String subnet = address.substring(0, address.lastIndexOf('.'));

// You can set [firstSubnet] and scan will start from this host in the network.
// Similarly set [lastSubnet] and scan will end at this host in the network.
final stream = HostScanner.discover(
final stream = HostScanner.getAllPingableDevices(
subnet,
// firstSubnet: 1,
// lastSubnet: 254,
progressCallback: (progress) {
_log.finer('Progress for host discovery : $progress');
log.finer('Progress for host discovery : $progress');
},
);

stream.listen(
(ActiveHost host) {
(ActiveHost host) async {
//Same host can be emitted multiple times
//Use Set<ActiveHost> instead of List<ActiveHost>
_log.fine('Found device: $host');
log.fine('Found device: ${await host.toStringFull()}');
},
onDone: () {
_log.fine('Scan completed');
log.fine('Scan completed');
},
); // Don't forget to cancel the stream when not in use.
}
6 changes: 2 additions & 4 deletions network_tools/example/mdns_scan.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import 'package:network_tools/network_tools.dart';
import 'package:network_tools/src/mdns_scanner/mdns_scanner.dart';
import 'package:network_tools/src/models/mdns_info.dart';

Future<void> main() async {
for (final ActiveHost activeHost in await MdnsScanner.searchMdnsDevices()) {
final MdnsInfo? mdnsInfo = activeHost.mdnsInfo;
final MdnsInfo? mdnsInfo = await activeHost.mdnsInfo;
print(
'IP: ${activeHost.ip}, Port: ${mdnsInfo!.mdnsPort}, ServiceType: ${mdnsInfo.mdnsServiceType}, MdnsName: ${mdnsInfo.getOnlyTheStartOfMdnsName()}',
'Address: ${activeHost.address}, Port: ${mdnsInfo!.mdnsPort}, ServiceType: ${mdnsInfo.mdnsServiceType}, MdnsName: ${mdnsInfo.getOnlyTheStartOfMdnsName()}',
);
}
}
29 changes: 15 additions & 14 deletions network_tools/example/port_scan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,58 @@ void main() {
'${DateFormat.Hms().format(record.time)}: ${record.level.name}: ${record.loggerName}: ${record.message}',
);
});
final _log = Logger('port_scan');
const String ip = '192.168.1.1';
// or You can also get ip using network_info_plus package
// final String? ip = await (NetworkInfo().getWifiIP());
final String subnet = ip.substring(0, ip.lastIndexOf('.'));
final log = Logger('port_scan');
const String address = '192.168.1.1';
// or You can also get address using network_info_plus package
// final String? address = await (NetworkInfo().getWifiIP());
final String subnet = address.substring(0, address.lastIndexOf('.'));

// [New] Scan for a single open port in a subnet
// You can set [firstSubnet] and scan will start from this host in the network.
// Similarly set [lastSubnet] and scan will end at this host in the network.
final stream2 = HostScanner.discoverPort(
final stream2 = HostScanner.scanDevicesForSinglePort(
subnet,
53,
// firstSubnet: 1,
// lastSubnet: 254,
progressCallback: (progress) {
_log.finer('Progress for port discovery on host : $progress');
log.finer('Progress for port discovery on host : $progress');
},
);

stream2.listen(
(activeHost) {
final OpenPort deviceWithOpenPort = activeHost.openPort[0];
if (deviceWithOpenPort.isOpen) {
_log.fine(
'Found open port: ${deviceWithOpenPort.port} on ${activeHost.ip}');
log.fine(
'Found open port: ${deviceWithOpenPort.port} on ${activeHost.address}',
);
}
},
onDone: () {
_log.fine('Port Scan completed');
log.fine('Port Scan completed');
},
); // Don't forget to cancel the stream when not in use.

const String target = '192.168.1.1';
PortScanner.discover(
PortScanner.scanPortsForSingleDevice(
target,
// Scan will start from this port.
// startPort: 1,
endPort: 9400,
progressCallback: (progress) {
_log.finer('Progress for port discovery : $progress');
log.finer('Progress for port discovery : $progress');
},
).listen(
(activeHost) {
final OpenPort deviceWithOpenPort = activeHost.openPort[0];

if (deviceWithOpenPort.isOpen) {
_log.fine('Found open port : ${deviceWithOpenPort.port}');
log.fine('Found open port: ${deviceWithOpenPort.port}');
}
},
onDone: () {
_log.fine('Port Scan from 1 to 9400 completed');
log.fine('Port Scan from 1 to 9400 completed');
},
);
}
20 changes: 10 additions & 10 deletions network_tools/lib/src/host_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class HostScanner {
/// Set maxHost to higher value if you are not getting results.
/// It won't firstSubnet again unless previous scan is completed due to heavy
/// resource consumption.
/// [resultsInIpAscendingOrder] = false will return results faster but not in
/// [resultsInAddressAscendingOrder] = false will return results faster but not in
/// ascending order and without [progressCallback].
static Stream<ActiveHost> discover(
static Stream<ActiveHost> getAllPingableDevices(
String subnet, {
int firstSubnet = 1,
int lastSubnet = 254,
int timeoutInSeconds = 1,
ProgressCallback? progressCallback,
bool resultsInIpAscendingOrder = true,
bool resultsInAddressAscendingOrder = true,
}) async* {
final int maxEnd = getMaxHost(subnet);
if (firstSubnet > lastSubnet ||
Expand All @@ -47,7 +47,7 @@ class HostScanner {
);
}

if (!resultsInIpAscendingOrder) {
if (!resultsInAddressAscendingOrder) {
yield* activeHostsController.stream;
}

Expand Down Expand Up @@ -79,7 +79,7 @@ class HostScanner {
final Duration? time = response.time;
if (time != null) {
final ActiveHost tempActiveHost =
ActiveHost(host, pingData: pingData);
ActiveHost.buildWithAddress(address: host, pingData: pingData);
activeHostsController.add(tempActiveHost);
return tempActiveHost;
}
Expand All @@ -89,16 +89,16 @@ class HostScanner {
}

/// Scans for all hosts that have the specific port that was given.
/// [resultsInIpAscendingOrder] = false will return results faster but not in
/// [resultsInAddressAscendingOrder] = false will return results faster but not in
/// ascending order and without [progressCallback].
static Stream<ActiveHost> discoverPort(
static Stream<ActiveHost> scanDevicesForSinglePort(
String subnet,
int port, {
int firstSubnet = 1,
int lastSubnet = 254,
Duration timeout = const Duration(milliseconds: 2000),
ProgressCallback? progressCallback,
bool resultsInIpAscendingOrder = true,
bool resultsInAddressAscendingOrder = true,
}) async* {
final int maxEnd = getMaxHost(subnet);
if (firstSubnet > lastSubnet ||
Expand All @@ -117,15 +117,15 @@ class HostScanner {
final host = '$subnet.$i';
activeHostOpenPortList.add(
PortScanner.connectToPort(
ip: host,
address: host,
port: port,
timeout: timeout,
activeHostsController: activeHostsController,
),
);
}

if (!resultsInIpAscendingOrder) {
if (!resultsInAddressAscendingOrder) {
yield* activeHostsController.stream;
}

Expand Down
Loading

0 comments on commit 1ce6689

Please sign in to comment.