feat: support set system proxy command

This commit is contained in:
GyDi 2021-12-17 02:15:40 +08:00
parent ad60013f52
commit 3a9734e97d
5 changed files with 76 additions and 4 deletions

View File

@ -8,6 +8,7 @@ use crate::{
app_home_dir,
clash::{self, put_clash_profile},
fetch::fetch_profile,
sysopt::{set_proxy_config, SysProxyConfig},
},
};
use std::fs::File;
@ -171,3 +172,44 @@ pub async fn put_profiles(
save_profiles(&profiles);
put_clash_profile(&clash_info).await
}
#[tauri::command]
/// set system proxy
pub fn set_sys_proxy(enable: bool, clash_info: State<'_, ClashInfoState>) -> Result<(), String> {
let clash_info = match clash_info.0.lock() {
Ok(arc) => arc.clone(),
_ => return Err(format!("can not get clash info")),
};
let port = match clash_info.controller {
Some(ctrl) => ctrl.port,
None => None,
};
if port.is_none() {
return Err(format!("can not get clash core's port"));
}
let config = if enable {
let server = format!("127.0.0.1:{}", port.unwrap());
// todo
let bypass = String::from("localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;192.168.*");
SysProxyConfig {
enable,
server,
bypass,
}
} else {
SysProxyConfig {
enable,
server: String::from(""),
bypass: String::from(""),
}
};
match set_proxy_config(&config) {
Ok(_) => Ok(()),
Err(_) => Err(format!("can not set proxy")),
}
}

View File

@ -21,6 +21,9 @@ pub struct ClashConfig {
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct ClashController {
/// clash core port
pub port: Option<String>,
/// same as `external-controller`
pub server: Option<String>,
pub secret: Option<String>,

View File

@ -44,9 +44,31 @@ pub fn read_clash() -> Mapping {
pub fn read_clash_controller() -> ClashController {
let config = read_clash();
let key_port_1 = Value::String("port".to_string());
let key_port_2 = Value::String("mixed-port".to_string());
let key_server = Value::String("external-controller".to_string());
let key_secret = Value::String("secret".to_string());
let port = match config.get(&key_port_1) {
Some(value) => match value {
Value::String(val_str) => Some(val_str.clone()),
Value::Number(val_num) => Some(val_num.to_string()),
_ => None,
},
_ => None,
};
let port = match port {
Some(_) => port,
None => match config.get(&key_port_2) {
Some(value) => match value {
Value::String(val_str) => Some(val_str.clone()),
Value::Number(val_num) => Some(val_num.to_string()),
_ => None,
},
_ => None,
},
};
let server = match config.get(&key_server) {
Some(value) => match value {
Value::String(val_str) => Some(val_str.clone()),
@ -64,7 +86,11 @@ pub fn read_clash_controller() -> ClashController {
_ => None,
};
ClashController { server, secret }
ClashController {
port,
server,
secret,
}
}
/// Get Profiles Config

View File

@ -63,6 +63,7 @@ fn main() -> std::io::Result<()> {
cmd::get_profiles,
cmd::set_profiles,
cmd::put_profiles,
cmd::set_sys_proxy,
])
.build(tauri::generate_context!())
.expect("error while running tauri application");

View File

@ -3,9 +3,9 @@ use std::io;
#[derive(Debug, Deserialize, Serialize)]
pub struct SysProxyConfig {
enable: bool,
server: String,
bypass: String,
pub enable: bool,
pub server: String,
pub bypass: String,
}
#[cfg(target_os = "windows")]