Skip to content

Commit

Permalink
Merge pull request #150 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 d054395 + 4a31686 commit 2f5d8d4
Show file tree
Hide file tree
Showing 23 changed files with 319 additions and 188 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
**/build/

# Generated sources
*.g.dart
*.g.dart
*.config.dart

# Downloaded files
/mac-vendors-export.csv
1 change: 1 addition & 0 deletions .pubignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
**/test/
**/donation/
*.sh
*.csv

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 4.0.0
### Breaking changes

1. Call configureNetworkTools in your main function


## 3.2.7

1. Removed example folder and added pubignore.
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ Partly Work:

Please check [network_tools_flutter](https://github.com/osociety/network_tools_flutter) package for extensive support to features on different platforms.

## Import package in your app
## Import package in your app

```dart
import 'package:network_tools/network_tools.dart';
```

## Configure network tools in main function

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

## Usage
Expand Down
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ analyzer:
exclude:
- "**/*.g.dart"
- "**/*.freezed.dart"
- "**/*.config.dart"
- "**/*.pb.dart"
- "**/*.pbenum.dart"
- "**/*.pbgrpc.dart"
Expand Down
9 changes: 2 additions & 7 deletions example/host_scan.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import 'package:logging/logging.dart';
import '../lib/network_tools.dart';
import '../lib/src/network_tools_utils.dart';

void main() async {
Logger.root.level = Level.FINE;
Logger.root.onRecord.listen((record) {
print(
'${record.time.toLocal()}: ${record.level.name}: ${record.loggerName}: ${record.message}',
);
});
final log = Logger("host_scan_example");
await configureNetworkTools();

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

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

Future<void> main() async {
await configureNetworkTools();
for (final ActiveHost activeHost in await MdnsScanner.searchMdnsDevices()) {
final MdnsInfo? mdnsInfo = await activeHost.mdnsInfo;
print(
Expand Down
39 changes: 22 additions & 17 deletions example/port_scan.dart
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
import 'package:logging/logging.dart';
import '../lib/src/network_tools_utils.dart';
import 'package:network_tools/network_tools.dart';

void main() {
final log = Logger("port-scan");
Logger.root.level = Level.FINE;
Logger.root.onRecord.listen((record) {
print(
'${record.time.toLocal()}: ${record.level.name}: ${record.loggerName}: ${record.message}',
);
});
void main() async {
await configureNetworkTools();

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('.'));
String subnet = '192.168.0'; //Default network id for home networks

final interface = await NetInterface.localInterface();
final netId = interface?.networkId;
if (netId != null) {
subnet = netId;
log.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,
53,
// firstHostId: 1,
// lastHostId: 254,
22,
progressCallback: (progress) {
log.finer('Progress for port discovery on host : $progress');
},
);

stream2.listen(
(activeHost) {
log.fine(
'[scanDevicesForSinglePort]: Found device : ${activeHost.toString()}');
final OpenPort deviceWithOpenPort = activeHost.openPorts[0];
if (deviceWithOpenPort.isOpen) {
log.fine(
'Found open port: ${deviceWithOpenPort.port} on ${activeHost.address}',
'[scanDevicesForSinglePort]: Found open port: ${deviceWithOpenPort.port} on ${activeHost.address}',
);
}
},
Expand All @@ -42,7 +41,13 @@ void main() {
},
); // Don't forget to cancel the stream when not in use.

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

PortScanner.scanPortsForSingleDevice(
target,
// Scan will start from this port.
Expand Down
12 changes: 12 additions & 0 deletions lib/injectable.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';
import 'package:network_tools/injectable.config.dart';

final getIt = GetIt.instance;

@InjectableInit(
initializerName: 'init', // default
preferRelativeImports: true, // default
asExtension: true, // default
)
void configureDependencies() => getIt.init();
28 changes: 27 additions & 1 deletion lib/network_tools.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/// Network tools base library
library network_tools;

export 'src/device_info/arp_table.dart';
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/services/arp_service.dart';

export 'src/device_info/net_interface.dart';
export 'src/device_info/vendor_table.dart';
export 'src/host_scanner.dart';
Expand All @@ -14,3 +19,24 @@ export 'src/models/open_port.dart';
export 'src/models/sendable_active_host.dart';
export 'src/models/vendor.dart';
export 'src/port_scanner.dart';

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

Future<void> configureNetworkTools({
bool enableDebugging = false,
}) async {
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}',
);
});
}
configureDependencies();

await (await _arpServiceFuture).buildTable();
await VendorTable.createVendorTableMap();
}
100 changes: 0 additions & 100 deletions lib/src/device_info/arp_table.dart

This file was deleted.

49 changes: 49 additions & 0 deletions lib/src/device_info/arp_table_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'dart:convert';
import 'dart:io';

import 'package:logging/logging.dart';
import 'package:network_tools/src/models/arp_data.dart';

class ARPTableHelper {
static final arpLogger = Logger("arp-table-logger");

static Future<List<ARPData>> buildTable() async {
final arpEntries = <ARPData>[];
final result = await Process.run('arp', ['-a']);
final entries = const LineSplitter().convert(result.stdout.toString());
RegExp? pattern;
if (Platform.isMacOS) {
pattern = RegExp(
r'(?<host>[\w.?]*)\s\((?<ip>.*)\)\sat\s(?<mac>.*)\son\s(?<intf>\w+)\sifscope\s*(\w*)\s*\[(?<typ>.*)\]',
);
} else if (Platform.isLinux) {
pattern = RegExp(
r'(?<host>[\w.?]*)\s\((?<ip>.*)\)\sat\s(?<mac>.*)\s\[(?<typ>.*)\]\son\s(?<intf>\w+)',
);
} else {
pattern = RegExp(r'(?<ip>.*)\s(?<mac>.*)\s(?<typ>.*)');
}

for (final entry in entries) {
final match = pattern.firstMatch(entry);
if (match != null) {
final arpData = ARPData(
hostname: match.groupNames.contains('host')
? match.namedGroup("host") ?? ''
: '',
iPAddress: match.namedGroup("ip") ?? ARPData.nullIPAddress,
macAddress: match.namedGroup("mac") ?? ARPData.nullMacAddress,
interfaceName: match.groupNames.contains('intf')
? match.namedGroup("intf") ?? ''
: '',
interfaceType: match.namedGroup("typ") ?? ARPData.nullInterfaceType,
);
if (arpData.macAddress != '(incomplete)') {
arpLogger.fine("Adding entry to table -> $arpData");
arpEntries.add(arpData);
}
}
}
return arpEntries;
}
}
5 changes: 3 additions & 2 deletions lib/src/device_info/net_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class NetInterface {
final String ipAddress;

static Future<NetInterface?> localInterface() async {
final interfaceList =
await NetworkInterface.list(); //will give interface list
final interfaceList = await NetworkInterface.list(
type: InternetAddressType.IPv4,
); //will give interface list
if (interfaceList.isNotEmpty) {
final localInterface =
interfaceList.first; //fetching first interface like en0/eth0
Expand Down
Loading

0 comments on commit 2f5d8d4

Please sign in to comment.