feat(fl_query_connectivity_plus_adapter): lookup based offline detection

This commit is contained in:
Kingkor Roy Tirtho
2023-10-14 14:42:12 +06:00
parent 3e3047ffd4
commit 363404f9ed
5 changed files with 169 additions and 23 deletions
@@ -0,0 +1,55 @@
import 'dart:io';
import 'package:fl_query_connectivity_plus_adapter/connection_checker.dart';
mixin ConnectionLookupMixin implements ConnectionLookup {
@override
Future<bool> doesConnectTo(String address) async {
try {
final result = await InternetAddress.lookup(address);
return result.isNotEmpty && result[0].rawAddress.isNotEmpty;
} catch (_) {
return false;
}
}
static const _vpnNames = [
'tun',
'tap',
'ppp',
'pptp',
'l2tp',
'ipsec',
'vpn',
'wireguard',
'openvpn',
'softether',
'proton',
'strongswan',
'cisco',
'forticlient',
'fortinet',
'hideme',
'hidemy',
'hideman',
'hidester',
'lightway',
];
@override
Future<bool> isVpnActive() async {
final interfaces = await NetworkInterface.list(
includeLoopback: false,
type: InternetAddressType.any,
);
if (interfaces.isEmpty) {
return false;
}
return interfaces.any(
(interface) =>
_vpnNames.any((name) => interface.name.toLowerCase().contains(name)),
);
}
}
@@ -0,0 +1,29 @@
import 'dart:html';
import 'package:fl_query_connectivity_plus_adapter/connection_checker.dart';
mixin ConnectionLookupMixin implements ConnectionLookup {
@override
Future<bool> doesConnectTo(String address) async {
try {
final url = address.startsWith("http") || address.startsWith("/")
? address
: "https://$address";
final request = await HttpRequest.request(
url,
requestHeaders: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
},
);
return (request.status ?? 0) < 400;
} catch (_) {
return false;
}
}
@override
Future<bool> isVpnActive() {
return Future.value(false);
}
}