dns resolvectl or /etc/resolv.conf is tested in linux

This commit is contained in:
alex 2025-12-23 22:43:19 +08:00
parent 6672e78c42
commit 0300eb9ade
2 changed files with 14 additions and 17 deletions

View File

@ -113,8 +113,7 @@ async fn main() {
match tokio::signal::ctrl_c().await { match tokio::signal::ctrl_c().await {
Ok(()) => { Ok(()) => {
restore_dns(); let _ = restore_dns();
println!("restoreing dns");
} }
Err(err) => { Err(err) => {
eprintln!("failed to listen for shutdown signal: {}", err); eprintln!("failed to listen for shutdown signal: {}", err);

View File

@ -153,10 +153,7 @@ impl Iface {
} }
if let Err(e) = set_dns(self, &self.name, network_domain, &ip_to_string(&default_gw)) { if let Err(e) = set_dns(self, &self.name, network_domain, &ip_to_string(&default_gw)) {
println!("failed to set dns: {}", e.as_str());
error!("failed to set dns: {}", e.as_str()); error!("failed to set dns: {}", e.as_str());
} else {
println!("set dns ok");
} }
} else { } else {
info!("set tun device"); info!("set tun device");
@ -179,10 +176,7 @@ impl Iface {
} }
if let Err(e) = set_dns(self, &self.name, network_domain, &ip_to_string(&default_gw)) { if let Err(e) = set_dns(self, &self.name, network_domain, &ip_to_string(&default_gw)) {
println!("failed to set dns: {}", e.as_str());
error!("failed to set dns: {}", e.as_str()); error!("failed to set dns: {}", e.as_str());
} else {
println!("set dns ok");
} }
} }
} }
@ -228,12 +222,10 @@ impl TunTapPacketHandler for Iface {
if let Some(ip) = headers.net { if let Some(ip) = headers.net {
match ip { match ip {
etherparse::NetHeaders::Ipv4(ipv4, _) => { etherparse::NetHeaders::Ipv4(ipv4, _) => {
println!("3, target = {}.{}.{}.{}", ipv4.destination[0], ipv4.destination[1], ipv4.destination[2], ipv4.destination[3]);
if u32::from_be_bytes(ipv4.destination) == DNS_IP { if u32::from_be_bytes(ipv4.destination) == DNS_IP {
// should send to dns // should send to dns
println!("got dns request");
if let Err(e) = edge.udp_sock_for_dns.send_to(&data[14..], format!("{}:15353", edge.server_ip)).await { if let Err(e) = edge.udp_sock_for_dns.send_to(&data[14..], format!("{}:15353", edge.server_ip)).await {
println!("failed to send request to 15353: {}", e); error!("failed to send request to 15353: {}", e);
} }
// edge.udp_sock_for_dns.send_to() // edge.udp_sock_for_dns.send_to()
return Ok(()) return Ok(())
@ -597,7 +589,7 @@ fn set_dns(
add_resolvectl(name, network_domain)?; add_resolvectl(name, network_domain)?;
} else { } else {
backup_resolv_conf()?; backup_resolv_conf()?;
modify_resolv_conf(&vec!["100.100.100.100".to_owned()], true)?; modify_resolv_conf(&vec!["100.100.100.100".to_owned()], network_domain, true)?;
} }
add_dns_route(gw)?; add_dns_route(gw)?;
Ok(()) Ok(())
@ -617,7 +609,7 @@ pub fn restore_dns() -> Result<()> {
/// ///
/// - `new_nameservers`: 新的 nameserver 列表IPv4/IPv6 字符串) /// - `new_nameservers`: 新的 nameserver 列表IPv4/IPv6 字符串)
/// - `keep_other_ns`: 是否保留原有的 nameservertrue = 追加到新列表后false = 完全替换) /// - `keep_other_ns`: 是否保留原有的 nameservertrue = 追加到新列表后false = 完全替换)
pub fn modify_resolv_conf(new_nameservers: &[String], keep_other_ns: bool) -> Result<()> { pub fn modify_resolv_conf(new_nameservers: &[String], search_domain: &str, keep_other_ns: bool) -> Result<()> {
let path = Path::new(RESOLV_FILE); let path = Path::new(RESOLV_FILE);
if !path.exists() { if !path.exists() {
return Err(SDLanError::IOError(format!("{} does not exists", RESOLV_FILE))); return Err(SDLanError::IOError(format!("{} does not exists", RESOLV_FILE)));
@ -636,6 +628,7 @@ pub fn modify_resolv_conf(new_nameservers: &[String], keep_other_ns: bool) -> Re
let mut inserted = false; let mut inserted = false;
let mut encounted_nameserver = false; let mut encounted_nameserver = false;
let mut search_added = false;
for line in reader.lines() { for line in reader.lines() {
let line = line?; let line = line?;
@ -657,20 +650,25 @@ pub fn modify_resolv_conf(new_nameservers: &[String], keep_other_ns: bool) -> Re
} }
} }
// 保留非 nameserver 行注释、search、options 等) // 保留非 nameserver 行注释、search、options 等)
lines.push(line); if trimmed.starts_with("search ") {
lines.push(format!("{} {}", trimmed, search_domain));
search_added = true;
} else {
lines.push(line);
}
} }
} }
if !search_added {
lines.push(format!("search {}", search_domain));
}
// 原子写入:先写临时文件 // 原子写入:先写临时文件
let tmp_dir = Path::new("/etc"); let tmp_dir = Path::new("/etc");
let tmp_path = tmp_dir.join("resolv.conf.tmp"); let tmp_path = tmp_dir.join("resolv.conf.tmp");
let mut tmp_file = fs::File::create(&tmp_path)?; let mut tmp_file = fs::File::create(&tmp_path)?;
println!("new resolv.conf: ");
for l in &lines { for l in &lines {
writeln!(tmp_file, "{}", l)?; writeln!(tmp_file, "{}", l)?;
println!("{}", l);
} }
println!("");
tmp_file.flush()?; tmp_file.flush()?;
// 设置权限 // 设置权限