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

View File

@ -4,7 +4,7 @@ use etherparse::{Ethernet2Header, IpHeaders, NetSlice, SlicedPacket, TransportSl
use ipnet::Ipv4Net;
use sdlan_sn_rs::config::SDLAN_DEFAULT_TTL;
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::net::Ipv4Addr;
@ -91,11 +91,16 @@ impl Iface {
.arg(&format!("addr={}", ip))
.arg(&format!("mask={}", netbit));
let res = command.output();
let res = command.status();
// let res = command.output();
match res {
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) => {
error!("failed to run netsh: {}", e.to_string());
@ -113,11 +118,15 @@ impl Iface {
.arg(format!("mtu={}", device_config.mtu))
.arg("store=persistent");
let res = command.output();
let res = command.status();
match res {
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) => {
error!("failed to run netsh2: {}", e.to_string());
@ -125,7 +134,7 @@ impl Iface {
}
// 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) {
error!("failed to set dns: {:?}", e);
} else {
@ -748,7 +757,7 @@ pub fn get_install_channel() -> String {
"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")
.arg("ADD")
.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(ifidx.to_string())
.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());
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("validate=no")
.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());
debug!("netsh set ok");
@ -792,14 +808,19 @@ pub fn del_route(net: &Ipv4Net, gw: &Ipv4Addr) -> Result<()> {
.arg("MASK")
.arg(mask)
.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(())
}
pub fn add_route(net: &Ipv4Net, gw: &Ipv4Addr, if_idx: u32) -> Result<()> {
let mask = net.netmask().to_string();
let network = net.network().to_string();
match Command::new("route")
let result = Command::new("route")
.arg("add")
.arg(network)
.arg("MASK")
@ -807,23 +828,12 @@ pub fn add_route(net: &Ipv4Net, gw: &Ipv4Addr, if_idx: u32) -> Result<()> {
.arg(gw.to_string())
.arg("if")
.arg(format!("{}", if_idx))
.output()
{
Err(e) => {
error!("failed to add route: {}", e);
Err(e.into())
.status()?;
if !result.success() {
error!("failed to add route: {:?}", result.code());
return Err(SDLanError::IOError("failed to add route".to_owned()));
}
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(())
}
}
}
pub async fn arp_reply_arrived(edge: &Node, data: SdlArpResponse) {