From 23829229953f6266a49e56e5cd0d187bf23a6b1d Mon Sep 17 00:00:00 2001 From: ImBenji Date: Fri, 29 Aug 2025 01:51:34 +0100 Subject: [PATCH] Refactor packet monitoring to capture all traffic on wg0 and enhance logging for peer traffic detection --- lib/services/protocol_blocking_service.dart | 60 +++++++++++---------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/lib/services/protocol_blocking_service.dart b/lib/services/protocol_blocking_service.dart index 78c6eab..5b06a9a 100644 --- a/lib/services/protocol_blocking_service.dart +++ b/lib/services/protocol_blocking_service.dart @@ -89,36 +89,42 @@ class ProtocolBlockingService { } static Future _monitorPeerTraffic() async { - for (final peerIP in _activePeerIPs) { - try { - // Capture any new outbound traffic from this peer - final process = await Process.start('timeout', [ - '0.1', // Very short timeout - just check for new packets - 'tcpdump', - '-i', 'wg0', - '-c', '1', - '-l', // Line buffered - 'src $peerIP and (tcp[tcpflags] & tcp-syn != 0 or udp)', - ]); + // Capture ANY packet on wg0 interface and print it + try { + final process = await Process.start('timeout', [ + '0.1', // Very short timeout + 'tcpdump', + '-i', 'wg0', + '-c', '1', + '-v', // Verbose + ]); + + final output = []; + await for (final data in process.stdout) { + output.add(String.fromCharCodes(data)); + } + + final exitCode = await process.exitCode; + process.kill(); + + if (exitCode == 0 && output.isNotEmpty) { + final packetData = output.join().trim(); + print('📦 PACKET DETECTED: $packetData'); - final output = []; - await for (final data in process.stdout) { - output.add(String.fromCharCodes(data)); - } - - final exitCode = await process.exitCode; - process.kill(); - - if (exitCode == 0 && output.isNotEmpty) { - final packetData = output.join(); - await _analyzeNewPacket(packetData, peerIP); - } - } catch (e) { - // Ignore timeout errors - normal when no new packets - if (!e.toString().contains('timeout')) { - print('❌ Error monitoring peer $peerIP: $e'); + // Check if it's from our monitored peers + for (final peerIP in _activePeerIPs) { + if (packetData.contains(peerIP)) { + print('🎯 PEER TRAFFIC FROM $peerIP: $packetData'); + await _analyzeNewPacket(packetData, peerIP); + break; + } } } + } catch (e) { + // Ignore timeout errors - normal when no packets + if (!e.toString().contains('timeout') && !e.toString().contains('No such device')) { + print('❌ Error monitoring traffic: $e'); + } } }