From bc4724711172e00477a7302a6df67caf6959f9fb Mon Sep 17 00:00:00 2001 From: Tien Do Nam Date: Fri, 24 Feb 2023 23:25:53 +0100 Subject: [PATCH] feat: improve local ip ranking --- lib/provider/network_info_provider.dart | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/provider/network_info_provider.dart b/lib/provider/network_info_provider.dart index 7574ee4d..ec4869eb 100644 --- a/lib/provider/network_info_provider.dart +++ b/lib/provider/network_info_provider.dart @@ -63,6 +63,11 @@ class NetworkStateNotifier extends StateNotifier { try { // fallback with dart:io NetworkInterface final result = (await NetworkInterface.list()).map((networkInterface) => networkInterface.addresses).expand((ip) => ip); + + for (final i in await NetworkInterface.list()) { + print('INTERFACE: ${i.index} - ${i.name} - ${i.addresses.firstWhereOrNull((element) => element.type == InternetAddressType.IPv4)}'); + } + nativeResult = result.where((ip) => ip.type == InternetAddressType.IPv4).map((address) => address.address).toList(); } catch (e, st) { print(e); @@ -92,6 +97,40 @@ List rankIpAddresses(List nativeResult, String? thirdPartyResult } } +// List rankNetworkInterfaces(List interfaces) { +// +// } + +/// Maps network interface names to a score. +/// The higher, the more relevant to the user. +/// +/// In summary, hotspots are preferred over wifi +/// and wifi are preferred over unidentified interfaces (they have score 0) +final _networkInterfaceScore = { + if (checkPlatform([TargetPlatform.windows])) + ...{ + 'Local Area Connection': 2, // hotspot + 'Wi-Fi': 1, // wifi + 'WLAN': 1, // wifi + }, + if (checkPlatform([TargetPlatform.macOS, TargetPlatform.iOS])) + ...{ + 'bridge': 2, // hotspot + 'en':1 , // wifi + }, + if (checkPlatform([TargetPlatform.linux])) + ...{ + 'hotspot': 3, // hotspot + 'eth': 2, // ethernet + 'wl': 1, // wifi + }, + if (checkPlatform([TargetPlatform.android])) + ...{ + 'swlan0': 2, // hotspot + 'wlan0': 1, // wifi + }, +}; + /// Sorts Ip addresses with first being the most likely primary local address /// Currently, /// - sorts ending with ".1" last