clash_api.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. use crate::config::Config;
  2. use anyhow::{bail, Result};
  3. use reqwest::header::HeaderMap;
  4. use serde::{Deserialize, Serialize};
  5. use serde_yaml::Mapping;
  6. use std::collections::HashMap;
  7. /// PUT /configs
  8. /// path 是绝对路径
  9. pub async fn put_configs(path: &str) -> Result<()> {
  10. let (url, headers) = clash_client_info()?;
  11. let url = format!("{url}/configs");
  12. let mut data = HashMap::new();
  13. data.insert("path", path);
  14. let client = reqwest::ClientBuilder::new().no_proxy().build()?;
  15. let builder = client.put(&url).headers(headers).json(&data);
  16. let response = builder.send().await?;
  17. match response.status().as_u16() {
  18. 204 => Ok(()),
  19. status @ _ => {
  20. bail!("failed to put configs with status \"{status}\"")
  21. }
  22. }
  23. }
  24. /// PATCH /configs
  25. pub async fn patch_configs(config: &Mapping) -> Result<()> {
  26. let (url, headers) = clash_client_info()?;
  27. let url = format!("{url}/configs");
  28. let client = reqwest::ClientBuilder::new().no_proxy().build()?;
  29. let builder = client.patch(&url).headers(headers.clone()).json(config);
  30. builder.send().await?;
  31. Ok(())
  32. }
  33. #[derive(Default, Debug, Clone, Deserialize, Serialize)]
  34. pub struct DelayRes {
  35. delay: u64,
  36. }
  37. /// GET /proxies/{name}/delay
  38. /// 获取代理延迟
  39. pub async fn get_proxy_delay(name: String, test_url: Option<String>) -> Result<DelayRes> {
  40. let (url, headers) = clash_client_info()?;
  41. let url = format!("{url}/proxies/{name}/delay");
  42. let default_url = "http://1.1.1.1";
  43. let test_url = test_url
  44. .map(|s| if s.is_empty() { default_url.into() } else { s })
  45. .unwrap_or(default_url.into());
  46. let client = reqwest::ClientBuilder::new().no_proxy().build()?;
  47. let builder = client
  48. .get(&url)
  49. .headers(headers)
  50. .query(&[("timeout", "10000"), ("url", &test_url)]);
  51. let response = builder.send().await?;
  52. Ok(response.json::<DelayRes>().await?)
  53. }
  54. /// 根据clash info获取clash服务地址和请求头
  55. fn clash_client_info() -> Result<(String, HeaderMap)> {
  56. let client = { Config::clash().data().get_client_info() };
  57. let server = format!("http://{}", client.server);
  58. let mut headers = HeaderMap::new();
  59. headers.insert("Content-Type", "application/json".parse()?);
  60. if let Some(secret) = client.secret {
  61. let secret = format!("Bearer {}", secret).parse()?;
  62. headers.insert("Authorization", secret);
  63. }
  64. Ok((server, headers))
  65. }
  66. /// 缩短clash的日志
  67. pub fn parse_log(log: String) -> String {
  68. if log.starts_with("time=") && log.len() > 33 {
  69. return (&log[33..]).to_owned();
  70. }
  71. if log.len() > 9 {
  72. return (&log[9..]).to_owned();
  73. }
  74. return log;
  75. }
  76. /// 缩短clash -t的错误输出
  77. /// 仅适配 clash p核 8-26、clash meta 1.13.1
  78. pub fn parse_check_output(log: String) -> String {
  79. let t = log.find("time=");
  80. let m = log.find("msg=");
  81. let mr = log.rfind('"');
  82. if let (Some(_), Some(m), Some(mr)) = (t, m, mr) {
  83. let e = match log.find("level=error msg=") {
  84. Some(e) => e + 17,
  85. None => m + 5,
  86. };
  87. if mr > m {
  88. return (&log[e..mr]).to_owned();
  89. }
  90. }
  91. let l = log.find("error=");
  92. let r = log.find("path=").or(Some(log.len()));
  93. if let (Some(l), Some(r)) = (l, r) {
  94. return (&log[(l + 6)..(r - 1)]).to_owned();
  95. }
  96. log
  97. }
  98. #[test]
  99. fn test_parse_check_output() {
  100. let str1 = r#"xxxx\n time="2022-11-18T20:42:58+08:00" level=error msg="proxy 0: 'alpn' expected type 'string', got unconvertible type '[]interface {}'""#;
  101. let str2 = r#"20:43:49 ERR [Config] configuration file test failed error=proxy 0: unsupport proxy type: hysteria path=xxx"#;
  102. let str3 = r#"
  103. "time="2022-11-18T21:38:01+08:00" level=info msg="Start initial configuration in progress"
  104. time="2022-11-18T21:38:01+08:00" level=error msg="proxy 0: 'alpn' expected type 'string', got unconvertible type '[]interface {}'"
  105. configuration file xxx\n
  106. "#;
  107. let res1 = parse_check_output(str1.into());
  108. let res2 = parse_check_output(str2.into());
  109. let res3 = parse_check_output(str3.into());
  110. println!("res1: {res1}");
  111. println!("res2: {res2}");
  112. println!("res3: {res3}");
  113. assert_eq!(res1, res3);
  114. }