From 8a0677caf243b74a4f11d771018ef414abde1eb7 Mon Sep 17 00:00:00 2001 From: Revertron Date: Mon, 27 Oct 2025 14:56:32 +0100 Subject: [PATCH] =?UTF-8?q?Added=20automatic=20config=20migration=20for=20?= =?UTF-8?q?incorrect=20test=20port=2042440=20=E2=86=92=204244?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Automatically fixes net.listen port for public nodes that have the incorrect test port 42440. Migration preserves comments and formatting by using text replacement instead of TOML re-serialization. --- src/settings.rs | 108 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-) diff --git a/src/settings.rs b/src/settings.rs index 0a43e46..bae75e1 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -29,7 +29,22 @@ impl Settings { Ok(mut file) => { let mut text = String::new(); file.read_to_string(&mut text).unwrap(); - if let Ok(settings) = toml::from_str(&text) { + if let Ok(mut settings) = toml::from_str::(&text) { + // Migrate incorrect test port 42440 to correct port 4244 for public nodes + if settings.net.public && settings.net.listen.contains(":42440") { + warn!("Migrating incorrect port 42440 to 4244 in net.listen configuration"); + + // Update the in-memory settings + settings.net.listen = settings.net.listen.replace(":42440", ":4244"); + + // Try to save the corrected configuration back to file + if let Err(e) = Self::save_migration(filename, &text) { + warn!("Could not save migrated config to {}: {}", filename, e); + info!("Please manually update net.listen from :42440 to :4244 in your config"); + } else { + info!("Successfully migrated config file {} (port 42440 → 4244)", filename); + } + } return Some(settings); } None @@ -38,6 +53,15 @@ impl Settings { } } + fn save_migration(filename: &str, original_text: &str) -> Result<(), std::io::Error> { + use std::io::Write; + // Simple text replacement preserves all comments and formatting + let migrated_text = original_text.replace(":42440", ":4244"); + let mut file = File::create(filename)?; + file.write_all(migrated_text.as_bytes())?; + Ok(()) + } + pub fn get_origin(&self) -> Bytes { if self.origin.eq("") { return Bytes::zero32(); @@ -152,4 +176,86 @@ fn default_dns_bootstraps() -> Vec { fn default_dns_0x20() -> bool { true +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Write; + + #[test] + fn test_port_migration_for_public_nodes() { + // Create a temporary config file with incorrect port + let test_file = "test_migration_unit.toml"; + let config_content = r#" +origin = "0000001D2A77D63477172678502E51DE7F346061FF7EB188A2445ECA3FC0780E" +key_files = ["key1.toml"] + +[net] +# Comment should be preserved +listen = "[::]:42440" +public = true +"#; + + // Write test config + let mut file = File::create(test_file).unwrap(); + file.write_all(config_content.as_bytes()).unwrap(); + drop(file); + + // Load the config (should trigger migration) + let settings = Settings::load(test_file).unwrap(); + + // Verify the setting in memory is correct + assert_eq!(settings.net.listen, "[::]:4244"); + + // Read the file to verify it was actually modified + let mut file = File::open(test_file).unwrap(); + let mut content = String::new(); + file.read_to_string(&mut content).unwrap(); + + // Verify file was migrated + assert!(content.contains(":4244")); + assert!(!content.contains(":42440")); + // Verify comment was preserved + assert!(content.contains("# Comment should be preserved")); + + // Cleanup + std::fs::remove_file(test_file).ok(); + } + + #[test] + fn test_no_migration_for_private_nodes() { + // Create a temporary config file with incorrect port but public = false + let test_file = "test_no_migration_unit.toml"; + let config_content = r#" +origin = "0000001D2A77D63477172678502E51DE7F346061FF7EB188A2445ECA3FC0780E" +key_files = ["key1.toml"] + +[net] +listen = "[::]:42440" +public = false +"#; + + // Write test config + let mut file = File::create(test_file).unwrap(); + file.write_all(config_content.as_bytes()).unwrap(); + drop(file); + + // Load the config (should NOT trigger migration because public = false) + let settings = Settings::load(test_file).unwrap(); + + // Verify the setting remains unchanged + assert_eq!(settings.net.listen, "[::]:42440"); + + // Read the file to verify it was NOT modified + let mut file = File::open(test_file).unwrap(); + let mut content = String::new(); + file.read_to_string(&mut content).unwrap(); + + // Verify file was NOT migrated (still has 42440) + assert!(content.contains(":42440")); + + // Cleanup + std::fs::remove_file(test_file).ok(); + } } \ No newline at end of file