mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 05:03:45 +08:00
feat: set dns by service
This commit is contained in:
parent
59b4f1ebab
commit
212518c682
1
src-tauri/Cargo.lock
generated
1
src-tauri/Cargo.lock
generated
@ -4505,6 +4505,7 @@ dependencies = [
|
|||||||
"base64 0.22.1",
|
"base64 0.22.1",
|
||||||
"bytes",
|
"bytes",
|
||||||
"encoding_rs",
|
"encoding_rs",
|
||||||
|
"futures-channel",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"h2 0.4.5",
|
"h2 0.4.5",
|
||||||
|
@ -35,7 +35,7 @@ percent-encoding = "2.3.1"
|
|||||||
window-shadows = { version = "0.2" }
|
window-shadows = { version = "0.2" }
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
reqwest = { version = "0.12", features = ["json", "rustls-tls"] }
|
reqwest = { version = "0.12", features = ["json", "rustls-tls", "blocking"] }
|
||||||
sysproxy = { git="https://github.com/zzzgydi/sysproxy-rs", branch = "main" }
|
sysproxy = { git="https://github.com/zzzgydi/sysproxy-rs", branch = "main" }
|
||||||
tauri = { git="https://github.com/tauri-apps/tauri",branch = "1.x", features = [ "fs-read-file", "fs-exists", "path-all", "protocol-asset", "dialog-open", "notification-all", "icon-png", "icon-ico", "clipboard-all", "global-shortcut-all", "process-all", "shell-all", "system-tray", "updater", "window-all", "devtools"] }
|
tauri = { git="https://github.com/tauri-apps/tauri",branch = "1.x", features = [ "fs-read-file", "fs-exists", "path-all", "protocol-asset", "dialog-open", "notification-all", "icon-png", "icon-ico", "clipboard-all", "global-shortcut-all", "process-all", "shell-all", "system-tray", "updater", "window-all", "devtools"] }
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
|
@ -312,3 +312,39 @@ pub(super) async fn stop_core_by_service() -> Result<()> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// set dns by service
|
||||||
|
pub fn set_dns_by_service() -> Result<()> {
|
||||||
|
let url = format!("{SERVICE_URL}/set_dns");
|
||||||
|
let res = reqwest::blocking::ClientBuilder::new()
|
||||||
|
.no_proxy()
|
||||||
|
.build()?
|
||||||
|
.post(url)
|
||||||
|
.send()?
|
||||||
|
.json::<JsonResponse>()
|
||||||
|
.context("failed to connect to the Clash Verge Service")?;
|
||||||
|
|
||||||
|
if res.code != 0 {
|
||||||
|
bail!(res.msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// unset dns by service
|
||||||
|
pub fn unset_dns_by_service() -> Result<()> {
|
||||||
|
let url = format!("{SERVICE_URL}/unset_dns");
|
||||||
|
let res = reqwest::blocking::ClientBuilder::new()
|
||||||
|
.no_proxy()
|
||||||
|
.build()?
|
||||||
|
.post(url)
|
||||||
|
.send()?
|
||||||
|
.json::<JsonResponse>()
|
||||||
|
.context("failed to connect to the Clash Verge Service")?;
|
||||||
|
|
||||||
|
if res.code != 0 {
|
||||||
|
bail!(res.msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use crate::core::service;
|
||||||
use serde_yaml::{Mapping, Value};
|
use serde_yaml::{Mapping, Value};
|
||||||
|
|
||||||
macro_rules! revise {
|
macro_rules! revise {
|
||||||
@ -34,60 +35,10 @@ pub fn use_tun(mut config: Mapping, enable: bool) -> Mapping {
|
|||||||
revise!(config, "tun", tun_val);
|
revise!(config, "tun", tun_val);
|
||||||
|
|
||||||
if enable {
|
if enable {
|
||||||
#[cfg(target_os = "macos")]
|
let _ = service::set_dns_by_service();
|
||||||
{
|
|
||||||
use crate::utils::dirs;
|
|
||||||
use tauri::api::process::Command;
|
|
||||||
log::info!(target: "app", "try to set system dns");
|
|
||||||
let resource_dir = dirs::app_resources_dir().unwrap();
|
|
||||||
let script = resource_dir.join("set_dns.sh");
|
|
||||||
let script = script.to_string_lossy();
|
|
||||||
match Command::new("bash")
|
|
||||||
.args([script])
|
|
||||||
.current_dir(resource_dir)
|
|
||||||
.status()
|
|
||||||
{
|
|
||||||
Ok(status) => {
|
|
||||||
if status.success() {
|
|
||||||
log::info!(target: "app", "set system dns successfully");
|
|
||||||
} else {
|
|
||||||
let code = status.code().unwrap_or(-1);
|
|
||||||
log::error!(target: "app", "set system dns failed: {code}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
log::error!(target: "app", "set system dns failed: {err}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
use_dns_for_tun(config)
|
use_dns_for_tun(config)
|
||||||
} else {
|
} else {
|
||||||
#[cfg(target_os = "macos")]
|
let _ = service::unset_dns_by_service();
|
||||||
{
|
|
||||||
use crate::utils::dirs;
|
|
||||||
use tauri::api::process::Command;
|
|
||||||
log::info!(target: "app", "try to unset system dns");
|
|
||||||
let resource_dir = dirs::app_resources_dir().unwrap();
|
|
||||||
let script = resource_dir.join("unset_dns.sh");
|
|
||||||
let script = script.to_string_lossy();
|
|
||||||
match Command::new("bash")
|
|
||||||
.args([script])
|
|
||||||
.current_dir(resource_dir)
|
|
||||||
.status()
|
|
||||||
{
|
|
||||||
Ok(status) => {
|
|
||||||
if status.success() {
|
|
||||||
log::info!(target: "app", "unset system dns successfully");
|
|
||||||
} else {
|
|
||||||
let code = status.code().unwrap_or(-1);
|
|
||||||
log::error!(target: "app", "unset system dns failed: {code}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
log::error!(target: "app", "unset system dns failed: {err}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
config
|
config
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user