Refactor RollingCodesService to remove configuration file handling and update initialization logic
This commit is contained in:
@@ -7,25 +7,23 @@ import 'package:waylume_server/core/utils.dart';
|
|||||||
import '../config/supabase_config.dart';
|
import '../config/supabase_config.dart';
|
||||||
|
|
||||||
class RollingCodesService {
|
class RollingCodesService {
|
||||||
// Shared constants (these should match edge function constants)
|
// Shared constants (hardcoded to match edge function constants)
|
||||||
static const String registrationSeed = String.fromEnvironment('REGISTRATION_SEED', defaultValue: 'default_registration_seed_change_in_production');
|
static const String registrationSeed = 'waylume_rolling_registration_seed_2025';
|
||||||
static const String secretSalt = String.fromEnvironment('SECRET_SALT', defaultValue: 'default_secret_salt_change_in_production');
|
static const String secretSalt = 'waylume_operational_secret_salt_2025';
|
||||||
static const int timeWindowSeconds = 300; // 5 minutes
|
static const int timeWindowSeconds = 300; // 5 minutes
|
||||||
|
|
||||||
// Server configuration file path
|
|
||||||
static const String configFile = '/tmp/waylume_server_config.json';
|
|
||||||
|
|
||||||
static String? _operationalSeed;
|
static String? _operationalSeed;
|
||||||
static String? _serverId;
|
static String? _serverId;
|
||||||
|
|
||||||
/// Initialize service and load/generate configuration
|
/// Initialize service
|
||||||
static Future<void> initialize() async {
|
static Future<void> initialize() async {
|
||||||
_serverId = fromEnivronment('SERVER_ID');
|
_serverId = fromEnivronment('SERVER_ID');
|
||||||
if (_serverId == null) {
|
if (_serverId == null) {
|
||||||
throw Exception('SERVER_ID environment variable is required');
|
throw Exception('SERVER_ID environment variable is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
await _loadConfiguration();
|
// No need to load configuration - server re-registers on every restart
|
||||||
|
print('Rolling codes service initialized for server $_serverId');
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate registration rolling code
|
/// Generate registration rolling code
|
||||||
@@ -67,9 +65,9 @@ class RollingCodesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set operational seed after successful registration
|
/// Set operational seed after successful registration
|
||||||
static Future<void> setOperationalSeed(String seed) async {
|
static void setOperationalSeed(String seed) {
|
||||||
_operationalSeed = seed;
|
_operationalSeed = seed;
|
||||||
await _saveConfiguration();
|
print('Operational seed set for server $_serverId');
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get current operational seed
|
/// Get current operational seed
|
||||||
@@ -91,42 +89,4 @@ class RollingCodesService {
|
|||||||
|
|
||||||
return digest.toString();
|
return digest.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Load configuration from persistent storage
|
|
||||||
static Future<void> _loadConfiguration() async {
|
|
||||||
try {
|
|
||||||
final file = File(configFile);
|
|
||||||
if (await file.exists()) {
|
|
||||||
final content = await file.readAsString();
|
|
||||||
final config = jsonDecode(content) as Map<String, dynamic>;
|
|
||||||
|
|
||||||
if (config['server_id'] == _serverId) {
|
|
||||||
_operationalSeed = config['operational_seed'];
|
|
||||||
print('Loaded operational seed for server $_serverId');
|
|
||||||
} else {
|
|
||||||
print('Configuration file exists but server_id does not match. Need to re-register.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
print('Error loading configuration: $e');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Save configuration to persistent storage
|
|
||||||
static Future<void> _saveConfiguration() async {
|
|
||||||
try {
|
|
||||||
final config = {
|
|
||||||
'server_id': _serverId,
|
|
||||||
'operational_seed': _operationalSeed,
|
|
||||||
'last_updated': DateTime.now().toIso8601String(),
|
|
||||||
};
|
|
||||||
|
|
||||||
final file = File(configFile);
|
|
||||||
await file.writeAsString(jsonEncode(config));
|
|
||||||
print('Configuration saved for server $_serverId');
|
|
||||||
} catch (e) {
|
|
||||||
print('Error saving configuration: $e');
|
|
||||||
throw Exception('Failed to save server configuration');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -9,11 +9,7 @@ class ServerService {
|
|||||||
static Future<void> registerServer() async {
|
static Future<void> registerServer() async {
|
||||||
await RollingCodesService.initialize();
|
await RollingCodesService.initialize();
|
||||||
|
|
||||||
// If already registered and has operational seed, skip registration
|
// Server always registers on startup to get fresh operational seed
|
||||||
if (RollingCodesService.isRegistered) {
|
|
||||||
print('Server already registered with operational seed');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
GeolocationData geolocationData = await getGeolocationData();
|
GeolocationData geolocationData = await getGeolocationData();
|
||||||
|
|
||||||
@@ -50,8 +46,8 @@ class ServerService {
|
|||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
final responseData = jsonDecode(response.body);
|
final responseData = jsonDecode(response.body);
|
||||||
if (responseData['success']) {
|
if (responseData['success']) {
|
||||||
// Store operational seed
|
// Store operational seed in memory (no persistence needed)
|
||||||
await RollingCodesService.setOperationalSeed(responseData['operational_seed']);
|
RollingCodesService.setOperationalSeed(responseData['operational_seed']);
|
||||||
print('Server registered successfully with server-manager');
|
print('Server registered successfully with server-manager');
|
||||||
} else {
|
} else {
|
||||||
throw Exception('Registration failed: ${responseData['error']}');
|
throw Exception('Registration failed: ${responseData['error']}');
|
||||||
|
|||||||
@@ -21,10 +21,10 @@ void initHeartbeat() {
|
|||||||
DateTime now = DateTime.now().toUtc();
|
DateTime now = DateTime.now().toUtc();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Wait until rolling codes service is initialized and registered
|
// Wait until rolling codes service is initialized and has operational seed
|
||||||
if (!RollingCodesService.isRegistered) {
|
if (!RollingCodesService.isRegistered) {
|
||||||
print("Server not registered yet, skipping heartbeat");
|
print("Server not registered yet, skipping heartbeat");
|
||||||
await Future.delayed(Duration(seconds: 30));
|
await Future.delayed(Duration(seconds: 10)); // Check more frequently during startup
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user