]> git.proxmox.com Git - proxmox-backup.git/blob - src/client/http_client.rs
fa3680ab62598f0bff0284d86bc7c575fb4ace8f
[proxmox-backup.git] / src / client / http_client.rs
1 use failure::*;
2
3 use http::Uri;
4 use hyper::Body;
5 use hyper::client::Client;
6 use xdg::BaseDirectories;
7 use chrono::{DateTime, Utc};
8 use std::collections::HashSet;
9 use std::sync::{Arc, Mutex};
10 use std::io::Write;
11
12 use http::{Request, Response};
13 use http::header::HeaderValue;
14
15 use futures::*;
16 use futures::stream::Stream;
17 use std::sync::atomic::{AtomicUsize, Ordering};
18 use tokio::sync::mpsc;
19 use openssl::ssl::{SslConnector, SslMethod};
20
21 use serde_json::{json, Value};
22 use url::percent_encoding::{percent_encode, DEFAULT_ENCODE_SET};
23
24 use crate::tools::{self, BroadcastFuture, tty};
25 use crate::tools::futures::{cancellable, Canceller};
26 use super::pipe_to_stream::*;
27 use super::merge_known_chunks::*;
28
29 use crate::backup::*;
30
31 #[derive(Clone)]
32 struct AuthInfo {
33 username: String,
34 ticket: String,
35 token: String,
36 }
37
38 /// HTTP(S) API client
39 pub struct HttpClient {
40 client: Client<hyper_openssl::HttpsConnector<hyper::client::HttpConnector>>,
41 server: String,
42 auth: BroadcastFuture<AuthInfo>,
43 }
44
45 fn store_ticket_info(server: &str, username: &str, ticket: &str, token: &str) -> Result<(), Error> {
46
47 let base = BaseDirectories::with_prefix("proxmox-backup")?;
48
49 // usually /run/user/<uid>/...
50 let path = base.place_runtime_file("tickets")?;
51
52 let mode = nix::sys::stat::Mode::from_bits_truncate(0o0600);
53
54 let mut data = tools::file_get_json(&path, Some(json!({})))?;
55
56 let now = Utc::now().timestamp();
57
58 data[server][username] = json!({ "timestamp": now, "ticket": ticket, "token": token});
59
60 let mut new_data = json!({});
61
62 let ticket_lifetime = tools::ticket::TICKET_LIFETIME - 60;
63
64 let empty = serde_json::map::Map::new();
65 for (server, info) in data.as_object().unwrap_or(&empty) {
66 for (_user, uinfo) in info.as_object().unwrap_or(&empty) {
67 if let Some(timestamp) = uinfo["timestamp"].as_i64() {
68 let age = now - timestamp;
69 if age < ticket_lifetime {
70 new_data[server][username] = uinfo.clone();
71 }
72 }
73 }
74 }
75
76 tools::file_set_contents(path, new_data.to_string().as_bytes(), Some(mode))?;
77
78 Ok(())
79 }
80
81 fn load_ticket_info(server: &str, username: &str) -> Option<(String, String)> {
82 let base = match BaseDirectories::with_prefix("proxmox-backup") {
83 Ok(b) => b,
84 _ => return None,
85 };
86
87 // usually /run/user/<uid>/...
88 let path = match base.place_runtime_file("tickets") {
89 Ok(p) => p,
90 _ => return None,
91 };
92
93 let data = match tools::file_get_json(&path, None) {
94 Ok(v) => v,
95 _ => return None,
96 };
97
98 let now = Utc::now().timestamp();
99
100 let ticket_lifetime = tools::ticket::TICKET_LIFETIME - 60;
101
102 if let Some(uinfo) = data[server][username].as_object() {
103 if let Some(timestamp) = uinfo["timestamp"].as_i64() {
104 let age = now - timestamp;
105 if age < ticket_lifetime {
106 let ticket = match uinfo["ticket"].as_str() {
107 Some(t) => t,
108 None => return None,
109 };
110 let token = match uinfo["token"].as_str() {
111 Some(t) => t,
112 None => return None,
113 };
114 return Some((ticket.to_owned(), token.to_owned()));
115 }
116 }
117 }
118
119 None
120 }
121
122 impl HttpClient {
123
124 pub fn new(server: &str, username: &str) -> Result<Self, Error> {
125 let client = Self::build_client();
126
127 let password = if let Some((ticket, _token)) = load_ticket_info(server, username) {
128 ticket
129 } else {
130 Self::get_password(&username)?
131 };
132
133 let login = Self::credentials(client.clone(), server.to_owned(), username.to_owned(), password);
134
135 Ok(Self {
136 client,
137 server: String::from(server),
138 auth: BroadcastFuture::new(login),
139 })
140 }
141
142 fn get_password(_username: &str) -> Result<String, Error> {
143 use std::env::VarError::*;
144 match std::env::var("PBS_PASSWORD") {
145 Ok(p) => return Ok(p),
146 Err(NotUnicode(_)) => bail!("PBS_PASSWORD contains bad characters"),
147 Err(NotPresent) => {
148 // Try another method
149 }
150 }
151
152 // If we're on a TTY, query the user for a password
153 if tty::stdin_isatty() {
154 return Ok(String::from_utf8(tty::read_password("Password: ")?)?);
155 }
156
157 bail!("no password input mechanism available");
158 }
159
160 fn build_client() -> Client<hyper_openssl::HttpsConnector<hyper::client::HttpConnector>> {
161
162 let mut ssl_connector_builder = SslConnector::builder(SslMethod::tls()).unwrap();
163
164 ssl_connector_builder.set_verify(openssl::ssl::SslVerifyMode::NONE); // fixme!
165
166 let mut httpc = hyper::client::HttpConnector::new(1);
167 httpc.set_nodelay(true); // important for h2 download performance!
168 httpc.set_recv_buffer_size(Some(1024*1024)); //important for h2 download performance!
169 httpc.enforce_http(false); // we want https...
170
171 let https = hyper_openssl::HttpsConnector::with_connector(httpc, ssl_connector_builder).unwrap();
172
173 Client::builder()
174 //.http2_initial_stream_window_size( (1 << 31) - 2)
175 //.http2_initial_connection_window_size( (1 << 31) - 2)
176 .build::<_, Body>(https)
177 }
178
179 pub fn request(&self, mut req: Request<Body>) -> impl Future<Item=Value, Error=Error> {
180
181 let login = self.auth.listen();
182
183 let client = self.client.clone();
184
185 login.and_then(move |auth| {
186
187 let enc_ticket = format!("PBSAuthCookie={}", percent_encode(auth.ticket.as_bytes(), DEFAULT_ENCODE_SET));
188 req.headers_mut().insert("Cookie", HeaderValue::from_str(&enc_ticket).unwrap());
189 req.headers_mut().insert("CSRFPreventionToken", HeaderValue::from_str(&auth.token).unwrap());
190
191 let request = Self::api_request(client, req);
192
193 request
194 })
195 }
196
197 pub fn get(&self, path: &str, data: Option<Value>) -> impl Future<Item=Value, Error=Error> {
198
199 let req = Self::request_builder(&self.server, "GET", path, data).unwrap();
200 self.request(req)
201 }
202
203 pub fn delete(&mut self, path: &str, data: Option<Value>) -> impl Future<Item=Value, Error=Error> {
204
205 let req = Self::request_builder(&self.server, "DELETE", path, data).unwrap();
206 self.request(req)
207 }
208
209 pub fn post(&mut self, path: &str, data: Option<Value>) -> impl Future<Item=Value, Error=Error> {
210
211 let req = Self::request_builder(&self.server, "POST", path, data).unwrap();
212 self.request(req)
213 }
214
215 pub fn download<W: Write>(&mut self, path: &str, output: W) -> impl Future<Item=W, Error=Error> {
216
217 let mut req = Self::request_builder(&self.server, "GET", path, None).unwrap();
218
219 let login = self.auth.listen();
220
221 let client = self.client.clone();
222
223 login.and_then(move |auth| {
224
225 let enc_ticket = format!("PBSAuthCookie={}", percent_encode(auth.ticket.as_bytes(), DEFAULT_ENCODE_SET));
226 req.headers_mut().insert("Cookie", HeaderValue::from_str(&enc_ticket).unwrap());
227
228 client.request(req)
229 .map_err(Error::from)
230 .and_then(|resp| {
231 let status = resp.status();
232 if !status.is_success() {
233 future::Either::A(
234 HttpClient::api_response(resp)
235 .and_then(|_| { bail!("unknown error"); })
236 )
237 } else {
238 future::Either::B(
239 resp.into_body()
240 .map_err(Error::from)
241 .fold(output, move |mut acc, chunk| {
242 acc.write_all(&chunk)?;
243 Ok::<_, Error>(acc)
244 })
245 )
246 }
247 })
248 })
249 }
250
251 pub fn upload(
252 &mut self,
253 content_type: &str,
254 body: Body,
255 path: &str,
256 data: Option<Value>,
257 ) -> impl Future<Item=Value, Error=Error> {
258
259 let path = path.trim_matches('/');
260 let mut url = format!("https://{}:8007/{}", &self.server, path);
261
262 if let Some(data) = data {
263 let query = tools::json_object_to_query(data).unwrap();
264 url.push('?');
265 url.push_str(&query);
266 }
267
268 let url: Uri = url.parse().unwrap();
269
270 let req = Request::builder()
271 .method("POST")
272 .uri(url)
273 .header("User-Agent", "proxmox-backup-client/1.0")
274 .header("Content-Type", content_type)
275 .body(body).unwrap();
276
277 self.request(req)
278 }
279
280 pub fn start_backup(
281 &self,
282 datastore: &str,
283 backup_type: &str,
284 backup_id: &str,
285 backup_time: DateTime<Utc>,
286 debug: bool,
287 ) -> impl Future<Item=Arc<BackupClient>, Error=Error> {
288
289 let param = json!({
290 "backup-type": backup_type,
291 "backup-id": backup_id,
292 "backup-time": backup_time.timestamp(),
293 "store": datastore,
294 "debug": debug
295 });
296
297 let req = Self::request_builder(&self.server, "GET", "/api2/json/backup", Some(param)).unwrap();
298
299 self.start_h2_connection(req, String::from(PROXMOX_BACKUP_PROTOCOL_ID_V1!()))
300 .map(|(h2, canceller)| BackupClient::new(h2, canceller))
301 }
302
303 pub fn start_backup_reader(
304 &self,
305 datastore: &str,
306 backup_type: &str,
307 backup_id: &str,
308 backup_time: DateTime<Utc>,
309 debug: bool,
310 ) -> impl Future<Item=Arc<BackupReader>, Error=Error> {
311
312 let param = json!({
313 "backup-type": backup_type,
314 "backup-id": backup_id,
315 "backup-time": backup_time.timestamp(),
316 "store": datastore,
317 "debug": debug,
318 });
319 let req = Self::request_builder(&self.server, "GET", "/api2/json/reader", Some(param)).unwrap();
320
321 self.start_h2_connection(req, String::from(PROXMOX_BACKUP_READER_PROTOCOL_ID_V1!()))
322 .map(|(h2, canceller)| BackupReader::new(h2, canceller))
323 }
324
325 pub fn start_h2_connection(
326 &self,
327 mut req: Request<Body>,
328 protocol_name: String,
329 ) -> impl Future<Item=(H2Client, Canceller), Error=Error> {
330
331 let login = self.auth.listen();
332 let client = self.client.clone();
333
334 login.and_then(move |auth| {
335
336 let enc_ticket = format!("PBSAuthCookie={}", percent_encode(auth.ticket.as_bytes(), DEFAULT_ENCODE_SET));
337 req.headers_mut().insert("Cookie", HeaderValue::from_str(&enc_ticket).unwrap());
338 req.headers_mut().insert("UPGRADE", HeaderValue::from_str(&protocol_name).unwrap());
339
340 client.request(req)
341 .map_err(Error::from)
342 .and_then(|resp| {
343
344 let status = resp.status();
345 if status != http::StatusCode::SWITCHING_PROTOCOLS {
346 future::Either::A(Self::api_response(resp).and_then(|_| { bail!("unknown error"); }))
347 } else {
348 future::Either::B(resp.into_body().on_upgrade().map_err(Error::from))
349 }
350 })
351 .and_then(|upgraded| {
352 let max_window_size = (1 << 31) - 2;
353
354 h2::client::Builder::new()
355 .initial_connection_window_size(max_window_size)
356 .initial_window_size(max_window_size)
357 .max_frame_size(4*1024*1024)
358 .handshake(upgraded)
359 .map_err(Error::from)
360 })
361 .and_then(|(h2, connection)| {
362 let connection = connection
363 .map_err(|_| panic!("HTTP/2.0 connection failed"));
364
365 let (connection, canceller) = cancellable(connection)?;
366 // A cancellable future returns an Option which is None when cancelled and
367 // Some when it finished instead, since we don't care about the return type we
368 // need to map it away:
369 let connection = connection.map(|_| ());
370
371 // Spawn a new task to drive the connection state
372 hyper::rt::spawn(connection);
373
374 // Wait until the `SendRequest` handle has available capacity.
375 Ok(h2.ready()
376 .map(move |c| (H2Client::new(c), canceller))
377 .map_err(Error::from))
378 })
379 .flatten()
380 })
381 }
382
383 fn credentials(
384 client: Client<hyper_openssl::HttpsConnector<hyper::client::HttpConnector>>,
385 server: String,
386 username: String,
387 password: String,
388 ) -> Box<dyn Future<Item=AuthInfo, Error=Error> + Send> {
389
390 let server2 = server.clone();
391
392 let create_request = futures::future::lazy(move || {
393 let data = json!({ "username": username, "password": password });
394 let req = Self::request_builder(&server, "POST", "/api2/json/access/ticket", Some(data)).unwrap();
395 Self::api_request(client, req)
396 });
397
398 let login_future = create_request
399 .and_then(move |cred| {
400 let auth = AuthInfo {
401 username: cred["data"]["username"].as_str().unwrap().to_owned(),
402 ticket: cred["data"]["ticket"].as_str().unwrap().to_owned(),
403 token: cred["data"]["CSRFPreventionToken"].as_str().unwrap().to_owned(),
404 };
405
406 let _ = store_ticket_info(&server2, &auth.username, &auth.ticket, &auth.token);
407
408 Ok(auth)
409 });
410
411 Box::new(login_future)
412 }
413
414 fn api_response(response: Response<Body>) -> impl Future<Item=Value, Error=Error> {
415
416 let status = response.status();
417
418 response
419 .into_body()
420 .concat2()
421 .map_err(Error::from)
422 .and_then(move |data| {
423
424 let text = String::from_utf8(data.to_vec()).unwrap();
425 if status.is_success() {
426 if text.len() > 0 {
427 let value: Value = serde_json::from_str(&text)?;
428 Ok(value)
429 } else {
430 Ok(Value::Null)
431 }
432 } else {
433 bail!("HTTP Error {}: {}", status, text);
434 }
435 })
436 }
437
438 fn api_request(
439 client: Client<hyper_openssl::HttpsConnector<hyper::client::HttpConnector>>,
440 req: Request<Body>
441 ) -> impl Future<Item=Value, Error=Error> {
442
443 client.request(req)
444 .map_err(Error::from)
445 .and_then(Self::api_response)
446 }
447
448 pub fn request_builder(server: &str, method: &str, path: &str, data: Option<Value>) -> Result<Request<Body>, Error> {
449 let path = path.trim_matches('/');
450 let url: Uri = format!("https://{}:8007/{}", server, path).parse()?;
451
452 if let Some(data) = data {
453 if method == "POST" {
454 let request = Request::builder()
455 .method(method)
456 .uri(url)
457 .header("User-Agent", "proxmox-backup-client/1.0")
458 .header(hyper::header::CONTENT_TYPE, "application/json")
459 .body(Body::from(data.to_string()))?;
460 return Ok(request);
461 } else {
462 let query = tools::json_object_to_query(data)?;
463 let url: Uri = format!("https://{}:8007/{}?{}", server, path, query).parse()?;
464 let request = Request::builder()
465 .method(method)
466 .uri(url)
467 .header("User-Agent", "proxmox-backup-client/1.0")
468 .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
469 .body(Body::empty())?;
470 return Ok(request);
471 }
472 }
473
474 let request = Request::builder()
475 .method(method)
476 .uri(url)
477 .header("User-Agent", "proxmox-backup-client/1.0")
478 .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
479 .body(Body::empty())?;
480
481 Ok(request)
482 }
483 }
484
485
486 pub struct BackupReader {
487 h2: H2Client,
488 canceller: Canceller,
489 }
490
491 impl Drop for BackupReader {
492
493 fn drop(&mut self) {
494 self.canceller.cancel();
495 }
496 }
497
498 impl BackupReader {
499
500 pub fn new(h2: H2Client, canceller: Canceller) -> Arc<Self> {
501 Arc::new(Self { h2, canceller: canceller })
502 }
503
504 pub fn get(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
505 self.h2.get(path, param)
506 }
507
508 pub fn put(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
509 self.h2.put(path, param)
510 }
511
512 pub fn post(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
513 self.h2.post(path, param)
514 }
515
516 pub fn download<W: Write>(
517 &self,
518 file_name: &str,
519 output: W,
520 ) -> impl Future<Item=W, Error=Error> {
521 let path = "download";
522 let param = json!({ "file-name": file_name });
523 self.h2.download(path, Some(param), output)
524 }
525
526 pub fn speedtest<W: Write>(
527 &self,
528 output: W,
529 ) -> impl Future<Item=W, Error=Error> {
530 self.h2.download("speedtest", None, output)
531 }
532
533 pub fn download_chunk<W: Write>(
534 &self,
535 digest: &[u8; 32],
536 output: W,
537 ) -> impl Future<Item=W, Error=Error> {
538 let path = "chunk";
539 let param = json!({ "digest": proxmox::tools::digest_to_hex(digest) });
540 self.h2.download(path, Some(param), output)
541 }
542
543 pub fn force_close(self) {
544 self.canceller.cancel();
545 }
546 }
547
548 pub struct BackupClient {
549 h2: H2Client,
550 canceller: Canceller,
551 }
552
553 impl Drop for BackupClient {
554
555 fn drop(&mut self) {
556 self.canceller.cancel();
557 }
558 }
559
560 pub struct BackupStats {
561 pub size: u64,
562 }
563
564 impl BackupClient {
565
566 pub fn new(h2: H2Client, canceller: Canceller) -> Arc<Self> {
567 Arc::new(Self { h2, canceller })
568 }
569
570 pub fn get(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
571 self.h2.get(path, param)
572 }
573
574 pub fn put(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
575 self.h2.put(path, param)
576 }
577
578 pub fn post(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
579 self.h2.post(path, param)
580 }
581
582 pub fn finish(self: Arc<Self>) -> impl Future<Item=(), Error=Error> {
583 self.h2.clone()
584 .post("finish", None)
585 .map(move |_| {
586 self.canceller.cancel();
587 })
588 }
589
590 pub fn force_close(self) {
591 self.canceller.cancel();
592 }
593
594 pub fn upload_blob_from_data(
595 &self,
596 data: Vec<u8>,
597 file_name: &str,
598 crypt_config: Option<Arc<CryptConfig>>,
599 compress: bool,
600 sign_only: bool,
601 ) -> impl Future<Item=BackupStats, Error=Error> {
602
603 let h2 = self.h2.clone();
604 let file_name = file_name.to_owned();
605 let size = data.len() as u64;
606
607 futures::future::ok(())
608 .and_then(move |_| {
609 let blob = if let Some(ref crypt_config) = crypt_config {
610 if sign_only {
611 DataBlob::create_signed(&data, crypt_config, compress)?
612 } else {
613 DataBlob::encode(&data, Some(crypt_config), compress)?
614 }
615 } else {
616 DataBlob::encode(&data, None, compress)?
617 };
618
619 let raw_data = blob.into_inner();
620 Ok(raw_data)
621 })
622 .and_then(move |raw_data| {
623 let param = json!({"encoded-size": raw_data.len(), "file-name": file_name });
624 h2.upload("blob", Some(param), raw_data)
625 .map(move |_| {
626 BackupStats { size: size }
627 })
628 })
629 }
630
631 pub fn upload_blob_from_file<P: AsRef<std::path::Path>>(
632 &self,
633 src_path: P,
634 file_name: &str,
635 crypt_config: Option<Arc<CryptConfig>>,
636 compress: bool,
637 ) -> impl Future<Item=BackupStats, Error=Error> {
638
639 let h2 = self.h2.clone();
640 let file_name = file_name.to_owned();
641 let src_path = src_path.as_ref().to_owned();
642
643 let task = tokio::fs::File::open(src_path.clone())
644 .map_err(move |err| format_err!("unable to open file {:?} - {}", src_path, err))
645 .and_then(move |file| {
646 let contents = vec![];
647 tokio::io::read_to_end(file, contents)
648 .map_err(Error::from)
649 .and_then(move |(_, contents)| {
650 let blob = if let Some(ref crypt_config) = crypt_config {
651 DataBlob::encode(&contents, Some(crypt_config), compress)?
652 } else {
653 DataBlob::encode(&contents, None, compress)?
654 };
655 let raw_data = blob.into_inner();
656 Ok((raw_data, contents.len()))
657 })
658 .and_then(move |(raw_data, size)| {
659 let param = json!({"encoded-size": raw_data.len(), "file-name": file_name });
660 h2.upload("blob", Some(param), raw_data)
661 .map(move |_| {
662 BackupStats { size: size as u64 }
663 })
664 })
665 });
666
667 task
668 }
669
670 pub fn upload_stream(
671 &self,
672 archive_name: &str,
673 stream: impl Stream<Item=bytes::BytesMut, Error=Error>,
674 prefix: &str,
675 fixed_size: Option<u64>,
676 crypt_config: Option<Arc<CryptConfig>>,
677 ) -> impl Future<Item=BackupStats, Error=Error> {
678
679 let known_chunks = Arc::new(Mutex::new(HashSet::new()));
680
681 let h2 = self.h2.clone();
682 let h2_2 = self.h2.clone();
683 let h2_3 = self.h2.clone();
684 let h2_4 = self.h2.clone();
685
686 let mut param = json!({ "archive-name": archive_name });
687 if let Some(size) = fixed_size {
688 param["size"] = size.into();
689 }
690
691 let index_path = format!("{}_index", prefix);
692 let close_path = format!("{}_close", prefix);
693
694 let prefix = prefix.to_owned();
695
696 Self::download_chunk_list(h2, &index_path, archive_name, known_chunks.clone())
697 .and_then(move |_| {
698 h2_2.post(&index_path, Some(param))
699 })
700 .and_then(move |res| {
701 let wid = res.as_u64().unwrap();
702 Self::upload_chunk_info_stream(h2_3, wid, stream, &prefix, known_chunks.clone(), crypt_config)
703 .and_then(move |(chunk_count, size, _speed)| {
704 let param = json!({
705 "wid": wid ,
706 "chunk-count": chunk_count,
707 "size": size,
708 });
709 h2_4.post(&close_path, Some(param))
710 .map(move |_| {
711 BackupStats { size: size as u64 }
712 })
713 })
714 })
715 }
716
717 fn response_queue() -> (
718 mpsc::Sender<h2::client::ResponseFuture>,
719 sync::oneshot::Receiver<Result<(), Error>>
720 ) {
721 let (verify_queue_tx, verify_queue_rx) = mpsc::channel(100);
722 let (verify_result_tx, verify_result_rx) = sync::oneshot::channel();
723
724 hyper::rt::spawn(
725 verify_queue_rx
726 .map_err(Error::from)
727 .for_each(|response: h2::client::ResponseFuture| {
728 response
729 .map_err(Error::from)
730 .and_then(H2Client::h2api_response)
731 .and_then(|result| {
732 println!("RESPONSE: {:?}", result);
733 Ok(())
734 })
735 .map_err(|err| format_err!("pipelined request failed: {}", err))
736 })
737 .then(|result|
738 verify_result_tx.send(result)
739 )
740 .map_err(|_| { /* ignore closed channel */ })
741 );
742
743 (verify_queue_tx, verify_result_rx)
744 }
745
746 fn append_chunk_queue(h2: H2Client, wid: u64, path: String) -> (
747 mpsc::Sender<(MergedChunkInfo, Option<h2::client::ResponseFuture>)>,
748 sync::oneshot::Receiver<Result<(), Error>>
749 ) {
750 let (verify_queue_tx, verify_queue_rx) = mpsc::channel(64);
751 let (verify_result_tx, verify_result_rx) = sync::oneshot::channel();
752
753 let h2_2 = h2.clone();
754
755 hyper::rt::spawn(
756 verify_queue_rx
757 .map_err(Error::from)
758 .and_then(move |(merged_chunk_info, response): (MergedChunkInfo, Option<h2::client::ResponseFuture>)| {
759 match (response, merged_chunk_info) {
760 (Some(response), MergedChunkInfo::Known(list)) => {
761 future::Either::A(
762 response
763 .map_err(Error::from)
764 .and_then(H2Client::h2api_response)
765 .and_then(move |_result| {
766 Ok(MergedChunkInfo::Known(list))
767 })
768 )
769 }
770 (None, MergedChunkInfo::Known(list)) => {
771 future::Either::B(future::ok(MergedChunkInfo::Known(list)))
772 }
773 _ => unreachable!(),
774 }
775 })
776 .merge_known_chunks()
777 .and_then(move |merged_chunk_info| {
778 match merged_chunk_info {
779 MergedChunkInfo::Known(chunk_list) => {
780 let mut digest_list = vec![];
781 let mut offset_list = vec![];
782 for (offset, digest) in chunk_list {
783 //println!("append chunk {} (offset {})", proxmox::tools::digest_to_hex(&digest), offset);
784 digest_list.push(proxmox::tools::digest_to_hex(&digest));
785 offset_list.push(offset);
786 }
787 println!("append chunks list len ({})", digest_list.len());
788 let param = json!({ "wid": wid, "digest-list": digest_list, "offset-list": offset_list });
789 let mut request = H2Client::request_builder("localhost", "PUT", &path, None).unwrap();
790 request.headers_mut().insert(hyper::header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
791 let param_data = bytes::Bytes::from(param.to_string().as_bytes());
792 let upload_data = Some(param_data);
793 h2_2.send_request(request, upload_data)
794 .and_then(move |response| {
795 response
796 .map_err(Error::from)
797 .and_then(H2Client::h2api_response)
798 .and_then(|_| Ok(()))
799 })
800 .map_err(|err| format_err!("pipelined request failed: {}", err))
801 }
802 _ => unreachable!(),
803 }
804 })
805 .for_each(|_| Ok(()))
806 .then(|result|
807 verify_result_tx.send(result)
808 )
809 .map_err(|_| { /* ignore closed channel */ })
810 );
811
812 (verify_queue_tx, verify_result_rx)
813 }
814
815 fn download_chunk_list(
816 h2: H2Client,
817 path: &str,
818 archive_name: &str,
819 known_chunks: Arc<Mutex<HashSet<[u8;32]>>>,
820 ) -> impl Future<Item=(), Error=Error> {
821
822 let param = json!({ "archive-name": archive_name });
823 let request = H2Client::request_builder("localhost", "GET", path, Some(param)).unwrap();
824
825 h2.send_request(request, None)
826 .and_then(move |response| {
827 response
828 .map_err(Error::from)
829 .and_then(move |resp| {
830 let status = resp.status();
831
832 if !status.is_success() {
833 future::Either::A(H2Client::h2api_response(resp).and_then(|_| { bail!("unknown error"); }))
834 } else {
835 future::Either::B(future::ok(resp.into_body()))
836 }
837 })
838 .and_then(move |mut body| {
839
840 let mut release_capacity = body.release_capacity().clone();
841
842 DigestListDecoder::new(body.map_err(Error::from))
843 .for_each(move |chunk| {
844 let _ = release_capacity.release_capacity(chunk.len());
845 println!("GOT DOWNLOAD {}", proxmox::tools::digest_to_hex(&chunk));
846 known_chunks.lock().unwrap().insert(chunk);
847 Ok(())
848 })
849 })
850 })
851 }
852
853 fn upload_chunk_info_stream(
854 h2: H2Client,
855 wid: u64,
856 stream: impl Stream<Item=bytes::BytesMut, Error=Error>,
857 prefix: &str,
858 known_chunks: Arc<Mutex<HashSet<[u8;32]>>>,
859 crypt_config: Option<Arc<CryptConfig>>,
860 ) -> impl Future<Item=(usize, usize, usize), Error=Error> {
861
862 let repeat = std::sync::Arc::new(AtomicUsize::new(0));
863 let repeat2 = repeat.clone();
864
865 let stream_len = std::sync::Arc::new(AtomicUsize::new(0));
866 let stream_len2 = stream_len.clone();
867
868 let append_chunk_path = format!("{}_index", prefix);
869 let upload_chunk_path = format!("{}_chunk", prefix);
870
871 let (upload_queue, upload_result) = Self::append_chunk_queue(h2.clone(), wid, append_chunk_path.to_owned());
872
873 let start_time = std::time::Instant::now();
874
875 stream
876 .and_then(move |data| {
877
878 let chunk_len = data.len();
879
880 repeat.fetch_add(1, Ordering::SeqCst);
881 let offset = stream_len.fetch_add(chunk_len, Ordering::SeqCst) as u64;
882
883 let mut chunk_builder = DataChunkBuilder::new(data.as_ref())
884 .compress(true);
885
886 if let Some(ref crypt_config) = crypt_config {
887 chunk_builder = chunk_builder.crypt_config(crypt_config);
888 }
889
890 let mut known_chunks = known_chunks.lock().unwrap();
891 let digest = chunk_builder.digest();
892 let chunk_is_known = known_chunks.contains(digest);
893 if chunk_is_known {
894 Ok(MergedChunkInfo::Known(vec![(offset, *digest)]))
895 } else {
896 known_chunks.insert(*digest);
897 let chunk = chunk_builder.build()?;
898 Ok(MergedChunkInfo::New(ChunkInfo { chunk, chunk_len: chunk_len as u64, offset }))
899 }
900 })
901 .merge_known_chunks()
902 .for_each(move |merged_chunk_info| {
903
904 if let MergedChunkInfo::New(chunk_info) = merged_chunk_info {
905 let offset = chunk_info.offset;
906 let digest = *chunk_info.chunk.digest();
907 let digest_str = proxmox::tools::digest_to_hex(&digest);
908 let upload_queue = upload_queue.clone();
909
910 println!("upload new chunk {} ({} bytes, offset {})", digest_str,
911 chunk_info.chunk_len, offset);
912
913 let chunk_data = chunk_info.chunk.raw_data();
914 let param = json!({
915 "wid": wid,
916 "digest": digest_str,
917 "size": chunk_info.chunk_len,
918 "encoded-size": chunk_data.len(),
919 });
920
921 let request = H2Client::request_builder("localhost", "POST", &upload_chunk_path, Some(param)).unwrap();
922 let upload_data = Some(bytes::Bytes::from(chunk_data));
923
924 let new_info = MergedChunkInfo::Known(vec![(offset, digest)]);
925
926 future::Either::A(
927 h2.send_request(request, upload_data)
928 .and_then(move |response| {
929 upload_queue.clone().send((new_info, Some(response)))
930 .map(|_| ()).map_err(Error::from)
931 })
932 )
933 } else {
934
935 future::Either::B(
936 upload_queue.clone().send((merged_chunk_info, None))
937 .map(|_| ()).map_err(Error::from)
938 )
939 }
940 })
941 .then(move |result| {
942 //println!("RESULT {:?}", result);
943 upload_result.map_err(Error::from).and_then(|upload1_result| {
944 Ok(upload1_result.and(result))
945 })
946 })
947 .flatten()
948 .and_then(move |_| {
949 let repeat = repeat2.load(Ordering::SeqCst);
950 let stream_len = stream_len2.load(Ordering::SeqCst);
951 let speed = ((stream_len*1000000)/(1024*1024))/(start_time.elapsed().as_micros() as usize);
952 println!("Uploaded {} chunks in {} seconds ({} MB/s).", repeat, start_time.elapsed().as_secs(), speed);
953 if repeat > 0 {
954 println!("Average chunk size was {} bytes.", stream_len/repeat);
955 println!("Time per request: {} microseconds.", (start_time.elapsed().as_micros())/(repeat as u128));
956 }
957 Ok((repeat, stream_len, speed))
958 })
959 }
960
961 pub fn upload_speedtest(&self) -> impl Future<Item=usize, Error=Error> {
962
963 let mut data = vec![];
964 // generate pseudo random byte sequence
965 for i in 0..1024*1024 {
966 for j in 0..4 {
967 let byte = ((i >> (j<<3))&0xff) as u8;
968 data.push(byte);
969 }
970 }
971
972 let item_len = data.len();
973
974 let repeat = std::sync::Arc::new(AtomicUsize::new(0));
975 let repeat2 = repeat.clone();
976
977 let (upload_queue, upload_result) = Self::response_queue();
978
979 let start_time = std::time::Instant::now();
980
981 let h2 = self.h2.clone();
982
983 futures::stream::repeat(data)
984 .take_while(move |_| {
985 repeat.fetch_add(1, Ordering::SeqCst);
986 Ok(start_time.elapsed().as_secs() < 5)
987 })
988 .for_each(move |data| {
989 let h2 = h2.clone();
990
991 let upload_queue = upload_queue.clone();
992
993 println!("send test data ({} bytes)", data.len());
994 let request = H2Client::request_builder("localhost", "POST", "speedtest", None).unwrap();
995 h2.send_request(request, Some(bytes::Bytes::from(data)))
996 .and_then(move |response| {
997 upload_queue.send(response)
998 .map(|_| ()).map_err(Error::from)
999 })
1000 })
1001 .then(move |result| {
1002 println!("RESULT {:?}", result);
1003 upload_result.map_err(Error::from).and_then(|upload1_result| {
1004 Ok(upload1_result.and(result))
1005 })
1006 })
1007 .flatten()
1008 .and_then(move |_| {
1009 let repeat = repeat2.load(Ordering::SeqCst);
1010 println!("Uploaded {} chunks in {} seconds.", repeat, start_time.elapsed().as_secs());
1011 let speed = ((item_len*1000000*(repeat as usize))/(1024*1024))/(start_time.elapsed().as_micros() as usize);
1012 if repeat > 0 {
1013 println!("Time per request: {} microseconds.", (start_time.elapsed().as_micros())/(repeat as u128));
1014 }
1015 Ok(speed)
1016 })
1017 }
1018 }
1019
1020 #[derive(Clone)]
1021 pub struct H2Client {
1022 h2: h2::client::SendRequest<bytes::Bytes>,
1023 }
1024
1025 impl H2Client {
1026
1027 pub fn new(h2: h2::client::SendRequest<bytes::Bytes>) -> Self {
1028 Self { h2 }
1029 }
1030
1031 pub fn get(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
1032 let req = Self::request_builder("localhost", "GET", path, param).unwrap();
1033 self.request(req)
1034 }
1035
1036 pub fn put(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
1037 let req = Self::request_builder("localhost", "PUT", path, param).unwrap();
1038 self.request(req)
1039 }
1040
1041 pub fn post(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
1042 let req = Self::request_builder("localhost", "POST", path, param).unwrap();
1043 self.request(req)
1044 }
1045
1046 pub fn download<W: Write>(&self, path: &str, param: Option<Value>, output: W) -> impl Future<Item=W, Error=Error> {
1047 let request = Self::request_builder("localhost", "GET", path, param).unwrap();
1048
1049 self.send_request(request, None)
1050 .and_then(move |response| {
1051 response
1052 .map_err(Error::from)
1053 .and_then(move |resp| {
1054 let status = resp.status();
1055 if !status.is_success() {
1056 future::Either::A(
1057 H2Client::h2api_response(resp)
1058 .and_then(|_| { bail!("unknown error"); })
1059 )
1060 } else {
1061 let mut body = resp.into_body();
1062 let mut release_capacity = body.release_capacity().clone();
1063
1064 future::Either::B(
1065 body
1066 .map_err(Error::from)
1067 .fold(output, move |mut acc, chunk| {
1068 let _ = release_capacity.release_capacity(chunk.len());
1069 acc.write_all(&chunk)?;
1070 Ok::<_, Error>(acc)
1071 })
1072 )
1073 }
1074 })
1075 })
1076 }
1077
1078 pub fn upload(&self, path: &str, param: Option<Value>, data: Vec<u8>) -> impl Future<Item=Value, Error=Error> {
1079 let request = Self::request_builder("localhost", "POST", path, param).unwrap();
1080
1081 self.h2.clone()
1082 .ready()
1083 .map_err(Error::from)
1084 .and_then(move |mut send_request| {
1085 let (response, stream) = send_request.send_request(request, false).unwrap();
1086 PipeToSendStream::new(bytes::Bytes::from(data), stream)
1087 .and_then(|_| {
1088 response
1089 .map_err(Error::from)
1090 .and_then(Self::h2api_response)
1091 })
1092 })
1093 }
1094
1095 fn request(
1096 &self,
1097 request: Request<()>,
1098 ) -> impl Future<Item=Value, Error=Error> {
1099
1100 self.send_request(request, None)
1101 .and_then(move |response| {
1102 response
1103 .map_err(Error::from)
1104 .and_then(Self::h2api_response)
1105 })
1106 }
1107
1108 fn send_request(
1109 &self,
1110 request: Request<()>,
1111 data: Option<bytes::Bytes>,
1112 ) -> impl Future<Item=h2::client::ResponseFuture, Error=Error> {
1113
1114 self.h2.clone()
1115 .ready()
1116 .map_err(Error::from)
1117 .and_then(move |mut send_request| {
1118 if let Some(data) = data {
1119 let (response, stream) = send_request.send_request(request, false).unwrap();
1120 future::Either::A(PipeToSendStream::new(data, stream)
1121 .and_then(move |_| {
1122 future::ok(response)
1123 }))
1124 } else {
1125 let (response, _stream) = send_request.send_request(request, true).unwrap();
1126 future::Either::B(future::ok(response))
1127 }
1128 })
1129 }
1130
1131 fn h2api_response(response: Response<h2::RecvStream>) -> impl Future<Item=Value, Error=Error> {
1132
1133 let status = response.status();
1134
1135 let (_head, mut body) = response.into_parts();
1136
1137 // The `release_capacity` handle allows the caller to manage
1138 // flow control.
1139 //
1140 // Whenever data is received, the caller is responsible for
1141 // releasing capacity back to the server once it has freed
1142 // the data from memory.
1143 let mut release_capacity = body.release_capacity().clone();
1144
1145 body
1146 .map(move |chunk| {
1147 // Let the server send more data.
1148 let _ = release_capacity.release_capacity(chunk.len());
1149 chunk
1150 })
1151 .concat2()
1152 .map_err(Error::from)
1153 .and_then(move |data| {
1154 let text = String::from_utf8(data.to_vec()).unwrap();
1155 if status.is_success() {
1156 if text.len() > 0 {
1157 let mut value: Value = serde_json::from_str(&text)?;
1158 if let Some(map) = value.as_object_mut() {
1159 if let Some(data) = map.remove("data") {
1160 return Ok(data);
1161 }
1162 }
1163 bail!("got result without data property");
1164 } else {
1165 Ok(Value::Null)
1166 }
1167 } else {
1168 bail!("HTTP Error {}: {}", status, text);
1169 }
1170 })
1171 }
1172
1173 // Note: We always encode parameters with the url
1174 pub fn request_builder(server: &str, method: &str, path: &str, data: Option<Value>) -> Result<Request<()>, Error> {
1175 let path = path.trim_matches('/');
1176
1177 if let Some(data) = data {
1178 let query = tools::json_object_to_query(data)?;
1179 // We detected problem with hyper around 6000 characters - seo we try to keep on the safe side
1180 if query.len() > 4096 { bail!("h2 query data too large ({} bytes) - please encode data inside body", query.len()); }
1181 let url: Uri = format!("https://{}:8007/{}?{}", server, path, query).parse()?;
1182 let request = Request::builder()
1183 .method(method)
1184 .uri(url)
1185 .header("User-Agent", "proxmox-backup-client/1.0")
1186 .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
1187 .body(())?;
1188 return Ok(request);
1189 } else {
1190 let url: Uri = format!("https://{}:8007/{}", server, path).parse()?;
1191 let request = Request::builder()
1192 .method(method)
1193 .uri(url)
1194 .header("User-Agent", "proxmox-backup-client/1.0")
1195 .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
1196 .body(())?;
1197
1198 Ok(request)
1199 }
1200 }
1201 }