]> git.proxmox.com Git - proxmox-backup.git/blob - src/client/http_client.rs
src/client/http_client.rs - download: use generic Write type, return writer.
[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::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
20 use serde_json::{json, Value};
21 use url::percent_encoding::{percent_encode, DEFAULT_ENCODE_SET};
22
23 use crate::tools::{self, BroadcastFuture, tty};
24 use crate::tools::futures::{cancellable, Canceller};
25 use super::pipe_to_stream::*;
26 use super::merge_known_chunks::*;
27
28 use crate::backup::*;
29
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_tls::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_tls::HttpsConnector<hyper::client::HttpConnector>> {
161 let mut builder = native_tls::TlsConnector::builder();
162 // FIXME: We need a CLI option for this!
163 builder.danger_accept_invalid_certs(true);
164 let tlsconnector = builder.build().unwrap();
165 let mut httpc = hyper::client::HttpConnector::new(1);
166 //httpc.set_nodelay(true); // not sure if this help?
167 httpc.enforce_http(false); // we want https...
168 let mut https = hyper_tls::HttpsConnector::from((httpc, tlsconnector));
169 https.https_only(true); // force it!
170 Client::builder()
171 //.http2_initial_stream_window_size( (1 << 31) - 2)
172 //.http2_initial_connection_window_size( (1 << 31) - 2)
173 .build::<_, Body>(https)
174 }
175
176 pub fn request(&self, mut req: Request<Body>) -> impl Future<Item=Value, Error=Error> {
177
178 let login = self.auth.listen();
179
180 let client = self.client.clone();
181
182 login.and_then(move |auth| {
183
184 let enc_ticket = format!("PBSAuthCookie={}", percent_encode(auth.ticket.as_bytes(), DEFAULT_ENCODE_SET));
185 req.headers_mut().insert("Cookie", HeaderValue::from_str(&enc_ticket).unwrap());
186 req.headers_mut().insert("CSRFPreventionToken", HeaderValue::from_str(&auth.token).unwrap());
187
188 let request = Self::api_request(client, req);
189
190 request
191 })
192 }
193
194 pub fn get(&self, path: &str, data: Option<Value>) -> impl Future<Item=Value, Error=Error> {
195
196 let req = Self::request_builder(&self.server, "GET", path, data).unwrap();
197 self.request(req)
198 }
199
200 pub fn delete(&mut self, path: &str, data: Option<Value>) -> impl Future<Item=Value, Error=Error> {
201
202 let req = Self::request_builder(&self.server, "DELETE", path, data).unwrap();
203 self.request(req)
204 }
205
206 pub fn post(&mut self, path: &str, data: Option<Value>) -> impl Future<Item=Value, Error=Error> {
207
208 let req = Self::request_builder(&self.server, "POST", path, data).unwrap();
209 self.request(req)
210 }
211
212 pub fn download<W: Write>(&mut self, path: &str, output: W) -> impl Future<Item=W, Error=Error> {
213
214 let mut req = Self::request_builder(&self.server, "GET", path, None).unwrap();
215
216 let login = self.auth.listen();
217
218 let client = self.client.clone();
219
220 login.and_then(move |auth| {
221
222 let enc_ticket = format!("PBSAuthCookie={}", percent_encode(auth.ticket.as_bytes(), DEFAULT_ENCODE_SET));
223 req.headers_mut().insert("Cookie", HeaderValue::from_str(&enc_ticket).unwrap());
224
225 client.request(req)
226 .map_err(Error::from)
227 .and_then(|resp| {
228 let status = resp.status();
229 if !status.is_success() {
230 future::Either::A(
231 HttpClient::api_response(resp)
232 .and_then(|_| { bail!("unknown error"); })
233 )
234 } else {
235 future::Either::B(
236 resp.into_body()
237 .map_err(Error::from)
238 .fold(output, move |mut acc, chunk| {
239 acc.write_all(&chunk)?;
240 Ok::<_, Error>(acc)
241 })
242 )
243 }
244 })
245 })
246 }
247
248 pub fn upload(&mut self, content_type: &str, body: Body, path: &str) -> impl Future<Item=Value, Error=Error> {
249
250 let path = path.trim_matches('/');
251 let url: Uri = format!("https://{}:8007/{}", &self.server, path).parse().unwrap();
252
253 let req = Request::builder()
254 .method("POST")
255 .uri(url)
256 .header("User-Agent", "proxmox-backup-client/1.0")
257 .header("Content-Type", content_type)
258 .body(body).unwrap();
259
260 self.request(req)
261 }
262
263 pub fn start_backup(
264 &self,
265 datastore: &str,
266 backup_type: &str,
267 backup_id: &str,
268 debug: bool,
269 ) -> impl Future<Item=BackupClient, Error=Error> {
270
271 let param = json!({"backup-type": backup_type, "backup-id": backup_id, "store": datastore, "debug": debug});
272 let mut req = Self::request_builder(&self.server, "GET", "/api2/json/backup", Some(param)).unwrap();
273
274 let login = self.auth.listen();
275
276 let client = self.client.clone();
277
278 login.and_then(move |auth| {
279
280 let enc_ticket = format!("PBSAuthCookie={}", percent_encode(auth.ticket.as_bytes(), DEFAULT_ENCODE_SET));
281 req.headers_mut().insert("Cookie", HeaderValue::from_str(&enc_ticket).unwrap());
282 req.headers_mut().insert("UPGRADE", HeaderValue::from_str(PROXMOX_BACKUP_PROTOCOL_ID_V1!()).unwrap());
283
284 client.request(req)
285 .map_err(Error::from)
286 .and_then(|resp| {
287
288 let status = resp.status();
289 if status != http::StatusCode::SWITCHING_PROTOCOLS {
290 future::Either::A(Self::api_response(resp).and_then(|_| { bail!("unknown error"); }))
291 } else {
292 future::Either::B(resp.into_body().on_upgrade().map_err(Error::from))
293 }
294 })
295 .and_then(|upgraded| {
296 h2::client::handshake(upgraded).map_err(Error::from)
297 })
298 .and_then(|(h2, connection)| {
299 let connection = connection
300 .map_err(|_| panic!("HTTP/2.0 connection failed"));
301
302 let (connection, canceller) = cancellable(connection)?;
303 // A cancellable future returns an Option which is None when cancelled and
304 // Some when it finished instead, since we don't care about the return type we
305 // need to map it away:
306 let connection = connection.map(|_| ());
307
308 // Spawn a new task to drive the connection state
309 hyper::rt::spawn(connection);
310
311 // Wait until the `SendRequest` handle has available capacity.
312 Ok(h2.ready()
313 .map(move |c| BackupClient::new(c, canceller))
314 .map_err(Error::from))
315 })
316 .flatten()
317 })
318 }
319
320 fn credentials(
321 client: Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>,
322 server: String,
323 username: String,
324 password: String,
325 ) -> Box<dyn Future<Item=AuthInfo, Error=Error> + Send> {
326
327 let server2 = server.clone();
328
329 let create_request = futures::future::lazy(move || {
330 let data = json!({ "username": username, "password": password });
331 let req = Self::request_builder(&server, "POST", "/api2/json/access/ticket", Some(data)).unwrap();
332 Self::api_request(client, req)
333 });
334
335 let login_future = create_request
336 .and_then(move |cred| {
337 let auth = AuthInfo {
338 username: cred["data"]["username"].as_str().unwrap().to_owned(),
339 ticket: cred["data"]["ticket"].as_str().unwrap().to_owned(),
340 token: cred["data"]["CSRFPreventionToken"].as_str().unwrap().to_owned(),
341 };
342
343 let _ = store_ticket_info(&server2, &auth.username, &auth.ticket, &auth.token);
344
345 Ok(auth)
346 });
347
348 Box::new(login_future)
349 }
350
351 fn api_response(response: Response<Body>) -> impl Future<Item=Value, Error=Error> {
352
353 let status = response.status();
354
355 response
356 .into_body()
357 .concat2()
358 .map_err(Error::from)
359 .and_then(move |data| {
360
361 let text = String::from_utf8(data.to_vec()).unwrap();
362 if status.is_success() {
363 if text.len() > 0 {
364 let value: Value = serde_json::from_str(&text)?;
365 Ok(value)
366 } else {
367 Ok(Value::Null)
368 }
369 } else {
370 bail!("HTTP Error {}: {}", status, text);
371 }
372 })
373 }
374
375 fn api_request(
376 client: Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>,
377 req: Request<Body>
378 ) -> impl Future<Item=Value, Error=Error> {
379
380 client.request(req)
381 .map_err(Error::from)
382 .and_then(Self::api_response)
383 }
384
385 pub fn request_builder(server: &str, method: &str, path: &str, data: Option<Value>) -> Result<Request<Body>, Error> {
386 let path = path.trim_matches('/');
387 let url: Uri = format!("https://{}:8007/{}", server, path).parse()?;
388
389 if let Some(data) = data {
390 if method == "POST" {
391 let request = Request::builder()
392 .method(method)
393 .uri(url)
394 .header("User-Agent", "proxmox-backup-client/1.0")
395 .header(hyper::header::CONTENT_TYPE, "application/json")
396 .body(Body::from(data.to_string()))?;
397 return Ok(request);
398 } else {
399 let query = tools::json_object_to_query(data)?;
400 let url: Uri = format!("https://{}:8007/{}?{}", server, path, query).parse()?;
401 let request = Request::builder()
402 .method(method)
403 .uri(url)
404 .header("User-Agent", "proxmox-backup-client/1.0")
405 .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
406 .body(Body::empty())?;
407 return Ok(request);
408 }
409 }
410
411 let request = Request::builder()
412 .method(method)
413 .uri(url)
414 .header("User-Agent", "proxmox-backup-client/1.0")
415 .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
416 .body(Body::empty())?;
417
418 Ok(request)
419 }
420 }
421
422 //#[derive(Clone)]
423 pub struct BackupClient {
424 h2: H2Client,
425 canceller: Option<Canceller>,
426 }
427
428
429 impl BackupClient {
430
431 pub fn new(h2: h2::client::SendRequest<bytes::Bytes>, canceller: Canceller) -> Self {
432 Self {
433 h2: H2Client::new(h2),
434 canceller: Some(canceller),
435 }
436 }
437
438 pub fn get(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
439 self.h2.get(path, param)
440 }
441
442 pub fn put(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
443 self.h2.put(path, param)
444 }
445
446 pub fn post(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
447 self.h2.post(path, param)
448 }
449
450 pub fn finish(mut self) -> impl Future<Item=(), Error=Error> {
451 let canceler = self.canceller.take().unwrap();
452 self.h2.clone().post("finish", None).map(move |_| {
453 canceler.cancel();
454 ()
455 })
456 }
457
458 pub fn force_close(mut self) {
459 self.canceller.take().unwrap().cancel();
460 }
461
462 pub fn upload_blob_from_data(
463 &self,
464 data: Vec<u8>,
465 file_name: &str,
466 crypt_config: Option<Arc<CryptConfig>>,
467 compress: bool,
468 ) -> impl Future<Item=(), Error=Error> {
469
470 let h2 = self.h2.clone();
471 let file_name = file_name.to_owned();
472
473 futures::future::ok(())
474 .and_then(move |_| {
475 let blob = if let Some(ref crypt_config) = crypt_config {
476 DataBlob::encode(&data, Some(crypt_config), compress)?
477 } else {
478 DataBlob::encode(&data, None, compress)?
479 };
480
481 let raw_data = blob.into_inner();
482 Ok(raw_data)
483 })
484 .and_then(move |raw_data| {
485 let param = json!({"encoded-size": raw_data.len(), "file-name": file_name });
486 h2.upload("blob", Some(param), raw_data)
487 .map(|_| {})
488 })
489 }
490
491 pub fn upload_blob_from_file<P: AsRef<std::path::Path>>(
492 &self,
493 src_path: P,
494 file_name: &str,
495 crypt_config: Option<Arc<CryptConfig>>,
496 compress: bool,
497 ) -> impl Future<Item=(), Error=Error> {
498
499 let h2 = self.h2.clone();
500 let file_name = file_name.to_owned();
501 let src_path = src_path.as_ref().to_owned();
502
503 let task = tokio::fs::File::open(src_path.clone())
504 .map_err(move |err| format_err!("unable to open file {:?} - {}", src_path, err))
505 .and_then(move |file| {
506 let contents = vec![];
507 tokio::io::read_to_end(file, contents)
508 .map_err(Error::from)
509 .and_then(move |(_, contents)| {
510 let blob = if let Some(ref crypt_config) = crypt_config {
511 DataBlob::encode(&contents, Some(crypt_config), compress)?
512 } else {
513 DataBlob::encode(&contents, None, compress)?
514 };
515 let raw_data = blob.into_inner();
516 Ok(raw_data)
517 })
518 .and_then(move |raw_data| {
519 let param = json!({"encoded-size": raw_data.len(), "file-name": file_name });
520 h2.upload("blob", Some(param), raw_data)
521 .map(|_| {})
522 })
523 });
524
525 task
526 }
527
528 pub fn upload_stream(
529 &self,
530 archive_name: &str,
531 stream: impl Stream<Item=bytes::BytesMut, Error=Error>,
532 prefix: &str,
533 fixed_size: Option<u64>,
534 crypt_config: Option<Arc<CryptConfig>>,
535 ) -> impl Future<Item=(), Error=Error> {
536
537 let known_chunks = Arc::new(Mutex::new(HashSet::new()));
538
539 let h2 = self.h2.clone();
540 let h2_2 = self.h2.clone();
541 let h2_3 = self.h2.clone();
542 let h2_4 = self.h2.clone();
543
544 let mut param = json!({ "archive-name": archive_name });
545 if let Some(size) = fixed_size {
546 param["size"] = size.into();
547 }
548
549 let index_path = format!("{}_index", prefix);
550 let close_path = format!("{}_close", prefix);
551
552 let prefix = prefix.to_owned();
553
554 Self::download_chunk_list(h2, &index_path, archive_name, known_chunks.clone())
555 .and_then(move |_| {
556 h2_2.post(&index_path, Some(param))
557 })
558 .and_then(move |res| {
559 let wid = res.as_u64().unwrap();
560 Self::upload_chunk_info_stream(h2_3, wid, stream, &prefix, known_chunks.clone(), crypt_config)
561 .and_then(move |(chunk_count, size, _speed)| {
562 let param = json!({
563 "wid": wid ,
564 "chunk-count": chunk_count,
565 "size": size,
566 });
567 h2_4.post(&close_path, Some(param))
568 })
569 .map(|_| ())
570 })
571 }
572
573 fn response_queue() -> (
574 mpsc::Sender<h2::client::ResponseFuture>,
575 sync::oneshot::Receiver<Result<(), Error>>
576 ) {
577 let (verify_queue_tx, verify_queue_rx) = mpsc::channel(100);
578 let (verify_result_tx, verify_result_rx) = sync::oneshot::channel();
579
580 hyper::rt::spawn(
581 verify_queue_rx
582 .map_err(Error::from)
583 .for_each(|response: h2::client::ResponseFuture| {
584 response
585 .map_err(Error::from)
586 .and_then(H2Client::h2api_response)
587 .and_then(|result| {
588 println!("RESPONSE: {:?}", result);
589 Ok(())
590 })
591 .map_err(|err| format_err!("pipelined request failed: {}", err))
592 })
593 .then(|result|
594 verify_result_tx.send(result)
595 )
596 .map_err(|_| { /* ignore closed channel */ })
597 );
598
599 (verify_queue_tx, verify_result_rx)
600 }
601
602 fn append_chunk_queue(h2: H2Client, wid: u64, path: String) -> (
603 mpsc::Sender<(MergedChunkInfo, Option<h2::client::ResponseFuture>)>,
604 sync::oneshot::Receiver<Result<(), Error>>
605 ) {
606 let (verify_queue_tx, verify_queue_rx) = mpsc::channel(64);
607 let (verify_result_tx, verify_result_rx) = sync::oneshot::channel();
608
609 let h2_2 = h2.clone();
610
611 hyper::rt::spawn(
612 verify_queue_rx
613 .map_err(Error::from)
614 .and_then(move |(merged_chunk_info, response): (MergedChunkInfo, Option<h2::client::ResponseFuture>)| {
615 match (response, merged_chunk_info) {
616 (Some(response), MergedChunkInfo::Known(list)) => {
617 future::Either::A(
618 response
619 .map_err(Error::from)
620 .and_then(H2Client::h2api_response)
621 .and_then(move |_result| {
622 Ok(MergedChunkInfo::Known(list))
623 })
624 )
625 }
626 (None, MergedChunkInfo::Known(list)) => {
627 future::Either::B(future::ok(MergedChunkInfo::Known(list)))
628 }
629 _ => unreachable!(),
630 }
631 })
632 .merge_known_chunks()
633 .and_then(move |merged_chunk_info| {
634 match merged_chunk_info {
635 MergedChunkInfo::Known(chunk_list) => {
636 let mut digest_list = vec![];
637 let mut offset_list = vec![];
638 for (offset, digest) in chunk_list {
639 //println!("append chunk {} (offset {})", proxmox::tools::digest_to_hex(&digest), offset);
640 digest_list.push(proxmox::tools::digest_to_hex(&digest));
641 offset_list.push(offset);
642 }
643 println!("append chunks list len ({})", digest_list.len());
644 let param = json!({ "wid": wid, "digest-list": digest_list, "offset-list": offset_list });
645 let mut request = H2Client::request_builder("localhost", "PUT", &path, None).unwrap();
646 request.headers_mut().insert(hyper::header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
647 let param_data = bytes::Bytes::from(param.to_string().as_bytes());
648 let upload_data = Some(param_data);
649 h2_2.send_request(request, upload_data)
650 .and_then(move |response| {
651 response
652 .map_err(Error::from)
653 .and_then(H2Client::h2api_response)
654 .and_then(|_| Ok(()))
655 })
656 .map_err(|err| format_err!("pipelined request failed: {}", err))
657 }
658 _ => unreachable!(),
659 }
660 })
661 .for_each(|_| Ok(()))
662 .then(|result|
663 verify_result_tx.send(result)
664 )
665 .map_err(|_| { /* ignore closed channel */ })
666 );
667
668 (verify_queue_tx, verify_result_rx)
669 }
670
671 fn download_chunk_list(
672 h2: H2Client,
673 path: &str,
674 archive_name: &str,
675 known_chunks: Arc<Mutex<HashSet<[u8;32]>>>,
676 ) -> impl Future<Item=(), Error=Error> {
677
678 let param = json!({ "archive-name": archive_name });
679 let request = H2Client::request_builder("localhost", "GET", path, Some(param)).unwrap();
680
681 h2.send_request(request, None)
682 .and_then(move |response| {
683 response
684 .map_err(Error::from)
685 .and_then(move |resp| {
686 let status = resp.status();
687
688 if !status.is_success() {
689 future::Either::A(H2Client::h2api_response(resp).and_then(|_| { bail!("unknown error"); }))
690 } else {
691 future::Either::B(future::ok(resp.into_body()))
692 }
693 })
694 .and_then(move |mut body| {
695
696 let mut release_capacity = body.release_capacity().clone();
697
698 DigestListDecoder::new(body.map_err(Error::from))
699 .for_each(move |chunk| {
700 let _ = release_capacity.release_capacity(chunk.len());
701 println!("GOT DOWNLOAD {}", proxmox::tools::digest_to_hex(&chunk));
702 known_chunks.lock().unwrap().insert(chunk);
703 Ok(())
704 })
705 })
706 })
707 }
708
709 fn upload_chunk_info_stream(
710 h2: H2Client,
711 wid: u64,
712 stream: impl Stream<Item=bytes::BytesMut, Error=Error>,
713 prefix: &str,
714 known_chunks: Arc<Mutex<HashSet<[u8;32]>>>,
715 crypt_config: Option<Arc<CryptConfig>>,
716 ) -> impl Future<Item=(usize, usize, usize), Error=Error> {
717
718 let repeat = std::sync::Arc::new(AtomicUsize::new(0));
719 let repeat2 = repeat.clone();
720
721 let stream_len = std::sync::Arc::new(AtomicUsize::new(0));
722 let stream_len2 = stream_len.clone();
723
724 let append_chunk_path = format!("{}_index", prefix);
725 let upload_chunk_path = format!("{}_chunk", prefix);
726
727 let (upload_queue, upload_result) = Self::append_chunk_queue(h2.clone(), wid, append_chunk_path.to_owned());
728
729 let start_time = std::time::Instant::now();
730
731 stream
732 .and_then(move |data| {
733
734 let chunk_len = data.len();
735
736 repeat.fetch_add(1, Ordering::SeqCst);
737 let offset = stream_len.fetch_add(chunk_len, Ordering::SeqCst) as u64;
738
739 let mut chunk_builder = DataChunkBuilder::new(data.as_ref())
740 .compress(true);
741
742 if let Some(ref crypt_config) = crypt_config {
743 chunk_builder = chunk_builder.crypt_config(crypt_config);
744 }
745
746 let mut known_chunks = known_chunks.lock().unwrap();
747 let digest = chunk_builder.digest();
748 let chunk_is_known = known_chunks.contains(digest);
749 if chunk_is_known {
750 Ok(MergedChunkInfo::Known(vec![(offset, *digest)]))
751 } else {
752 known_chunks.insert(*digest);
753 let chunk = chunk_builder.build()?;
754 Ok(MergedChunkInfo::New(ChunkInfo { chunk, chunk_len: chunk_len as u64, offset }))
755 }
756 })
757 .merge_known_chunks()
758 .for_each(move |merged_chunk_info| {
759
760 if let MergedChunkInfo::New(chunk_info) = merged_chunk_info {
761 let offset = chunk_info.offset;
762 let digest = *chunk_info.chunk.digest();
763 let digest_str = proxmox::tools::digest_to_hex(&digest);
764 let upload_queue = upload_queue.clone();
765
766 println!("upload new chunk {} ({} bytes, offset {})", digest_str,
767 chunk_info.chunk_len, offset);
768
769 let chunk_data = chunk_info.chunk.raw_data();
770 let param = json!({
771 "wid": wid,
772 "digest": digest_str,
773 "size": chunk_info.chunk_len,
774 "encoded-size": chunk_data.len(),
775 });
776
777 let request = H2Client::request_builder("localhost", "POST", &upload_chunk_path, Some(param)).unwrap();
778 let upload_data = Some(bytes::Bytes::from(chunk_data));
779
780 let new_info = MergedChunkInfo::Known(vec![(offset, digest)]);
781
782 future::Either::A(
783 h2.send_request(request, upload_data)
784 .and_then(move |response| {
785 upload_queue.clone().send((new_info, Some(response)))
786 .map(|_| ()).map_err(Error::from)
787 })
788 )
789 } else {
790
791 future::Either::B(
792 upload_queue.clone().send((merged_chunk_info, None))
793 .map(|_| ()).map_err(Error::from)
794 )
795 }
796 })
797 .then(move |result| {
798 println!("RESULT {:?}", result);
799 upload_result.map_err(Error::from).and_then(|upload1_result| {
800 Ok(upload1_result.and(result))
801 })
802 })
803 .flatten()
804 .and_then(move |_| {
805 let repeat = repeat2.load(Ordering::SeqCst);
806 let stream_len = stream_len2.load(Ordering::SeqCst);
807 let speed = ((stream_len*1000000)/(1024*1024))/(start_time.elapsed().as_micros() as usize);
808 println!("Uploaded {} chunks in {} seconds ({} MB/s).", repeat, start_time.elapsed().as_secs(), speed);
809 if repeat > 0 {
810 println!("Average chunk size was {} bytes.", stream_len/repeat);
811 println!("Time per request: {} microseconds.", (start_time.elapsed().as_micros())/(repeat as u128));
812 }
813 Ok((repeat, stream_len, speed))
814 })
815 }
816
817 pub fn upload_speedtest(&self) -> impl Future<Item=usize, Error=Error> {
818
819 let mut data = vec![];
820 // generate pseudo random byte sequence
821 for i in 0..1024*1024 {
822 for j in 0..4 {
823 let byte = ((i >> (j<<3))&0xff) as u8;
824 data.push(byte);
825 }
826 }
827
828 let item_len = data.len();
829
830 let repeat = std::sync::Arc::new(AtomicUsize::new(0));
831 let repeat2 = repeat.clone();
832
833 let (upload_queue, upload_result) = Self::response_queue();
834
835 let start_time = std::time::Instant::now();
836
837 let h2 = self.h2.clone();
838
839 futures::stream::repeat(data)
840 .take_while(move |_| {
841 repeat.fetch_add(1, Ordering::SeqCst);
842 Ok(start_time.elapsed().as_secs() < 5)
843 })
844 .for_each(move |data| {
845 let h2 = h2.clone();
846
847 let upload_queue = upload_queue.clone();
848
849 println!("send test data ({} bytes)", data.len());
850 let request = H2Client::request_builder("localhost", "POST", "speedtest", None).unwrap();
851 h2.send_request(request, Some(bytes::Bytes::from(data)))
852 .and_then(move |response| {
853 upload_queue.send(response)
854 .map(|_| ()).map_err(Error::from)
855 })
856 })
857 .then(move |result| {
858 println!("RESULT {:?}", result);
859 upload_result.map_err(Error::from).and_then(|upload1_result| {
860 Ok(upload1_result.and(result))
861 })
862 })
863 .flatten()
864 .and_then(move |_| {
865 let repeat = repeat2.load(Ordering::SeqCst);
866 println!("Uploaded {} chunks in {} seconds.", repeat, start_time.elapsed().as_secs());
867 let speed = ((item_len*1000000*(repeat as usize))/(1024*1024))/(start_time.elapsed().as_micros() as usize);
868 if repeat > 0 {
869 println!("Time per request: {} microseconds.", (start_time.elapsed().as_micros())/(repeat as u128));
870 }
871 Ok(speed)
872 })
873 }
874 }
875
876 #[derive(Clone)]
877 pub struct H2Client {
878 h2: h2::client::SendRequest<bytes::Bytes>,
879 }
880
881 impl H2Client {
882
883 pub fn new(h2: h2::client::SendRequest<bytes::Bytes>) -> Self {
884 Self { h2 }
885 }
886
887 pub fn get(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
888 let req = Self::request_builder("localhost", "GET", path, param).unwrap();
889 self.request(req)
890 }
891
892 pub fn put(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
893 let req = Self::request_builder("localhost", "PUT", path, param).unwrap();
894 self.request(req)
895 }
896
897 pub fn post(&self, path: &str, param: Option<Value>) -> impl Future<Item=Value, Error=Error> {
898 let req = Self::request_builder("localhost", "POST", path, param).unwrap();
899 self.request(req)
900 }
901
902 pub fn upload(&self, path: &str, param: Option<Value>, data: Vec<u8>) -> impl Future<Item=Value, Error=Error> {
903 let request = Self::request_builder("localhost", "POST", path, param).unwrap();
904
905
906 self.h2.clone()
907 .ready()
908 .map_err(Error::from)
909 .and_then(move |mut send_request| {
910 let (response, stream) = send_request.send_request(request, false).unwrap();
911 PipeToSendStream::new(bytes::Bytes::from(data), stream)
912 .and_then(|_| {
913 response
914 .map_err(Error::from)
915 .and_then(Self::h2api_response)
916 })
917 })
918 }
919
920 fn request(
921 &self,
922 request: Request<()>,
923 ) -> impl Future<Item=Value, Error=Error> {
924
925 self.send_request(request, None)
926 .and_then(move |response| {
927 response
928 .map_err(Error::from)
929 .and_then(Self::h2api_response)
930 })
931 }
932
933 fn send_request(
934 &self,
935 request: Request<()>,
936 data: Option<bytes::Bytes>,
937 ) -> impl Future<Item=h2::client::ResponseFuture, Error=Error> {
938
939 self.h2.clone()
940 .ready()
941 .map_err(Error::from)
942 .and_then(move |mut send_request| {
943 if let Some(data) = data {
944 let (response, stream) = send_request.send_request(request, false).unwrap();
945 future::Either::A(PipeToSendStream::new(data, stream)
946 .and_then(move |_| {
947 future::ok(response)
948 }))
949 } else {
950 let (response, _stream) = send_request.send_request(request, true).unwrap();
951 future::Either::B(future::ok(response))
952 }
953 })
954 }
955
956 fn h2api_response(response: Response<h2::RecvStream>) -> impl Future<Item=Value, Error=Error> {
957
958 let status = response.status();
959
960 let (_head, mut body) = response.into_parts();
961
962 // The `release_capacity` handle allows the caller to manage
963 // flow control.
964 //
965 // Whenever data is received, the caller is responsible for
966 // releasing capacity back to the server once it has freed
967 // the data from memory.
968 let mut release_capacity = body.release_capacity().clone();
969
970 body
971 .map(move |chunk| {
972 // Let the server send more data.
973 let _ = release_capacity.release_capacity(chunk.len());
974 chunk
975 })
976 .concat2()
977 .map_err(Error::from)
978 .and_then(move |data| {
979 let text = String::from_utf8(data.to_vec()).unwrap();
980 if status.is_success() {
981 if text.len() > 0 {
982 let mut value: Value = serde_json::from_str(&text)?;
983 if let Some(map) = value.as_object_mut() {
984 if let Some(data) = map.remove("data") {
985 return Ok(data);
986 }
987 }
988 bail!("got result without data property");
989 } else {
990 Ok(Value::Null)
991 }
992 } else {
993 bail!("HTTP Error {}: {}", status, text);
994 }
995 })
996 }
997
998 // Note: We always encode parameters with the url
999 pub fn request_builder(server: &str, method: &str, path: &str, data: Option<Value>) -> Result<Request<()>, Error> {
1000 let path = path.trim_matches('/');
1001
1002 if let Some(data) = data {
1003 let query = tools::json_object_to_query(data)?;
1004 // We detected problem with hyper around 6000 characters - seo we try to keep on the safe side
1005 if query.len() > 4096 { bail!("h2 query data too large ({} bytes) - please encode data inside body", query.len()); }
1006 let url: Uri = format!("https://{}:8007/{}?{}", server, path, query).parse()?;
1007 let request = Request::builder()
1008 .method(method)
1009 .uri(url)
1010 .header("User-Agent", "proxmox-backup-client/1.0")
1011 .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
1012 .body(())?;
1013 return Ok(request);
1014 } else {
1015 let url: Uri = format!("https://{}:8007/{}", server, path).parse()?;
1016 let request = Request::builder()
1017 .method(method)
1018 .uri(url)
1019 .header("User-Agent", "proxmox-backup-client/1.0")
1020 .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
1021 .body(())?;
1022
1023 Ok(request)
1024 }
1025 }
1026 }