Skip to content

Commit 861b12d

Browse files
committed
powershell issues
1 parent c2fdc9b commit 861b12d

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

src/windows.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ pub fn tproxy_setup(tproxy_args: &TproxyArgs) -> std::io::Result<TproxyState> {
99
let unspecified = Ipv4Addr::UNSPECIFIED.to_string();
1010
let gateway = tproxy_args.tun_gateway.to_string();
1111
let args = &["add", &unspecified, "mask", &unspecified, &gateway, "metric", "6"];
12-
run_command("route", args)?;
1312
#[cfg(feature = "log")]
1413
log::info!("route {:?}", args);
14+
run_command("route", args)?;
1515

1616
let (original_gateway, _) = get_default_gateway()?;
1717

@@ -27,9 +27,9 @@ pub fn tproxy_setup(tproxy_args: &TproxyArgs) -> std::io::Result<TproxyState> {
2727
// command: `netsh interface ip set dns "utun3" static 10.0.0.1`
2828
let tun_name = format!("\"{}\"", tproxy_args.tun_name);
2929
let args = &["interface", "ip", "set", "dns", &tun_name, "static", &gateway];
30-
run_command("netsh", args)?;
3130
#[cfg(feature = "log")]
3231
log::info!("netsh {:?}", args);
32+
run_command("netsh", args)?;
3333

3434
let state = TproxyState {
3535
tproxy_args: Some(tproxy_args.clone()),
@@ -49,9 +49,9 @@ fn do_bypass_ip(bypass_ip: cidr::IpCidr, original_gateway: IpAddr) -> std::io::R
4949
// route the bypass ip to the original gateway
5050
// command: `route add bypass_ip/24 original_gateway metric 1`
5151
let args = &["add", &bypass_ip.to_string(), &original_gateway.to_string(), "metric", "1"];
52-
run_command("route", args)?;
5352
#[cfg(feature = "log")]
5453
log::info!("route {:?}", args);
54+
run_command("route", args)?;
5555
Ok(())
5656
}
5757

@@ -138,7 +138,14 @@ fn _tproxy_remove(state: &mut TproxyState) -> std::io::Result<()> {
138138

139139
pub(crate) fn get_default_gateway() -> std::io::Result<(IpAddr, String)> {
140140
let cmd = "Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | ForEach-Object { $_.DefaultIPGateway }";
141-
let gateways = run_command("powershell", &["-Command", cmd])?;
141+
let gateways = match run_command("powershell", &["-Command", cmd]) {
142+
Ok(gateways) => gateways,
143+
Err(e) => {
144+
let str = format!("Command \"powershell -Command {}\" error: {}", cmd, e);
145+
let err = std::io::Error::new(std::io::ErrorKind::Other, str);
146+
return Err(err);
147+
}
148+
};
142149

143150
let stdout = String::from_utf8_lossy(&gateways).into_owned();
144151
let lines: Vec<&str> = stdout.lines().collect();
@@ -168,7 +175,14 @@ pub(crate) fn get_default_gateway() -> std::io::Result<(IpAddr, String)> {
168175

169176
pub(crate) fn get_default_gateway_interface() -> std::io::Result<String> {
170177
let cmd = "Get-WmiObject -Class Win32_NetworkAdapter | Where-Object { $_.NetConnectionStatus -eq 2 } | Select-Object -First 1 -ExpandProperty NetConnectionID";
171-
let iface = run_command("powershell", &["-Command", cmd])?;
178+
let iface = match run_command("powershell", &["-Command", cmd]) {
179+
Ok(iface) => iface,
180+
Err(e) => {
181+
let str = format!("Command \"powershell -Command {}\" error: {}", cmd, e);
182+
let err = std::io::Error::new(std::io::ErrorKind::Other, str);
183+
return Err(err);
184+
}
185+
};
172186

173187
let stdout = String::from_utf8_lossy(&iface).into_owned();
174188
let iface = stdout.trim().to_string();

0 commit comments

Comments
 (0)