2021-12-04 09:31:26 +03:00
|
|
|
#![cfg_attr(
|
|
|
|
all(not(debug_assertions), target_os = "windows"),
|
|
|
|
windows_subsystem = "windows"
|
|
|
|
)]
|
|
|
|
|
2021-12-17 21:01:02 +03:00
|
|
|
mod cmds;
|
2022-01-07 20:27:25 +03:00
|
|
|
mod core;
|
2021-12-14 11:07:15 +03:00
|
|
|
mod utils;
|
2021-12-04 09:31:26 +03:00
|
|
|
|
2022-03-19 20:36:43 +03:00
|
|
|
use crate::{
|
2022-04-20 06:17:54 +03:00
|
|
|
core::Verge,
|
2022-04-13 20:29:02 +03:00
|
|
|
utils::{resolve, server},
|
2022-03-19 20:36:43 +03:00
|
|
|
};
|
2021-12-08 18:40:52 +03:00
|
|
|
use tauri::{
|
2022-02-26 20:47:56 +03:00
|
|
|
api, CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem,
|
2021-12-08 18:40:52 +03:00
|
|
|
};
|
2021-12-04 09:31:26 +03:00
|
|
|
|
|
|
|
fn main() -> std::io::Result<()> {
|
2021-12-26 21:29:28 +03:00
|
|
|
if server::check_singleton().is_err() {
|
2021-12-22 20:35:17 +03:00
|
|
|
println!("app exists");
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-04-12 18:09:32 +03:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
unsafe {
|
2022-04-13 20:29:02 +03:00
|
|
|
use crate::utils::dirs;
|
|
|
|
|
2022-04-12 18:09:32 +03:00
|
|
|
dirs::init_portable_flag();
|
|
|
|
}
|
|
|
|
|
2022-02-26 20:29:57 +03:00
|
|
|
let tray_menu = SystemTrayMenu::new()
|
2022-01-07 21:54:37 +03:00
|
|
|
.add_item(CustomMenuItem::new("open_window", "Show"))
|
2022-03-19 20:36:43 +03:00
|
|
|
.add_item(CustomMenuItem::new("system_proxy", "System Proxy"))
|
2022-04-12 20:09:51 +03:00
|
|
|
.add_item(CustomMenuItem::new("tun_mode", "Tun Mode"))
|
2022-01-07 21:54:37 +03:00
|
|
|
.add_item(CustomMenuItem::new("restart_clash", "Restart Clash"))
|
2021-12-08 18:40:52 +03:00
|
|
|
.add_native_item(SystemTrayMenuItem::Separator)
|
2022-01-07 21:54:37 +03:00
|
|
|
.add_item(CustomMenuItem::new("quit", "Quit").accelerator("CmdOrControl+Q"));
|
2021-12-04 09:31:26 +03:00
|
|
|
|
2022-02-26 20:47:56 +03:00
|
|
|
#[allow(unused_mut)]
|
|
|
|
let mut builder = tauri::Builder::default()
|
2022-04-18 20:41:20 +03:00
|
|
|
.manage(core::Core::new())
|
2021-12-26 21:29:28 +03:00
|
|
|
.setup(|app| Ok(resolve::resolve_setup(app)))
|
2022-02-26 20:29:57 +03:00
|
|
|
.system_tray(SystemTray::new().with_menu(tray_menu))
|
2021-12-26 21:29:28 +03:00
|
|
|
.on_system_tray_event(move |app_handle, event| match event {
|
2021-12-04 09:31:26 +03:00
|
|
|
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
|
2021-12-08 18:40:52 +03:00
|
|
|
"open_window" => {
|
2021-12-26 21:29:28 +03:00
|
|
|
let window = app_handle.get_window("main").unwrap();
|
2022-01-15 22:09:36 +03:00
|
|
|
window.unminimize().unwrap();
|
2021-12-06 05:31:17 +03:00
|
|
|
window.show().unwrap();
|
|
|
|
window.set_focus().unwrap();
|
|
|
|
}
|
2022-03-19 20:36:43 +03:00
|
|
|
"system_proxy" => {
|
2022-04-18 20:41:20 +03:00
|
|
|
let core = app_handle.state::<core::Core>();
|
2022-03-19 20:36:43 +03:00
|
|
|
|
2022-04-19 20:44:47 +03:00
|
|
|
let new_value = {
|
|
|
|
let verge = core.verge.lock();
|
2022-04-20 06:17:54 +03:00
|
|
|
!verge.enable_system_proxy.clone().unwrap_or(false)
|
2022-04-19 20:44:47 +03:00
|
|
|
};
|
2022-03-19 20:36:43 +03:00
|
|
|
|
2022-04-20 06:17:54 +03:00
|
|
|
let patch = Verge {
|
2022-03-19 20:36:43 +03:00
|
|
|
enable_system_proxy: Some(new_value),
|
2022-04-20 06:17:54 +03:00
|
|
|
..Verge::default()
|
2022-04-19 20:44:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
crate::log_if_err!(core.patch_verge(patch, app_handle));
|
2022-04-12 20:09:51 +03:00
|
|
|
}
|
|
|
|
"tun_mode" => {
|
2022-04-18 20:41:20 +03:00
|
|
|
let core = app_handle.state::<core::Core>();
|
2022-03-19 20:36:43 +03:00
|
|
|
|
2022-04-19 20:44:47 +03:00
|
|
|
let new_value = {
|
|
|
|
let verge = core.verge.lock();
|
2022-04-20 06:17:54 +03:00
|
|
|
!verge.enable_tun_mode.clone().unwrap_or(false)
|
2022-04-19 20:44:47 +03:00
|
|
|
};
|
2022-04-12 20:09:51 +03:00
|
|
|
|
2022-04-20 06:17:54 +03:00
|
|
|
let patch = Verge {
|
2022-04-12 20:09:51 +03:00
|
|
|
enable_tun_mode: Some(new_value),
|
2022-04-20 06:17:54 +03:00
|
|
|
..Verge::default()
|
2022-04-19 20:44:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
crate::log_if_err!(core.patch_verge(patch, app_handle));
|
2022-03-19 20:36:43 +03:00
|
|
|
}
|
2021-12-31 13:24:50 +03:00
|
|
|
"restart_clash" => {
|
2022-04-18 20:41:20 +03:00
|
|
|
let core = app_handle.state::<core::Core>();
|
|
|
|
crate::log_if_err!(core.restart_clash());
|
2021-12-31 13:24:50 +03:00
|
|
|
}
|
2021-12-08 18:40:52 +03:00
|
|
|
"quit" => {
|
2021-12-26 21:29:28 +03:00
|
|
|
resolve::resolve_reset(app_handle);
|
2022-04-25 15:00:11 +03:00
|
|
|
app_handle.exit(0);
|
2021-12-04 09:31:26 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
2022-03-19 20:36:43 +03:00
|
|
|
#[cfg(target_os = "windows")]
|
2021-12-08 18:40:52 +03:00
|
|
|
SystemTrayEvent::LeftClick { .. } => {
|
2022-03-19 20:36:43 +03:00
|
|
|
let window = app_handle.get_window("main").unwrap();
|
|
|
|
window.unminimize().unwrap();
|
|
|
|
window.show().unwrap();
|
|
|
|
window.set_focus().unwrap();
|
2021-12-08 18:40:52 +03:00
|
|
|
}
|
2021-12-04 09:31:26 +03:00
|
|
|
_ => {}
|
|
|
|
})
|
2021-12-08 18:40:52 +03:00
|
|
|
.invoke_handler(tauri::generate_handler![
|
2022-01-08 17:23:48 +03:00
|
|
|
// common
|
2022-01-07 18:29:20 +03:00
|
|
|
cmds::restart_sidecar,
|
|
|
|
cmds::get_sys_proxy,
|
|
|
|
cmds::get_cur_proxy,
|
2022-04-19 20:44:47 +03:00
|
|
|
cmds::kill_sidecar,
|
2022-02-15 22:21:34 +03:00
|
|
|
cmds::open_app_dir,
|
|
|
|
cmds::open_logs_dir,
|
2022-01-08 17:23:48 +03:00
|
|
|
// clash
|
2022-01-07 18:29:20 +03:00
|
|
|
cmds::get_clash_info,
|
|
|
|
cmds::patch_clash_config,
|
2022-01-08 17:23:48 +03:00
|
|
|
// verge
|
2022-01-07 18:29:20 +03:00
|
|
|
cmds::get_verge_config,
|
|
|
|
cmds::patch_verge_config,
|
2022-01-08 17:23:48 +03:00
|
|
|
// profile
|
2022-01-19 18:58:34 +03:00
|
|
|
cmds::view_profile,
|
2022-01-16 21:16:17 +03:00
|
|
|
cmds::patch_profile,
|
2022-03-05 14:04:20 +03:00
|
|
|
cmds::create_profile,
|
2022-01-07 18:29:20 +03:00
|
|
|
cmds::import_profile,
|
|
|
|
cmds::update_profile,
|
|
|
|
cmds::delete_profile,
|
|
|
|
cmds::select_profile,
|
|
|
|
cmds::get_profiles,
|
2022-03-06 09:59:25 +03:00
|
|
|
cmds::enhance_profiles,
|
2022-03-26 19:58:17 +03:00
|
|
|
cmds::change_profile_chain,
|
2022-04-06 20:20:44 +03:00
|
|
|
cmds::change_profile_valid,
|
2022-03-26 19:58:17 +03:00
|
|
|
cmds::read_profile_file,
|
2022-04-24 16:00:17 +03:00
|
|
|
cmds::save_profile_file,
|
|
|
|
// service mode
|
|
|
|
cmds::service::start_service,
|
2022-04-25 11:12:04 +03:00
|
|
|
cmds::service::stop_service,
|
2022-04-24 16:00:17 +03:00
|
|
|
cmds::service::check_service,
|
|
|
|
cmds::service::install_service,
|
|
|
|
cmds::service::uninstall_service,
|
2022-02-26 20:47:56 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
{
|
|
|
|
use tauri::{Menu, MenuItem, Submenu};
|
|
|
|
|
|
|
|
let submenu_file = Submenu::new(
|
|
|
|
"File",
|
|
|
|
Menu::new()
|
|
|
|
.add_native_item(MenuItem::Undo)
|
|
|
|
.add_native_item(MenuItem::Redo)
|
|
|
|
.add_native_item(MenuItem::Copy)
|
|
|
|
.add_native_item(MenuItem::Paste)
|
|
|
|
.add_native_item(MenuItem::Cut)
|
|
|
|
.add_native_item(MenuItem::SelectAll),
|
|
|
|
);
|
|
|
|
builder = builder.menu(Menu::new().add_submenu(submenu_file));
|
|
|
|
}
|
|
|
|
|
|
|
|
builder
|
2021-12-04 09:31:26 +03:00
|
|
|
.build(tauri::generate_context!())
|
2021-12-25 21:31:18 +03:00
|
|
|
.expect("error while running tauri application")
|
|
|
|
.run(|app_handle, e| match e {
|
2022-03-29 18:05:32 +03:00
|
|
|
tauri::RunEvent::WindowEvent { label, event, .. } => match event {
|
|
|
|
tauri::WindowEvent::CloseRequested { api, .. } => {
|
|
|
|
let app_handle = app_handle.clone();
|
|
|
|
api.prevent_close();
|
|
|
|
app_handle.get_window(&label).unwrap().hide().unwrap();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
2022-02-13 13:45:03 +03:00
|
|
|
tauri::RunEvent::ExitRequested { .. } => {
|
2021-12-26 21:29:28 +03:00
|
|
|
resolve::resolve_reset(app_handle);
|
2021-12-25 21:31:18 +03:00
|
|
|
api::process::kill_children();
|
|
|
|
}
|
2022-04-13 20:29:33 +03:00
|
|
|
tauri::RunEvent::Exit => {
|
|
|
|
resolve::resolve_reset(app_handle);
|
|
|
|
api::process::kill_children();
|
|
|
|
}
|
2021-12-25 21:31:18 +03:00
|
|
|
_ => {}
|
|
|
|
});
|
2021-12-04 09:31:26 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|