]>
Commit | Line | Data |
---|---|---|
c2b94534 | 1 | use std::io::Write; |
db0cb9ce | 2 | use std::task::{Context, Poll}; |
d59dbeca | 3 | use std::sync::{Arc, Mutex}; |
597641fd | 4 | |
f7d4e4b5 | 5 | use anyhow::{bail, format_err, Error}; |
82ab7230 | 6 | use futures::*; |
7a57cb77 WB |
7 | use http::Uri; |
8 | use http::header::HeaderValue; | |
9 | use http::{Request, Response}; | |
10 | use hyper::Body; | |
1434f4f8 | 11 | use hyper::client::{Client, HttpConnector}; |
d59dbeca | 12 | use openssl::{ssl::{SslConnector, SslMethod}, x509::X509StoreContextRef}; |
ba3a60b2 | 13 | use serde_json::{json, Value}; |
8a1028e0 | 14 | use percent_encoding::percent_encode; |
7a57cb77 | 15 | use xdg::BaseDirectories; |
1fdb4c6f | 16 | |
501f4fa2 | 17 | use proxmox::{ |
91f5594c | 18 | api::error::HttpError, |
501f4fa2 DM |
19 | sys::linux::tty, |
20 | tools::{ | |
21 | fs::{file_get_json, replace_file, CreateOptions}, | |
22 | } | |
e18a6c9e DM |
23 | }; |
24 | ||
7a57cb77 | 25 | use super::pipe_to_stream::PipeToSendStream; |
e7cb4dc5 | 26 | use crate::api2::types::Userid; |
1434f4f8 | 27 | use crate::tools::async_io::EitherStream; |
501f4fa2 | 28 | use crate::tools::{self, BroadcastFuture, DEFAULT_ENCODE_SET}; |
986bef16 | 29 | |
5a2df000 | 30 | #[derive(Clone)] |
e240d8be | 31 | pub struct AuthInfo { |
3743dee6 DM |
32 | pub username: String, |
33 | pub ticket: String, | |
34 | pub token: String, | |
5a2df000 | 35 | } |
56458d97 | 36 | |
d59dbeca | 37 | pub struct HttpClientOptions { |
5030b7ce | 38 | prefix: Option<String>, |
d59dbeca DM |
39 | password: Option<String>, |
40 | fingerprint: Option<String>, | |
41 | interactive: bool, | |
42 | ticket_cache: bool, | |
5a74756c | 43 | fingerprint_cache: bool, |
d59dbeca DM |
44 | verify_cert: bool, |
45 | } | |
46 | ||
47 | impl HttpClientOptions { | |
48 | ||
49 | pub fn new() -> Self { | |
50 | Self { | |
5030b7ce | 51 | prefix: None, |
d59dbeca DM |
52 | password: None, |
53 | fingerprint: None, | |
54 | interactive: false, | |
55 | ticket_cache: false, | |
5a74756c | 56 | fingerprint_cache: false, |
d59dbeca DM |
57 | verify_cert: true, |
58 | } | |
59 | } | |
60 | ||
5030b7ce DM |
61 | pub fn prefix(mut self, prefix: Option<String>) -> Self { |
62 | self.prefix = prefix; | |
63 | self | |
64 | } | |
65 | ||
d59dbeca DM |
66 | pub fn password(mut self, password: Option<String>) -> Self { |
67 | self.password = password; | |
68 | self | |
69 | } | |
70 | ||
71 | pub fn fingerprint(mut self, fingerprint: Option<String>) -> Self { | |
72 | self.fingerprint = fingerprint; | |
73 | self | |
74 | } | |
75 | ||
76 | pub fn interactive(mut self, interactive: bool) -> Self { | |
77 | self.interactive = interactive; | |
78 | self | |
79 | } | |
80 | ||
81 | pub fn ticket_cache(mut self, ticket_cache: bool) -> Self { | |
82 | self.ticket_cache = ticket_cache; | |
83 | self | |
84 | } | |
85 | ||
5a74756c DM |
86 | pub fn fingerprint_cache(mut self, fingerprint_cache: bool) -> Self { |
87 | self.fingerprint_cache = fingerprint_cache; | |
88 | self | |
89 | } | |
90 | ||
d59dbeca DM |
91 | pub fn verify_cert(mut self, verify_cert: bool) -> Self { |
92 | self.verify_cert = verify_cert; | |
93 | self | |
94 | } | |
95 | } | |
96 | ||
151c6ce2 | 97 | /// HTTP(S) API client |
597641fd | 98 | pub struct HttpClient { |
1434f4f8 | 99 | client: Client<HttpsConnector>, |
597641fd | 100 | server: String, |
d59dbeca | 101 | fingerprint: Arc<Mutex<Option<String>>>, |
5a2df000 | 102 | auth: BroadcastFuture<AuthInfo>, |
d59dbeca | 103 | _options: HttpClientOptions, |
597641fd DM |
104 | } |
105 | ||
e240d8be | 106 | /// Delete stored ticket data (logout) |
e7cb4dc5 | 107 | pub fn delete_ticket_info(prefix: &str, server: &str, username: &Userid) -> Result<(), Error> { |
e240d8be | 108 | |
5030b7ce | 109 | let base = BaseDirectories::with_prefix(prefix)?; |
e240d8be DM |
110 | |
111 | // usually /run/user/<uid>/... | |
112 | let path = base.place_runtime_file("tickets")?; | |
113 | ||
114 | let mode = nix::sys::stat::Mode::from_bits_truncate(0o0600); | |
115 | ||
116 | let mut data = file_get_json(&path, Some(json!({})))?; | |
117 | ||
118 | if let Some(map) = data[server].as_object_mut() { | |
e7cb4dc5 | 119 | map.remove(username.as_str()); |
e240d8be DM |
120 | } |
121 | ||
feaa1ad3 | 122 | replace_file(path, data.to_string().as_bytes(), CreateOptions::new().perm(mode))?; |
e240d8be DM |
123 | |
124 | Ok(()) | |
125 | } | |
126 | ||
5030b7ce | 127 | fn store_fingerprint(prefix: &str, server: &str, fingerprint: &str) -> Result<(), Error> { |
5a74756c | 128 | |
5030b7ce | 129 | let base = BaseDirectories::with_prefix(prefix)?; |
5a74756c | 130 | |
5030b7ce | 131 | // usually ~/.config/<prefix>/fingerprints |
5a74756c DM |
132 | let path = base.place_config_file("fingerprints")?; |
133 | ||
134 | let raw = match std::fs::read_to_string(&path) { | |
135 | Ok(v) => v, | |
136 | Err(err) => { | |
137 | if err.kind() == std::io::ErrorKind::NotFound { | |
138 | String::new() | |
139 | } else { | |
140 | bail!("unable to read fingerprints from {:?} - {}", path, err); | |
141 | } | |
142 | } | |
143 | }; | |
144 | ||
145 | let mut result = String::new(); | |
146 | ||
147 | raw.split('\n').for_each(|line| { | |
148 | let items: Vec<String> = line.split_whitespace().map(String::from).collect(); | |
149 | if items.len() == 2 { | |
150 | if &items[0] == server { | |
151 | // found, add later with new fingerprint | |
152 | } else { | |
153 | result.push_str(line); | |
154 | result.push('\n'); | |
155 | } | |
156 | } | |
157 | }); | |
158 | ||
159 | result.push_str(server); | |
160 | result.push(' '); | |
161 | result.push_str(fingerprint); | |
162 | result.push('\n'); | |
163 | ||
164 | replace_file(path, result.as_bytes(), CreateOptions::new())?; | |
165 | ||
166 | Ok(()) | |
167 | } | |
168 | ||
5030b7ce | 169 | fn load_fingerprint(prefix: &str, server: &str) -> Option<String> { |
5a74756c | 170 | |
5030b7ce | 171 | let base = BaseDirectories::with_prefix(prefix).ok()?; |
5a74756c | 172 | |
5030b7ce | 173 | // usually ~/.config/<prefix>/fingerprints |
5a74756c DM |
174 | let path = base.place_config_file("fingerprints").ok()?; |
175 | ||
176 | let raw = std::fs::read_to_string(&path).ok()?; | |
177 | ||
178 | for line in raw.split('\n') { | |
179 | let items: Vec<String> = line.split_whitespace().map(String::from).collect(); | |
180 | if items.len() == 2 { | |
181 | if &items[0] == server { | |
182 | return Some(items[1].clone()); | |
183 | } | |
184 | } | |
185 | } | |
186 | ||
187 | None | |
188 | } | |
189 | ||
5030b7ce | 190 | fn store_ticket_info(prefix: &str, server: &str, username: &str, ticket: &str, token: &str) -> Result<(), Error> { |
ba3a60b2 | 191 | |
5030b7ce | 192 | let base = BaseDirectories::with_prefix(prefix)?; |
ba3a60b2 DM |
193 | |
194 | // usually /run/user/<uid>/... | |
195 | let path = base.place_runtime_file("tickets")?; | |
196 | ||
197 | let mode = nix::sys::stat::Mode::from_bits_truncate(0o0600); | |
198 | ||
e18a6c9e | 199 | let mut data = file_get_json(&path, Some(json!({})))?; |
ba3a60b2 | 200 | |
6a7be83e | 201 | let now = proxmox::tools::time::epoch_i64(); |
ba3a60b2 DM |
202 | |
203 | data[server][username] = json!({ "timestamp": now, "ticket": ticket, "token": token}); | |
204 | ||
205 | let mut new_data = json!({}); | |
206 | ||
207 | let ticket_lifetime = tools::ticket::TICKET_LIFETIME - 60; | |
208 | ||
209 | let empty = serde_json::map::Map::new(); | |
210 | for (server, info) in data.as_object().unwrap_or(&empty) { | |
211 | for (_user, uinfo) in info.as_object().unwrap_or(&empty) { | |
212 | if let Some(timestamp) = uinfo["timestamp"].as_i64() { | |
213 | let age = now - timestamp; | |
214 | if age < ticket_lifetime { | |
215 | new_data[server][username] = uinfo.clone(); | |
216 | } | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
feaa1ad3 | 221 | replace_file(path, new_data.to_string().as_bytes(), CreateOptions::new().perm(mode))?; |
ba3a60b2 DM |
222 | |
223 | Ok(()) | |
224 | } | |
225 | ||
e7cb4dc5 | 226 | fn load_ticket_info(prefix: &str, server: &str, userid: &Userid) -> Option<(String, String)> { |
5030b7ce | 227 | let base = BaseDirectories::with_prefix(prefix).ok()?; |
ba3a60b2 DM |
228 | |
229 | // usually /run/user/<uid>/... | |
66c8eb93 CE |
230 | let path = base.place_runtime_file("tickets").ok()?; |
231 | let data = file_get_json(&path, None).ok()?; | |
6a7be83e | 232 | let now = proxmox::tools::time::epoch_i64(); |
ba3a60b2 | 233 | let ticket_lifetime = tools::ticket::TICKET_LIFETIME - 60; |
e7cb4dc5 | 234 | let uinfo = data[server][userid.as_str()].as_object()?; |
66c8eb93 CE |
235 | let timestamp = uinfo["timestamp"].as_i64()?; |
236 | let age = now - timestamp; | |
237 | ||
238 | if age < ticket_lifetime { | |
239 | let ticket = uinfo["ticket"].as_str()?; | |
240 | let token = uinfo["token"].as_str()?; | |
241 | Some((ticket.to_owned(), token.to_owned())) | |
242 | } else { | |
243 | None | |
ba3a60b2 | 244 | } |
ba3a60b2 DM |
245 | } |
246 | ||
597641fd | 247 | impl HttpClient { |
e7cb4dc5 WB |
248 | pub fn new( |
249 | server: &str, | |
250 | userid: &Userid, | |
251 | mut options: HttpClientOptions, | |
252 | ) -> Result<Self, Error> { | |
d59dbeca DM |
253 | |
254 | let verified_fingerprint = Arc::new(Mutex::new(None)); | |
255 | ||
5a74756c | 256 | let mut fingerprint = options.fingerprint.take(); |
a6e3da98 DM |
257 | |
258 | if fingerprint.is_some() { | |
259 | // do not store fingerprints passed via options in cache | |
260 | options.fingerprint_cache = false; | |
261 | } else if options.fingerprint_cache && options.prefix.is_some() { | |
5030b7ce | 262 | fingerprint = load_fingerprint(options.prefix.as_ref().unwrap(), server); |
5a74756c DM |
263 | } |
264 | ||
5030b7ce DM |
265 | let mut ssl_connector_builder = SslConnector::builder(SslMethod::tls()).unwrap(); |
266 | ||
267 | if options.verify_cert { | |
268 | let server = server.to_string(); | |
269 | let verified_fingerprint = verified_fingerprint.clone(); | |
270 | let interactive = options.interactive; | |
271 | let fingerprint_cache = options.fingerprint_cache; | |
272 | let prefix = options.prefix.clone(); | |
273 | ssl_connector_builder.set_verify_callback(openssl::ssl::SslVerifyMode::PEER, move |valid, ctx| { | |
274 | let (valid, fingerprint) = Self::verify_callback(valid, ctx, fingerprint.clone(), interactive); | |
275 | if valid { | |
276 | if let Some(fingerprint) = fingerprint { | |
277 | if fingerprint_cache && prefix.is_some() { | |
278 | if let Err(err) = store_fingerprint( | |
279 | prefix.as_ref().unwrap(), &server, &fingerprint) { | |
280 | eprintln!("{}", err); | |
281 | } | |
282 | } | |
283 | *verified_fingerprint.lock().unwrap() = Some(fingerprint); | |
284 | } | |
285 | } | |
286 | valid | |
287 | }); | |
288 | } else { | |
289 | ssl_connector_builder.set_verify(openssl::ssl::SslVerifyMode::NONE); | |
290 | } | |
291 | ||
292 | let mut httpc = hyper::client::HttpConnector::new(); | |
293 | httpc.set_nodelay(true); // important for h2 download performance! | |
5030b7ce DM |
294 | httpc.enforce_http(false); // we want https... |
295 | ||
296 | let https = HttpsConnector::with_connector(httpc, ssl_connector_builder.build()); | |
297 | ||
298 | let client = Client::builder() | |
299 | //.http2_initial_stream_window_size( (1 << 31) - 2) | |
300 | //.http2_initial_connection_window_size( (1 << 31) - 2) | |
301 | .build::<_, Body>(https); | |
d59dbeca DM |
302 | |
303 | let password = options.password.take(); | |
5030b7ce | 304 | let use_ticket_cache = options.ticket_cache && options.prefix.is_some(); |
5a2df000 | 305 | |
cc2ce4a9 DM |
306 | let password = if let Some(password) = password { |
307 | password | |
45cdce06 | 308 | } else { |
d59dbeca | 309 | let mut ticket_info = None; |
5030b7ce | 310 | if use_ticket_cache { |
e7cb4dc5 | 311 | ticket_info = load_ticket_info(options.prefix.as_ref().unwrap(), server, userid); |
d59dbeca DM |
312 | } |
313 | if let Some((ticket, _token)) = ticket_info { | |
314 | ticket | |
315 | } else { | |
e7cb4dc5 | 316 | Self::get_password(userid, options.interactive)? |
d59dbeca | 317 | } |
45cdce06 DM |
318 | }; |
319 | ||
d59dbeca DM |
320 | let login_future = Self::credentials( |
321 | client.clone(), | |
322 | server.to_owned(), | |
e7cb4dc5 WB |
323 | userid.to_owned(), |
324 | password.to_owned(), | |
5030b7ce DM |
325 | ).map_ok({ |
326 | let server = server.to_string(); | |
327 | let prefix = options.prefix.clone(); | |
328 | ||
329 | move |auth| { | |
330 | if use_ticket_cache & &prefix.is_some() { | |
331 | let _ = store_ticket_info(prefix.as_ref().unwrap(), &server, &auth.username, &auth.ticket, &auth.token); | |
332 | } | |
333 | ||
334 | auth | |
335 | } | |
336 | }); | |
45cdce06 DM |
337 | |
338 | Ok(Self { | |
5a2df000 | 339 | client, |
597641fd | 340 | server: String::from(server), |
d59dbeca | 341 | fingerprint: verified_fingerprint, |
96f5e80a | 342 | auth: BroadcastFuture::new(Box::new(login_future)), |
d59dbeca | 343 | _options: options, |
45cdce06 | 344 | }) |
597641fd DM |
345 | } |
346 | ||
1a7a0e74 | 347 | /// Login |
e240d8be | 348 | /// |
add5861e | 349 | /// Login is done on demand, so this is only required if you need |
e240d8be | 350 | /// access to authentication data in 'AuthInfo'. |
96f5e80a DM |
351 | pub async fn login(&self) -> Result<AuthInfo, Error> { |
352 | self.auth.listen().await | |
e240d8be DM |
353 | } |
354 | ||
d59dbeca DM |
355 | /// Returns the optional fingerprint passed to the new() constructor. |
356 | pub fn fingerprint(&self) -> Option<String> { | |
357 | (*self.fingerprint.lock().unwrap()).clone() | |
358 | } | |
359 | ||
e7cb4dc5 | 360 | fn get_password(username: &Userid, interactive: bool) -> Result<String, Error> { |
56458d97 | 361 | // If we're on a TTY, query the user for a password |
d59dbeca | 362 | if interactive && tty::stdin_isatty() { |
99d863d7 DM |
363 | let msg = format!("Password for \"{}\": ", username); |
364 | return Ok(String::from_utf8(tty::read_password(&msg)?)?); | |
56458d97 WB |
365 | } |
366 | ||
367 | bail!("no password input mechanism available"); | |
368 | } | |
369 | ||
d59dbeca DM |
370 | fn verify_callback( |
371 | valid: bool, ctx: | |
372 | &mut X509StoreContextRef, | |
373 | expected_fingerprint: Option<String>, | |
374 | interactive: bool, | |
5030b7ce DM |
375 | ) -> (bool, Option<String>) { |
376 | if valid { return (true, None); } | |
d59dbeca DM |
377 | |
378 | let cert = match ctx.current_cert() { | |
379 | Some(cert) => cert, | |
5030b7ce | 380 | None => return (false, None), |
d59dbeca DM |
381 | }; |
382 | ||
383 | let depth = ctx.error_depth(); | |
5030b7ce | 384 | if depth != 0 { return (false, None); } |
d59dbeca DM |
385 | |
386 | let fp = match cert.digest(openssl::hash::MessageDigest::sha256()) { | |
387 | Ok(fp) => fp, | |
5030b7ce | 388 | Err(_) => return (false, None), // should not happen |
d59dbeca DM |
389 | }; |
390 | let fp_string = proxmox::tools::digest_to_hex(&fp); | |
391 | let fp_string = fp_string.as_bytes().chunks(2).map(|v| std::str::from_utf8(v).unwrap()) | |
392 | .collect::<Vec<&str>>().join(":"); | |
393 | ||
394 | if let Some(expected_fingerprint) = expected_fingerprint { | |
1bd6f32b | 395 | if expected_fingerprint.to_lowercase() == fp_string { |
5030b7ce | 396 | return (true, Some(fp_string)); |
d59dbeca | 397 | } else { |
5030b7ce | 398 | return (false, None); |
d59dbeca DM |
399 | } |
400 | } | |
401 | ||
402 | // If we're on a TTY, query the user | |
403 | if interactive && tty::stdin_isatty() { | |
404 | println!("fingerprint: {}", fp_string); | |
405 | loop { | |
a595f0fe | 406 | print!("Are you sure you want to continue connecting? (y/n): "); |
d59dbeca | 407 | let _ = std::io::stdout().flush(); |
a595f0fe WB |
408 | use std::io::{BufRead, BufReader}; |
409 | let mut line = String::new(); | |
410 | match BufReader::new(std::io::stdin()).read_line(&mut line) { | |
411 | Ok(_) => { | |
412 | let trimmed = line.trim(); | |
413 | if trimmed == "y" || trimmed == "Y" { | |
5030b7ce | 414 | return (true, Some(fp_string)); |
a595f0fe | 415 | } else if trimmed == "n" || trimmed == "N" { |
5030b7ce | 416 | return (false, None); |
a595f0fe WB |
417 | } else { |
418 | continue; | |
d59dbeca DM |
419 | } |
420 | } | |
a595f0fe | 421 | Err(_) => return (false, None), |
d59dbeca DM |
422 | } |
423 | } | |
424 | } | |
5030b7ce | 425 | (false, None) |
a6b75513 DM |
426 | } |
427 | ||
1a7a0e74 | 428 | pub async fn request(&self, mut req: Request<Body>) -> Result<Value, Error> { |
597641fd | 429 | |
5a2df000 | 430 | let client = self.client.clone(); |
597641fd | 431 | |
1a7a0e74 | 432 | let auth = self.login().await?; |
597641fd | 433 | |
1a7a0e74 DM |
434 | let enc_ticket = format!("PBSAuthCookie={}", percent_encode(auth.ticket.as_bytes(), DEFAULT_ENCODE_SET)); |
435 | req.headers_mut().insert("Cookie", HeaderValue::from_str(&enc_ticket).unwrap()); | |
436 | req.headers_mut().insert("CSRFPreventionToken", HeaderValue::from_str(&auth.token).unwrap()); | |
597641fd | 437 | |
1a7a0e74 | 438 | Self::api_request(client, req).await |
1fdb4c6f DM |
439 | } |
440 | ||
1a7a0e74 | 441 | pub async fn get( |
a6782ca1 WB |
442 | &self, |
443 | path: &str, | |
444 | data: Option<Value>, | |
1a7a0e74 | 445 | ) -> Result<Value, Error> { |
9e391bb7 | 446 | let req = Self::request_builder(&self.server, "GET", path, data).unwrap(); |
1a7a0e74 | 447 | self.request(req).await |
a6b75513 DM |
448 | } |
449 | ||
1a7a0e74 | 450 | pub async fn delete( |
a6782ca1 WB |
451 | &mut self, |
452 | path: &str, | |
453 | data: Option<Value>, | |
1a7a0e74 | 454 | ) -> Result<Value, Error> { |
9e391bb7 | 455 | let req = Self::request_builder(&self.server, "DELETE", path, data).unwrap(); |
1a7a0e74 | 456 | self.request(req).await |
a6b75513 DM |
457 | } |
458 | ||
1a7a0e74 | 459 | pub async fn post( |
a6782ca1 WB |
460 | &mut self, |
461 | path: &str, | |
462 | data: Option<Value>, | |
1a7a0e74 | 463 | ) -> Result<Value, Error> { |
5a2df000 | 464 | let req = Self::request_builder(&self.server, "POST", path, data).unwrap(); |
1a7a0e74 | 465 | self.request(req).await |
024f11bb DM |
466 | } |
467 | ||
1a7a0e74 | 468 | pub async fn download( |
a6782ca1 WB |
469 | &mut self, |
470 | path: &str, | |
1a7a0e74 | 471 | output: &mut (dyn Write + Send), |
3d571d55 | 472 | ) -> Result<(), Error> { |
5a2df000 | 473 | let mut req = Self::request_builder(&self.server, "GET", path, None).unwrap(); |
024f11bb | 474 | |
5a2df000 | 475 | let client = self.client.clone(); |
1fdb4c6f | 476 | |
1a7a0e74 | 477 | let auth = self.login().await?; |
81da38c1 | 478 | |
1a7a0e74 DM |
479 | let enc_ticket = format!("PBSAuthCookie={}", percent_encode(auth.ticket.as_bytes(), DEFAULT_ENCODE_SET)); |
480 | req.headers_mut().insert("Cookie", HeaderValue::from_str(&enc_ticket).unwrap()); | |
6f62c924 | 481 | |
1a7a0e74 DM |
482 | let resp = client.request(req).await?; |
483 | let status = resp.status(); | |
484 | if !status.is_success() { | |
485 | HttpClient::api_response(resp) | |
486 | .map(|_| Err(format_err!("unknown error"))) | |
487 | .await? | |
488 | } else { | |
489 | resp.into_body() | |
5a2df000 | 490 | .map_err(Error::from) |
1a7a0e74 DM |
491 | .try_fold(output, move |acc, chunk| async move { |
492 | acc.write_all(&chunk)?; | |
493 | Ok::<_, Error>(acc) | |
5a2df000 | 494 | }) |
1a7a0e74 DM |
495 | .await?; |
496 | } | |
497 | Ok(()) | |
6f62c924 DM |
498 | } |
499 | ||
1a7a0e74 | 500 | pub async fn upload( |
04512d30 DM |
501 | &mut self, |
502 | content_type: &str, | |
503 | body: Body, | |
504 | path: &str, | |
505 | data: Option<Value>, | |
1a7a0e74 | 506 | ) -> Result<Value, Error> { |
81da38c1 DM |
507 | |
508 | let path = path.trim_matches('/'); | |
04512d30 DM |
509 | let mut url = format!("https://{}:8007/{}", &self.server, path); |
510 | ||
511 | if let Some(data) = data { | |
512 | let query = tools::json_object_to_query(data).unwrap(); | |
513 | url.push('?'); | |
514 | url.push_str(&query); | |
515 | } | |
516 | ||
517 | let url: Uri = url.parse().unwrap(); | |
81da38c1 | 518 | |
5a2df000 | 519 | let req = Request::builder() |
81da38c1 DM |
520 | .method("POST") |
521 | .uri(url) | |
522 | .header("User-Agent", "proxmox-backup-client/1.0") | |
5a2df000 DM |
523 | .header("Content-Type", content_type) |
524 | .body(body).unwrap(); | |
81da38c1 | 525 | |
1a7a0e74 | 526 | self.request(req).await |
1fdb4c6f DM |
527 | } |
528 | ||
1a7a0e74 | 529 | pub async fn start_h2_connection( |
fb047083 DM |
530 | &self, |
531 | mut req: Request<Body>, | |
532 | protocol_name: String, | |
dc089345 | 533 | ) -> Result<(H2Client, futures::future::AbortHandle), Error> { |
cf639a47 | 534 | |
1a7a0e74 | 535 | let auth = self.login().await?; |
cf639a47 DM |
536 | let client = self.client.clone(); |
537 | ||
1a7a0e74 DM |
538 | let enc_ticket = format!("PBSAuthCookie={}", percent_encode(auth.ticket.as_bytes(), DEFAULT_ENCODE_SET)); |
539 | req.headers_mut().insert("Cookie", HeaderValue::from_str(&enc_ticket).unwrap()); | |
540 | req.headers_mut().insert("UPGRADE", HeaderValue::from_str(&protocol_name).unwrap()); | |
cf639a47 | 541 | |
1a7a0e74 DM |
542 | let resp = client.request(req).await?; |
543 | let status = resp.status(); | |
cf639a47 | 544 | |
1a7a0e74 | 545 | if status != http::StatusCode::SWITCHING_PROTOCOLS { |
ca611955 DM |
546 | Self::api_response(resp).await?; |
547 | bail!("unknown error"); | |
1a7a0e74 DM |
548 | } |
549 | ||
550 | let upgraded = resp | |
551 | .into_body() | |
552 | .on_upgrade() | |
553 | .await?; | |
554 | ||
555 | let max_window_size = (1 << 31) - 2; | |
556 | ||
557 | let (h2, connection) = h2::client::Builder::new() | |
558 | .initial_connection_window_size(max_window_size) | |
559 | .initial_window_size(max_window_size) | |
560 | .max_frame_size(4*1024*1024) | |
561 | .handshake(upgraded) | |
562 | .await?; | |
563 | ||
564 | let connection = connection | |
565 | .map_err(|_| panic!("HTTP/2.0 connection failed")); | |
566 | ||
dc089345 | 567 | let (connection, abort) = futures::future::abortable(connection); |
1a7a0e74 DM |
568 | // A cancellable future returns an Option which is None when cancelled and |
569 | // Some when it finished instead, since we don't care about the return type we | |
570 | // need to map it away: | |
571 | let connection = connection.map(|_| ()); | |
572 | ||
573 | // Spawn a new task to drive the connection state | |
db0cb9ce | 574 | tokio::spawn(connection); |
1a7a0e74 DM |
575 | |
576 | // Wait until the `SendRequest` handle has available capacity. | |
577 | let c = h2.ready().await?; | |
dc089345 | 578 | Ok((H2Client::new(c), abort)) |
cf639a47 DM |
579 | } |
580 | ||
9d35dbbb | 581 | async fn credentials( |
1434f4f8 | 582 | client: Client<HttpsConnector>, |
45cdce06 | 583 | server: String, |
e7cb4dc5 | 584 | username: Userid, |
45cdce06 | 585 | password: String, |
9d35dbbb DM |
586 | ) -> Result<AuthInfo, Error> { |
587 | let data = json!({ "username": username, "password": password }); | |
588 | let req = Self::request_builder(&server, "POST", "/api2/json/access/ticket", Some(data)).unwrap(); | |
589 | let cred = Self::api_request(client, req).await?; | |
590 | let auth = AuthInfo { | |
591 | username: cred["data"]["username"].as_str().unwrap().to_owned(), | |
592 | ticket: cred["data"]["ticket"].as_str().unwrap().to_owned(), | |
593 | token: cred["data"]["CSRFPreventionToken"].as_str().unwrap().to_owned(), | |
594 | }; | |
595 | ||
9d35dbbb | 596 | Ok(auth) |
ba3a60b2 DM |
597 | } |
598 | ||
a6782ca1 | 599 | async fn api_response(response: Response<Body>) -> Result<Value, Error> { |
d2c48afc | 600 | let status = response.status(); |
db0cb9ce | 601 | let data = hyper::body::to_bytes(response.into_body()).await?; |
a6782ca1 WB |
602 | |
603 | let text = String::from_utf8(data.to_vec()).unwrap(); | |
604 | if status.is_success() { | |
11377a47 DM |
605 | if text.is_empty() { |
606 | Ok(Value::Null) | |
607 | } else { | |
a6782ca1 WB |
608 | let value: Value = serde_json::from_str(&text)?; |
609 | Ok(value) | |
a6782ca1 WB |
610 | } |
611 | } else { | |
91f5594c | 612 | Err(Error::from(HttpError::new(status, text))) |
a6782ca1 | 613 | } |
d2c48afc DM |
614 | } |
615 | ||
1a7a0e74 | 616 | async fn api_request( |
1434f4f8 | 617 | client: Client<HttpsConnector>, |
5a2df000 | 618 | req: Request<Body> |
1a7a0e74 | 619 | ) -> Result<Value, Error> { |
ba3a60b2 | 620 | |
5a2df000 DM |
621 | client.request(req) |
622 | .map_err(Error::from) | |
d2c48afc | 623 | .and_then(Self::api_response) |
1a7a0e74 | 624 | .await |
0dffe3f9 DM |
625 | } |
626 | ||
9e490a74 DM |
627 | // Read-only access to server property |
628 | pub fn server(&self) -> &str { | |
629 | &self.server | |
630 | } | |
631 | ||
5a2df000 | 632 | pub fn request_builder(server: &str, method: &str, path: &str, data: Option<Value>) -> Result<Request<Body>, Error> { |
591f570b | 633 | let path = path.trim_matches('/'); |
5a2df000 DM |
634 | let url: Uri = format!("https://{}:8007/{}", server, path).parse()?; |
635 | ||
636 | if let Some(data) = data { | |
637 | if method == "POST" { | |
638 | let request = Request::builder() | |
639 | .method(method) | |
640 | .uri(url) | |
641 | .header("User-Agent", "proxmox-backup-client/1.0") | |
642 | .header(hyper::header::CONTENT_TYPE, "application/json") | |
643 | .body(Body::from(data.to_string()))?; | |
644 | return Ok(request); | |
645 | } else { | |
9e391bb7 DM |
646 | let query = tools::json_object_to_query(data)?; |
647 | let url: Uri = format!("https://{}:8007/{}?{}", server, path, query).parse()?; | |
648 | let request = Request::builder() | |
649 | .method(method) | |
650 | .uri(url) | |
651 | .header("User-Agent", "proxmox-backup-client/1.0") | |
652 | .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded") | |
653 | .body(Body::empty())?; | |
654 | return Ok(request); | |
5a2df000 | 655 | } |
5a2df000 | 656 | } |
0dffe3f9 | 657 | |
1fdb4c6f | 658 | let request = Request::builder() |
5a2df000 | 659 | .method(method) |
1fdb4c6f DM |
660 | .uri(url) |
661 | .header("User-Agent", "proxmox-backup-client/1.0") | |
5a2df000 DM |
662 | .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded") |
663 | .body(Body::empty())?; | |
1fdb4c6f | 664 | |
5a2df000 | 665 | Ok(request) |
597641fd DM |
666 | } |
667 | } | |
b57cb264 | 668 | |
9af37c8f DM |
669 | |
670 | #[derive(Clone)] | |
671 | pub struct H2Client { | |
672 | h2: h2::client::SendRequest<bytes::Bytes>, | |
673 | } | |
674 | ||
675 | impl H2Client { | |
676 | ||
677 | pub fn new(h2: h2::client::SendRequest<bytes::Bytes>) -> Self { | |
678 | Self { h2 } | |
679 | } | |
680 | ||
2a1e6d7d DM |
681 | pub async fn get( |
682 | &self, | |
683 | path: &str, | |
684 | param: Option<Value> | |
685 | ) -> Result<Value, Error> { | |
792a70b9 | 686 | let req = Self::request_builder("localhost", "GET", path, param, None).unwrap(); |
2a1e6d7d | 687 | self.request(req).await |
9af37c8f DM |
688 | } |
689 | ||
2a1e6d7d DM |
690 | pub async fn put( |
691 | &self, | |
692 | path: &str, | |
693 | param: Option<Value> | |
694 | ) -> Result<Value, Error> { | |
792a70b9 | 695 | let req = Self::request_builder("localhost", "PUT", path, param, None).unwrap(); |
2a1e6d7d | 696 | self.request(req).await |
9af37c8f DM |
697 | } |
698 | ||
2a1e6d7d DM |
699 | pub async fn post( |
700 | &self, | |
701 | path: &str, | |
702 | param: Option<Value> | |
703 | ) -> Result<Value, Error> { | |
792a70b9 | 704 | let req = Self::request_builder("localhost", "POST", path, param, None).unwrap(); |
2a1e6d7d | 705 | self.request(req).await |
9af37c8f DM |
706 | } |
707 | ||
d4a085e5 | 708 | pub async fn download<W: Write + Send>( |
a6782ca1 WB |
709 | &self, |
710 | path: &str, | |
711 | param: Option<Value>, | |
2a1e6d7d | 712 | mut output: W, |
3d571d55 | 713 | ) -> Result<(), Error> { |
792a70b9 | 714 | let request = Self::request_builder("localhost", "GET", path, param, None).unwrap(); |
dd066d28 | 715 | |
2a1e6d7d | 716 | let response_future = self.send_request(request, None).await?; |
984a7c35 | 717 | |
2a1e6d7d DM |
718 | let resp = response_future.await?; |
719 | ||
720 | let status = resp.status(); | |
721 | if !status.is_success() { | |
44f59dc7 DM |
722 | H2Client::h2api_response(resp).await?; // raise error |
723 | unreachable!(); | |
2a1e6d7d DM |
724 | } |
725 | ||
726 | let mut body = resp.into_body(); | |
db0cb9ce WB |
727 | while let Some(chunk) = body.data().await { |
728 | let chunk = chunk?; | |
729 | body.flow_control().release_capacity(chunk.len())?; | |
2a1e6d7d DM |
730 | output.write_all(&chunk)?; |
731 | } | |
732 | ||
3d571d55 | 733 | Ok(()) |
dd066d28 DM |
734 | } |
735 | ||
2a1e6d7d | 736 | pub async fn upload( |
a6782ca1 | 737 | &self, |
f011dba0 | 738 | method: &str, // POST or PUT |
a6782ca1 WB |
739 | path: &str, |
740 | param: Option<Value>, | |
792a70b9 | 741 | content_type: &str, |
a6782ca1 | 742 | data: Vec<u8>, |
2a1e6d7d | 743 | ) -> Result<Value, Error> { |
f011dba0 | 744 | let request = Self::request_builder("localhost", method, path, param, Some(content_type)).unwrap(); |
9af37c8f | 745 | |
2a1e6d7d DM |
746 | let mut send_request = self.h2.clone().ready().await?; |
747 | ||
748 | let (response, stream) = send_request.send_request(request, false).unwrap(); | |
2a05048b DM |
749 | |
750 | PipeToSendStream::new(bytes::Bytes::from(data), stream).await?; | |
751 | ||
752 | response | |
753 | .map_err(Error::from) | |
754 | .and_then(Self::h2api_response) | |
2a1e6d7d | 755 | .await |
9af37c8f | 756 | } |
adec8ea2 | 757 | |
2a1e6d7d | 758 | async fn request( |
9af37c8f | 759 | &self, |
b57cb264 | 760 | request: Request<()>, |
2a1e6d7d | 761 | ) -> Result<Value, Error> { |
b57cb264 | 762 | |
9af37c8f | 763 | self.send_request(request, None) |
82ab7230 DM |
764 | .and_then(move |response| { |
765 | response | |
766 | .map_err(Error::from) | |
767 | .and_then(Self::h2api_response) | |
768 | }) | |
2a1e6d7d | 769 | .await |
82ab7230 DM |
770 | } |
771 | ||
cf9271e2 | 772 | pub fn send_request( |
9af37c8f | 773 | &self, |
82ab7230 DM |
774 | request: Request<()>, |
775 | data: Option<bytes::Bytes>, | |
a6782ca1 | 776 | ) -> impl Future<Output = Result<h2::client::ResponseFuture, Error>> { |
82ab7230 | 777 | |
9af37c8f | 778 | self.h2.clone() |
10130cf4 DM |
779 | .ready() |
780 | .map_err(Error::from) | |
2a05048b | 781 | .and_then(move |mut send_request| async move { |
82ab7230 DM |
782 | if let Some(data) = data { |
783 | let (response, stream) = send_request.send_request(request, false).unwrap(); | |
2a05048b DM |
784 | PipeToSendStream::new(data, stream).await?; |
785 | Ok(response) | |
82ab7230 DM |
786 | } else { |
787 | let (response, _stream) = send_request.send_request(request, true).unwrap(); | |
2a05048b | 788 | Ok(response) |
82ab7230 | 789 | } |
b57cb264 DM |
790 | }) |
791 | } | |
792 | ||
f16aea68 | 793 | pub async fn h2api_response( |
a6782ca1 | 794 | response: Response<h2::RecvStream>, |
9edd3bf1 | 795 | ) -> Result<Value, Error> { |
b57cb264 DM |
796 | let status = response.status(); |
797 | ||
798 | let (_head, mut body) = response.into_parts(); | |
799 | ||
9edd3bf1 | 800 | let mut data = Vec::new(); |
db0cb9ce WB |
801 | while let Some(chunk) = body.data().await { |
802 | let chunk = chunk?; | |
803 | // Whenever data is received, the caller is responsible for | |
804 | // releasing capacity back to the server once it has freed | |
805 | // the data from memory. | |
9edd3bf1 | 806 | // Let the server send more data. |
db0cb9ce | 807 | body.flow_control().release_capacity(chunk.len())?; |
9edd3bf1 DM |
808 | data.extend(chunk); |
809 | } | |
810 | ||
811 | let text = String::from_utf8(data.to_vec()).unwrap(); | |
812 | if status.is_success() { | |
11377a47 DM |
813 | if text.is_empty() { |
814 | Ok(Value::Null) | |
815 | } else { | |
9edd3bf1 DM |
816 | let mut value: Value = serde_json::from_str(&text)?; |
817 | if let Some(map) = value.as_object_mut() { | |
818 | if let Some(data) = map.remove("data") { | |
819 | return Ok(data); | |
b57cb264 | 820 | } |
b57cb264 | 821 | } |
9edd3bf1 | 822 | bail!("got result without data property"); |
9edd3bf1 DM |
823 | } |
824 | } else { | |
91f5594c | 825 | Err(Error::from(HttpError::new(status, text))) |
9edd3bf1 | 826 | } |
b57cb264 DM |
827 | } |
828 | ||
eb2bdd1b | 829 | // Note: We always encode parameters with the url |
792a70b9 DM |
830 | pub fn request_builder( |
831 | server: &str, | |
832 | method: &str, | |
833 | path: &str, | |
834 | param: Option<Value>, | |
835 | content_type: Option<&str>, | |
836 | ) -> Result<Request<()>, Error> { | |
b57cb264 | 837 | let path = path.trim_matches('/'); |
b57cb264 | 838 | |
792a70b9 DM |
839 | let content_type = content_type.unwrap_or("application/x-www-form-urlencoded"); |
840 | ||
a55b2975 DM |
841 | if let Some(param) = param { |
842 | let query = tools::json_object_to_query(param)?; | |
eb2bdd1b DM |
843 | // We detected problem with hyper around 6000 characters - seo we try to keep on the safe side |
844 | if query.len() > 4096 { bail!("h2 query data too large ({} bytes) - please encode data inside body", query.len()); } | |
b57cb264 | 845 | let url: Uri = format!("https://{}:8007/{}?{}", server, path, query).parse()?; |
eb2bdd1b | 846 | let request = Request::builder() |
b57cb264 DM |
847 | .method(method) |
848 | .uri(url) | |
849 | .header("User-Agent", "proxmox-backup-client/1.0") | |
792a70b9 | 850 | .header(hyper::header::CONTENT_TYPE, content_type) |
b57cb264 | 851 | .body(())?; |
62ee2eb4 | 852 | Ok(request) |
eb2bdd1b DM |
853 | } else { |
854 | let url: Uri = format!("https://{}:8007/{}", server, path).parse()?; | |
855 | let request = Request::builder() | |
856 | .method(method) | |
857 | .uri(url) | |
858 | .header("User-Agent", "proxmox-backup-client/1.0") | |
792a70b9 | 859 | .header(hyper::header::CONTENT_TYPE, content_type) |
eb2bdd1b | 860 | .body(())?; |
b57cb264 | 861 | |
eb2bdd1b DM |
862 | Ok(request) |
863 | } | |
b57cb264 DM |
864 | } |
865 | } | |
1434f4f8 | 866 | |
db0cb9ce | 867 | #[derive(Clone)] |
1434f4f8 WB |
868 | pub struct HttpsConnector { |
869 | http: HttpConnector, | |
db0cb9ce | 870 | ssl_connector: std::sync::Arc<SslConnector>, |
1434f4f8 WB |
871 | } |
872 | ||
873 | impl HttpsConnector { | |
874 | pub fn with_connector(mut http: HttpConnector, ssl_connector: SslConnector) -> Self { | |
875 | http.enforce_http(false); | |
876 | ||
877 | Self { | |
878 | http, | |
db0cb9ce | 879 | ssl_connector: std::sync::Arc::new(ssl_connector), |
1434f4f8 WB |
880 | } |
881 | } | |
882 | } | |
883 | ||
884 | type MaybeTlsStream = EitherStream< | |
885 | tokio::net::TcpStream, | |
886 | tokio_openssl::SslStream<tokio::net::TcpStream>, | |
887 | >; | |
888 | ||
db0cb9ce WB |
889 | impl hyper::service::Service<Uri> for HttpsConnector { |
890 | type Response = MaybeTlsStream; | |
1434f4f8 | 891 | type Error = Error; |
db0cb9ce WB |
892 | type Future = std::pin::Pin<Box< |
893 | dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static | |
894 | >>; | |
1434f4f8 | 895 | |
db0cb9ce WB |
896 | fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
897 | // This connector is always ready, but others might not be. | |
898 | Poll::Ready(Ok(())) | |
899 | } | |
1434f4f8 | 900 | |
db0cb9ce WB |
901 | fn call(&mut self, dst: Uri) -> Self::Future { |
902 | let mut this = self.clone(); | |
903 | async move { | |
904 | let is_https = dst | |
905 | .scheme() | |
906 | .ok_or_else(|| format_err!("missing URL scheme"))? | |
907 | == "https"; | |
908 | let host = dst | |
909 | .host() | |
910 | .ok_or_else(|| format_err!("missing hostname in destination url?"))? | |
911 | .to_string(); | |
912 | ||
913 | let config = this.ssl_connector.configure(); | |
914 | let conn = this.http.call(dst).await?; | |
1434f4f8 WB |
915 | if is_https { |
916 | let conn = tokio_openssl::connect(config?, &host, conn).await?; | |
db0cb9ce | 917 | Ok(MaybeTlsStream::Right(conn)) |
1434f4f8 | 918 | } else { |
db0cb9ce | 919 | Ok(MaybeTlsStream::Left(conn)) |
1434f4f8 | 920 | } |
db0cb9ce | 921 | }.boxed() |
1434f4f8 WB |
922 | } |
923 | } |