Skip to content

Commit

Permalink
Merge pull request #154 from osociety/dev
Browse files Browse the repository at this point in the history
Dev -> Main
  • Loading branch information
git-elliot authored Sep 24, 2023
2 parents 2f5d8d4 + e204fe1 commit 83cbe1c
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 39 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

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

## 4.0.0
### Breaking changes

Expand Down
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,23 @@ import 'package:network_tools/network_tools.dart';

## Configure network tools in main function

### For dart native

```dart
Future<void> main() async {
await configureNetworkTools('build', enableDebugging: true);
runApp(const MyApp());
}
```

### For flutter apps

```dart
Future<void> main() async {
await configureNetworkTools(enableDebugging: true);
WidgetsFlutterBinding.ensureInitialized();
// It's necessary to pass correct path to be able to use this library.
final appDocDirectory = await getApplicationDocumentsDirectory();
await configureNetworkTools(appDocDirectory.path, enableDebugging: true);
runApp(const MyApp());
}
```
Expand Down Expand Up @@ -102,17 +116,6 @@ Future<void> main() async {
2. Run port scan : `dart example/port_scan.dart`
3. Run mdns scan : `dart example/mdns_scan.dart`

## Enable Debugging

Add this code to your `main.dart` file

```dart
Logger.root.level = Level.FINE; //set to finest for detailed log
Logger.root.onRecord.listen((record) {
print(
'${DateFormat.Hms().format(record.time)}: ${record.level.name}: ${record.loggerName}: ${record.message}');
});
```

## Sample App

Expand Down
2 changes: 1 addition & 1 deletion example/host_scan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import '../lib/network_tools.dart';
import '../lib/src/network_tools_utils.dart';

void main() async {
await configureNetworkTools();
await configureNetworkTools('build');

String subnet = '192.168.0'; //Default network id for home networks

Expand Down
2 changes: 1 addition & 1 deletion example/mdns_scan.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:network_tools/network_tools.dart';

Future<void> main() async {
await configureNetworkTools();
await configureNetworkTools('build');
for (final ActiveHost activeHost in await MdnsScanner.searchMdnsDevices()) {
final MdnsInfo? mdnsInfo = await activeHost.mdnsInfo;
print(
Expand Down
2 changes: 1 addition & 1 deletion example/port_scan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import '../lib/src/network_tools_utils.dart';
import 'package:network_tools/network_tools.dart';

void main() async {
await configureNetworkTools();
await configureNetworkTools('build');

String subnet = '192.168.0'; //Default network id for home networks

Expand Down
16 changes: 12 additions & 4 deletions lib/network_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ export 'src/models/vendor.dart';
export 'src/port_scanner.dart';

final _getIt = GetIt.instance;
final _arpServiceFuture = _getIt<ARPService>().open();
late bool _enableDebugging;

Future<void> configureNetworkTools({
late String _dbDirectory;

String get dbDirectory => _dbDirectory;
bool get enableDebugging => _enableDebugging;

Future<void> configureNetworkTools(
String dbDirectory, {
bool enableDebugging = false,
}) async {
_enableDebugging = enableDebugging;
if (enableDebugging) {
Logger.root.level = Level.FINE;
Logger.root.onRecord.listen((record) {
Expand All @@ -36,7 +43,8 @@ Future<void> configureNetworkTools({
});
}
configureDependencies();

await (await _arpServiceFuture).buildTable();
_dbDirectory = dbDirectory;
final arpServiceFuture = await _getIt<ARPService>().open();
await arpServiceFuture.buildTable();
await VendorTable.createVendorTableMap();
}
1 change: 1 addition & 0 deletions lib/src/device_info/arp_table_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ARPTableHelper {

static Future<List<ARPData>> buildTable() async {
final arpEntries = <ARPData>[];
if (Platform.isAndroid || Platform.isIOS) return arpEntries;
final result = await Process.run('arp', ['-a']);
final entries = const LineSplitter().convert(result.stdout.toString());
RegExp? pattern;
Expand Down
15 changes: 8 additions & 7 deletions lib/src/device_info/vendor_table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import 'dart:io';

import 'package:csv/csv.dart';
import 'package:http/http.dart' as http;
import 'package:network_tools/src/models/arp_data.dart';
import 'package:network_tools/src/models/vendor.dart';
import 'package:network_tools/network_tools.dart';
import 'package:network_tools/src/network_tools_utils.dart';
import 'package:path/path.dart' as p;

class VendorTable {
static Map<dynamic, dynamic> _vendorTableMap = {};
Expand All @@ -32,22 +32,23 @@ class VendorTable {

static Future<Map<dynamic, dynamic>> _fetchVendorTable() async {
//Download and store
log.fine("Downloading mac-vendors-export.csv from network_tools");
final directory = Directory("mac-vendors-export.csv");
if (!directory.existsSync()) {
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");
final response = await http.get(
Uri.https(
"raw.githubusercontent.com",
"osociety/network_tools/main/lib/assets/mac-vendors-export.csv",
),
);
File(directory.path).writeAsBytesSync(response.bodyBytes);
file.writeAsBytesSync(response.bodyBytes);
log.fine("Downloaded mac-vendors-export.csv successfully");
} else {
log.fine("File mac-vendors-export.csv already exists");
}

final input = File(directory.path).openRead();
final input = file.openRead();

List<List<dynamic>> fields = await input
.transform(utf8.decoder)
Expand Down
26 changes: 18 additions & 8 deletions lib/src/host_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,17 @@ class HostScanner {

await for (final message in receivePort) {
if (message is SendPort) {
message.send(<String>[
subnet,
i.toString(),
limit.toString(),
timeoutInSeconds.toString(),
resultsInAddressAscendingOrder.toString(),
]);
message.send(
<String>[
subnet,
i.toString(),
limit.toString(),
timeoutInSeconds.toString(),
resultsInAddressAscendingOrder.toString(),
dbDirectory,
enableDebugging.toString(),
],
);
} else if (message is SendableActiveHost) {
progressCallback
?.call((i - firstHostId) * 100 / (lastValidSubnet - firstHostId));
Expand All @@ -206,7 +210,6 @@ class HostScanner {
/// Will search devices in the network inside new isolate
@pragma('vm:entry-point')
static Future<void> _startSearchingDevices(SendPort sendPort) async {
await configureNetworkTools();
final port = ReceivePort();
sendPort.send(port.sendPort);

Expand All @@ -217,6 +220,13 @@ class HostScanner {
final int lastSubnetIsolate = int.parse(message[2]);
final int timeoutInSeconds = int.parse(message[3]);
final bool resultsInAddressAscendingOrder = message[4] == "true";
final String dbDirectory = message[5];
final bool enableDebugging = message[6] == "true";
// configure again
await configureNetworkTools(
dbDirectory,
enableDebugging: enableDebugging,
);

/// Will contain all the hosts that got discovered in the network, will
/// be use inorder to cancel on dispose of the page.
Expand Down
10 changes: 7 additions & 3 deletions lib/src/services/impls/arp_service_sembast_impl.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'dart:async';

import 'package:injectable/injectable.dart';
import 'package:network_tools/network_tools.dart';
import 'package:network_tools/src/device_info/arp_table_helper.dart';
import 'package:network_tools/src/models/arp_data.dart';
import 'package:network_tools/src/services/arp_service.dart';
import 'package:path/path.dart' as p;
import 'package:sembast/sembast.dart';
import 'package:sembast/sembast_io.dart';

Expand All @@ -16,7 +17,9 @@ class ARPServiceSembastImpl extends ARPService {
Future<void> buildTable() async {
final entries =
(await ARPTableHelper.buildTable()).map((e) => e.toJson()).toList();
await _store.addAll(_db!, entries);
if (entries.isNotEmpty) {
await _store.addAll(_db!, entries);
}
}

@override
Expand Down Expand Up @@ -55,7 +58,8 @@ class ARPServiceSembastImpl extends ARPService {
Future<ARPService> open() async {
if (_db != null) return Future.value(this);
final dbFactory = databaseFactoryIo;
_db = await dbFactory.openDatabase('build/network_tools.db');
final dbPath = p.join(dbDirectory, 'network_tools.db');
_db = await dbFactory.openDatabase(dbPath);
return Future.value(this);
}
}
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.0
version: 4.0.1
issue_tracker: https://github.com/osociety/network_tools/issues
repository: https://github.com/osociety/network_tools

Expand Down
2 changes: 1 addition & 1 deletion test/network_tools_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void main() {
late ServerSocket server;
// Fetching interfaceIp and hostIp
setUpAll(() async {
await configureNetworkTools();
await configureNetworkTools('build', enableDebugging: true);
//open a port in shared way because of portscanner using same,
//if passed false then two hosts come up in search and breaks test.
server =
Expand Down

0 comments on commit 83cbe1c

Please sign in to comment.