Add peer creation time tracking and enhance DEAD peer handling

This commit is contained in:
ImBenji
2025-08-28 23:00:14 +01:00
parent 1b0ee56d79
commit 73afa2f7a3
2 changed files with 97 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
import 'dart:io';
import 'package:waylume_server/wireguard/peers.dart';
class VpnSessionService {
@@ -47,13 +48,14 @@ class VpnSessionService {
/// Parses keepalive info string into structured data
static Map<String, dynamic> _parseKeepaliveInfo(String keepaliveInfo, String publicKey, String ipAddress) {
if (keepaliveInfo.startsWith('DEAD')) {
final regex = RegExp(r'DEAD - Last handshake: ([^(]+) \((\d+)m (\d+)s ago\)');
final match = regex.firstMatch(keepaliveInfo);
// Handle regular DEAD peers (with handshake)
final handshakeRegex = RegExp(r'DEAD - Last handshake: ([^(]+) \((\d+)m (\d+)s ago\)');
final handshakeMatch = handshakeRegex.firstMatch(keepaliveInfo);
if (match != null) {
final lastHandshake = match.group(1)?.trim();
final minutes = int.tryParse(match.group(2) ?? '0') ?? 0;
final seconds = int.tryParse(match.group(3) ?? '0') ?? 0;
if (handshakeMatch != null) {
final lastHandshake = handshakeMatch.group(1)?.trim();
final minutes = int.tryParse(handshakeMatch.group(2) ?? '0') ?? 0;
final seconds = int.tryParse(handshakeMatch.group(3) ?? '0') ?? 0;
final totalMinutes = minutes + (seconds / 60).round();
return {
@@ -64,6 +66,24 @@ class VpnSessionService {
'status': 'DEAD'
};
}
// Handle DEAD peers without handshake (never connected)
final creationRegex = RegExp(r'DEAD - Last handshake: Never \((\d+)m (\d+)s since creation\)');
final creationMatch = creationRegex.firstMatch(keepaliveInfo);
if (creationMatch != null) {
final minutes = int.tryParse(creationMatch.group(1) ?? '0') ?? 0;
final seconds = int.tryParse(creationMatch.group(2) ?? '0') ?? 0;
final totalMinutes = minutes + (seconds / 60).round();
return {
'public_key': publicKey,
'ip_address': ipAddress,
'last_handshake': 'Never',
'minutes_since_handshake': totalMinutes,
'status': 'DEAD'
};
}
} else if (keepaliveInfo.startsWith('ALIVE')) {
final regex = RegExp(r'ALIVE - Last handshake: ([^(]+) \((\d+)s ago\)');
final match = regex.firstMatch(keepaliveInfo);
@@ -115,6 +135,17 @@ class VpnSessionService {
final handshakeTime = int.tryParse(latestHandshake) ?? 0;
if (handshakeTime == 0) {
// Check peer creation time to see if it's stale
final creationTime = await getPeerCreationTime(publicKey);
if (creationTime != null) {
final now = DateTime.now();
final duration = now.difference(creationTime);
// Mark as DEAD if created more than 2 minutes 30 seconds ago (same threshold as handshake)
if (duration.inSeconds > 150) {
return 'DEAD - Last handshake: Never (${duration.inMinutes}m ${duration.inSeconds % 60}s since creation)';
}
}
return 'No handshake yet';
} else {
final handshakeDateTime = DateTime.fromMillisecondsSinceEpoch(handshakeTime * 1000);