-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontroller.rs
More file actions
357 lines (308 loc) · 10.7 KB
/
controller.rs
File metadata and controls
357 lines (308 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
use eyre::Context;
use http_body_util::{BodyExt, Full};
use hyper::Request;
use hyper::body::Bytes;
use hyper_util::client::legacy::Client;
use hyper_util::rt::TokioExecutor;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use urlencoding::encode;
#[cfg(unix)]
use hyperlocal::{UnixConnector, Uri as UnixUri};
use crate::EyreError;
#[derive(Debug, Clone, Copy, Serialize, Deserialize, uniffi::Enum)]
#[serde(rename_all = "lowercase")]
pub enum Mode {
Rule,
Global,
Direct,
}
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct Proxy {
/// Proxy name
pub name: String,
/// Proxy type (e.g., Selector, URLTest, Fallback, Direct, Reject, etc.)
#[serde(rename = "type")]
pub proxy_type: String,
/// All proxy node names contained in the proxy group (only for proxy groups)
#[serde(default)]
pub all: Vec<String>,
/// Currently selected proxy node name (only for proxy groups)
pub now: Option<String>,
/// Delay test history records
#[serde(default)]
pub history: Vec<DelayHistory>,
}
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct DelayHistory {
pub time: String,
pub delay: i32,
}
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct DelayResponse {
pub delay: i32,
}
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct MemoryResponse {
pub inuse: i64,
pub oslimit: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct Connection {
pub id: String,
pub metadata: Metadata,
pub upload: i64,
pub download: i64,
pub start: String,
pub chains: Vec<String>,
pub rule: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct Metadata {
pub network: String,
#[serde(rename = "type")]
pub metadata_type: String,
#[serde(rename = "sourceIP")]
pub source_ip: String,
#[serde(rename = "destinationIP")]
pub destination_ip: String,
#[serde(rename = "destinationPort")]
pub destination_port: u16,
pub host: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct ConnectionsResponse {
#[serde(rename = "downloadTotal")]
pub download_total: i64,
#[serde(rename = "uploadTotal")]
pub upload_total: i64,
pub connections: Vec<Connection>,
}
#[derive(Debug, Clone, Serialize, Deserialize, uniffi::Record)]
pub struct ConfigResponse {
#[serde(rename = "external-controller")]
pub external_controller: Option<String>,
pub secret: Option<String>,
pub mode: Option<Mode>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct ProxiesResponse {
pub proxies: HashMap<String, Proxy>,
}
/// Clash HTTP API client using Unix domain socket
#[derive(uniffi::Object)]
pub struct ClashController {
socket_path: String,
}
#[uniffi::export(async_runtime = "tokio")]
impl ClashController {
/// Create a new HTTP client that connects via Unix domain socket
#[uniffi::constructor]
pub fn new(socket_path: String) -> Arc<Self> {
Arc::new(Self { socket_path })
}
/// Get all proxies
pub async fn get_proxies(&self) -> Result<Vec<Proxy>, EyreError> {
let mode = self.get_mode().await?.unwrap_or(Mode::Rule);
// If in direct mode, return a single DIRECT proxy
if matches!(mode, Mode::Direct) {
return Ok(vec![Proxy {
name: "DIRECT".to_string(),
proxy_type: "Direct".to_string(),
all: Vec::new(),
now: None,
history: Vec::new(),
}]);
}
let mut response: ProxiesResponse = self.request("GET", "/proxies", None).await?;
// Get the order from GLOBAL proxy group's 'all' field
if let Some(global_group) = response.proxies.remove("GLOBAL") {
let mut sorted_proxies = Vec::new();
// First add proxies in GLOBAL's 'all' order
for name in &global_group.all {
if let Some(proxy) = response.proxies.get(name) {
sorted_proxies.push(proxy.clone());
}
}
// Then add any remaining proxies not in GLOBAL's 'all'
for (name, proxy) in &response.proxies {
if !global_group.all.contains(name) {
sorted_proxies.push(proxy.clone());
}
}
// Add GLOBAL group at the front when GLOBAL mode is active
if matches!(mode, Mode::Global) {
sorted_proxies.insert(0, global_group);
}
Ok(sorted_proxies)
} else {
// If no GLOBAL group, return proxies as a vec
Ok(response.proxies.values().cloned().collect())
}
}
/// Select a proxy for a group
pub async fn select_proxy(
&self,
group_name: String,
proxy_name: String,
) -> Result<(), EyreError> {
let body = serde_json::json!(
{
"name": proxy_name
}
);
let path = format!("/proxies/{}", encode(&group_name));
self.request_no_response("PUT", &path, Some(serde_json::to_vec(&body)?))
.await
}
/// Get proxy delay
pub async fn get_proxy_delay(
&self,
name: String,
url: Option<String>,
timeout: Option<i32>,
) -> Result<DelayResponse, EyreError> {
let test_url = url.unwrap_or_else(|| "http://www.gstatic.com/generate_204".to_string());
let timeout_ms = timeout.unwrap_or(5000);
let path = format!(
"/proxies/{}/delay?url={}&timeout={}",
encode(&name),
encode(&test_url),
timeout_ms
);
self.request("GET", &path, None).await
}
/// Get memory statistics
pub async fn get_memory(&self) -> Result<MemoryResponse, EyreError> {
#[cfg(feature = "jemallocator")]
{
use jemalloc_ctl::{epoch, stats};
// Advance the epoch to update statistics
epoch::advance()
.map_err(|e| eyre::eyre!("Failed to advance jemalloc epoch: {}", e))?;
// Read allocated memory in bytes
let allocated = stats::allocated::read()
.map_err(|e| eyre::eyre!("Failed to read allocated memory: {}", e))?;
// Read resident memory in bytes
let resident = stats::resident::read()
.map_err(|e| eyre::eyre!("Failed to read resident memory: {}", e))?;
Ok(MemoryResponse {
inuse: allocated as i64,
oslimit: resident as i64,
})
}
#[cfg(not(feature = "jemallocator"))]
{
self.request("GET", "/memory", None).await
}
}
/// Get active connections
pub async fn get_connections(&self) -> Result<ConnectionsResponse, EyreError> {
self.request("GET", "/connections", None).await
}
/// Get current configuration
pub async fn get_configs(&self) -> Result<ConfigResponse, EyreError> {
self.request("GET", "/configs", None).await
}
/// Update configuration
pub async fn update_config(&self, config: HashMap<String, String>) -> Result<(), EyreError> {
let body_bytes = serde_json::to_vec(&config).wrap_err("Failed to serialize config")?;
self.request_no_response("PATCH", "/configs", Some(body_bytes))
.await
}
/// Set proxy mode (rule, global, direct)
pub async fn set_mode(&self, mode: Mode) -> Result<(), EyreError> {
let mode_str = match mode {
Mode::Rule => "rule",
Mode::Global => "global",
Mode::Direct => "direct",
};
let mut config = HashMap::new();
config.insert("mode".to_string(), mode_str.to_string());
self.update_config(config).await
}
/// Get current proxy mode
pub async fn get_mode(&self) -> Result<Option<Mode>, EyreError> {
let config = self.get_configs().await?;
Ok(config.mode)
}
}
impl ClashController {
async fn request_no_response(
&self,
method: &str,
path: &str,
body: Option<Vec<u8>>,
) -> Result<(), EyreError> {
let client = Client::builder(TokioExecutor::new()).build(UnixConnector);
let uri: hyper::Uri = UnixUri::new(&self.socket_path, path).into();
let request_builder = Request::builder()
.uri(uri)
.method(method)
.header("Content-Type", "application/json");
let request = if let Some(body_data) = body {
request_builder
.body(Full::new(Bytes::from(body_data)))
.wrap_err("Failed to build request with body")?
} else {
request_builder
.body(Full::new(Bytes::new()))
.wrap_err("Failed to build request")?
};
let response = client
.request(request)
.await
.wrap_err("HTTP request failed")?;
if !response.status().is_success() {
return Err(eyre::eyre!("HTTP status error: {}", response.status()).into());
}
Ok(())
}
async fn request<T>(
&self,
method: &str,
path: &str,
body: Option<Vec<u8>>,
) -> Result<T, EyreError>
where
T: serde::de::DeserializeOwned,
{
let uri: hyper::Uri = UnixUri::new(&self.socket_path, path).into();
let client = Client::builder(TokioExecutor::new()).build(UnixConnector);
let request_builder = Request::builder()
.uri(uri)
.method(method)
.header("Content-Type", "application/json");
let request = if let Some(body_data) = body {
request_builder
.body(Full::new(Bytes::from(body_data)))
.wrap_err("Failed to build request with body")?
} else {
request_builder
.body(Full::new(Bytes::new()))
.wrap_err("Failed to build request")?
};
let response = client
.request(request)
.await
.wrap_err("HTTP request failed")?;
if !response.status().is_success() {
return Err(eyre::eyre!("HTTP status error: {}", response.status()).into());
}
let body_bytes = response
.into_body()
.collect()
.await
.wrap_err("Failed to read response body")?
.to_bytes();
serde_json::from_slice(&body_bytes)
.wrap_err_with(|| {
format!(
"Failed to parse JSON response: {}",
String::from_utf8_lossy(&body_bytes)
)
})
.map_err(Into::into)
}
}