2 use std
::task
::{Context, Poll}
;
8 use http
::header
::HeaderValue
;
9 use http
::{Request, Response}
;
11 use hyper
::client
::{Client, HttpConnector}
;
12 use openssl
::ssl
::{SslConnector, SslMethod}
;
13 use serde_json
::{json, Value}
;
14 use percent_encoding
::percent_encode
;
15 use xdg
::BaseDirectories
;
18 fs
::{file_get_json, replace_file, CreateOptions}
,
21 use super::pipe_to_stream
::PipeToSendStream
;
22 use crate::tools
::async_io
::EitherStream
;
23 use crate::tools
::{self, tty, BroadcastFuture, DEFAULT_ENCODE_SET}
;
32 /// HTTP(S) API client
33 pub struct HttpClient
{
34 client
: Client
<HttpsConnector
>,
36 auth
: BroadcastFuture
<AuthInfo
>,
39 /// Delete stored ticket data (logout)
40 pub fn delete_ticket_info(server
: &str, username
: &str) -> Result
<(), Error
> {
42 let base
= BaseDirectories
::with_prefix("proxmox-backup")?
;
44 // usually /run/user/<uid>/...
45 let path
= base
.place_runtime_file("tickets")?
;
47 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0600);
49 let mut data
= file_get_json(&path
, Some(json
!({}
)))?
;
51 if let Some(map
) = data
[server
].as_object_mut() {
55 replace_file(path
, data
.to_string().as_bytes(), CreateOptions
::new().perm(mode
))?
;
60 fn store_ticket_info(server
: &str, username
: &str, ticket
: &str, token
: &str) -> Result
<(), Error
> {
62 let base
= BaseDirectories
::with_prefix("proxmox-backup")?
;
64 // usually /run/user/<uid>/...
65 let path
= base
.place_runtime_file("tickets")?
;
67 let mode
= nix
::sys
::stat
::Mode
::from_bits_truncate(0o0600);
69 let mut data
= file_get_json(&path
, Some(json
!({}
)))?
;
71 let now
= Utc
::now().timestamp();
73 data
[server
][username
] = json
!({ "timestamp": now, "ticket": ticket, "token": token}
);
75 let mut new_data
= json
!({}
);
77 let ticket_lifetime
= tools
::ticket
::TICKET_LIFETIME
- 60;
79 let empty
= serde_json
::map
::Map
::new();
80 for (server
, info
) in data
.as_object().unwrap_or(&empty
) {
81 for (_user
, uinfo
) in info
.as_object().unwrap_or(&empty
) {
82 if let Some(timestamp
) = uinfo
["timestamp"].as_i64() {
83 let age
= now
- timestamp
;
84 if age
< ticket_lifetime
{
85 new_data
[server
][username
] = uinfo
.clone();
91 replace_file(path
, new_data
.to_string().as_bytes(), CreateOptions
::new().perm(mode
))?
;
96 fn load_ticket_info(server
: &str, username
: &str) -> Option
<(String
, String
)> {
97 let base
= BaseDirectories
::with_prefix("proxmox-backup").ok()?
;
99 // usually /run/user/<uid>/...
100 let path
= base
.place_runtime_file("tickets").ok()?
;
101 let data
= file_get_json(&path
, None
).ok()?
;
102 let now
= Utc
::now().timestamp();
103 let ticket_lifetime
= tools
::ticket
::TICKET_LIFETIME
- 60;
104 let uinfo
= data
[server
][username
].as_object()?
;
105 let timestamp
= uinfo
["timestamp"].as_i64()?
;
106 let age
= now
- timestamp
;
108 if age
< ticket_lifetime
{
109 let ticket
= uinfo
["ticket"].as_str()?
;
110 let token
= uinfo
["token"].as_str()?
;
111 Some((ticket
.to_owned(), token
.to_owned()))
119 pub fn new(server
: &str, username
: &str, password
: Option
<String
>) -> Result
<Self, Error
> {
120 let client
= Self::build_client();
122 let password
= if let Some(password
) = password
{
124 } else if let Some((ticket
, _token
)) = load_ticket_info(server
, username
) {
127 Self::get_password(&username
)?
130 let login_future
= Self::credentials(client
.clone(), server
.to_owned(), username
.to_owned(), password
);
134 server
: String
::from(server
),
135 auth
: BroadcastFuture
::new(Box
::new(login_future
)),
141 /// Login is done on demand, so this is onyl required if you need
142 /// access to authentication data in 'AuthInfo'.
143 pub async
fn login(&self) -> Result
<AuthInfo
, Error
> {
144 self.auth
.listen().await
147 fn get_password(_username
: &str) -> Result
<String
, Error
> {
148 use std
::env
::VarError
::*;
149 match std
::env
::var("PBS_PASSWORD") {
150 Ok(p
) => return Ok(p
),
151 Err(NotUnicode(_
)) => bail
!("PBS_PASSWORD contains bad characters"),
153 // Try another method
157 // If we're on a TTY, query the user for a password
158 if tty
::stdin_isatty() {
159 return Ok(String
::from_utf8(tty
::read_password("Password: ")?
)?
);
162 bail
!("no password input mechanism available");
165 fn build_client() -> Client
<HttpsConnector
> {
167 let mut ssl_connector_builder
= SslConnector
::builder(SslMethod
::tls()).unwrap();
169 ssl_connector_builder
.set_verify(openssl
::ssl
::SslVerifyMode
::NONE
); // fixme!
171 let mut httpc
= hyper
::client
::HttpConnector
::new();
172 httpc
.set_nodelay(true); // important for h2 download performance!
173 httpc
.set_recv_buffer_size(Some(1024*1024)); //important for h2 download performance!
174 httpc
.enforce_http(false); // we want https...
176 let https
= HttpsConnector
::with_connector(httpc
, ssl_connector_builder
.build());
179 //.http2_initial_stream_window_size( (1 << 31) - 2)
180 //.http2_initial_connection_window_size( (1 << 31) - 2)
181 .build
::<_
, Body
>(https
)
184 pub async
fn request(&self, mut req
: Request
<Body
>) -> Result
<Value
, Error
> {
186 let client
= self.client
.clone();
188 let auth
= self.login().await?
;
190 let enc_ticket
= format
!("PBSAuthCookie={}", percent_encode(auth
.ticket
.as_bytes(), DEFAULT_ENCODE_SET
));
191 req
.headers_mut().insert("Cookie", HeaderValue
::from_str(&enc_ticket
).unwrap());
192 req
.headers_mut().insert("CSRFPreventionToken", HeaderValue
::from_str(&auth
.token
).unwrap());
194 Self::api_request(client
, req
).await
201 ) -> Result
<Value
, Error
> {
202 let req
= Self::request_builder(&self.server
, "GET", path
, data
).unwrap();
203 self.request(req
).await
210 ) -> Result
<Value
, Error
> {
211 let req
= Self::request_builder(&self.server
, "DELETE", path
, data
).unwrap();
212 self.request(req
).await
219 ) -> Result
<Value
, Error
> {
220 let req
= Self::request_builder(&self.server
, "POST", path
, data
).unwrap();
221 self.request(req
).await
224 pub async
fn download(
227 output
: &mut (dyn Write
+ Send
),
228 ) -> Result
<(), Error
> {
229 let mut req
= Self::request_builder(&self.server
, "GET", path
, None
).unwrap();
231 let client
= self.client
.clone();
233 let auth
= self.login().await?
;
235 let enc_ticket
= format
!("PBSAuthCookie={}", percent_encode(auth
.ticket
.as_bytes(), DEFAULT_ENCODE_SET
));
236 req
.headers_mut().insert("Cookie", HeaderValue
::from_str(&enc_ticket
).unwrap());
238 let resp
= client
.request(req
).await?
;
239 let status
= resp
.status();
240 if !status
.is_success() {
241 HttpClient
::api_response(resp
)
242 .map(|_
| Err(format_err
!("unknown error")))
246 .map_err(Error
::from
)
247 .try_fold(output
, move |acc
, chunk
| async
move {
248 acc
.write_all(&chunk
)?
;
262 ) -> Result
<Value
, Error
> {
264 let path
= path
.trim_matches('
/'
);
265 let mut url
= format
!("https://{}:8007/{}", &self.server
, path
);
267 if let Some(data
) = data
{
268 let query
= tools
::json_object_to_query(data
).unwrap();
270 url
.push_str(&query
);
273 let url
: Uri
= url
.parse().unwrap();
275 let req
= Request
::builder()
278 .header("User-Agent", "proxmox-backup-client/1.0")
279 .header("Content-Type", content_type
)
280 .body(body
).unwrap();
282 self.request(req
).await
285 pub async
fn start_h2_connection(
287 mut req
: Request
<Body
>,
288 protocol_name
: String
,
289 ) -> Result
<(H2Client
, futures
::future
::AbortHandle
), Error
> {
291 let auth
= self.login().await?
;
292 let client
= self.client
.clone();
294 let enc_ticket
= format
!("PBSAuthCookie={}", percent_encode(auth
.ticket
.as_bytes(), DEFAULT_ENCODE_SET
));
295 req
.headers_mut().insert("Cookie", HeaderValue
::from_str(&enc_ticket
).unwrap());
296 req
.headers_mut().insert("UPGRADE", HeaderValue
::from_str(&protocol_name
).unwrap());
298 let resp
= client
.request(req
).await?
;
299 let status
= resp
.status();
301 if status
!= http
::StatusCode
::SWITCHING_PROTOCOLS
{
302 Self::api_response(resp
).await?
;
303 bail
!("unknown error");
311 let max_window_size
= (1 << 31) - 2;
313 let (h2
, connection
) = h2
::client
::Builder
::new()
314 .initial_connection_window_size(max_window_size
)
315 .initial_window_size(max_window_size
)
316 .max_frame_size(4*1024*1024)
320 let connection
= connection
321 .map_err(|_
| panic
!("HTTP/2.0 connection failed"));
323 let (connection
, abort
) = futures
::future
::abortable(connection
);
324 // A cancellable future returns an Option which is None when cancelled and
325 // Some when it finished instead, since we don't care about the return type we
326 // need to map it away:
327 let connection
= connection
.map(|_
| ());
329 // Spawn a new task to drive the connection state
330 tokio
::spawn(connection
);
332 // Wait until the `SendRequest` handle has available capacity.
333 let c
= h2
.ready().await?
;
334 Ok((H2Client
::new(c
), abort
))
337 async
fn credentials(
338 client
: Client
<HttpsConnector
>,
342 ) -> Result
<AuthInfo
, Error
> {
343 let data
= json
!({ "username": username, "password": password }
);
344 let req
= Self::request_builder(&server
, "POST", "/api2/json/access/ticket", Some(data
)).unwrap();
345 let cred
= Self::api_request(client
, req
).await?
;
346 let auth
= AuthInfo
{
347 username
: cred
["data"]["username"].as_str().unwrap().to_owned(),
348 ticket
: cred
["data"]["ticket"].as_str().unwrap().to_owned(),
349 token
: cred
["data"]["CSRFPreventionToken"].as_str().unwrap().to_owned(),
352 let _
= store_ticket_info(&server
, &auth
.username
, &auth
.ticket
, &auth
.token
);
357 async
fn api_response(response
: Response
<Body
>) -> Result
<Value
, Error
> {
358 let status
= response
.status();
359 let data
= hyper
::body
::to_bytes(response
.into_body()).await?
;
361 let text
= String
::from_utf8(data
.to_vec()).unwrap();
362 if status
.is_success() {
366 let value
: Value
= serde_json
::from_str(&text
)?
;
370 bail
!("HTTP Error {}: {}", status
, text
);
374 async
fn api_request(
375 client
: Client
<HttpsConnector
>,
377 ) -> Result
<Value
, Error
> {
380 .map_err(Error
::from
)
381 .and_then(Self::api_response
)
385 // Read-only access to server property
386 pub fn server(&self) -> &str {
390 pub fn request_builder(server
: &str, method
: &str, path
: &str, data
: Option
<Value
>) -> Result
<Request
<Body
>, Error
> {
391 let path
= path
.trim_matches('
/'
);
392 let url
: Uri
= format
!("https://{}:8007/{}", server
, path
).parse()?
;
394 if let Some(data
) = data
{
395 if method
== "POST" {
396 let request
= Request
::builder()
399 .header("User-Agent", "proxmox-backup-client/1.0")
400 .header(hyper
::header
::CONTENT_TYPE
, "application/json")
401 .body(Body
::from(data
.to_string()))?
;
404 let query
= tools
::json_object_to_query(data
)?
;
405 let url
: Uri
= format
!("https://{}:8007/{}?{}", server
, path
, query
).parse()?
;
406 let request
= Request
::builder()
409 .header("User-Agent", "proxmox-backup-client/1.0")
410 .header(hyper
::header
::CONTENT_TYPE
, "application/x-www-form-urlencoded")
411 .body(Body
::empty())?
;
416 let request
= Request
::builder()
419 .header("User-Agent", "proxmox-backup-client/1.0")
420 .header(hyper
::header
::CONTENT_TYPE
, "application/x-www-form-urlencoded")
421 .body(Body
::empty())?
;
429 pub struct H2Client
{
430 h2
: h2
::client
::SendRequest
<bytes
::Bytes
>,
435 pub fn new(h2
: h2
::client
::SendRequest
<bytes
::Bytes
>) -> Self {
443 ) -> Result
<Value
, Error
> {
444 let req
= Self::request_builder("localhost", "GET", path
, param
, None
).unwrap();
445 self.request(req
).await
452 ) -> Result
<Value
, Error
> {
453 let req
= Self::request_builder("localhost", "PUT", path
, param
, None
).unwrap();
454 self.request(req
).await
461 ) -> Result
<Value
, Error
> {
462 let req
= Self::request_builder("localhost", "POST", path
, param
, None
).unwrap();
463 self.request(req
).await
466 pub async
fn download
<W
: Write
+ Send
>(
469 param
: Option
<Value
>,
471 ) -> Result
<W
, Error
> {
472 let request
= Self::request_builder("localhost", "GET", path
, param
, None
).unwrap();
474 let response_future
= self.send_request(request
, None
).await?
;
476 let resp
= response_future
.await?
;
478 let status
= resp
.status();
479 if !status
.is_success() {
480 H2Client
::h2api_response(resp
).await?
; // raise error
484 let mut body
= resp
.into_body();
485 while let Some(chunk
) = body
.data().await
{
487 body
.flow_control().release_capacity(chunk
.len())?
;
488 output
.write_all(&chunk
)?
;
496 method
: &str, // POST or PUT
498 param
: Option
<Value
>,
501 ) -> Result
<Value
, Error
> {
502 let request
= Self::request_builder("localhost", method
, path
, param
, Some(content_type
)).unwrap();
504 let mut send_request
= self.h2
.clone().ready().await?
;
506 let (response
, stream
) = send_request
.send_request(request
, false).unwrap();
508 PipeToSendStream
::new(bytes
::Bytes
::from(data
), stream
).await?
;
511 .map_err(Error
::from
)
512 .and_then(Self::h2api_response
)
518 request
: Request
<()>,
519 ) -> Result
<Value
, Error
> {
521 self.send_request(request
, None
)
522 .and_then(move |response
| {
524 .map_err(Error
::from
)
525 .and_then(Self::h2api_response
)
532 request
: Request
<()>,
533 data
: Option
<bytes
::Bytes
>,
534 ) -> impl Future
<Output
= Result
<h2
::client
::ResponseFuture
, Error
>> {
538 .map_err(Error
::from
)
539 .and_then(move |mut send_request
| async
move {
540 if let Some(data
) = data
{
541 let (response
, stream
) = send_request
.send_request(request
, false).unwrap();
542 PipeToSendStream
::new(data
, stream
).await?
;
545 let (response
, _stream
) = send_request
.send_request(request
, true).unwrap();
551 pub async
fn h2api_response(
552 response
: Response
<h2
::RecvStream
>,
553 ) -> Result
<Value
, Error
> {
554 let status
= response
.status();
556 let (_head
, mut body
) = response
.into_parts();
558 let mut data
= Vec
::new();
559 while let Some(chunk
) = body
.data().await
{
561 // Whenever data is received, the caller is responsible for
562 // releasing capacity back to the server once it has freed
563 // the data from memory.
564 // Let the server send more data.
565 body
.flow_control().release_capacity(chunk
.len())?
;
569 let text
= String
::from_utf8(data
.to_vec()).unwrap();
570 if status
.is_success() {
574 let mut value
: Value
= serde_json
::from_str(&text
)?
;
575 if let Some(map
) = value
.as_object_mut() {
576 if let Some(data
) = map
.remove("data") {
580 bail
!("got result without data property");
583 bail
!("HTTP Error {}: {}", status
, text
);
587 // Note: We always encode parameters with the url
588 pub fn request_builder(
592 param
: Option
<Value
>,
593 content_type
: Option
<&str>,
594 ) -> Result
<Request
<()>, Error
> {
595 let path
= path
.trim_matches('
/'
);
597 let content_type
= content_type
.unwrap_or("application/x-www-form-urlencoded");
599 if let Some(param
) = param
{
600 let query
= tools
::json_object_to_query(param
)?
;
601 // We detected problem with hyper around 6000 characters - seo we try to keep on the safe side
602 if query
.len() > 4096 { bail!("h2 query data too large ({} bytes
) - please encode data inside body
", query.len()); }
603 let url: Uri = format!("https
://{}:8007/{}?{}", server, path, query).parse()?;
604 let request
= Request
::builder()
607 .header("User-Agent", "proxmox-backup-client/1.0")
608 .header(hyper
::header
::CONTENT_TYPE
, content_type
)
612 let url
: Uri
= format
!("https://{}:8007/{}", server
, path
).parse()?
;
613 let request
= Request
::builder()
616 .header("User-Agent", "proxmox-backup-client/1.0")
617 .header(hyper
::header
::CONTENT_TYPE
, content_type
)
626 pub struct HttpsConnector
{
628 ssl_connector
: std
::sync
::Arc
<SslConnector
>,
631 impl HttpsConnector
{
632 pub fn with_connector(mut http
: HttpConnector
, ssl_connector
: SslConnector
) -> Self {
633 http
.enforce_http(false);
637 ssl_connector
: std
::sync
::Arc
::new(ssl_connector
),
642 type MaybeTlsStream
= EitherStream
<
643 tokio
::net
::TcpStream
,
644 tokio_openssl
::SslStream
<tokio
::net
::TcpStream
>,
647 impl hyper
::service
::Service
<Uri
> for HttpsConnector
{
648 type Response
= MaybeTlsStream
;
650 type Future
= std
::pin
::Pin
<Box
<
651 dyn Future
<Output
= Result
<Self::Response
, Self::Error
>> + Send
+ '
static
654 fn poll_ready(&mut self, _
: &mut Context
<'_
>) -> Poll
<Result
<(), Self::Error
>> {
655 // This connector is always ready, but others might not be.
659 fn call(&mut self, dst
: Uri
) -> Self::Future
{
660 let mut this
= self.clone();
664 .ok_or_else(|| format_err
!("missing URL scheme"))?
668 .ok_or_else(|| format_err
!("missing hostname in destination url?"))?
671 let config
= this
.ssl_connector
.configure();
672 let conn
= this
.http
.call(dst
).await?
;
674 let conn
= tokio_openssl
::connect(config?
, &host
, conn
).await?
;
675 Ok(MaybeTlsStream
::Right(conn
))
677 Ok(MaybeTlsStream
::Left(conn
))