2019-01-17 10:29:38 +00:00
|
|
|
use failure::*;
|
|
|
|
|
|
|
|
use http::Uri;
|
|
|
|
use hyper::Body;
|
|
|
|
use hyper::client::Client;
|
|
|
|
use hyper::rt::{self, Future};
|
|
|
|
|
2019-01-21 17:56:48 +00:00
|
|
|
use http::Request;
|
|
|
|
use futures::stream::Stream;
|
|
|
|
|
|
|
|
use serde_json::{Value};
|
2019-02-13 13:31:43 +00:00
|
|
|
use url::percent_encoding::{percent_encode, DEFAULT_ENCODE_SET};
|
2019-01-21 17:56:48 +00:00
|
|
|
|
2019-02-14 10:11:39 +00:00
|
|
|
/// HTTP(S) API client
|
2019-01-17 10:29:38 +00:00
|
|
|
pub struct HttpClient {
|
2019-02-13 13:31:43 +00:00
|
|
|
username: String,
|
2019-01-17 10:29:38 +00:00
|
|
|
server: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HttpClient {
|
|
|
|
|
2019-02-13 13:31:43 +00:00
|
|
|
pub fn new(server: &str, username: &str) -> Self {
|
2019-01-17 10:29:38 +00:00
|
|
|
Self {
|
|
|
|
server: String::from(server),
|
2019-02-13 13:31:43 +00:00
|
|
|
username: String::from(username),
|
2019-01-17 10:29:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 10:03:02 +00:00
|
|
|
fn run_request(
|
|
|
|
request: Request<Body>,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
let mut builder = native_tls::TlsConnector::builder();
|
|
|
|
// FIXME: We need a CLI option for this!
|
|
|
|
builder.danger_accept_invalid_certs(true);
|
|
|
|
let tlsconnector = builder.build()?;
|
|
|
|
let mut httpc = hyper::client::HttpConnector::new(1);
|
|
|
|
httpc.enforce_http(false); // we want https...
|
|
|
|
let mut https = hyper_tls::HttpsConnector::from((httpc, tlsconnector));
|
|
|
|
https.https_only(true); // force it!
|
|
|
|
let client = Client::builder().build::<_, Body>(https);
|
2019-01-17 10:29:38 +00:00
|
|
|
|
2019-01-22 09:36:04 +00:00
|
|
|
let (tx, rx) = std::sync::mpsc::channel();
|
2019-01-17 10:29:38 +00:00
|
|
|
|
|
|
|
let future = client
|
|
|
|
.request(request)
|
2019-02-18 12:21:25 +00:00
|
|
|
.map_err(Error::from)
|
2019-01-17 10:29:38 +00:00
|
|
|
.and_then(|resp| {
|
|
|
|
|
|
|
|
let status = resp.status();
|
|
|
|
|
2019-02-18 12:21:25 +00:00
|
|
|
resp.into_body().concat2().map_err(Error::from)
|
2019-01-17 10:29:38 +00:00
|
|
|
.and_then(move |data| {
|
|
|
|
|
|
|
|
let text = String::from_utf8(data.to_vec()).unwrap();
|
|
|
|
if status.is_success() {
|
2019-01-21 17:56:48 +00:00
|
|
|
if text.len() > 0 {
|
|
|
|
let value: Value = serde_json::from_str(&text)?;
|
|
|
|
Ok(value)
|
|
|
|
} else {
|
|
|
|
Ok(Value::Null)
|
|
|
|
}
|
2019-01-17 10:29:38 +00:00
|
|
|
} else {
|
2019-01-21 17:56:48 +00:00
|
|
|
bail!("HTTP Error {}: {}", status, text);
|
2019-01-17 10:29:38 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2019-01-22 09:36:04 +00:00
|
|
|
.then(move |res| {
|
|
|
|
tx.send(res).unwrap();
|
|
|
|
Ok(())
|
2019-01-17 10:29:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// drop client, else client keeps connectioon open (keep-alive feature)
|
|
|
|
drop(client);
|
|
|
|
|
|
|
|
rt::run(future);
|
|
|
|
|
2019-01-22 09:36:04 +00:00
|
|
|
rx.recv().unwrap()
|
2019-01-21 17:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, path: &str) -> Result<Value, Error> {
|
|
|
|
|
2019-02-18 05:24:28 +00:00
|
|
|
let path = path.trim_matches('/');
|
2019-02-13 10:03:02 +00:00
|
|
|
let url: Uri = format!("https://{}:8007/{}", self.server, path).parse()?;
|
2019-01-21 17:56:48 +00:00
|
|
|
|
2019-02-18 10:01:49 +00:00
|
|
|
let (ticket, _token) = self.login()?;
|
2019-02-13 13:31:43 +00:00
|
|
|
|
|
|
|
let enc_ticket = percent_encode(ticket.as_bytes(), DEFAULT_ENCODE_SET).to_string();
|
|
|
|
|
2019-01-21 17:56:48 +00:00
|
|
|
let request = Request::builder()
|
|
|
|
.method("GET")
|
|
|
|
.uri(url)
|
|
|
|
.header("User-Agent", "proxmox-backup-client/1.0")
|
2019-02-13 13:31:43 +00:00
|
|
|
.header("Cookie", format!("PBSAuthCookie={}", enc_ticket))
|
2019-01-21 17:56:48 +00:00
|
|
|
.body(Body::empty())?;
|
|
|
|
|
|
|
|
Self::run_request(request)
|
|
|
|
}
|
|
|
|
|
2019-02-18 10:01:49 +00:00
|
|
|
fn login(&self) -> Result<(String, String), Error> {
|
2019-02-13 13:31:43 +00:00
|
|
|
|
|
|
|
let url: Uri = format!("https://{}:8007/{}", self.server, "/api2/json/access/ticket").parse()?;
|
|
|
|
|
|
|
|
let password = match std::env::var("PBS_PASSWORD") {
|
|
|
|
Ok(p) => p,
|
|
|
|
Err(err) => bail!("missing passphrase - {}", err),
|
|
|
|
};
|
|
|
|
|
|
|
|
let query = url::form_urlencoded::Serializer::new(String::new())
|
|
|
|
.append_pair("username", &self.username)
|
|
|
|
.append_pair("password", &password)
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
let request = Request::builder()
|
|
|
|
.method("POST")
|
|
|
|
.uri(url)
|
|
|
|
.header("User-Agent", "proxmox-backup-client/1.0")
|
|
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
.body(Body::from(query))?;
|
|
|
|
|
|
|
|
let auth_res = Self::run_request(request)?;
|
|
|
|
|
|
|
|
let ticket = match auth_res["data"]["ticket"].as_str() {
|
|
|
|
Some(t) => t,
|
|
|
|
None => bail!("got unexpected respose for login request."),
|
|
|
|
};
|
2019-02-18 10:01:49 +00:00
|
|
|
let token = match auth_res["data"]["CSRFPreventionToken"].as_str() {
|
|
|
|
Some(t) => t,
|
|
|
|
None => bail!("got unexpected respose for login request."),
|
|
|
|
};
|
2019-02-13 13:31:43 +00:00
|
|
|
|
2019-02-18 10:01:49 +00:00
|
|
|
Ok((ticket.to_owned(), token.to_owned()))
|
2019-02-13 13:31:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 17:56:48 +00:00
|
|
|
pub fn upload(&self, content_type: &str, body: Body, path: &str) -> Result<Value, Error> {
|
|
|
|
|
2019-02-18 05:24:28 +00:00
|
|
|
let path = path.trim_matches('/');
|
2019-02-13 10:03:02 +00:00
|
|
|
let url: Uri = format!("https://{}:8007/{}", self.server, path).parse()?;
|
2019-01-21 17:56:48 +00:00
|
|
|
|
2019-02-18 10:01:49 +00:00
|
|
|
let (ticket, token) = self.login()?;
|
2019-02-13 13:31:43 +00:00
|
|
|
|
|
|
|
let enc_ticket = percent_encode(ticket.as_bytes(), DEFAULT_ENCODE_SET).to_string();
|
|
|
|
|
2019-01-21 17:56:48 +00:00
|
|
|
let request = Request::builder()
|
|
|
|
.method("POST")
|
|
|
|
.uri(url)
|
|
|
|
.header("User-Agent", "proxmox-backup-client/1.0")
|
2019-02-13 13:31:43 +00:00
|
|
|
.header("Cookie", format!("PBSAuthCookie={}", enc_ticket))
|
2019-02-18 10:01:49 +00:00
|
|
|
.header("CSRFPreventionToken", token)
|
2019-01-21 17:56:48 +00:00
|
|
|
.header("Content-Type", content_type)
|
|
|
|
.body(body)?;
|
|
|
|
|
|
|
|
Self::run_request(request)
|
2019-01-17 10:29:38 +00:00
|
|
|
}
|
|
|
|
}
|