Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update vpn_connection_detector.dart #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

flareso
Copy link

@flareso flareso commented Sep 4, 2024

Enhance VPN Detection Logic and Real-Time Stream Integration

Summary:

This PR significantly improves the VPN detection logic by adding a robust and dynamic VpnConnectionDetector class. The changes provide a real-time stream to monitor VPN connection states and include enhanced flexibility for detecting various VPN types across platforms.


Key Changes:

  1. New VpnConnectionDetector Class:

    • Introduced a singleton class that allows real-time VPN detection using network interfaces.
    • The class checks network interfaces for common VPN-related patterns (tun, tap, vpn, etc.) to determine if a VPN is active.
    • VPN detection is triggered by both network connectivity changes and periodic checks every 30 seconds.
  2. Real-Time VPN Connection State Stream:

    • Developers can subscribe to vpnConnectionStream to listen for updates on whether a VPN connection is active or disconnected.
    • The stream is broadcast-based, allowing multiple listeners.
  3. Custom VPN Pattern Support:

    • The addVpnPattern() method enables developers to add custom VPN interface patterns dynamically to account for unique VPN implementations.
  4. Static Method for One-Time VPN Checks:

    • Added isVpnActive() as a static method for cases where a one-time VPN status check is needed, without subscribing to the stream.
  5. Optimized Resource Management:

    • Proper resource management is implemented with a dispose() method that cleans up stream controllers, connectivity subscriptions, and timers.
  6. Extensive Documentation:

    • Included comprehensive inline documentation for easier integration and understanding of the class functionality.

Example Usage:

final vpnDetector = VpnConnectionDetector();

// Listen for real-time VPN connection state changes
vpnDetector.vpnConnectionStream.listen((state) {
  if (state == VpnConnectionState.connected) {
    print("VPN is connected.");
  } else {
    print("VPN is disconnected.");
  }
});

// One-time check for VPN connection
bool isVpnActive = await VpnConnectionDetector.isVpnActive();
print('VPN active: $isVpnActive');

// Add custom VPN pattern if needed
vpnDetector.addVpnPattern('customvpn');

// Clean up resources when done
vpnDetector.dispose();

Benefits:

  • Cross-platform support: Works across Android, iOS, macOS, and other platforms using the NetworkInterface API.
  • Real-time detection: Ensures that changes in VPN status are captured instantly or within the 30-second interval.
  • Customizable: Allows developers to extend the detection capabilities by adding their own patterns for VPN interfaces.
  • Lightweight: Optimized to handle periodic checks and network changes without impacting app performance.

Next Steps:

  • Review the changes for cross-platform compatibility.
  • Merge into the main branch to allow other developers to start leveraging real-time VPN detection.

Looking forward to feedback and suggestions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant