Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate lib and example logs #159

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}