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

Dev -> Main #195

Merged
merged 7 commits into from
Mar 30, 2024
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
22 changes: 8 additions & 14 deletions lib/src/device_info/vendor_table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,14 @@
class VendorTable {
static Map<dynamic, dynamic> _vendorTableMap = {};

static Future<Vendor?> getVendor(Future<ARPData?> arpDataFuture) async {
final arpData = await arpDataFuture;
if (arpData != null) {
if (arpData.notNullMacAddress) {
await createVendorTableMap();
final pattern = arpData.macAddress.contains(':') ? ':' : '-';
return _vendorTableMap[arpData.macAddress
.split(pattern)
.sublist(0, 3)
.join()
.toUpperCase()] as Vendor?;
}
}
return null;
static Future<Vendor?> macToVendor(String macAddress) async {
await createVendorTableMap();
final pattern = macAddress.contains(':') ? ':' : '-';
return _vendorTableMap[macAddress
.split(pattern)
.sublist(0, 3)
.join()
.toUpperCase()] as Vendor?;

Check warning on line 20 in lib/src/device_info/vendor_table.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/device_info/vendor_table.dart#L13-L20

Added lines #L13 - L20 were not covered by tests
}

static Future<void> createVendorTableMap() async {
Expand Down
21 changes: 19 additions & 2 deletions lib/src/models/active_host.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
ActiveHost({
required this.internetAddress,
this.openPorts = const [],
String? macAddress,
PingData? pingData,
MdnsInfo? mdnsInfoVar,
}) {
_macAddress = macAddress;
final String tempAddress = internetAddress.address;

if (tempAddress.contains('.')) {
Expand Down Expand Up @@ -53,6 +55,7 @@
}
factory ActiveHost.buildWithAddress({
required String address,
String? macAddress,
List<OpenPort> openPorts = const [],
PingData? pingData,
MdnsInfo? mdnsInfo,
Expand All @@ -64,6 +67,7 @@
}
return ActiveHost(
internetAddress: internetAddressTemp,
macAddress: macAddress,
openPorts: openPorts,
pingData: pingData,
mdnsInfoVar: mdnsInfo,
Expand All @@ -72,6 +76,7 @@

factory ActiveHost.fromSendableActiveHost({
required SendableActiveHost sendableActiveHost,
String? macAddress,
MdnsInfo? mdnsInfo,
}) {
final InternetAddress? internetAddressTemp =
Expand All @@ -81,6 +86,7 @@
}
return ActiveHost(
internetAddress: internetAddressTemp,
macAddress: macAddress,
openPorts: sendableActiveHost.openPorts,
pingData: sendableActiveHost.pingData,
mdnsInfoVar: mdnsInfo,
Expand Down Expand Up @@ -111,6 +117,10 @@
/// Only works if arpData is not null and have valid mac address.
late Future<Vendor?> vendor;

String? _macAddress;
Future<String?> getMacAddress() async =>
_macAddress ?? (await arpData)?.macAddress;

/// List of all the open port of this device
List<OpenPort> openPorts;

Expand All @@ -130,7 +140,12 @@

PingData tempPingData = const PingData();

Ping(host, count: 1, timeout: timeoutInSeconds).stream.listen((pingData) {
Ping(
host,
count: 1,
timeout: timeoutInSeconds,
forceCodepage: Platform.isWindows,
).stream.listen((pingData) {
final PingResponse? response = pingData.response;
if (response != null) {
final Duration? time = response.time;
Expand Down Expand Up @@ -184,7 +199,9 @@
}

Future<Vendor?> setVendor() async {
return VendorTable.getVendor(arpData);
final String? macAddress = await getMacAddress();

return macAddress == null ? null : VendorTable.macToVendor(macAddress);

Check warning on line 204 in lib/src/models/active_host.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/models/active_host.dart#L204

Added line #L204 was not covered by tests
}

/// Try to find the mdns name of this device, if not exist mdns name will
Expand Down
9 changes: 7 additions & 2 deletions lib/src/services/impls/host_scanner_service_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:dart_ping/dart_ping.dart';
import 'package:network_tools/network_tools.dart';
import 'package:network_tools/src/network_tools_utils.dart';
import 'package:network_tools/src/services/arp_service.dart';
import 'package:universal_io/io.dart';

/// Scans for all hosts in a subnet.
class HostScannerServiceImpl extends HostScannerService {
Expand Down Expand Up @@ -106,8 +107,12 @@ class HostScannerServiceImpl extends HostScannerService {
}) async {
SendableActiveHost? tempSendableActivateHost;

await for (final PingData pingData
in Ping(host, count: 1, timeout: timeoutInSeconds).stream) {
await for (final PingData pingData in Ping(
host,
count: 1,
timeout: timeoutInSeconds,
forceCodepage: Platform.isWindows,
).stream) {
final PingResponse? response = pingData.response;
final PingError? pingError = pingData.error;
if (response != null) {
Expand Down
Loading