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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//! Core HTTP client for the UGOS API.
//!
//! [`UgosClient`] wraps a [`reqwest::Client`] with automatic token
//! management and transparent re-authentication on token expiry (code 1024).
use std::future::Future;
use std::sync::Arc;
use serde::de::DeserializeOwned;
use tokio::sync::RwLock;
use crate::auth::{self, Credentials, Session};
use crate::crypto::{RequestKey, md5_hex, query_string, rsa_seal, sha256_hex};
use crate::error::{Result, UgosError};
use crate::types::common::ApiResponse;
/// Client for interacting with a UGOS NAS.
#[derive(Debug, Clone)]
pub struct UgosClient {
http: reqwest::Client,
base_url: String,
creds: Credentials,
session: Arc<RwLock<Session>>,
/// Cached RSA key, fetched on first use by an encrypted request.
public_key: Arc<RwLock<Option<rsa::RsaPublicKey>>>,
/// Serialises re-authentication so one expiry causes one login.
reauth: Arc<tokio::sync::Mutex<()>>,
}
impl UgosClient {
/// Create a new client and authenticate.
///
/// Builds a reqwest client that enforces `tls`, then performs the full
/// login flow. See [`crate::tls::TlsPolicy`] for what the policies mean.
///
/// # Errors
///
/// Returns an error if the HTTP client cannot be built or login fails.
pub async fn connect(
host: &str,
port: u16,
creds: Credentials,
tls: &crate::tls::TlsPolicy,
) -> Result<Self> {
let http = crate::tls::http_client(tls)?;
let base_url = format!("https://{host}:{port}/ugreen");
let session = auth::login(&http, &base_url, &creds).await?;
Ok(Self {
http,
base_url,
creds,
session: Arc::new(RwLock::new(session)),
public_key: Arc::new(RwLock::new(None)),
reauth: Arc::new(tokio::sync::Mutex::new(())),
})
}
/// Create a client from an existing session (e.g. loaded from cache).
///
/// # Errors
///
/// Returns an error if the HTTP client cannot be built.
pub fn from_session(
host: &str,
port: u16,
creds: Credentials,
session: Session,
tls: &crate::tls::TlsPolicy,
) -> Result<Self> {
let http = crate::tls::http_client(tls)?;
let base_url = format!("https://{host}:{port}/ugreen");
Ok(Self {
http,
base_url,
creds,
session: Arc::new(RwLock::new(session)),
public_key: Arc::new(RwLock::new(None)),
reauth: Arc::new(tokio::sync::Mutex::new(())),
})
}
/// The current session token.
pub async fn session(&self) -> Session {
self.session.read().await.clone()
}
/// Build a full URL for an API path.
///
/// A path may name its API version (`v2/filemgr/...`); anything else gets
/// `v1/`, which is what the KVM and Docker apps use.
fn url_for(&self, path: &str) -> String {
if path.starts_with("v1/") || path.starts_with("v2/") {
format!("{}/{path}", self.base_url)
} else {
format!("{}/v1/{path}", self.base_url)
}
}
/// Append `?token=` (or `&token=`) to a URL.
///
/// The token is percent-encoded. It is an opaque server-issued string,
/// and a `&` in it would split the query, a `#` would drop everything
/// after it, a `+` would arrive as a space.
pub(crate) fn append_token(url: &str, token: &str) -> String {
let token = crate::crypto::urlencode_component(token);
if url.contains('?') {
format!("{url}&token={token}")
} else {
format!("{url}?token={token}")
}
}
/// Perform a GET request, deserialize the [`ApiResponse`], and check the status code.
/// Automatically retries once on token expiry (code 1024).
///
/// # Errors
///
/// Returns the appropriate [`UgosError`] on API or network failure.
pub async fn get<T: DeserializeOwned + Send>(&self, path: &str) -> Result<T> {
self.get_with_params(path, &[]).await
}
/// Perform a GET request with query parameters.
///
/// # Errors
///
/// Returns the appropriate [`UgosError`] on API or network failure.
pub async fn get_with_params<T: DeserializeOwned + Send>(
&self,
path: &str,
params: &[(&str, &str)],
) -> Result<T> {
self.retrying(|| self.do_get::<T>(path, params)).await
}
/// Perform a POST request with a JSON body.
///
/// # Errors
///
/// Returns the appropriate [`UgosError`] on API or network failure.
pub async fn post<T: DeserializeOwned + Send, B: serde::Serialize + Sync>(
&self,
path: &str,
body: &B,
) -> Result<T> {
self.retrying(|| self.do_post::<T, B>(path, body)).await
}
/// Perform a PUT request with a JSON body.
///
/// The snapshot app is the only part of UGOS with a REST-shaped API;
/// everything else posts to verb-named endpoints.
///
/// # Errors
///
/// Returns the appropriate [`UgosError`] on API or network failure.
pub async fn put<T: DeserializeOwned + Send, B: serde::Serialize + Sync>(
&self,
path: &str,
body: &B,
) -> Result<T> {
self.retrying(|| self.do_request::<T, B>(reqwest::Method::PUT, path, body))
.await
}
/// Perform a DELETE request that carries a JSON body.
///
/// Unusual, but the snapshot app deletes by posting a list of ids in the
/// body rather than naming one in the path.
///
/// # Errors
///
/// Returns the appropriate [`UgosError`] on API or network failure.
pub async fn delete_with_body<T: DeserializeOwned + Send, B: serde::Serialize + Sync>(
&self,
path: &str,
body: &B,
) -> Result<T> {
self.retrying(|| self.do_request::<T, B>(reqwest::Method::DELETE, path, body))
.await
}
/// Internal request with a JSON body for methods other than POST.
async fn do_request<T: DeserializeOwned + Send, B: serde::Serialize + Sync>(
&self,
method: reqwest::Method,
path: &str,
body: &B,
) -> Result<T> {
let token = self.session.read().await.token.clone();
let url = Self::append_token(&self.url_for(path), &token);
let resp: ApiResponse<serde_json::Value> = self
.http
.request(method, &url)
.json(body)
.send()
.await?
.json()
.await?;
Self::decode(resp)
}
/// Internal GET without retry.
async fn do_get<T: DeserializeOwned + Send>(
&self,
path: &str,
params: &[(&str, &str)],
) -> Result<T> {
let token = self.session.read().await.token.clone();
let url = Self::append_token(&self.url_for(path), &token);
let resp: ApiResponse<serde_json::Value> = self
.http
.get(&url)
.query(params)
.send()
.await?
.json()
.await?;
Self::decode(resp)
}
/// Internal POST without retry.
async fn do_post<T: DeserializeOwned + Send, B: serde::Serialize + Sync>(
&self,
path: &str,
body: &B,
) -> Result<T> {
let token = self.session.read().await.token.clone();
let url = Self::append_token(&self.url_for(path), &token);
let resp: ApiResponse<serde_json::Value> =
self.http.post(&url).json(body).send().await?.json().await?;
Self::decode(resp)
}
/// Perform a POST request with a `multipart/form-data` body.
///
/// Used for chunked uploads, which is the only place UGOS expects a
/// multipart body rather than JSON.
///
/// # Errors
///
/// Returns the appropriate [`UgosError`] on API or network failure.
pub async fn post_multipart<T: DeserializeOwned + Send>(
&self,
path: &str,
form: reqwest::multipart::Form,
) -> Result<T> {
// A multipart::Form cannot be cloned, so no retrying() here: on
// expiry the client logs in once more and the caller sends the form
// again.
let token = self.session.read().await.token.clone();
let url = Self::append_token(&self.url_for(path), &token);
let resp: ApiResponse<serde_json::Value> = self
.http
.post(&url)
.multipart(form)
.send()
.await?
.json()
.await?;
Self::decode(resp)
}
/// GET raw bytes, for file downloads that answer with the file itself.
///
/// # Errors
///
/// Returns the appropriate [`UgosError`] on network failure or a non-2xx
/// status.
pub async fn get_bytes(
&self,
path: &str,
params: &[(&str, &str)],
) -> Result<reqwest::Response> {
self.retrying(|| self.do_get_bytes(path, params)).await
}
/// One attempt at a byte GET.
///
/// UGOS answers a failed download with HTTP 200 and the ordinary
/// `{code, msg}` envelope. Only the status was checked here, so an
/// expired session ended up written into the target file and reported as
/// a successful download. A JSON content type is now read back and
/// decoded; anything that is not an error envelope is handed on
/// unchanged, because a downloaded file may legitimately be JSON.
async fn do_get_bytes(&self, path: &str, params: &[(&str, &str)]) -> Result<reqwest::Response> {
let token = self.session.read().await.token.clone();
let url = Self::append_token(&self.url_for(path), &token);
let resp = self
.http
.get(&url)
.query(params)
.send()
.await?
.error_for_status()?;
let is_json = resp
.headers()
.get(reqwest::header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.is_some_and(|v| v.starts_with("application/json"));
if !is_json {
return Ok(resp);
}
let bytes = resp.bytes().await?;
if let Ok(envelope) = serde_json::from_slice::<ApiResponse<serde_json::Value>>(&bytes)
&& envelope.code != 200
{
// Denselben Weg wie jede andere Antwort: decode() bildet den Code
// auf die passende Variante ab, 1024 also auf LoginExpired, das
// retrying() dann als Wiederholungsgrund erkennt.
return match Self::decode::<serde_json::Value>(envelope) {
Err(e) => Err(e),
Ok(_) => Err(UgosError::Api {
code: 0,
msg: "unexpected envelope in a byte response".into(),
}),
};
}
// No error envelope: the bytes are the content. They were read for the
// check and are handed back as a new response.
Ok(reqwest::Response::from(
http::Response::builder()
.status(reqwest::StatusCode::OK)
.body(bytes)
.map_err(|e| UgosError::Encryption(format!("rebuilding response: {e}")))?,
))
}
/// POST a raw body, which is how the file manager takes file contents.
///
/// The transfer metadata rides in a `ug-param` header rather than in the
/// body, and the token is sealed the way encrypted endpoints expect it.
///
/// # Errors
///
/// Returns the appropriate [`UgosError`] on API, crypto or network failure.
pub async fn post_bytes<T: DeserializeOwned + Send>(
&self,
path: &str,
body: Vec<u8>,
file_name: &str,
ug_param: &serde_json::Value,
) -> Result<T> {
let public_key = self.public_key().await?;
let token = self.session.read().await.token.clone();
let resp: ApiResponse<serde_json::Value> = self
.http
.post(self.url_for(path))
.header(
"Content-Disposition",
format!("attachment; filename=\"{file_name}\""),
)
.header("Content-Type", "application/octet-stream")
.header("ug-param", ug_param.to_string())
.header("X-Ugreen-Security-Key", md5_hex(&token))
.header("X-Ugreen-Token", rsa_seal(&public_key, &token)?)
.body(body)
.send()
.await?
.json()
.await?;
Self::decode(resp)
}
/// Run an operation, and once more if the token expired in between.
///
/// Used to be spelled out four times and was missing from six further
/// methods. Anyone adding a new kind of request now gets the behaviour by
/// passing through here.
async fn retrying<T, F, Fut>(&self, mut op: F) -> Result<T>
where
F: FnMut() -> Fut,
Fut: Future<Output = Result<T>>,
{
match op().await {
Err(UgosError::LoginExpired) => {
self.re_auth().await?;
op().await
}
other => other,
}
}
/// The RSA key used to wrap per-request AES keys.
///
/// It comes from the login response, not from the `verify/check`
/// handshake — those are different keys. A session restored from an older
/// cache has none, in which case a fresh login provides it.
async fn public_key(&self) -> Result<rsa::RsaPublicKey> {
let cached = self.public_key.read().await.clone();
if let Some(key) = cached {
return Ok(key);
}
let mut pem = self.session.read().await.public_key.clone();
if pem.is_empty() {
let session = auth::login(&self.http, &self.base_url, &self.creds).await?;
pem = session.public_key.clone();
*self.session.write().await = session;
}
if pem.is_empty() {
return Err(UgosError::Encryption(
"NAS did not provide an encryption key at login".into(),
));
}
let key = auth::parse_public_key(&pem)?;
*self.public_key.write().await = Some(key.clone());
Ok(key)
}
/// Perform an encrypted GET, for endpoints that reject plain requests.
///
/// # Errors
///
/// Returns the appropriate [`UgosError`] on API, crypto or network failure.
pub async fn get_encrypted<T: DeserializeOwned + Send>(
&self,
path: &str,
params: &[(&str, &str)],
) -> Result<T> {
let public_key = self.public_key().await?;
let key = RequestKey::generate(&public_key)?;
let token = self.session.read().await.token.clone();
let raw: serde_json::Value = self
.http
.get(self.url_for(path))
// The token rides inside the encrypted query, and RSA-sealed in
// the header alongside an MD5 of it.
.query(&[(
"encrypt_query",
key.encrypt(&Self::with_token(params, &token))?,
)])
.header("X-Ugreen-Security-Code", &key.security_code)
.header("X-Ugreen-Security-Key", md5_hex(&token))
.header("X-Ugreen-Token", rsa_seal(&public_key, &token)?)
.send()
.await?
.json()
.await?;
Self::decode_encrypted(raw, &key)
}
/// Perform an encrypted DELETE, which the Download Center uses.
///
/// # Errors
///
/// Returns the appropriate [`UgosError`] on API, crypto or network failure.
pub async fn delete_encrypted<T: DeserializeOwned + Send>(
&self,
path: &str,
params: &[(&str, &str)],
) -> Result<T> {
let public_key = self.public_key().await?;
let key = RequestKey::generate(&public_key)?;
let token = self.session.read().await.token.clone();
let raw: serde_json::Value = self
.http
.delete(self.url_for(path))
.query(&[(
"encrypt_query",
key.encrypt(&Self::with_token(params, &token))?,
)])
.header("X-Ugreen-Security-Code", &key.security_code)
.header("X-Ugreen-Security-Key", md5_hex(&token))
.header("X-Ugreen-Token", rsa_seal(&public_key, &token)?)
.send()
.await?
.json()
.await?;
Self::decode_encrypted(raw, &key)
}
/// Perform an encrypted POST, for endpoints that reject plain requests.
///
/// # Errors
///
/// Returns the appropriate [`UgosError`] on API, crypto or network failure.
pub async fn post_encrypted<T: DeserializeOwned + Send, B: serde::Serialize + Sync>(
&self,
path: &str,
body: &B,
) -> Result<T> {
let public_key = self.public_key().await?;
let key = RequestKey::generate(&public_key)?;
let token = self.session.read().await.token.clone();
let json = serde_json::to_string(body)?;
let payload = serde_json::json!({
"encrypt_req_body": key.encrypt(&json)?,
"req_body_sha256": sha256_hex(&json),
});
let raw: serde_json::Value = self
.http
.post(self.url_for(path))
.query(&[(
"encrypt_query",
key.encrypt(&Self::with_token(&[], &token))?,
)])
.header("X-Ugreen-Security-Code", &key.security_code)
.header("X-Ugreen-Security-Key", md5_hex(&token))
.header("X-Ugreen-Token", rsa_seal(&public_key, &token)?)
.json(&payload)
.send()
.await?
.json()
.await?;
Self::decode_encrypted(raw, &key)
}
/// Render params with the session token appended.
fn with_token(params: &[(&str, &str)], token: &str) -> String {
let mut all: Vec<(&str, &str)> = params.to_vec();
all.push(("token", token));
query_string(&all)
}
/// Unwrap an encrypted response.
///
/// An encrypted answer arrives as a bare `{"encrypt_resp_body": "..."}`
/// without the usual `code`/`msg`/`data` envelope; the envelope is inside.
/// A plain answer keeps the envelope, so both shapes are handled.
fn decode_encrypted<T: DeserializeOwned>(
raw: serde_json::Value,
key: &RequestKey,
) -> Result<T> {
let envelope: ApiResponse<serde_json::Value> =
if let Some(sealed) = raw.get("encrypt_resp_body").and_then(|v| v.as_str()) {
serde_json::from_str(&key.decrypt(sealed)?)?
} else {
serde_json::from_value(raw)?
};
Ok(serde_json::from_value(envelope.into_result()?)?)
}
/// Check the status code first, then deserialize the payload.
///
/// The order matters: on an error code UGOS sends a `data` payload that
/// does not match the success shape, so decoding first would report a
/// deserialization failure and swallow the actual error message.
fn decode<T: DeserializeOwned>(resp: ApiResponse<serde_json::Value>) -> Result<T> {
Ok(serde_json::from_value(resp.into_result()?)?)
}
/// Re-authenticate and update the stored session.
async fn re_auth(&self) -> Result<()> {
// An expiry hits every request in flight at once. Without this lock
// each of them logs in separately, and every login can invalidate the
// previous one's token. Whoever takes the lock logs in; whoever waits
// checks afterwards whether that is still necessary.
let _guard = self.reauth.lock().await;
let token_before = self.session.read().await.token.clone();
tracing::info!("token expired, re-authenticating");
let new_session = auth::login(&self.http, &self.base_url, &self.creds).await?;
{
let current = self.session.read().await;
if current.token != token_before {
// Someone else renewed while this call was waiting.
return Ok(());
}
}
*self.session.write().await = new_session;
// The RSA key belongs to the session. If it stayed, every later
// encrypted request would wrap its AES key with the key of the previous
// login.
*self.public_key.write().await = None;
Ok(())
}
}