|
|
@ -1,6 +1,6 @@ |
|
|
|
use std::collections::HashMap; |
|
|
|
|
|
|
|
use std::fmt::Debug; |
|
|
|
use std::fmt::Debug; |
|
|
|
use std::net::SocketAddr; |
|
|
|
use std::net::SocketAddr; |
|
|
|
|
|
|
|
use std::{collections::HashMap, time::Duration}; |
|
|
|
|
|
|
|
|
|
|
|
use anyhow::{Context, Result}; |
|
|
|
use anyhow::{Context, Result}; |
|
|
|
use reqwest::{header, header::HeaderMap, Certificate, Client, Identity, Method, Url}; |
|
|
|
use reqwest::{header, header::HeaderMap, Certificate, Client, Identity, Method, Url}; |
|
|
@ -11,7 +11,7 @@ use crate::{ |
|
|
|
config::{get_self_id, MASTER_PORT}, |
|
|
|
config::{get_self_id, MASTER_PORT}, |
|
|
|
conv::opt_to_string, |
|
|
|
conv::opt_to_string, |
|
|
|
messaging::{self, AsMsg}, |
|
|
|
messaging::{self, AsMsg}, |
|
|
|
misc::OneOrVec, |
|
|
|
misc::OneOrVecRef, |
|
|
|
models::*, |
|
|
|
models::*, |
|
|
|
types::Id, |
|
|
|
types::Id, |
|
|
|
UError, UResult, |
|
|
|
UError, UResult, |
|
|
@ -21,12 +21,12 @@ const AGENT_IDENTITY: &[u8] = include_bytes!("../../../certs/alice.p12"); |
|
|
|
const ROOT_CA_CERT: &[u8] = include_bytes!("../../../certs/ca.crt"); |
|
|
|
const ROOT_CA_CERT: &[u8] = include_bytes!("../../../certs/ca.crt"); |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)] |
|
|
|
#[derive(Clone, Debug)] |
|
|
|
pub struct ClientHandler { |
|
|
|
pub struct HttpClient { |
|
|
|
base_url: Url, |
|
|
|
base_url: Url, |
|
|
|
client: Client, |
|
|
|
client: Client, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl ClientHandler { |
|
|
|
impl HttpClient { |
|
|
|
pub async fn new(server: &str, password: Option<String>) -> UResult<Self> { |
|
|
|
pub async fn new(server: &str, password: Option<String>) -> UResult<Self> { |
|
|
|
let identity = Identity::from_pkcs12_der(AGENT_IDENTITY, "").unwrap(); |
|
|
|
let identity = Identity::from_pkcs12_der(AGENT_IDENTITY, "").unwrap(); |
|
|
|
let mut default_headers = |
|
|
|
let mut default_headers = |
|
|
@ -41,7 +41,8 @@ impl ClientHandler { |
|
|
|
let client = Client::builder() |
|
|
|
let client = Client::builder() |
|
|
|
.identity(identity) |
|
|
|
.identity(identity) |
|
|
|
.default_headers(HeaderMap::try_from(&default_headers).unwrap()) |
|
|
|
.default_headers(HeaderMap::try_from(&default_headers).unwrap()) |
|
|
|
.add_root_certificate(Certificate::from_pem(ROOT_CA_CERT).unwrap()); |
|
|
|
.add_root_certificate(Certificate::from_pem(ROOT_CA_CERT).unwrap()) |
|
|
|
|
|
|
|
.timeout(Duration::from_secs(20)); |
|
|
|
|
|
|
|
|
|
|
|
let dns_response = Client::new() |
|
|
|
let dns_response = Client::new() |
|
|
|
.request( |
|
|
|
.request( |
|
|
@ -76,18 +77,18 @@ impl ClientHandler { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async fn req<R: AsMsg + DeserializeOwned>(&self, url: impl AsRef<str>) -> Result<R> { |
|
|
|
async fn req<R: AsMsg + DeserializeOwned>(&self, url: impl AsRef<str>) -> Result<R> { |
|
|
|
self.req_with_payload(url, ()).await |
|
|
|
self.req_with_payload(url, &()).await |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async fn req_with_payload<P: AsMsg, R: AsMsg + DeserializeOwned>( |
|
|
|
async fn req_with_payload<P: AsMsg, R: AsMsg + DeserializeOwned>( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
url: impl AsRef<str>, |
|
|
|
url: impl AsRef<str>, |
|
|
|
payload: P, |
|
|
|
payload: &P, |
|
|
|
) -> Result<R> { |
|
|
|
) -> Result<R> { |
|
|
|
let request = self |
|
|
|
let request = self |
|
|
|
.client |
|
|
|
.client |
|
|
|
.post(self.base_url.join(url.as_ref()).unwrap()) |
|
|
|
.post(self.base_url.join(url.as_ref()).unwrap()) |
|
|
|
.json(&payload); |
|
|
|
.json(payload); |
|
|
|
|
|
|
|
|
|
|
|
let response = request |
|
|
|
let response = request |
|
|
|
.send() |
|
|
|
.send() |
|
|
@ -112,23 +113,24 @@ impl ClientHandler { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// get jobs for client
|
|
|
|
// get jobs for client
|
|
|
|
pub async fn get_personal_jobs(&self, url_param: Id) -> Result<Vec<AssignedJobById>> { |
|
|
|
pub async fn get_personal_jobs(&self, agent_id: Id) -> Result<Vec<AssignedJobById>> { |
|
|
|
self.req(format!("get_personal_jobs/{}", url_param)).await |
|
|
|
self.req(format!("get_personal_jobs/{}", agent_id)).await |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// send something to server
|
|
|
|
// send something to server
|
|
|
|
pub async fn report(&self, payload: impl OneOrVec<messaging::Reportable>) -> Result<()> { |
|
|
|
pub async fn report(&self, payload: impl OneOrVecRef<messaging::Reportable>) -> Result<()> { |
|
|
|
self.req_with_payload("report", payload.into_vec()).await |
|
|
|
self.req_with_payload("report", &payload.as_vec()).await |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// download file
|
|
|
|
// download payload
|
|
|
|
pub async fn dl(&self, file: String) -> Result<Vec<u8>> { |
|
|
|
pub async fn dl(&self, file: &str) -> Result<Vec<u8>> { |
|
|
|
self.req(format!("dl/{file}")).await |
|
|
|
self.req(format!("dl/{file}")).await |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// get exact job
|
|
|
|
/// get exact job
|
|
|
|
pub async fn get_job(&self, job: Id) -> Result<FatJob> { |
|
|
|
pub async fn get_job(&self, job: Id, force_payload: bool) -> Result<FatJob> { |
|
|
|
self.req(format!("get_job/{job}")).await |
|
|
|
self.req(format!("get_job/{job}?force_payload={force_payload}")) |
|
|
|
|
|
|
|
.await |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// get all available jobs
|
|
|
|
/// get all available jobs
|
|
|
@ -139,7 +141,7 @@ impl ClientHandler { |
|
|
|
|
|
|
|
|
|
|
|
//##########// Admin area //##########//
|
|
|
|
//##########// Admin area //##########//
|
|
|
|
#[cfg(feature = "panel")] |
|
|
|
#[cfg(feature = "panel")] |
|
|
|
impl ClientHandler { |
|
|
|
impl HttpClient { |
|
|
|
/// agent listing
|
|
|
|
/// agent listing
|
|
|
|
pub async fn get_agents(&self, agent: Option<Id>) -> Result<Vec<Agent>> { |
|
|
|
pub async fn get_agents(&self, agent: Option<Id>) -> Result<Vec<Agent>> { |
|
|
|
self.req(format!("get_agents/{}", opt_to_string(agent))) |
|
|
|
self.req(format!("get_agents/{}", opt_to_string(agent))) |
|
|
@ -147,23 +149,23 @@ impl ClientHandler { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// update agent
|
|
|
|
/// update agent
|
|
|
|
pub async fn update_agent(&self, agent: Agent) -> Result<()> { |
|
|
|
pub async fn update_agent(&self, agent: &Agent) -> Result<()> { |
|
|
|
self.req_with_payload("update_agent", agent).await |
|
|
|
self.req_with_payload("update_agent", agent).await |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// update job
|
|
|
|
/// update job
|
|
|
|
pub async fn update_job(&self, job: FatJob) -> Result<()> { |
|
|
|
pub async fn update_job(&self, job: &FatJob) -> Result<()> { |
|
|
|
self.req_with_payload("update_job", job).await |
|
|
|
self.req_with_payload("update_job", job).await |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// update result
|
|
|
|
/// update result
|
|
|
|
pub async fn update_result(&self, result: AssignedJob) -> Result<()> { |
|
|
|
pub async fn update_result(&self, result: &AssignedJob) -> Result<()> { |
|
|
|
self.req_with_payload("update_result", result).await |
|
|
|
self.req_with_payload("update_result", result).await |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// create and upload job
|
|
|
|
/// create and upload job
|
|
|
|
pub async fn upload_jobs(&self, payload: impl OneOrVec<FatJob>) -> Result<Vec<Id>> { |
|
|
|
pub async fn upload_jobs(&self, payload: impl OneOrVecRef<FatJob>) -> Result<Vec<Id>> { |
|
|
|
self.req_with_payload("upload_jobs", payload.into_vec()) |
|
|
|
self.req_with_payload("upload_jobs", &payload.as_vec()) |
|
|
|
.await |
|
|
|
.await |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -173,8 +175,12 @@ impl ClientHandler { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// set jobs for any agent
|
|
|
|
/// set jobs for any agent
|
|
|
|
pub async fn set_jobs(&self, agent: Id, job_idents: impl OneOrVec<String>) -> Result<Vec<Id>> { |
|
|
|
pub async fn set_jobs( |
|
|
|
self.req_with_payload(format!("set_jobs/{agent}"), job_idents.into_vec()) |
|
|
|
&self, |
|
|
|
|
|
|
|
agent: Id, |
|
|
|
|
|
|
|
job_idents: impl OneOrVecRef<String>, |
|
|
|
|
|
|
|
) -> Result<Vec<Id>> { |
|
|
|
|
|
|
|
self.req_with_payload(format!("set_jobs/{agent}"), &job_idents.as_vec()) |
|
|
|
.await |
|
|
|
.await |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|