mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
chore: init project
This commit is contained in:
3
src-tauri/src/build.rs
Normal file
3
src-tauri/src/build.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
tauri_build::build()
|
||||
}
|
||||
69
src-tauri/src/clash.rs
Normal file
69
src-tauri/src/clash.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
extern crate reqwest;
|
||||
extern crate serde_yaml;
|
||||
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tauri::api::path::home_dir;
|
||||
use tauri::api::process::{Command, CommandEvent};
|
||||
|
||||
/// Get the clash config dir
|
||||
pub fn get_config_dir() -> PathBuf {
|
||||
home_dir()
|
||||
.unwrap()
|
||||
.join(Path::new(".config"))
|
||||
.join(Path::new("clash-verge"))
|
||||
}
|
||||
|
||||
/// Initialize the default config dir for clash
|
||||
pub fn init_clash_config() {
|
||||
let config_dir = get_config_dir();
|
||||
let conifg_yaml = config_dir.join("config.yaml");
|
||||
|
||||
let default_yaml =
|
||||
"mixed-port: 7890\nallow-lan: false\nexternal-controller: 127.0.0.1:9090\nsecret: ''\n";
|
||||
let mut yaml_obj = serde_yaml::from_str::<serde_yaml::Value>(&default_yaml).unwrap();
|
||||
|
||||
if !config_dir.exists() {
|
||||
let config_dir = config_dir.clone();
|
||||
fs::create_dir(config_dir).unwrap();
|
||||
let mut file = fs::File::create(conifg_yaml).unwrap();
|
||||
file.write(default_yaml.as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
let yaml_path = &config_dir.join("config.yaml");
|
||||
let yaml_str = fs::read_to_string(yaml_path).unwrap();
|
||||
yaml_obj = serde_yaml::from_str::<serde_yaml::Value>(&yaml_str).unwrap();
|
||||
|
||||
println!("{:?}", yaml_obj);
|
||||
}
|
||||
|
||||
/// Run the clash bin
|
||||
pub fn run_clash_bin(config_dirs: &str) {
|
||||
let (mut rx, mut _child) = Command::new_sidecar("clash")
|
||||
.expect("failed to create clash binary")
|
||||
.args(["-d", config_dirs])
|
||||
.spawn()
|
||||
.expect("failed to spawn sidecar");
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
// read events such as stdout
|
||||
while let Some(event) = rx.recv().await {
|
||||
if let CommandEvent::Stdout(line) = event {
|
||||
println!("{:?}", line);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub async fn fetch_url(profile_url: &str) -> Result<(), reqwest::Error> {
|
||||
let resp = reqwest::get(profile_url).await?;
|
||||
println!("{:#?}", resp);
|
||||
|
||||
let header = resp.headers().clone();
|
||||
println!("{:?}", header);
|
||||
|
||||
let data = resp.text_with_charset("utf-8").await?;
|
||||
println!("{:#?}", data);
|
||||
Ok(())
|
||||
}
|
||||
4
src-tauri/src/cmd.rs
Normal file
4
src-tauri/src/cmd.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
use tauri::api::process::CommandChild;
|
||||
|
||||
#[tauri::command]
|
||||
fn set_clash_port(process: Option<CommandChild>, port: i32) {}
|
||||
62
src-tauri/src/main.rs
Normal file
62
src-tauri/src/main.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
#![cfg_attr(
|
||||
all(not(debug_assertions), target_os = "windows"),
|
||||
windows_subsystem = "windows"
|
||||
)]
|
||||
|
||||
extern crate tauri;
|
||||
|
||||
mod clash;
|
||||
mod sysopt;
|
||||
|
||||
use tauri::{CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu};
|
||||
|
||||
#[tauri::command]
|
||||
async fn get_config_data(url: String) -> Result<String, String> {
|
||||
match clash::fetch_url(&url).await {
|
||||
Ok(_) => Ok(String::from("success")),
|
||||
Err(_) => Err(String::from("error")),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
let config = sysopt::get_proxy_config()?;
|
||||
println!("{:?}", config);
|
||||
|
||||
let app = tauri::Builder::default()
|
||||
.system_tray(
|
||||
SystemTray::new()
|
||||
.with_menu(SystemTrayMenu::new().add_item(CustomMenuItem::new("tray_event_quit", "Quit"))),
|
||||
)
|
||||
.on_system_tray_event(move |app, event| match event {
|
||||
SystemTrayEvent::LeftClick { .. } => {
|
||||
let window = app.get_window("main").unwrap();
|
||||
window.show().unwrap();
|
||||
window.set_focus().unwrap();
|
||||
}
|
||||
|
||||
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
|
||||
"tray_event_quit" => {
|
||||
app.exit(0);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![get_config_data])
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
app.run(|app_handle, e| match e {
|
||||
tauri::Event::CloseRequested { label, api, .. } => {
|
||||
let app_handle = app_handle.clone();
|
||||
api.prevent_close();
|
||||
app_handle.get_window(&label).unwrap().hide().unwrap();
|
||||
}
|
||||
tauri::Event::ExitRequested { api, .. } => {
|
||||
api.prevent_exit();
|
||||
}
|
||||
_ => {}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
43
src-tauri/src/sysopt.rs
Normal file
43
src-tauri/src/sysopt.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io;
|
||||
use winreg::enums::*;
|
||||
use winreg::RegKey;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct ProxyConfig {
|
||||
enable: u32,
|
||||
server: String,
|
||||
bypass: String,
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
/// Get the windows system proxy config
|
||||
pub fn get_proxy_config() -> io::Result<ProxyConfig> {
|
||||
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
|
||||
let cur_var = hkcu.open_subkey_with_flags(
|
||||
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
|
||||
KEY_READ,
|
||||
)?;
|
||||
|
||||
Ok(ProxyConfig {
|
||||
enable: cur_var.get_value("ProxyEnable")?,
|
||||
server: cur_var.get_value("ProxyServer")?,
|
||||
bypass: cur_var.get_value("ProxyOverride")?,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
/// Set the windows system proxy config
|
||||
pub fn set_proxy_config(config: &ProxyConfig) -> io::Result<()> {
|
||||
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
|
||||
let cur_var = hkcu.open_subkey_with_flags(
|
||||
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
|
||||
KEY_SET_VALUE,
|
||||
)?;
|
||||
|
||||
cur_var.set_value("ProxyEnable", &config.enable)?;
|
||||
cur_var.set_value("ProxyServer", &config.server)?;
|
||||
cur_var.set_value("ProxyOverride", &config.bypass)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user