Line data Source code
1 : import 'package:connectivity_plus/connectivity_plus.dart';
2 : import 'package:meta/meta.dart';
3 :
4 : import 'vpn_detector_platform_interface.dart';
5 :
6 : /// Status of the VPN connection.
7 : enum VpnStatus {
8 : /// VPN is active.
9 : active,
10 :
11 : /// VPN is not active.
12 : notActive,
13 : }
14 :
15 : /// A utility to detect VPN status and react to connectivity changes.
16 : class VpnDetector {
17 : /// Creates a [VpnDetector] with default dependencies.
18 2 : factory VpnDetector() => VpnDetector.withDependencies(
19 1 : connectivity: Connectivity(),
20 1 : platform: VpnDetectorPlatform.instance,
21 : );
22 :
23 : /// Creates a [VpnDetector] with injected dependencies for testing.
24 1 : @visibleForTesting
25 : VpnDetector.withDependencies({
26 : required Connectivity connectivity,
27 : required VpnDetectorPlatform platform,
28 : }) : _connectivity = connectivity,
29 : _platform = platform;
30 :
31 : final Connectivity _connectivity;
32 : final VpnDetectorPlatform _platform;
33 :
34 : /// Checks whether VPN is active, returning a [VpnStatus].
35 1 : Future<VpnStatus> isVpnActive() async {
36 2 : final bool active = await _platform.isVpnActive();
37 : return active ? VpnStatus.active : VpnStatus.notActive;
38 : }
39 :
40 : /// A stream of [VpnStatus] that updates whenever the connectivity changes.
41 1 : Stream<VpnStatus> get onVpnStatusChanged =>
42 5 : _connectivity.onConnectivityChanged.asyncMap((_) => isVpnActive());
43 : }
|