Skip to content

Commit

Permalink
Merge pull request #169 from osociety/dev
Browse files Browse the repository at this point in the history
Dev -> Main
  • Loading branch information
git-elliot authored Dec 30, 2023
2 parents d148f37 + 565fdbc commit 3f31f10
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 49 deletions.
1 change: 1 addition & 0 deletions lib/network_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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';
export 'src/mdns_scanner/list_of_srv_records.dart';
export 'src/mdns_scanner/mdns_scanner.dart';
export 'src/models/active_host.dart';
export 'src/models/arp_data.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/device_info/arp_table_helper.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:convert';
import 'dart:io';

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

class ARPTableHelper {
static final arpLogger = Logger("arp-table-logger");
Expand Down
2 changes: 1 addition & 1 deletion lib/src/device_info/net_interface.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:io';
import 'package:universal_io/io.dart';

class NetInterface {
NetInterface({
Expand Down
2 changes: 1 addition & 1 deletion lib/src/device_info/vendor_table.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'dart:convert';
import 'dart:io';

import 'package:csv/csv.dart';
import 'package:http/http.dart' as http;
import 'package:network_tools/network_tools.dart';
import 'package:network_tools/src/network_tools_utils.dart';
import 'package:path/path.dart' as p;
import 'package:universal_io/io.dart';

class VendorTable {
static Map<dynamic, dynamic> _vendorTableMap = {};
Expand Down
3 changes: 2 additions & 1 deletion lib/src/mdns_scanner/list_of_srv_records.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ List<String> tcpSrvRecordsList = [
'_zwave-js-server._tcp', // "domain": "zwave_js"
'_axis-video._tcp', // "properties": { "macaddress": "00408c*" } "properties": { "macaddress": "accc8e*" } "properties": { "macaddress": "b8a44f*" }
'_androidtvremote2._tcp',
'_ewelink._tcp',
'_ewelink._tcp', // Ewelink devices
'_nvstream_dbd._tcp',
'_smartview._tcp', // Samsung tv's
];

List<String> udpSrvRecordsList = [
Expand Down
100 changes: 71 additions & 29 deletions lib/src/mdns_scanner/mdns_scanner.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:multicast_dns/multicast_dns.dart';
import 'package:network_tools/network_tools.dart';
import 'package:network_tools/src/mdns_scanner/get_srv_list_by_os/srv_list.dart';
import 'package:network_tools/src/mdns_scanner/list_of_srv_records.dart';
import 'package:network_tools/src/network_tools_utils.dart';
import 'package:universal_io/io.dart';

Expand Down Expand Up @@ -51,8 +50,6 @@ class MdnsScanner {
static Future<List<ActiveHost>> findingMdnsWithAddress(
String serviceType,
) async {
final List<MdnsInfo> mdnsFoundList = [];

final MDnsClient client = MDnsClient(
rawDatagramSocketFactory: (
dynamic host,
Expand All @@ -70,6 +67,7 @@ class MdnsScanner {
},
);

final List<ActiveHost> listOfActiveHost = [];
await client.start();

await for (final PtrResourceRecord ptr in client.lookup<PtrResourceRecord>(
Expand All @@ -79,42 +77,86 @@ class MdnsScanner {
in client.lookup<SrvResourceRecord>(
ResourceRecordQuery.service(ptr.domainName),
)) {
final MdnsInfo mdnsFound = MdnsInfo(
srvResourceRecord: srv,
ptrResourceRecord: ptr,
listOfActiveHost.addAll(
await findAllActiveHostForSrv(
addressType: AddressType.ipv4,
client: client,
ptr: ptr,
srv: srv,
),
);
listOfActiveHost.addAll(
await findAllActiveHostForSrv(
addressType: AddressType.ipv6,
client: client,
ptr: ptr,
srv: srv,
),
);
mdnsFoundList.add(mdnsFound);
}
}
client.stop();

return listOfActiveHost;
}

static Future<List<ActiveHost>> findAllActiveHostForSrv({
required AddressType addressType,
required MDnsClient client,
required PtrResourceRecord ptr,
required SrvResourceRecord srv,
}) async {
final List<ActiveHost> listOfActiveHost = [];
for (final MdnsInfo foundMdns in mdnsFoundList) {
final List<InternetAddress>? internetAddressList;
try {
internetAddressList =
await InternetAddress.lookup(foundMdns.mdnsSrvTarget);

// There can be multiple devices with the same name
for (final InternetAddress internetAddress in internetAddressList) {
final ActiveHost tempHost = ActiveHost(
internetAddress: internetAddress,
mdnsInfoVar: foundMdns,
);
listOfActiveHost.add(tempHost);
}
} catch (e) {
log.severe(
'Error finding ip of mdns record ${foundMdns.ptrResourceRecord.name} srv target ${foundMdns.mdnsSrvTarget}, will add it with ip 0.0.0.0\n$e',
try {
Stream<IPAddressResourceRecord> iPAddressResourceRecordStream;

if (addressType == AddressType.ipv4) {
iPAddressResourceRecordStream = client.lookup<IPAddressResourceRecord>(
ResourceRecordQuery.addressIPv4(srv.target),
);
} else {
iPAddressResourceRecordStream = client.lookup<IPAddressResourceRecord>(
ResourceRecordQuery.addressIPv6(srv.target),
);
final ActiveHost tempHost = ActiveHost(
internetAddress: InternetAddress('0.0.0.0'),
mdnsInfoVar: foundMdns,
}
await for (final IPAddressResourceRecord ip
in iPAddressResourceRecordStream) {
final ActiveHost activeHost = convertSrvToHostName(
internetAddress: ip.address,
ptr: ptr,
srv: srv,
);
listOfActiveHost.add(tempHost);

listOfActiveHost.add(activeHost);
}
} catch (e) {
log.severe(
'Error finding ip of mdns record ${ptr.name} srv target ${srv.target}, will add it with ip 0.0.0.0\n$e',
);
final ActiveHost activeHost = convertSrvToHostName(
internetAddress: InternetAddress('0.0.0.0'),
srv: srv,
ptr: ptr,
);
listOfActiveHost.add(activeHost);
}

return listOfActiveHost;
}

static ActiveHost convertSrvToHostName({
required InternetAddress internetAddress,
required PtrResourceRecord ptr,
required SrvResourceRecord srv,
}) {
final MdnsInfo mdnsInfo = MdnsInfo(
srvResourceRecord: srv,
ptrResourceRecord: ptr,
);
return ActiveHost(
internetAddress: internetAddress,
mdnsInfoVar: mdnsInfo,
);
}
}

enum AddressType { ipv4, ipv6 }
3 changes: 1 addition & 2 deletions lib/src/models/active_host.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final _getIt = GetIt.instance;

/// ActiveHost which implements comparable
/// By default sort by hostId ascending
class ActiveHost extends Comparable<ActiveHost> {
class ActiveHost {
ActiveHost({
required this.internetAddress,
this.openPorts = const [],
Expand Down Expand Up @@ -219,7 +219,6 @@ class ActiveHost extends Comparable<ActiveHost> {
@override
bool operator ==(Object o) => o is ActiveHost && address == o.address;

@override
int compareTo(ActiveHost other) {
return hostId.compareTo(other.hostId);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/models/arp_data.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:io';
import 'package:json_annotation/json_annotation.dart';
import 'package:universal_io/io.dart';

part 'arp_data.g.dart';

Expand Down
4 changes: 2 additions & 2 deletions lib/src/models/open_port.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import 'package:json_annotation/json_annotation.dart';

part 'open_port.g.dart';

/// Represents open port for a target Address
@JsonSerializable()
class OpenPort extends Comparable<OpenPort> {
class OpenPort {
OpenPort(this.port, {this.isOpen = true});
factory OpenPort.fromJson(Map<String, dynamic> json) =>
_$OpenPortFromJson(json);

final int port;
final bool isOpen;

@override
int compareTo(OpenPort other) {
return port.compareTo(other.port);
}
Expand Down
22 changes: 11 additions & 11 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ issue_tracker: https://github.com/osociety/network_tools/issues
repository: https://github.com/osociety/network_tools

environment:
sdk: ">=2.17.6 <4.0.0"
sdk: ">=3.2.0 <4.0.0"

topics:
- network
Expand All @@ -20,36 +20,36 @@ funding:

dependencies:
# A dart csv to list converter.
csv: ^5.0.2
csv: ^5.1.1
# Multi-platform network ping utility.
dart_ping: ^9.0.0
dart_ping: ^9.0.1
# Defines the annotations used by json_serializable
get_it: ^7.6.0
get_it: ^7.6.4
# A composable, Future-based library for making HTTP requests.
http: ^1.1.0
http: ^1.1.2
# Injectable is a convenient code generator for get_it.
injectable: ^2.2.0
injectable: ^2.3.2
# Defines the annotations used by json_serializable.
json_annotation: ^4.8.1
# Debugging and error logging.
logging: ^1.2.0
# Performing mDNS queries (e.g. Bonjour, Avahi).
multicast_dns: ^0.3.2+2
multicast_dns: ^0.3.2+6
# A comprehensive, cross-platform path manipulation library for Dart.
path: ^1.8.3
# Process run helpers
process_run: ^0.13.1
process_run: ^0.13.3+1
# Yet another NoSQL persistent store database solution for single process io apps.
sembast: ^3.5.0+1
# Cross-platform 'dart:io' that works in all platforms.
universal_io: ^2.0.4
universal_io: ^2.2.2


dev_dependencies:
# Standalone generator and watcher for Dart
build_runner: ^2.4.6
build_runner: ^2.4.7
# A generator for injectable library.
injectable_generator: ^2.4.0
injectable_generator: ^2.4.1
# Provides Dart Build System builders for handling JSON.
json_serializable: ^6.7.1
# Set of lint rules for Dart.
Expand Down

0 comments on commit 3f31f10

Please sign in to comment.