Skip to content

Commit

Permalink
Merge pull request #162 from osociety/dev
Browse files Browse the repository at this point in the history
Dev -> Main
  • Loading branch information
git-elliot authored Nov 17, 2023
2 parents 83cbe1c + a34ca22 commit d148f37
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ name: Dart
on:
pull_request:
branches: [ "main", "dev" ]
paths-ignore:
- '**/**.md'
- 'donation/**'

concurrency:
group: ${{ github.head_ref || github.run_id }}
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Change Log

## 4.0.2
1. Separated logs for example and library

## 4.0.1
1. Fixex path problem for dart and flutter sdk
1. Fixed path problem for dart and flutter sdk

## 4.0.0
### Breaking changes
Expand Down
1 change: 1 addition & 0 deletions example/lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Check examples/lib/main.dart
15 changes: 15 additions & 0 deletions example/lib/example_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:logging/logging.dart';

final examplesLog = Logger("network_tools_examples");

void enableExampleLogging() {
Logger.root.level = Level.FINE;
Logger.root.onRecord.listen((record) {
if (record.loggerName == examplesLog.name) {
// ignore: avoid_print
print(
'${record.time.toLocal()}: ${record.level.name}: ${record.loggerName}: ${record.message}',
);
}
});
}
5 changes: 5 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void main() {
print("Run host scan : 'dart example/lib/scan/host_scan.dart'");
print("Run port scan : 'dart example/lib/scan/port_scan.dart'");
print("Run mdns scan : 'dart example/lib/scan/mdns_scan.dart'");
}
15 changes: 8 additions & 7 deletions example/host_scan.dart → example/lib/scan/host_scan.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:logging/logging.dart';
import '../lib/network_tools.dart';
import '../lib/src/network_tools_utils.dart';
import 'package:network_tools/network_tools.dart';
import '../example_utils.dart';

void main() async {
Future<void> main() async {
enableExampleLogging();
await configureNetworkTools('build');

String subnet = '192.168.0'; //Default network id for home networks
Expand All @@ -15,7 +16,7 @@ void main() async {

// or You can also get address using network_info_plus package
// final String? address = await (NetworkInfo().getWifiIP());
log.fine("Starting scan on subnet $subnet");
examplesLog.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.
Expand All @@ -24,18 +25,18 @@ void main() async {
// firstHostId: 1,
// lastHostId: 254,
progressCallback: (progress) {
log.finer('Progress for host discovery : $progress');
examplesLog.finer('Progress for host discovery : $progress');
},
);

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

Future<void> main() async {
enableExampleLogging();
await configureNetworkTools('build');
for (final ActiveHost activeHost in await MdnsScanner.searchMdnsDevices()) {
final MdnsInfo? mdnsInfo = await activeHost.mdnsInfo;
print(
examplesLog.fine(
'Address: ${activeHost.address}, Port: ${mdnsInfo!.mdnsPort}, ServiceType: ${mdnsInfo.mdnsServiceType}, MdnsName: ${mdnsInfo.getOnlyTheStartOfMdnsName()}, Mdns Device Name: ${mdnsInfo.mdnsSrvTarget}\n',
);
}
Expand Down
26 changes: 14 additions & 12 deletions example/port_scan.dart → example/lib/scan/port_scan.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:logging/logging.dart';
import '../lib/src/network_tools_utils.dart';
import 'package:network_tools/network_tools.dart';
import '../example_utils.dart';

void main() async {
Future<void> main() async {
enableExampleLogging();
await configureNetworkTools('build');

String subnet = '192.168.0'; //Default network id for home networks
Expand All @@ -11,41 +12,41 @@ void main() async {
final netId = interface?.networkId;
if (netId != null) {
subnet = netId;
log.fine('subnet id $subnet');
examplesLog.fine('subnet id $subnet');
}

// [New] Scan for a single open port in a 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.
final stream2 = HostScanner.scanDevicesForSinglePort(
subnet,
22,
53,
progressCallback: (progress) {
log.finer('Progress for port discovery on host : $progress');
examplesLog.finer('Progress for port discovery on host : $progress');
},
);

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

String target = '192.168.1.1';
final addr = interface?.ipAddress;
if (addr != null) {
target = addr;
log.fine("Target is $target");
examplesLog.fine("Target is $target");
}

PortScanner.scanPortsForSingleDevice(
Expand All @@ -54,18 +55,19 @@ void main() async {
// startPort: 1,
endPort: 9400,
progressCallback: (progress) {
log.finer('Progress for port discovery : $progress');
examplesLog.finer('Progress for port discovery : $progress');
},
).listen(
(activeHost) {
final OpenPort deviceWithOpenPort = activeHost.openPorts[0];

if (deviceWithOpenPort.isOpen) {
log.fine('Found open port: ${deviceWithOpenPort.port}');
examplesLog.fine(
'Found open port: ${deviceWithOpenPort.port} on device $target');
}
},
onDone: () {
log.fine('Port Scan from 1 to 9400 completed');
examplesLog.fine('Port Scan from 1 to 9400 completed');
},
);
}
5 changes: 0 additions & 5 deletions example/main.dart

This file was deleted.

17 changes: 10 additions & 7 deletions lib/network_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:get_it/get_it.dart';
import 'package:logging/logging.dart';
import 'package:network_tools/injectable.dart';
import 'package:network_tools/src/device_info/vendor_table.dart';
import 'package:network_tools/src/network_tools_utils.dart';
import 'package:network_tools/src/services/arp_service.dart';

export 'src/device_info/net_interface.dart';
Expand Down Expand Up @@ -33,18 +34,20 @@ Future<void> configureNetworkTools(
bool enableDebugging = false,
}) async {
_enableDebugging = enableDebugging;
_dbDirectory = dbDirectory;
if (enableDebugging) {
Logger.root.level = Level.FINE;
Logger.root.onRecord.listen((record) {
// ignore: avoid_print
print(
'${record.time.toLocal()}: ${record.level.name}: ${record.loggerName}: ${record.message}',
);
if (record.loggerName == log.name) {
// ignore: avoid_print
print(
'${record.time.toLocal()}: ${record.level.name}: ${record.loggerName}: ${record.message}',
);
}
});
}
configureDependencies();
_dbDirectory = dbDirectory;
final arpServiceFuture = await _getIt<ARPService>().open();
await arpServiceFuture.buildTable();
final arpService = await _getIt<ARPService>().open();
await arpService.buildTable();
await VendorTable.createVendorTableMap();
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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: 4.0.1
version: 4.0.2
issue_tracker: https://github.com/osociety/network_tools/issues
repository: https://github.com/osociety/network_tools

Expand Down

0 comments on commit d148f37

Please sign in to comment.