refactor: update custom STUN server settings (#1310)

* refactor: update global context STUN server initialization

Modified global context initialization to use a single StunInfoCollector
instance with properly configured IPv4 and IPv6 servers instead of
creating separate instances.

feat: add IPv6 STUN server configuration support

Added interface methods and config struct fields to support both IPv4
and IPv6 STUN server configuration. Modified getter and setter methods
to handle Option<Vec<String>> type for both server types.

feat: enhance StunInfoCollector with IPv6 support

Updated StunInfoCollector to support both IPv4 and IPv6 STUN servers.
Added new constructor that accepts both server types and methods to set
them independently.

feat: add CLI argument for IPv6 STUN servers

Added command line argument support for configuring IPv6 STUN servers.
Updated configuration setup to handle both IPv4 and IPv6 STUN server
settings.

docs: add localization for STUN server configuration

Added English and Chinese localization strings for the new STUN server
configuration options, including both IPv4 and IPv6 variants.
This commit is contained in:
fanyang
2025-09-02 21:46:37 +08:00
committed by GitHub
parent 754439f03c
commit b87a05b457
10 changed files with 95 additions and 50 deletions
+23 -16
View File
@@ -200,8 +200,11 @@ pub trait ConfigLoader: Send + Sync {
fn get_udp_whitelist(&self) -> Vec<String>;
fn set_udp_whitelist(&self, whitelist: Vec<String>);
fn get_stun_servers(&self) -> Vec<String>;
fn set_stun_servers(&self, servers: Vec<String>);
fn get_stun_servers(&self) -> Option<Vec<String>>;
fn set_stun_servers(&self, servers: Option<Vec<String>>);
fn get_stun_servers_v6(&self) -> Option<Vec<String>>;
fn set_stun_servers_v6(&self, servers: Option<Vec<String>>);
fn dump(&self) -> String;
}
@@ -374,7 +377,7 @@ impl From<PortForwardConfig> for PortForwardConfigPb {
}
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct Config {
netns: Option<String>,
hostname: Option<String>,
@@ -412,6 +415,7 @@ struct Config {
tcp_whitelist: Option<Vec<String>>,
udp_whitelist: Option<Vec<String>>,
stun_servers: Option<Vec<String>>,
stun_servers_v6: Option<Vec<String>>,
}
#[derive(Debug, Clone)]
@@ -791,17 +795,20 @@ impl ConfigLoader for TomlConfigLoader {
self.config.lock().unwrap().udp_whitelist = Some(whitelist);
}
fn get_stun_servers(&self) -> Vec<String> {
self.config
.lock()
.unwrap()
.stun_servers
.clone()
.unwrap_or_default()
fn get_stun_servers(&self) -> Option<Vec<String>> {
self.config.lock().unwrap().stun_servers.clone()
}
fn set_stun_servers(&self, servers: Vec<String>) {
self.config.lock().unwrap().stun_servers = Some(servers);
fn set_stun_servers(&self, servers: Option<Vec<String>>) {
self.config.lock().unwrap().stun_servers = servers;
}
fn get_stun_servers_v6(&self) -> Option<Vec<String>> {
self.config.lock().unwrap().stun_servers_v6.clone()
}
fn set_stun_servers_v6(&self, servers: Option<Vec<String>>) {
self.config.lock().unwrap().stun_servers_v6 = servers;
}
fn dump(&self) -> String {
@@ -838,14 +845,14 @@ pub mod tests {
fn test_stun_servers_config() {
let config = TomlConfigLoader::default();
let stun_servers = config.get_stun_servers();
assert!(stun_servers.is_empty());
assert!(stun_servers.is_none());
// Test setting custom stun servers
let custom_servers = vec!["txt:stun.easytier.cn".to_string()];
config.set_stun_servers(custom_servers.clone());
config.set_stun_servers(Some(custom_servers.clone()));
let retrieved_servers = config.get_stun_servers();
assert_eq!(retrieved_servers, custom_servers);
assert_eq!(retrieved_servers.unwrap(), custom_servers);
}
#[test]
@@ -859,7 +866,7 @@ stun_servers = [
]"#;
let config = TomlConfigLoader::new_from_str(config_str).unwrap();
let stun_servers = config.get_stun_servers();
let stun_servers = config.get_stun_servers().unwrap();
assert_eq!(stun_servers.len(), 3);
assert_eq!(stun_servers[0], "stun.l.google.com:19302");