Add VPN session monitoring service
This commit is contained in:
26
lib/services/vpn_session_monitor.dart
Normal file
26
lib/services/vpn_session_monitor.dart
Normal file
@@ -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);
|
||||
}
|
||||
35
lib/services/vpn_session_service.dart
Normal file
35
lib/services/vpn_session_service.dart
Normal file
@@ -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<void> 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');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user