From 78c629485168a7a4c08dfab2a6bca2e8487dcad4 Mon Sep 17 00:00:00 2001 From: ImBenji Date: Tue, 5 Aug 2025 17:25:46 +0100 Subject: [PATCH] Add VPN session monitoring service --- lib/services/vpn_session_monitor.dart | 26 ++++++++++++++++++++ lib/services/vpn_session_service.dart | 35 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 lib/services/vpn_session_monitor.dart create mode 100644 lib/services/vpn_session_service.dart diff --git a/lib/services/vpn_session_monitor.dart b/lib/services/vpn_session_monitor.dart new file mode 100644 index 0000000..b8076f7 --- /dev/null +++ b/lib/services/vpn_session_monitor.dart @@ -0,0 +1,26 @@ +import 'dart:isolate'; +import 'dart:math'; +import 'package:waylume_server/services/vpn_session_service.dart'; + +void initVpnSessionMonitor() { + // Run this on a separate thread + Isolate.spawn((_) async { + + // To avoid server deadlock, add random delay + await Future.delayed(Duration(seconds: Random().nextInt(10))); + + print('VPN Session Monitor started'); + + while (true) { + try { + await VpnSessionService.detectSessions(); + } catch (e) { + print('Error in VPN session monitor: $e'); + } + + // Check for sessions every 60 seconds + await Future.delayed(Duration(seconds: 60)); + } + + }, null); +} \ No newline at end of file diff --git a/lib/services/vpn_session_service.dart b/lib/services/vpn_session_service.dart new file mode 100644 index 0000000..36609ae --- /dev/null +++ b/lib/services/vpn_session_service.dart @@ -0,0 +1,35 @@ +import 'package:supabase/supabase.dart'; +import 'package:waylume_server/config/supabase_config.dart'; + +class VpnSessionService { + + /// Detects existing VPN sessions for this server and prints their IDs + static Future detectSessions() async { + try { + final serverId = fromEnivronment('SERVER_ID'); + + if (serverId == null) { + print('ERROR: SERVER_ID environment variable not set'); + return; + } + + // Get all sessions for this server + final sessions = await SUPABASE_CLIENT + .from('vpn_sessions') + .select('id') + .eq('server_id', serverId); + + if (sessions.isEmpty) { + print('No VPN sessions found for server: $serverId'); + } else { + print('Found ${sessions.length} VPN sessions for server: $serverId'); + for (final session in sessions) { + print('Session ID: ${session['id']}'); + } + } + + } catch (e) { + print('Error detecting sessions: $e'); + } + } +} \ No newline at end of file