Skip to content

Commit

Permalink
Remove the code in Windows platform
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanChane committed Mar 19, 2024
1 parent 77e139b commit c1ce4a5
Show file tree
Hide file tree
Showing 11 changed files with 3 additions and 419 deletions.
65 changes: 0 additions & 65 deletions clashtui/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions clashtui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ nix = {version = "0.28.0", features = ["fs", "user"]}
libc = "0.2.153"
regex = "1.10.3"

[target.'cfg(target_os = "windows")'.dependencies]
encoding = "0.2.33"

[workspace]
resolver = '2'
members = ["api", "ui", "ui-derive"]
Expand Down
5 changes: 0 additions & 5 deletions clashtui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,9 @@ fn load_app_dir(flags: &mut Flags<Flag>) -> std::path::PathBuf {
flags.insert(Flag::PortableMode);
data_dir
} else {
#[cfg(target_os = "linux")]
let clashtui_config_dir_str = env::var("XDG_CONFIG_HOME").map(|p| format!("{}/clashtui", p))
.or_else(|_| env::var("HOME").map(|home| format!("{}/.config/clashtui", home)))
.unwrap();
#[cfg(target_os = "windows")]
let clashtui_config_dir_str = env::var("APPDATA")
.map(|appdata| format!("{}/clashtui", appdata))
.unwrap();
PathBuf::from(&clashtui_config_dir_str)
}
};
Expand Down
19 changes: 0 additions & 19 deletions clashtui/src/tui/tabs/clashsrvctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,10 @@ impl ClashSrvCtlTab {
pub fn new(clashtui_util: SharedClashTuiUtil, clashtui_state: SharedClashTuiState) -> Self {
let mut operations = List::new(CLASHSRVCTL.to_string());
operations.set_items(vec![
#[cfg(target_os = "linux")]
ClashSrvOp::SetPermission.into(),
ClashSrvOp::StartClashService.into(),
ClashSrvOp::StopClashService.into(),
ClashSrvOp::SwitchMode.into(),
#[cfg(target_os = "windows")]
ClashSrvOp::SwitchSysProxy.into(),
#[cfg(target_os = "windows")]
ClashSrvOp::EnableLoopback.into(),
#[cfg(target_os = "windows")]
ClashSrvOp::InstallSrv.into(),
#[cfg(target_os = "windows")]
ClashSrvOp::UnInstallSrv.into(),
]);
let mut modes = List::new("Mode".to_string());
modes.set_items(vec![
Expand Down Expand Up @@ -127,16 +118,6 @@ impl super::TabEvent for ClashSrvCtlTab {
self.hide_msgpopup();
match op {
ClashSrvOp::SwitchMode => unreachable!(),
#[cfg(target_os = "windows")]
ClashSrvOp::SwitchSysProxy => {
let cur = self
.clashtui_state
.borrow()
.get_sysproxy()
.map_or(true, |b| !b);
self.clashtui_state.borrow_mut().set_sysproxy(cur);
self.hide_msgpopup();
}
_ => match self.clashtui_util.clash_srv_ctl(op) {
Ok(output) => {
self.popup_list_msg(output.lines().map(|line| line.trim().to_string()));
Expand Down
14 changes: 0 additions & 14 deletions clashtui/src/tui/tabs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ macro_rules! define_enum {
};
}

#[cfg(target_os = "linux")]
define_enum!(
pub ClashSrvOp,
[
Expand All @@ -108,16 +107,3 @@ define_enum!(
SwitchMode
]
);
#[cfg(target_os = "windows")]
define_enum!(
pub ClashSrvOp,
[
StartClashService,
StopClashService,
SwitchSysProxy,
EnableLoopback,
InstallSrv,
UnInstallSrv,
SwitchMode
]
);
139 changes: 1 addition & 138 deletions clashtui/src/utils/ipc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(target_os = "windows")]
use encoding::{all::GBK, DecoderTrap, Encoding};
use std::process::{Command, Output, Stdio};

use std::io::Result;
Expand All @@ -20,7 +18,7 @@ pub fn spawn(pgm: &str, args: Vec<&str>) -> Result<()> {
.spawn()?;
Ok(())
}
#[cfg(target_os = "linux")]

pub fn exec_with_sbin(pgm: &str, args: Vec<&str>) -> Result<String> {
log::debug!("LIPC: {} {:?}", pgm, args);
let mut path = std::env::var("PATH").unwrap_or_default();
Expand All @@ -29,141 +27,6 @@ pub fn exec_with_sbin(pgm: &str, args: Vec<&str>) -> Result<String> {
string_process_output(output)
}

#[cfg(target_os = "windows")]
fn execute_powershell_script(script: &str) -> Result<String> {
string_process_output(
Command::new("powershell")
.arg("-Command")
.arg(script)
.output()?,
)
}
#[cfg(target_os = "windows")]
pub fn start_process_as_admin(path: &str, arg_list: &str, does_wait: bool) -> Result<String> {
let wait_op = if does_wait { "-Wait" } else { "" };
let arg_op = if arg_list.is_empty() {
String::new()
} else {
format!("-ArgumentList '{}'", arg_list)
};

let output = Command::new("powershell")
.arg("-Command")
.arg(&format!(
"Start-Process {} -FilePath '{}' {} -Verb 'RunAs'",
wait_op, path, arg_op
))
.output()?;

string_process_output(output)
}
#[cfg(target_os = "windows")]
pub fn execute_powershell_script_as_admin(cmd: &str, does_wait: bool) -> Result<String> {
let wait_op = if does_wait { "-Wait" } else { "" };
let cmd_op: String = if cmd.is_empty() {
String::new()
} else {
format!("-ArgumentList '-Command {}'", cmd)
};
let output = Command::new("powershell")
.arg("-Command")
.arg(&format!(
"Start-Process {} -FilePath powershell {} -Verb 'RunAs' 2>&1 | Out-String",
wait_op, cmd_op
))
.output()?;

string_process_output(output)
}
#[cfg(target_os = "windows")]
pub fn enable_system_proxy(proxy_addr: &str) -> Result<String> {
let enable_script = format!(
r#"
$proxyAddress = "{}"
$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-ItemProperty -Path $regPath -Name ProxyEnable -Value 1
Set-ItemProperty -Path $regPath -Name ProxyServer -Value $proxyAddress
gpupdate /force
"#,
proxy_addr
);

execute_powershell_script(&enable_script)
}

#[cfg(target_os = "windows")]
pub fn disable_system_proxy() -> Result<String> {
let disable_script = r#"
$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-ItemProperty -Path $regPath -Name ProxyEnable -Value 0
gpupdate /force
"#;
//Remove-ItemProperty -Path $regPath -Name ProxyServer

execute_powershell_script(disable_script)
}

#[cfg(target_os = "windows")]
pub fn is_system_proxy_enabled() -> Result<bool> {
let reg_query_output = Command::new("reg")
.args(&[
"query",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
"/v",
"ProxyEnable",
])
.output()?;

let output_str = String::from_utf8_lossy(&reg_query_output.stdout);

//log::error!("{}", output_str);
// Assuming the output format is like "ProxyEnable REG_DWORD 0x00000001"
let is_enabled = output_str.contains("REG_DWORD")
&& (output_str.contains("0x1") || output_str.contains("0x00000001"));

Ok(is_enabled)
}

#[cfg(target_os = "windows")]
fn string_process_output(output: Output) -> Result<String> {
let stdout_vec: Vec<u8> = output.stdout;
use std::io::{Error, ErrorKind};

let stdout_str = GBK
.decode(&stdout_vec, DecoderTrap::Strict)
.map_err(|err| {
Error::new(
ErrorKind::InvalidData,
format!("Failed to decode stdout: {err}"),
)
})?;

let stderr_str = GBK
.decode(&output.stderr, DecoderTrap::Strict)
.map_err(|err| {
Error::new(
ErrorKind::InvalidData,
format!("Failed to decode stderr: {err}"),
)
})?;

let result_str = format!(
r#"
Status:
{}
Stdout:
{}
Stderr:
{}
"#,
output.status, stdout_str, stderr_str
);

Ok(result_str)
}
#[cfg(target_os = "linux")]
fn string_process_output(output: Output) -> Result<String> {
let stdout_str = String::from_utf8(output.stdout).unwrap();
let stderr_str = String::from_utf8(output.stderr).unwrap();
Expand Down
Loading

0 comments on commit c1ce4a5

Please sign in to comment.