diff --git a/lib/services/protocol_blocking_service.dart b/lib/services/protocol_blocking_service.dart index 40fc445..3689368 100644 --- a/lib/services/protocol_blocking_service.dart +++ b/lib/services/protocol_blocking_service.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'dart:io'; +import 'package:waylume_server/services/vpn_session_service.dart'; class Connection { final String protocol; @@ -27,6 +28,7 @@ class Connection { class ProtocolBlockingService { static Timer? _monitorTimer; static final Set _processedConnections = {}; + static Set _activePeerIPs = {}; static void initialize() { print('Initializing Protocol Blocking Service...'); @@ -35,11 +37,32 @@ class ProtocolBlockingService { static void _startConnectionMonitoring() { print('📡 Starting connection monitoring timer (100ms intervals)...'); + // Update peer IPs every 30 seconds + Timer.periodic(Duration(seconds: 30), (_) async { + await _updateActivePeerIPs(); + }); + // Initial peer IP update + _updateActivePeerIPs(); + _monitorTimer = Timer.periodic(Duration(milliseconds: 100), (_) async { await _scanForNewConnections(); }); } + static Future _updateActivePeerIPs() async { + try { + final peers = await VpnSessionService.getAllLocalPeers(); + final newPeerIPs = peers.map((peer) => peer['ip_address'] as String).toSet(); + + if (newPeerIPs.length != _activePeerIPs.length || !_activePeerIPs.containsAll(newPeerIPs)) { + _activePeerIPs = newPeerIPs; + print('🔄 Updated active peer IPs: $_activePeerIPs'); + } + } catch (e) { + print('❌ Error updating peer IPs: $e'); + } + } + static int _scanCount = 0; static Future _scanForNewConnections() async { @@ -71,7 +94,7 @@ class ProtocolBlockingService { } for (final conn in [...tcpConnections, ...udpConnections]) { - if (_isNewConnection(conn)) { + if (_isNewConnection(conn) && _isPeerConnection(conn)) { await _handleNewConnection(conn); } } @@ -164,6 +187,11 @@ class ProtocolBlockingService { return true; } + static bool _isPeerConnection(Connection conn) { + // Check if the connection is FROM a VPN peer (local IP is peer IP) + return _activePeerIPs.contains(conn.localIP); + } + static Future _handleNewConnection(Connection conn) async { print('🔍 New connection detected: $conn');