clash.rs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. use super::{PrfEnhancedResult, Profiles, Verge};
  2. use crate::utils::{config, dirs, help};
  3. use anyhow::{bail, Result};
  4. use reqwest::header::HeaderMap;
  5. use serde::{Deserialize, Serialize};
  6. use serde_yaml::{Mapping, Value};
  7. use std::{collections::HashMap, time::Duration};
  8. use tauri::api::process::{Command, CommandChild, CommandEvent};
  9. use tokio::time::sleep;
  10. #[derive(Default, Debug, Clone, Deserialize, Serialize)]
  11. pub struct ClashInfo {
  12. /// clash sidecar status
  13. pub status: String,
  14. /// clash core port
  15. pub port: Option<String>,
  16. /// same as `external-controller`
  17. pub server: Option<String>,
  18. /// clash secret
  19. pub secret: Option<String>,
  20. }
  21. #[derive(Debug)]
  22. pub struct Clash {
  23. /// maintain the clash config
  24. pub config: Mapping,
  25. /// some info
  26. pub info: ClashInfo,
  27. /// clash sidecar
  28. pub sidecar: Option<CommandChild>,
  29. }
  30. impl Clash {
  31. pub fn new() -> Clash {
  32. let config = Clash::read_config();
  33. let info = Clash::get_info(&config);
  34. Clash {
  35. config,
  36. info,
  37. sidecar: None,
  38. }
  39. }
  40. /// get clash config
  41. fn read_config() -> Mapping {
  42. config::read_yaml::<Mapping>(dirs::clash_path())
  43. }
  44. /// save the clash config
  45. fn save_config(&self) -> Result<()> {
  46. config::save_yaml(
  47. dirs::clash_path(),
  48. &self.config,
  49. Some("# Default Config For Clash Core\n\n"),
  50. )
  51. }
  52. /// parse the clash's config.yaml
  53. /// get some information
  54. fn get_info(clash_config: &Mapping) -> ClashInfo {
  55. let key_port_1 = Value::String("port".to_string());
  56. let key_port_2 = Value::String("mixed-port".to_string());
  57. let key_server = Value::String("external-controller".to_string());
  58. let key_secret = Value::String("secret".to_string());
  59. let port = match clash_config.get(&key_port_1) {
  60. Some(value) => match value {
  61. Value::String(val_str) => Some(val_str.clone()),
  62. Value::Number(val_num) => Some(val_num.to_string()),
  63. _ => None,
  64. },
  65. _ => None,
  66. };
  67. let port = match port {
  68. Some(_) => port,
  69. None => match clash_config.get(&key_port_2) {
  70. Some(value) => match value {
  71. Value::String(val_str) => Some(val_str.clone()),
  72. Value::Number(val_num) => Some(val_num.to_string()),
  73. _ => None,
  74. },
  75. _ => None,
  76. },
  77. };
  78. let server = match clash_config.get(&key_server) {
  79. Some(value) => match value {
  80. Value::String(val_str) => Some(val_str.clone()),
  81. _ => None,
  82. },
  83. _ => None,
  84. };
  85. let secret = match clash_config.get(&key_secret) {
  86. Some(value) => match value {
  87. Value::String(val_str) => Some(val_str.clone()),
  88. Value::Bool(val_bool) => Some(val_bool.to_string()),
  89. Value::Number(val_num) => Some(val_num.to_string()),
  90. _ => None,
  91. },
  92. _ => None,
  93. };
  94. ClashInfo {
  95. status: "init".into(),
  96. port,
  97. server,
  98. secret,
  99. }
  100. }
  101. /// run clash sidecar
  102. pub fn run_sidecar(&mut self) -> Result<()> {
  103. let app_dir = dirs::app_home_dir();
  104. let app_dir = app_dir.as_os_str().to_str().unwrap();
  105. match Command::new_sidecar("clash") {
  106. Ok(cmd) => match cmd.args(["-d", app_dir]).spawn() {
  107. Ok((mut rx, cmd_child)) => {
  108. self.sidecar = Some(cmd_child);
  109. // clash log
  110. tauri::async_runtime::spawn(async move {
  111. while let Some(event) = rx.recv().await {
  112. match event {
  113. CommandEvent::Stdout(line) => log::info!("[clash]: {}", line),
  114. CommandEvent::Stderr(err) => log::error!("[clash]: {}", err),
  115. _ => {}
  116. }
  117. }
  118. });
  119. Ok(())
  120. }
  121. Err(err) => bail!(err.to_string()),
  122. },
  123. Err(err) => bail!(err.to_string()),
  124. }
  125. }
  126. /// drop clash sidecar
  127. pub fn drop_sidecar(&mut self) -> Result<()> {
  128. if let Some(sidecar) = self.sidecar.take() {
  129. sidecar.kill()?;
  130. }
  131. Ok(())
  132. }
  133. /// restart clash sidecar
  134. /// should reactivate profile after restart
  135. pub fn restart_sidecar(&mut self, profiles: &mut Profiles) -> Result<()> {
  136. self.update_config();
  137. self.drop_sidecar()?;
  138. self.run_sidecar()?;
  139. self.activate(profiles)
  140. }
  141. /// update the clash info
  142. pub fn update_config(&mut self) {
  143. self.config = Clash::read_config();
  144. self.info = Clash::get_info(&self.config);
  145. }
  146. /// patch update the clash config
  147. pub fn patch_config(
  148. &mut self,
  149. patch: Mapping,
  150. verge: &mut Verge,
  151. profiles: &mut Profiles,
  152. ) -> Result<()> {
  153. for (key, value) in patch.iter() {
  154. let value = value.clone();
  155. let key_str = key.as_str().clone().unwrap_or("");
  156. // restart the clash
  157. if key_str == "mixed-port" {
  158. self.restart_sidecar(profiles)?;
  159. let port = if value.is_number() {
  160. match value.as_i64().clone() {
  161. Some(num) => Some(format!("{num}")),
  162. None => None,
  163. }
  164. } else {
  165. match value.as_str().clone() {
  166. Some(num) => Some(num.into()),
  167. None => None,
  168. }
  169. };
  170. verge.init_sysproxy(port);
  171. }
  172. self.config.insert(key.clone(), value);
  173. }
  174. self.save_config()
  175. }
  176. /// enable tun mode
  177. /// only revise the config and restart the
  178. pub fn tun_mode(&mut self, enable: bool) -> Result<()> {
  179. let tun_key = Value::String("tun".into());
  180. let tun_val = self.config.get(&tun_key);
  181. let mut new_val = Mapping::new();
  182. if tun_val.is_some() && tun_val.as_ref().unwrap().is_mapping() {
  183. new_val = tun_val.as_ref().unwrap().as_mapping().unwrap().clone();
  184. }
  185. macro_rules! revise {
  186. ($map: expr, $key: expr, $val: expr) => {
  187. let ret_key = Value::String($key.into());
  188. if $map.contains_key(&ret_key) {
  189. $map[&ret_key] = $val;
  190. } else {
  191. $map.insert(ret_key, $val);
  192. }
  193. };
  194. }
  195. macro_rules! append {
  196. ($map: expr, $key: expr, $val: expr) => {
  197. let ret_key = Value::String($key.into());
  198. if !$map.contains_key(&ret_key) {
  199. $map.insert(ret_key, $val);
  200. }
  201. };
  202. }
  203. revise!(new_val, "enable", Value::from(enable));
  204. append!(new_val, "stack", Value::from("gvisor"));
  205. append!(new_val, "auto-route", Value::from(true));
  206. append!(new_val, "auto-detect-interface", Value::from(true));
  207. revise!(self.config, "tun", Value::from(new_val));
  208. self.save_config()
  209. }
  210. fn _activate(info: ClashInfo, config: Mapping) -> Result<()> {
  211. let temp_path = dirs::profiles_temp_path();
  212. config::save_yaml(temp_path.clone(), &config, Some("# Clash Verge Temp File"))?;
  213. tauri::async_runtime::spawn(async move {
  214. let server = info.server.clone().unwrap();
  215. let server = format!("http://{server}/configs");
  216. let mut headers = HeaderMap::new();
  217. headers.insert("Content-Type", "application/json".parse().unwrap());
  218. if let Some(secret) = info.secret.as_ref() {
  219. let secret = format!("Bearer {}", secret.clone()).parse().unwrap();
  220. headers.insert("Authorization", secret);
  221. }
  222. let mut data = HashMap::new();
  223. data.insert("path", temp_path.as_os_str().to_str().unwrap());
  224. // retry 5 times
  225. for _ in 0..5 {
  226. match reqwest::ClientBuilder::new().no_proxy().build() {
  227. Ok(client) => match client
  228. .put(&server)
  229. .headers(headers.clone())
  230. .json(&data)
  231. .send()
  232. .await
  233. {
  234. Ok(resp) => {
  235. if resp.status() != 204 {
  236. log::error!("failed to activate clash for status \"{}\"", resp.status());
  237. }
  238. // do not retry
  239. break;
  240. }
  241. Err(err) => log::error!("failed to activate for `{err}`"),
  242. },
  243. Err(err) => log::error!("failed to activate for `{err}`"),
  244. }
  245. sleep(Duration::from_millis(500)).await;
  246. }
  247. });
  248. Ok(())
  249. }
  250. /// activate the profile
  251. pub fn activate(&self, profiles: &Profiles) -> Result<()> {
  252. let info = self.info.clone();
  253. let mut config = self.config.clone();
  254. let gen_map = profiles.gen_activate()?;
  255. for (key, value) in gen_map.into_iter() {
  256. config.insert(key, value);
  257. }
  258. Self::_activate(info, config)
  259. }
  260. /// enhanced profiles mode
  261. pub fn activate_enhanced(
  262. &self,
  263. profiles: &Profiles,
  264. win: tauri::Window,
  265. delay: bool,
  266. ) -> Result<()> {
  267. let event_name = help::get_uid("e");
  268. let event_name = format!("enhanced-cb-{event_name}");
  269. let info = self.info.clone();
  270. let mut config = self.config.clone();
  271. // generate the payload
  272. let payload = profiles.gen_enhanced(event_name.clone())?;
  273. win.once(&event_name, move |event| {
  274. if let Some(result) = event.payload() {
  275. let result: PrfEnhancedResult = serde_json::from_str(result).unwrap();
  276. if let Some(data) = result.data {
  277. for (key, value) in data.into_iter() {
  278. config.insert(key, value);
  279. }
  280. Self::_activate(info, config).unwrap();
  281. }
  282. log::info!("profile enhanced status {}", result.status);
  283. result.error.map(|error| log::error!("{error}"));
  284. }
  285. });
  286. tauri::async_runtime::spawn(async move {
  287. // wait the window setup during resolve app
  288. if delay {
  289. sleep(Duration::from_secs(2)).await;
  290. }
  291. win.emit("script-handler", payload).unwrap();
  292. });
  293. Ok(())
  294. }
  295. }
  296. impl Default for Clash {
  297. fn default() -> Self {
  298. Clash::new()
  299. }
  300. }
  301. impl Drop for Clash {
  302. fn drop(&mut self) {
  303. if let Err(err) = self.drop_sidecar() {
  304. log::error!("{err}");
  305. }
  306. }
  307. }