mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-05-07 10:14:35 +00:00
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:
@@ -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");
|
||||
|
||||
@@ -114,12 +114,21 @@ impl GlobalCtx {
|
||||
|
||||
let (event_bus, _) = tokio::sync::broadcast::channel(8);
|
||||
|
||||
let stun_servers = config_fs.get_stun_servers();
|
||||
let stun_info_collection = Arc::new(if stun_servers.is_empty() {
|
||||
StunInfoCollector::new_with_default_servers()
|
||||
let stun_info_collector = StunInfoCollector::new_with_default_servers();
|
||||
|
||||
if let Some(stun_servers) = config_fs.get_stun_servers() {
|
||||
stun_info_collector.set_stun_servers(stun_servers);
|
||||
} else {
|
||||
StunInfoCollector::new(stun_servers)
|
||||
});
|
||||
stun_info_collector.set_stun_servers(Vec::new());
|
||||
}
|
||||
|
||||
if let Some(stun_servers) = config_fs.get_stun_servers_v6() {
|
||||
stun_info_collector.set_stun_servers_v6(stun_servers);
|
||||
} else {
|
||||
stun_info_collector.set_stun_servers_v6(Vec::new());
|
||||
}
|
||||
|
||||
let stun_info_collector = Arc::new(stun_info_collector);
|
||||
|
||||
let enable_exit_node = config_fs.get_flags().enable_exit_node || cfg!(target_env = "ohos");
|
||||
let proxy_forward_by_system = config_fs.get_flags().proxy_forward_by_system;
|
||||
@@ -145,12 +154,12 @@ impl GlobalCtx {
|
||||
|
||||
ip_collector: Mutex::new(Some(Arc::new(IPCollector::new(
|
||||
net_ns,
|
||||
stun_info_collection.clone(),
|
||||
stun_info_collector.clone(),
|
||||
)))),
|
||||
|
||||
hostname: Mutex::new(hostname),
|
||||
|
||||
stun_info_collection: Mutex::new(stun_info_collection),
|
||||
stun_info_collection: Mutex::new(stun_info_collector),
|
||||
|
||||
running_listeners: Mutex::new(Vec::new()),
|
||||
|
||||
|
||||
@@ -71,12 +71,7 @@ impl IfConfiguerTrait for MacIfConfiger {
|
||||
run_shell_cmd(format!("ifconfig {} inet delete", name).as_str()).await
|
||||
} else {
|
||||
run_shell_cmd(
|
||||
format!(
|
||||
"ifconfig {} inet {} delete",
|
||||
name,
|
||||
ip.unwrap().address().to_string()
|
||||
)
|
||||
.as_str(),
|
||||
format!("ifconfig {} inet {} delete", name, ip.unwrap().address()).as_str(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ impl InterfaceFilter {
|
||||
async fn is_interface_physical(&self) -> bool {
|
||||
let interface_name = &self.iface.name;
|
||||
let output = tokio::process::Command::new("networksetup")
|
||||
.args(&["-listallhardwareports"])
|
||||
.args(["-listallhardwareports"])
|
||||
.output()
|
||||
.await
|
||||
.expect("Failed to execute command");
|
||||
@@ -79,11 +79,7 @@ impl InterfaceFilter {
|
||||
|
||||
if line.contains("Device:") && line.contains(interface_name) {
|
||||
let next_line = lines[i + 1];
|
||||
if next_line.contains("Virtual Interface") {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
return !next_line.contains("Virtual Interface");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -718,10 +718,10 @@ impl StunInfoCollectorTrait for StunInfoCollector {
|
||||
}
|
||||
|
||||
impl StunInfoCollector {
|
||||
pub fn new(stun_servers: Vec<String>) -> Self {
|
||||
pub fn new(stun_servers: Vec<String>, stun_servers_v6: Vec<String>) -> Self {
|
||||
Self {
|
||||
stun_servers: Arc::new(RwLock::new(stun_servers)),
|
||||
stun_servers_v6: Arc::new(RwLock::new(Self::get_default_servers_v6())),
|
||||
stun_servers_v6: Arc::new(RwLock::new(stun_servers_v6)),
|
||||
udp_nat_test_result: Arc::new(RwLock::new(None)),
|
||||
public_ipv6: Arc::new(AtomicCell::new(None)),
|
||||
nat_test_result_time: Arc::new(AtomicCell::new(Local::now())),
|
||||
@@ -732,7 +732,17 @@ impl StunInfoCollector {
|
||||
}
|
||||
|
||||
pub fn new_with_default_servers() -> Self {
|
||||
Self::new(Self::get_default_servers())
|
||||
Self::new(Self::get_default_servers(), Self::get_default_servers_v6())
|
||||
}
|
||||
|
||||
pub fn set_stun_servers(&self, stun_servers: Vec<String>) {
|
||||
let mut g = self.stun_servers.write().unwrap();
|
||||
*g = stun_servers;
|
||||
}
|
||||
|
||||
pub fn set_stun_servers_v6(&self, stun_servers_v6: Vec<String>) {
|
||||
let mut g = self.stun_servers_v6.write().unwrap();
|
||||
*g = stun_servers_v6;
|
||||
}
|
||||
|
||||
pub fn get_default_servers() -> Vec<String> {
|
||||
|
||||
Reference in New Issue
Block a user