-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,95 @@ | ||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:fl_query/fl_query.dart'; | ||
import 'package:internet_connection_checker/internet_connection_checker.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
|
||
class FlQueryInternetConnectionCheckerAdapter extends ConnectivityAdapter | ||
with WidgetsBindingObserver { | ||
final _connectionStreamController = StreamController<bool>.broadcast(); | ||
|
||
FlQueryInternetConnectionCheckerAdapter() : super() { | ||
Timer.periodic(const Duration(minutes: 3), (timer) async { | ||
if (WidgetsBinding.instance.lifecycleState == AppLifecycleState.paused) { | ||
return; | ||
} | ||
await isConnected; | ||
}); | ||
} | ||
|
||
@override | ||
didChangeAppLifecycleState(AppLifecycleState state) async { | ||
if (state == AppLifecycleState.resumed) { | ||
await isConnected; | ||
} | ||
} | ||
|
||
final vpnNames = [ | ||
'tun', | ||
'tap', | ||
'ppp', | ||
'pptp', | ||
'l2tp', | ||
'ipsec', | ||
'vpn', | ||
'wireguard', | ||
'openvpn', | ||
'softether', | ||
'proton', | ||
'strongswan', | ||
'cisco', | ||
'forticlient', | ||
'fortinet', | ||
'hideme', | ||
'hidemy', | ||
'hideman', | ||
'hidester', | ||
'lightway', | ||
]; | ||
|
||
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)), | ||
); | ||
} | ||
|
||
Future<bool> doesConnectTo(String address) async { | ||
try { | ||
final result = await InternetAddress.lookup(address); | ||
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) { | ||
return true; | ||
} | ||
return false; | ||
} on SocketException catch (_) { | ||
return false; | ||
} | ||
} | ||
|
||
Future<bool> _isConnected() async { | ||
return await doesConnectTo('google.com') || | ||
await doesConnectTo('www.baidu.com') || // for China | ||
await isVpnActive(); // when VPN is active that means we are connected | ||
} | ||
|
||
class FlQueryInternetConnectionCheckerAdapter extends ConnectivityAdapter { | ||
@override | ||
Future<bool> get isConnected => InternetConnectionChecker().hasConnection; | ||
Future<bool> get isConnected async { | ||
final connected = await _isConnected(); | ||
if (connected != isConnectedSync /*previous value*/) { | ||
_connectionStreamController.add(connected); | ||
} | ||
return connected; | ||
} | ||
|
||
@override | ||
Stream<bool> get onConnectivityChanged => InternetConnectionChecker() | ||
.onStatusChange | ||
.map((status) => status == InternetConnectionStatus.connected); | ||
Stream<bool> get onConnectivityChanged => _connectionStreamController.stream; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters