Enhance protocol blocking service to monitor and update active VPN peer IPs
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import 'package:waylume_server/services/vpn_session_service.dart';
|
||||||
|
|
||||||
class Connection {
|
class Connection {
|
||||||
final String protocol;
|
final String protocol;
|
||||||
@@ -27,6 +28,7 @@ class Connection {
|
|||||||
class ProtocolBlockingService {
|
class ProtocolBlockingService {
|
||||||
static Timer? _monitorTimer;
|
static Timer? _monitorTimer;
|
||||||
static final Set<String> _processedConnections = {};
|
static final Set<String> _processedConnections = {};
|
||||||
|
static Set<String> _activePeerIPs = {};
|
||||||
|
|
||||||
static void initialize() {
|
static void initialize() {
|
||||||
print('Initializing Protocol Blocking Service...');
|
print('Initializing Protocol Blocking Service...');
|
||||||
@@ -35,11 +37,32 @@ class ProtocolBlockingService {
|
|||||||
|
|
||||||
static void _startConnectionMonitoring() {
|
static void _startConnectionMonitoring() {
|
||||||
print('📡 Starting connection monitoring timer (100ms intervals)...');
|
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 {
|
_monitorTimer = Timer.periodic(Duration(milliseconds: 100), (_) async {
|
||||||
await _scanForNewConnections();
|
await _scanForNewConnections();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Future<void> _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 int _scanCount = 0;
|
||||||
|
|
||||||
static Future<void> _scanForNewConnections() async {
|
static Future<void> _scanForNewConnections() async {
|
||||||
@@ -71,7 +94,7 @@ class ProtocolBlockingService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (final conn in [...tcpConnections, ...udpConnections]) {
|
for (final conn in [...tcpConnections, ...udpConnections]) {
|
||||||
if (_isNewConnection(conn)) {
|
if (_isNewConnection(conn) && _isPeerConnection(conn)) {
|
||||||
await _handleNewConnection(conn);
|
await _handleNewConnection(conn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -164,6 +187,11 @@ class ProtocolBlockingService {
|
|||||||
return true;
|
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<void> _handleNewConnection(Connection conn) async {
|
static Future<void> _handleNewConnection(Connection conn) async {
|
||||||
print('🔍 New connection detected: $conn');
|
print('🔍 New connection detected: $conn');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user