Support custom STUN servers configuration (#1212)
Some checks failed
EasyTier Core / pre_job (push) Has been cancelled
EasyTier Core / build_web (push) Has been cancelled
EasyTier Core / build (freebsd-13.2-x86_64, 13.2, ubuntu-22.04, x86_64-unknown-freebsd) (push) Has been cancelled
EasyTier Core / build (linux-aarch64, ubuntu-22.04, aarch64-unknown-linux-musl) (push) Has been cancelled
EasyTier Core / build (linux-arm, ubuntu-22.04, arm-unknown-linux-musleabi) (push) Has been cancelled
EasyTier Core / build (linux-armhf, ubuntu-22.04, arm-unknown-linux-musleabihf) (push) Has been cancelled
EasyTier Core / build (linux-armv7, ubuntu-22.04, armv7-unknown-linux-musleabi) (push) Has been cancelled
EasyTier Core / build (linux-armv7hf, ubuntu-22.04, armv7-unknown-linux-musleabihf) (push) Has been cancelled
EasyTier Core / build (linux-loongarch64, ubuntu-24.04, loongarch64-unknown-linux-musl) (push) Has been cancelled
EasyTier Core / build (linux-mips, ubuntu-22.04, mips-unknown-linux-musl) (push) Has been cancelled
EasyTier Core / build (linux-mipsel, ubuntu-22.04, mipsel-unknown-linux-musl) (push) Has been cancelled
EasyTier Core / build (linux-riscv64, ubuntu-22.04, riscv64gc-unknown-linux-musl) (push) Has been cancelled
EasyTier Core / build (linux-x86_64, ubuntu-22.04, x86_64-unknown-linux-musl) (push) Has been cancelled
EasyTier Core / build (macos-aarch64, macos-latest, aarch64-apple-darwin) (push) Has been cancelled
EasyTier Core / build (macos-x86_64, macos-latest, x86_64-apple-darwin) (push) Has been cancelled
EasyTier Core / build (windows-arm64, windows-latest, aarch64-pc-windows-msvc) (push) Has been cancelled
EasyTier Core / build (windows-i686, windows-latest, i686-pc-windows-msvc) (push) Has been cancelled
EasyTier Core / build (windows-x86_64, windows-latest, x86_64-pc-windows-msvc) (push) Has been cancelled
EasyTier Core / core-result (push) Has been cancelled
EasyTier Core / magisk_build (push) Has been cancelled
EasyTier GUI / pre_job (push) Has been cancelled
EasyTier GUI / build-gui (linux-aarch64, aarch64-unknown-linux-gnu, ubuntu-22.04, aarch64-unknown-linux-musl) (push) Has been cancelled
EasyTier GUI / build-gui (linux-x86_64, x86_64-unknown-linux-gnu, ubuntu-22.04, x86_64-unknown-linux-musl) (push) Has been cancelled
EasyTier GUI / build-gui (macos-aarch64, aarch64-apple-darwin, macos-latest, aarch64-apple-darwin) (push) Has been cancelled
EasyTier GUI / build-gui (macos-x86_64, x86_64-apple-darwin, macos-latest, x86_64-apple-darwin) (push) Has been cancelled
EasyTier GUI / build-gui (windows-arm64, aarch64-pc-windows-msvc, windows-latest, aarch64-pc-windows-msvc) (push) Has been cancelled
EasyTier GUI / build-gui (windows-i686, i686-pc-windows-msvc, windows-latest, i686-pc-windows-msvc) (push) Has been cancelled
EasyTier GUI / build-gui (windows-x86_64, x86_64-pc-windows-msvc, windows-latest, x86_64-pc-windows-msvc) (push) Has been cancelled
EasyTier GUI / gui-result (push) Has been cancelled
EasyTier Mobile / pre_job (push) Has been cancelled
EasyTier Mobile / build-mobile (android, ubuntu-22.04, android) (push) Has been cancelled
EasyTier Mobile / mobile-result (push) Has been cancelled
EasyTier OHOS / pre_job (push) Has been cancelled
EasyTier OHOS / build-ohos (push) Has been cancelled
EasyTier Test / pre_job (push) Has been cancelled
EasyTier Test / test (push) Has been cancelled

* Support custom STUN servers

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
fanyang
2025-08-13 10:35:59 +08:00
committed by GitHub
parent a511abb613
commit 35ff9b82fc
11 changed files with 93 additions and 45 deletions

View File

@@ -82,7 +82,7 @@ pub fn find_interface_index(iface_name: &str) -> io::Result<u32> {
tracing::error!("Failed to find interface index for {}", iface_name);
Err(io::Error::new(
io::ErrorKind::NotFound,
format!("{}", iface_name),
iface_name.to_string(),
))
}

View File

@@ -199,6 +199,9 @@ 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 dump(&self) -> String;
}
@@ -407,6 +410,7 @@ struct Config {
tcp_whitelist: Option<Vec<String>>,
udp_whitelist: Option<Vec<String>>,
stun_servers: Option<Vec<String>>,
}
#[derive(Debug, Clone)]
@@ -786,6 +790,19 @@ 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 set_stun_servers(&self, servers: Vec<String>) {
self.config.lock().unwrap().stun_servers = Some(servers);
}
fn dump(&self) -> String {
let default_flags_json = serde_json::to_string(&gen_default_flags()).unwrap();
let default_flags_hashmap =
@@ -816,6 +833,39 @@ impl ConfigLoader for TomlConfigLoader {
pub mod tests {
use super::*;
#[test]
fn test_stun_servers_config() {
let config = TomlConfigLoader::default();
let stun_servers = config.get_stun_servers();
assert!(stun_servers.is_empty());
// Test setting custom stun servers
let custom_servers = vec!["txt:stun.easytier.cn".to_string()];
config.set_stun_servers(custom_servers.clone());
let retrieved_servers = config.get_stun_servers();
assert_eq!(retrieved_servers, custom_servers);
}
#[test]
fn test_stun_servers_toml_parsing() {
let config_str = r#"
instance_name = "test"
stun_servers = [
"stun.l.google.com:19302",
"stun1.l.google.com:19302",
"txt:stun.easytier.cn"
]"#;
let config = TomlConfigLoader::new_from_str(config_str).unwrap();
let stun_servers = config.get_stun_servers();
assert_eq!(stun_servers.len(), 3);
assert_eq!(stun_servers[0], "stun.l.google.com:19302");
assert_eq!(stun_servers[1], "stun1.l.google.com:19302");
assert_eq!(stun_servers[2], "txt:stun.easytier.cn");
}
#[tokio::test]
async fn full_example_test() {
let config_str = r#"

View File

@@ -112,7 +112,12 @@ impl GlobalCtx {
let (event_bus, _) = tokio::sync::broadcast::channel(8);
let stun_info_collection = Arc::new(StunInfoCollector::new_with_default_servers());
let stun_servers = config_fs.get_stun_servers();
let stun_info_collection = Arc::new(if stun_servers.is_empty() {
StunInfoCollector::new_with_default_servers()
} else {
StunInfoCollector::new(stun_servers)
});
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;

View File

@@ -43,10 +43,9 @@ pub fn convert_ipv4addr_to_inaddr(ip: &Ipv4Addr) -> winapi::shared::inaddr::in_a
pub fn convert_ipv6addr_to_inaddr(ip: &Ipv6Addr) -> winapi::shared::in6addr::in6_addr {
let mut winaddr = winapi::shared::in6addr::in6_addr::default();
let octets = ip.octets();
for i in 0..octets.len() {
unsafe { winaddr.u.Byte_mut()[i] = octets[i] };
for (i, &octet) in octets.iter().enumerate() {
unsafe { winaddr.u.Byte_mut()[i] = octet };
}
winaddr
}

View File

@@ -35,7 +35,7 @@ fn format_win_error(error: u32) -> String {
null_mut(),
error,
0,
buffer.as_mut_ptr() as *mut u16,
buffer.as_mut_ptr(),
size,
null_mut(),
);
@@ -43,9 +43,7 @@ fn format_win_error(error: u32) -> String {
let str_end = buffer.iter().position(|&b| b == 0).unwrap_or(buffer.len());
format!(
"{} (code: {})",
String::from_utf16_lossy(&buffer[..str_end])
.trim()
.to_string(),
String::from_utf16_lossy(&buffer[..str_end]).trim(),
error
)
}

View File

@@ -736,8 +736,8 @@ impl StunInfoCollector {
}
pub fn get_default_servers() -> Vec<String> {
// NOTICE: we may need to choose stun stun server based on geo location
// stun server cross nation may return a external ip address with high latency and loss rate
// NOTICE: we may need to choose stun server based on geolocation
// stun server cross nation may return an external ip address with high latency and loss rate
[
"txt:stun.easytier.cn",
"stun.miwifi.com",

View File

@@ -2060,12 +2060,12 @@ mod win_service_manager {
let service = self
.service_manager
.create_service(&service_info, ServiceAccess::ALL_ACCESS)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
.map_err(io::Error::other)?;
if let Some(s) = description {
service
.set_description(s.clone())
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
.map_err(io::Error::other)?;
}
if let Some(work_dir) = ctx.working_directory {
@@ -2079,33 +2079,27 @@ mod win_service_manager {
let service = self
.service_manager
.open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
.map_err(io::Error::other)?;
service
.delete()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
service.delete().map_err(io::Error::other)
}
fn start(&self, ctx: ServiceStartCtx) -> io::Result<()> {
let service = self
.service_manager
.open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
.map_err(io::Error::other)?;
service
.start(&[] as &[&OsStr])
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
service.start(&[] as &[&OsStr]).map_err(io::Error::other)
}
fn stop(&self, ctx: ServiceStopCtx) -> io::Result<()> {
let service = self
.service_manager
.open_service(ctx.label.to_qualified_name(), ServiceAccess::ALL_ACCESS)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
.map_err(io::Error::other)?;
_ = service
.stop()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
_ = service.stop().map_err(io::Error::other)?;
Ok(())
}
@@ -2117,10 +2111,7 @@ mod win_service_manager {
fn set_level(&mut self, level: ServiceLevel) -> io::Result<()> {
match level {
ServiceLevel::System => Ok(()),
_ => Err(io::Error::new(
io::ErrorKind::Other,
"Unsupported service level",
)),
_ => Err(io::Error::other("Unsupported service level")),
}
}
@@ -2136,13 +2127,11 @@ mod win_service_manager {
return Ok(ServiceStatus::NotInstalled);
}
}
return Err(io::Error::new(io::ErrorKind::Other, e));
return Err(io::Error::other(e));
}
};
let status = service
.query_status()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
let status = service.query_status().map_err(io::Error::other)?;
match status.current_state {
windows_service::service::ServiceState::Stopped => Ok(ServiceStatus::Stopped(None)),

View File

@@ -555,6 +555,15 @@ struct NetworkOptions {
default_missing_value = "true"
)]
enable_relay_foreign_network_kcp: Option<bool>,
#[arg(
long,
env = "ET_STUN_SERVERS",
value_delimiter = ',',
help = t!("core_clap.stun_servers").to_string(),
num_args = 0..
)]
stun_servers: Option<Vec<String>>,
}
#[derive(Parser, Debug)]
@@ -924,6 +933,10 @@ impl NetworkOptions {
old_udp_whitelist.extend(self.udp_whitelist.clone());
cfg.set_udp_whitelist(old_udp_whitelist);
if let Some(stun_servers) = &self.stun_servers {
cfg.set_stun_servers(stun_servers.clone());
}
Ok(())
}
}

View File

@@ -39,7 +39,7 @@ impl InterfaceControl {
if matches!(e.kind(), io::ErrorKind::NotFound) {
Ok(()) // 忽略不存在的值
} else {
Err(e.into())
Err(e)
}
}
}
@@ -106,10 +106,7 @@ impl InterfaceControl {
.output()
.expect("failed to execute process");
if !output.status.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
"Failed to flush DNS cache",
));
return Err(io::Error::other("Failed to flush DNS cache"));
}
Ok(())
}
@@ -122,10 +119,7 @@ impl InterfaceControl {
.output()
.expect("failed to execute process");
if !output.status.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
"Failed to register DNS",
));
return Err(io::Error::other("Failed to register DNS"));
}
Ok(())
}

View File

@@ -399,7 +399,7 @@ impl VirtualNic {
Err(e) => {
println!("Failed to add Easytier to firewall allowlist, Subnet proxy and KCP proxy may not work properly. error: {}", e);
println!("You can add firewall rules manually, or use --use-smoltcp to run with user-space TCP/IP stack.");
println!("");
println!();
}
}
@@ -409,7 +409,7 @@ impl VirtualNic {
}
if !dev_name.is_empty() {
config.tun_name(format!("{}", dev_name));
config.tun_name(&dev_name);
} else {
use rand::distributions::Distribution as _;
let c = crate::arch::windows::interface_count()?;

View File

@@ -117,7 +117,7 @@ pub fn utf8_or_gbk_to_string(s: &[u8]) -> String {
utf8_str
} else {
// 如果解码失败则尝试使用GBK解码
if let Ok(gbk_str) = GBK.decode(&s, DecoderTrap::Strict) {
if let Ok(gbk_str) = GBK.decode(s, DecoderTrap::Strict) {
gbk_str
} else {
String::from_utf8_lossy(s).to_string()