fix command's status

This commit is contained in:
alex 2026-04-24 10:29:26 +08:00
parent 1789c8f62c
commit 0c045dd828
2 changed files with 103 additions and 54 deletions

View File

@ -177,8 +177,12 @@ impl Iface {
.arg("up") .arg("up")
.output(); .output();
match res { match res {
Ok(_) => { Ok(r) => {
debug!("ifconfig ok"); if !r.status.success() {
error!("ifconfig failed: {:?}", r.status.code())
} else {
debug!("ifconfig ok");
}
} }
Err(e) => { Err(e) => {
error!("failed to run ifconfig: {}", e.to_string()); error!("failed to run ifconfig: {}", e.to_string());
@ -207,8 +211,12 @@ impl Iface {
.arg("up") .arg("up")
.output(); .output();
match res { match res {
Ok(_) => { Ok(r) => {
debug!("ifconfig ok"); if r.status.success() {
debug!("ifconfig ok");
} else {
error!("failed to ifconfig2: {:?}", r.status.code());
}
} }
Err(e) => { Err(e) => {
error!("failed to run ifconfig: {}", e.to_string()); error!("failed to run ifconfig: {}", e.to_string());
@ -876,7 +884,7 @@ fn check_has_resolvectl() -> bool {
} }
fn add_dns_route(dev_name: &str) -> Result<()> { fn add_dns_route(dev_name: &str) -> Result<()> {
Command::new("route") let res = Command::new("route")
.arg("add") .arg("add")
.arg("-host") .arg("-host")
.arg("100.100.100.100") .arg("100.100.100.100")
@ -884,21 +892,31 @@ fn add_dns_route(dev_name: &str) -> Result<()> {
.arg(dev_name) .arg(dev_name)
.output()?; .output()?;
Ok(()) if res.status.success() {
return Ok(());
}
Err(SDLanError::IOError("failed to add dns route".to_owned()))
} }
fn add_resolvectl(name: &str, network_domain: &str) -> Result<()> { fn add_resolvectl(name: &str, network_domain: &str) -> Result<()> {
Command::new("resolvectl") if !Command::new("resolvectl")
.arg("dns") .arg("dns")
.arg(name) .arg(name)
.arg("100.100.100.100") .arg("100.100.100.100")
.output()?; .output()?.status.success() {
error!("faield to run resolvectl dns");
return Err(SDLanError::IOError("failed to resolvectl dns".to_owned()))
}
Command::new("resolvectl")
if !Command::new("resolvectl")
.arg("domain") .arg("domain")
.arg(name) .arg(name)
.arg(format!("~{}", network_domain)) .arg(format!("~{}", network_domain))
.output()?; .output()?.status.success() {
error!("failed to run resolvectl domain");
return Err(SDLanError::IOError("failed to resolvectl domain".to_owned()))
}
Ok(()) Ok(())
} }
@ -1096,68 +1114,89 @@ fn restore_resolv_conf() -> Result<()> {
} }
pub fn del_route(net: &Ipv4Net, gw: &Ipv4Addr) -> Result<()> { pub fn del_route(net: &Ipv4Net, gw: &Ipv4Addr) -> Result<()> {
let res = Command::new("route") if !Command::new("route")
.arg("del") .arg("del")
.arg("-net") .arg("-net")
.arg(net.to_string()) .arg(net.to_string())
.arg("gw") .arg("gw")
.arg(gw.to_string()) .arg(gw.to_string())
.output()?; .output()?.status.success() {
return Err(SDLanError::IOError("failed to delete route".to_owned()))
}
Ok(()) Ok(())
} }
pub fn add_route(net: &Ipv4Net, gw: &Ipv4Addr, _ifidx: u32) -> Result<()> { pub fn add_route(net: &Ipv4Net, gw: &Ipv4Addr, _ifidx: u32) -> Result<()> {
let res = Command::new("route") if !Command::new("route")
.arg("add") .arg("add")
.arg("-net") .arg("-net")
.arg(net.to_string()) .arg(net.to_string())
.arg("gw") .arg("gw")
.arg(gw.to_string()) .arg(gw.to_string())
.output()?; .output()?.status.success() {
return Err(SDLanError::IOError("failed to delete route".to_owned()))
}
Ok(()) Ok(())
} }
pub fn set_disallow_routing() { pub fn set_disallow_routing() -> Result<()> {
let _ = Command::new("sysctl") if !Command::new("sysctl")
.arg("-w") .arg("-w")
.arg("net.ipv4.ip_forward=0") .arg("net.ipv4.ip_forward=0")
.output(); .output()?.status.success() {
let _ = Command::new("iptables") return Err(SDLanError::IOError("failed to set ip_forward to 0".to_owned()))
}
if !Command::new("iptables")
.arg("-t") .arg("-t")
.arg("nat") .arg("nat")
.arg("-D") .arg("-D")
.arg("POSTROUTING") .arg("POSTROUTING")
.arg("-j") .arg("-j")
.arg("MASQUERADE") .arg("MASQUERADE")
.output(); .output()?.status.success() {
return Err(SDLanError::IOError("failed to delete masquerade".to_owned()))
}
Ok(())
} }
pub fn set_allow_routing() { pub fn set_allow_routing() -> Result<()>{
let _ = Command::new("sysctl") if !Command::new("sysctl")
.arg("-w") .arg("-w")
.arg("net.ipv4.ip_forward=1") .arg("net.ipv4.ip_forward=1")
.output(); .output()?.status.success() {
return Err(SDLanError::IOError("failed to set ip_forward to 1".to_owned()))
}
let _ = Command::new("iptables") if !Command::new("iptables")
.arg("-t") .arg("-t")
.arg("nat") .arg("nat")
.arg("-D") .arg("-D")
.arg("POSTROUTING") .arg("POSTROUTING")
.arg("-j") .arg("-j")
.arg("MASQUERADE") .arg("MASQUERADE")
.output(); .output()?.status.success() {
let _ = Command::new("iptables") return Err(SDLanError::IOError("failed to clear masquerade".to_owned()))
}
if !Command::new("iptables")
.arg("-t") .arg("-t")
.arg("nat") .arg("nat")
.arg("-A") .arg("-A")
.arg("POSTROUTING") .arg("POSTROUTING")
.arg("-j") .arg("-j")
.arg("MASQUERADE") .arg("MASQUERADE")
.output(); .output()?.status.success() {
return Err(SDLanError::IOError("failed to add masquerade".to_owned()))
}
Ok(())
} }
#[cfg(feature = "tun")] #[cfg(feature = "tun")]

View File

@ -4,7 +4,7 @@ use etherparse::{Ethernet2Header, IpHeaders, NetSlice, SlicedPacket, TransportSl
use ipnet::Ipv4Net; use ipnet::Ipv4Net;
use sdlan_sn_rs::config::SDLAN_DEFAULT_TTL; use sdlan_sn_rs::config::SDLAN_DEFAULT_TTL;
use sdlan_sn_rs::utils::{ use sdlan_sn_rs::utils::{
aes_encrypt, ip_to_string, is_multi_broadcast, net_bit_len_to_mask, Result, BROADCAST_MAC, BROADCAST_MAC, Result, SDLanError, aes_encrypt, ip_to_string, is_multi_broadcast, net_bit_len_to_mask
}; };
use std::io::{Error, ErrorKind}; use std::io::{Error, ErrorKind};
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
@ -91,11 +91,16 @@ impl Iface {
.arg(&format!("addr={}", ip)) .arg(&format!("addr={}", ip))
.arg(&format!("mask={}", netbit)); .arg(&format!("mask={}", netbit));
let res = command.output(); let res = command.status();
// let res = command.output();
match res { match res {
Ok(r) => { Ok(r) => {
debug!("netsh ok: [{:?}]", String::from_utf8_lossy(&r.stdout)); if r.success() {
debug!("netsh ok");
} else {
error!("failed to run netsh, returned {:?}", r.code())
}
} }
Err(e) => { Err(e) => {
error!("failed to run netsh: {}", e.to_string()); error!("failed to run netsh: {}", e.to_string());
@ -113,11 +118,15 @@ impl Iface {
.arg(format!("mtu={}", device_config.mtu)) .arg(format!("mtu={}", device_config.mtu))
.arg("store=persistent"); .arg("store=persistent");
let res = command.output(); let res = command.status();
match res { match res {
Ok(r) => { Ok(r) => {
debug!("netsh2 ok: [{:?}]", String::from_utf8_lossy(&r.stdout)); if r.success() {
debug!("netsh2 ok");
} else {
error!("failed to run netsh set mtu, returned {:?}", r.code())
}
} }
Err(e) => { Err(e) => {
error!("failed to run netsh2: {}", e.to_string()); error!("failed to run netsh2: {}", e.to_string());
@ -125,7 +134,7 @@ impl Iface {
} }
// let gw = ip_to_string(&default_gw); // let gw = ip_to_string(&default_gw);
debug!("gw = {}", ip); // debug!("gw = {}", ip);
if let Err(e) = set_dns(&self.name, network_domain, &ip, self.if_idx) { if let Err(e) = set_dns(&self.name, network_domain, &ip, self.if_idx) {
error!("failed to set dns: {:?}", e); error!("failed to set dns: {:?}", e);
} else { } else {
@ -748,7 +757,7 @@ pub fn get_install_channel() -> String {
"windows".to_owned() "windows".to_owned()
} }
pub fn set_dns(name: &str, _network_domain: &str, gw: &str, ifidx: u32) -> std::io::Result<()> { pub fn set_dns(name: &str, _network_domain: &str, gw: &str, ifidx: u32) -> Result<()> {
let res = Command::new("ROUTE") let res = Command::new("ROUTE")
.arg("ADD") .arg("ADD")
.arg("100.100.100.100") .arg("100.100.100.100")
@ -758,8 +767,11 @@ pub fn set_dns(name: &str, _network_domain: &str, gw: &str, ifidx: u32) -> std::
.arg("IF") .arg("IF")
.arg(ifidx.to_string()) .arg(ifidx.to_string())
.creation_flags(0x08000000) .creation_flags(0x08000000)
.output()?; .status()?;
if !res.success() {
error!("failed to add route for dns 100.100.100.100: {:?}", res.code());
return Err(SDLanError::IOError("failed to add route for dns".to_owned()));
}
//println!("res1: {}", res.status.success()); //println!("res1: {}", res.status.success());
debug!("route set ok"); debug!("route set ok");
@ -772,7 +784,11 @@ pub fn set_dns(name: &str, _network_domain: &str, gw: &str, ifidx: u32) -> std::
.arg("address=100.100.100.100") .arg("address=100.100.100.100")
.arg("validate=no") .arg("validate=no")
.creation_flags(0x08000000) .creation_flags(0x08000000)
.output()?; .status()?;
if !res.success() {
error!("failed to set dnsserver");
return Err(SDLanError::IOError("failed to add dnsserver".to_owned()));
}
// println!("res2: {}", res.status.success()); // println!("res2: {}", res.status.success());
debug!("netsh set ok"); debug!("netsh set ok");
@ -792,14 +808,19 @@ pub fn del_route(net: &Ipv4Net, gw: &Ipv4Addr) -> Result<()> {
.arg("MASK") .arg("MASK")
.arg(mask) .arg(mask)
.arg(gw.to_string()) .arg(gw.to_string())
.output()?; .status()?;
if !res.success() {
error!("failed to set dnsserver");
return Err(SDLanError::IOError("failed to delete route".to_owned()));
}
Ok(()) Ok(())
} }
pub fn add_route(net: &Ipv4Net, gw: &Ipv4Addr, if_idx: u32) -> Result<()> { pub fn add_route(net: &Ipv4Net, gw: &Ipv4Addr, if_idx: u32) -> Result<()> {
let mask = net.netmask().to_string(); let mask = net.netmask().to_string();
let network = net.network().to_string(); let network = net.network().to_string();
match Command::new("route") let result = Command::new("route")
.arg("add") .arg("add")
.arg(network) .arg(network)
.arg("MASK") .arg("MASK")
@ -807,23 +828,12 @@ pub fn add_route(net: &Ipv4Net, gw: &Ipv4Addr, if_idx: u32) -> Result<()> {
.arg(gw.to_string()) .arg(gw.to_string())
.arg("if") .arg("if")
.arg(format!("{}", if_idx)) .arg(format!("{}", if_idx))
.output() .status()?;
{ if !result.success() {
Err(e) => { error!("failed to add route: {:?}", result.code());
error!("failed to add route: {}", e); return Err(SDLanError::IOError("failed to add route".to_owned()));
Err(e.into())
}
Ok(value) => {
debug!(
"add route ok: {}, out={:?}, err={}",
value.status,
String::from_utf8_lossy(value.stdout.as_slice()),
String::from_utf8_lossy(value.stderr.as_slice()),
);
Ok(())
}
} }
Ok(())
} }
pub async fn arp_reply_arrived(edge: &Node, data: SdlArpResponse) { pub async fn arp_reply_arrived(edge: &Node, data: SdlArpResponse) {