diff --git a/src/bin/punchnet/main.rs b/src/bin/punchnet/main.rs index dab3d83..d6a8795 100755 --- a/src/bin/punchnet/main.rs +++ b/src/bin/punchnet/main.rs @@ -113,8 +113,7 @@ async fn main() { match tokio::signal::ctrl_c().await { Ok(()) => { - restore_dns(); - println!("restoreing dns"); + let _ = restore_dns(); } Err(err) => { eprintln!("failed to listen for shutdown signal: {}", err); diff --git a/src/network/tun_linux.rs b/src/network/tun_linux.rs index 01279cd..424cd91 100755 --- a/src/network/tun_linux.rs +++ b/src/network/tun_linux.rs @@ -153,10 +153,7 @@ impl Iface { } 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()); - } else { - println!("set dns ok"); } } else { 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)) { - println!("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 { match ip { 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 { // 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 { - println!("failed to send request to 15353: {}", e); + error!("failed to send request to 15353: {}", e); } // edge.udp_sock_for_dns.send_to() return Ok(()) @@ -597,7 +589,7 @@ fn set_dns( add_resolvectl(name, network_domain)?; } else { 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)?; Ok(()) @@ -617,7 +609,7 @@ pub fn restore_dns() -> Result<()> { /// /// - `new_nameservers`: 新的 nameserver 列表(IPv4/IPv6 字符串) /// - `keep_other_ns`: 是否保留原有的 nameserver(true = 追加到新列表后,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); if !path.exists() { 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 encounted_nameserver = false; + let mut search_added = false; for line in reader.lines() { let line = line?; @@ -657,20 +650,25 @@ pub fn modify_resolv_conf(new_nameservers: &[String], keep_other_ns: bool) -> Re } } // 保留非 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_path = tmp_dir.join("resolv.conf.tmp"); let mut tmp_file = fs::File::create(&tmp_path)?; - println!("new resolv.conf: "); for l in &lines { writeln!(tmp_file, "{}", l)?; - println!("{}", l); } - println!(""); tmp_file.flush()?; // 设置权限