refactor: impl structs methods

This commit is contained in:
GyDi
2022-01-07 23:29:20 +08:00
parent 72ff261fe3
commit e369311fc2
21 changed files with 676 additions and 762 deletions

View File

@@ -1,168 +0,0 @@
extern crate log;
use crate::{
config::ProfilesConfig,
events::{
emit::{clash_start, ClashInfoPayload},
state,
},
utils::{app_home_dir, clash, config},
};
use reqwest::header::HeaderMap;
use serde_yaml::{Mapping, Value};
use std::{collections::HashMap, env::temp_dir};
use tauri::{
api::process::{Command, CommandEvent},
AppHandle, Manager,
};
/// Run the clash bin
pub fn run_clash_bin(app_handle: &AppHandle) -> ClashInfoPayload {
let app_dir = app_home_dir();
let app_dir = app_dir.as_os_str().to_str().unwrap();
let mut payload = ClashInfoPayload {
status: "success".to_string(),
controller: None,
message: None,
};
let result = match Command::new_sidecar("clash") {
Ok(cmd) => match cmd.args(["-d", app_dir]).spawn() {
Ok(res) => Ok(res),
Err(err) => Err(err.to_string()),
},
Err(err) => Err(err.to_string()),
};
match result {
Ok((mut rx, cmd_child)) => {
log::info!("Successfully execute clash sidecar");
payload.controller = Some(config::read_clash_controller());
if let Ok(mut state) = app_handle.state::<state::ClashSidecarState>().0.lock() {
*state = Some(cmd_child);
};
tauri::async_runtime::spawn(async move {
while let Some(event) = rx.recv().await {
match event {
CommandEvent::Stdout(line) => log::info!("[stdout]: {}", line),
CommandEvent::Stderr(err) => log::error!("[stderr]: {}", err),
_ => {}
}
}
});
// update the profile
let payload_ = payload.clone();
tauri::async_runtime::spawn(async move {
let mut count = 5; // retry times
let mut err = String::from("");
while count > 0 {
match clash::put_clash_profile(&payload_).await {
Ok(_) => return,
Err(e) => err = e,
}
count -= 1;
}
log::error!("failed to put config for `{}`", err);
});
}
Err(err) => {
log::error!("Failed to execute clash sidecar for \"{}\"", err);
payload.status = "error".to_string();
payload.message = Some(err.to_string());
}
};
clash_start(app_handle, &payload);
payload
}
/// Update the clash profile firstly
pub async fn put_clash_profile(payload: &ClashInfoPayload) -> Result<(), String> {
let profile = {
let profiles = ProfilesConfig::read_file();
let current = profiles.current.unwrap_or(0) as usize;
match profiles.items {
Some(items) => {
if items.len() == 0 {
return Err("can not read profiles".to_string());
}
let idx = if current < items.len() { current } else { 0 };
items[idx].clone()
}
None => {
return Err("can not read profiles".to_string());
}
}
};
// temp profile's path
let temp_path = temp_dir().join("clash-verge-runtime.yaml");
// generate temp profile
{
let file_name = match profile.file {
Some(file_name) => file_name.clone(),
None => return Err(format!("profile item should have `file` field")),
};
let file_path = app_home_dir().join("profiles").join(file_name);
if !file_path.exists() {
return Err(format!("profile `{:?}` not exists", file_path));
}
// Only the following fields are allowed:
// proxies/proxy-providers/proxy-groups/rule-providers/rules
let config = config::read_yaml::<Mapping>(file_path.clone());
let mut new_config = Mapping::new();
vec![
"proxies",
"proxy-providers",
"proxy-groups",
"rule-providers",
"rules",
]
.iter()
.map(|item| Value::String(item.to_string()))
.for_each(|key| {
if config.contains_key(&key) {
let value = config[&key].clone();
new_config.insert(key, value);
}
});
match config::save_yaml(
temp_path.clone(),
&new_config,
Some("# Clash Verge Temp File"),
) {
Err(err) => return Err(err),
_ => {}
};
}
let ctrl = payload.controller.clone().unwrap();
let server = format!("http://{}/configs", ctrl.server.unwrap());
let mut headers = HeaderMap::new();
headers.insert("Content-Type", "application/json".parse().unwrap());
if let Some(secret) = ctrl.secret {
headers.insert(
"Authorization",
format!("Bearer {}", secret).parse().unwrap(),
);
}
let mut data = HashMap::new();
data.insert("path", temp_path.as_os_str().to_str().unwrap());
let client = reqwest::Client::new();
match client.put(server).headers(headers).json(&data).send().await {
Ok(_) => Ok(()),
Err(err) => Err(format!("request failed `{}`", err.to_string())),
}
}

View File

@@ -1,9 +1,6 @@
use crate::{
config::{ClashController, VergeConfig},
utils::app_home_dir,
};
use crate::utils::dirs;
use serde::{de::DeserializeOwned, Serialize};
use serde_yaml::{Mapping, Value};
use serde_yaml::Mapping;
use std::{fs, path::PathBuf};
/// read data from yaml as struct T
@@ -19,100 +16,33 @@ pub fn save_yaml<T: Serialize>(
data: &T,
prefix: Option<&str>,
) -> Result<(), String> {
if let Ok(data_str) = serde_yaml::to_string(data) {
let yaml_str = if prefix.is_some() {
prefix.unwrap().to_string() + &data_str
} else {
data_str
};
match serde_yaml::to_string(data) {
Ok(data_str) => {
let yaml_str = match prefix {
Some(prefix) => format!("{}{}", prefix, data_str),
None => data_str,
};
if fs::write(path.clone(), yaml_str.as_bytes()).is_err() {
Err(format!("can not save file `{:?}`", path))
} else {
Ok(())
let path_str = path.as_os_str().to_string_lossy().to_string();
match fs::write(path, yaml_str.as_bytes()) {
Ok(_) => Ok(()),
Err(_) => Err(format!("can not save file `{}`", path_str)),
}
}
} else {
Err(String::from("can not convert the data to yaml"))
Err(_) => Err("can not convert the data to yaml".into()),
}
}
/// Get Clash Core Config `config.yaml`
pub fn read_clash() -> Mapping {
read_yaml::<Mapping>(app_home_dir().join("config.yaml"))
read_yaml::<Mapping>(dirs::app_home_dir().join("config.yaml"))
}
/// Save the clash core Config `config.yaml`
pub fn save_clash(config: &Mapping) -> Result<(), String> {
save_yaml(
app_home_dir().join("config.yaml"),
dirs::app_home_dir().join("config.yaml"),
config,
Some("# Default Config For Clash Core\n\n"),
)
}
/// Get infomation of the clash's `external-controller` and `secret`
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()),
_ => None,
},
_ => None,
};
let secret = match config.get(&key_secret) {
Some(value) => match value {
Value::String(val_str) => Some(val_str.clone()),
Value::Bool(val_bool) => Some(val_bool.to_string()),
Value::Number(val_num) => Some(val_num.to_string()),
_ => None,
},
_ => None,
};
ClashController {
port,
server,
secret,
}
}
/// Get the `verge.yaml`
pub fn read_verge() -> VergeConfig {
read_yaml::<VergeConfig>(app_home_dir().join("verge.yaml"))
}
/// Save Verge App Config
pub fn save_verge(verge: &VergeConfig) -> Result<(), String> {
save_yaml(
app_home_dir().join("verge.yaml"),
verge,
Some("# The Config for Clash Verge App\n\n"),
)
}

View File

@@ -1,5 +1,6 @@
extern crate serde_yaml;
use crate::utils::dirs;
use chrono::Local;
use log::LevelFilter;
use log4rs::append::console::ConsoleAppender;
@@ -11,8 +12,6 @@ use std::io::Write;
use std::path::PathBuf;
use tauri::PackageInfo;
use crate::utils::{app_home_dir, app_resources_dir};
/// initialize this instance's log file
fn init_log(log_dir: &PathBuf) {
let local_time = Local::now().format("%Y-%m-%d-%H%M%S").to_string();
@@ -85,11 +84,11 @@ fn init_config_file(app_dir: &PathBuf, res_dir: &PathBuf) {
/// initialize app
pub fn init_app(package_info: &PackageInfo) {
// create app dir
let app_dir = app_home_dir();
let app_dir = dirs::app_home_dir();
let log_dir = app_dir.join("logs");
let profiles_dir = app_dir.join("profiles");
let res_dir = app_resources_dir(package_info);
let res_dir = dirs::app_resources_dir(package_info);
if !app_dir.exists() {
fs::create_dir(&app_dir).unwrap();

View File

@@ -1,8 +1,5 @@
mod dirs;
pub use self::dirs::*;
pub mod clash;
pub mod config;
pub mod dirs;
pub mod fetch;
pub mod init;
pub mod resolve;

View File

@@ -1,5 +1,5 @@
use super::{clash, config, init, server, sysopt};
use crate::{config::ProfilesConfig, events::state};
use super::{init, server};
use crate::{config::ProfilesConfig, states};
use tauri::{App, AppHandle, Manager};
/// handle something when start app
@@ -10,68 +10,31 @@ pub fn resolve_setup(app: &App) {
// init app config
init::init_app(app.package_info());
// run clash sidecar
let info = clash::run_clash_bin(&app.handle());
// init states
let clash_state = app.state::<states::ClashState>();
let verge_state = app.state::<states::VergeState>();
let profiles_state = app.state::<states::ProfilesState>();
// resolve the verge config - enable system proxy
let mut original: Option<sysopt::SysProxyConfig> = None;
let verge = config::read_verge();
let enable = verge.enable_system_proxy.unwrap_or(false);
let mut clash = clash_state.0.lock().unwrap();
let mut verge = verge_state.0.lock().unwrap();
let mut profiles = profiles_state.0.lock().unwrap();
if enable && info.controller.is_some() {
if let Ok(original_conf) = sysopt::get_proxy_config() {
original = Some(original_conf)
};
let ctl = info.controller.clone().unwrap();
if ctl.port.is_some() {
let server = format!("127.0.0.1:{}", ctl.port.unwrap());
let bypass = verge
.system_proxy_bypass
.clone()
.unwrap_or(String::from(sysopt::DEFAULT_BYPASS));
let config = sysopt::SysProxyConfig {
enable,
server,
bypass,
};
if let Err(err) = sysopt::set_proxy_config(&config) {
log::error!("can not set system proxy for `{}`", err);
}
}
if let Err(err) = clash.run_sidecar() {
log::error!("{}", err);
}
// update state
let profiles_state = app.state::<state::ProfilesState>();
let mut profiles = profiles_state.0.lock().unwrap();
*profiles = ProfilesConfig::read_file();
if let Err(err) = profiles.activate(clash.info.clone()) {
log::error!("{}", err);
}
let verge_state = app.state::<state::VergeConfLock>();
let mut verge_arc = verge_state.0.lock().unwrap();
*verge_arc = verge;
let clash_state = app.state::<state::ClashInfoState>();
let mut clash_arc = clash_state.0.lock().unwrap();
*clash_arc = info;
let some_state = app.state::<state::SomthingState>();
let mut some_arc = some_state.0.lock().unwrap();
*some_arc = original;
verge.init_sysproxy(clash.info.port.clone());
}
/// reset system proxy
pub fn resolve_reset(app_handle: &AppHandle) {
let state = app_handle.try_state::<state::SomthingState>();
if state.is_none() {
return;
}
match state.unwrap().0.lock() {
Ok(arc) => {
if arc.is_some() {
if let Err(err) = sysopt::set_proxy_config(arc.as_ref().unwrap()) {
log::error!("failed to reset proxy for `{}`", err);
}
}
}
_ => {}
};
let verge_state = app_handle.state::<states::VergeState>();
let mut verge_arc = verge_state.0.lock().unwrap();
verge_arc.reset_sysproxy();
}

View File

@@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};
use std::io;
static DEFAULT_BYPASS: &str = "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.*;<local>";
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct SysProxyConfig {
pub enable: bool,
@@ -18,16 +20,21 @@ impl Default for SysProxyConfig {
}
}
pub static DEFAULT_BYPASS: &str = "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.*;<local>";
#[cfg(target_os = "windows")]
mod win {
use super::*;
use winreg::enums::*;
use winreg::RegKey;
impl SysProxyConfig {
pub fn new(enable: bool, port: String, bypass: Option<String>) -> Self {
SysProxyConfig {
enable,
server: format!("127.0.0.1:{}", port),
bypass: bypass.unwrap_or(DEFAULT_BYPASS.into()),
}
}
/// Get the windows system proxy config
pub fn get_proxy_config() -> io::Result<SysProxyConfig> {
#[cfg(target_os = "windows")]
pub fn get_sys() -> io::Result<Self> {
use winreg::enums::*;
use winreg::RegKey;
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let cur_var = hkcu.open_subkey_with_flags(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
@@ -41,43 +48,38 @@ mod win {
})
}
#[cfg(target_os = "windows")]
/// Set the windows system proxy config
pub fn set_proxy_config(config: &SysProxyConfig) -> io::Result<()> {
pub fn set_sys(&self) -> io::Result<()> {
use winreg::enums::*;
use winreg::RegKey;
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let cur_var = hkcu.open_subkey_with_flags(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
KEY_SET_VALUE,
)?;
let enable: u32 = if config.enable { 1u32 } else { 0u32 };
let enable: u32 = if self.enable { 1u32 } else { 0u32 };
cur_var.set_value("ProxyEnable", &enable)?;
cur_var.set_value("ProxyServer", &config.server)?;
cur_var.set_value("ProxyOverride", &config.bypass)?;
Ok(())
cur_var.set_value("ProxyServer", &self.server)?;
cur_var.set_value("ProxyOverride", &self.bypass)
}
}
#[cfg(target_os = "macos")]
mod macos {
use super::*;
// #[cfg(target_os = "macos")]
// mod macos {
// use super::*;
pub fn get_proxy_config() -> io::Result<SysProxyConfig> {
Ok(SysProxyConfig {
enable: false,
server: "server".into(),
bypass: "bypass".into(),
})
}
// pub fn get_proxy_config() -> io::Result<SysProxyConfig> {
// Ok(SysProxyConfig {
// enable: false,
// server: "server".into(),
// bypass: "bypass".into(),
// })
// }
pub fn set_proxy_config(config: &SysProxyConfig) -> io::Result<()> {
Ok(())
}
}
#[cfg(target_os = "windows")]
pub use win::*;
#[cfg(target_os = "macos")]
pub use macos::*;
// pub fn set_proxy_config(config: &SysProxyConfig) -> io::Result<()> {
// Ok(())
// }
// }