getMostLikelyHost static method

Future<String> getMostLikelyHost(
  1. Uri uri
)

Implementation

static Future<String> getMostLikelyHost(Uri uri) async {
  final parts = uri.host.split('.');
  final interfaces = await NetworkInterface.list();

  String? calc(int skip) {
    final prefix = '${parts.take(parts.length - skip).join('.')}.';

    for (NetworkInterface interface in interfaces) {
      for (InternetAddress addr in interface.addresses) {
        if (addr.address.startsWith(prefix)) {
          return addr.address;
        }
      }
    }

    return null;
  }

  for (var i = 1; i <= 3; i++) {
    final ip = calc(i);
    if (ip != null) {
      return ip;
    }
  }

  return Platform.localHostname;
}