use crate::messaging; //#[allow(non_upper_case_globals)] use crate::{ config::{MASTER_PORT, MASTER_SERVER}, messaging::{AsMsg, BaseMessage}, models, utils::{opt_to_string, VecDisplay}, UError, UResult, }; use reqwest::{Certificate, Client, Identity, RequestBuilder, Url}; use std::env; use u_api_proc_macro::api_route; use uuid::Uuid; const AGENT_IDENTITY: &[u8] = include_bytes!("../../../certs/alice.p12"); const ROOT_CA_CERT: &[u8] = include_bytes!("../../../certs/ca.crt"); pub struct ClientHandler { base_url: Url, client: Client, password: Option, } impl ClientHandler { pub fn new(server: Option<&str>) -> Self { let env_server = env::var("U_SERVER").unwrap_or(String::from(MASTER_SERVER)); let master_server = server.unwrap_or(env_server.as_str()); let identity = Identity::from_pkcs12_der(AGENT_IDENTITY, "").unwrap(); let client = Client::builder() .identity(identity) .add_root_certificate(Certificate::from_pem(ROOT_CA_CERT).unwrap()) .build() .unwrap(); Self { client, base_url: Url::parse(&format!("https://{}:{}", master_server, MASTER_PORT)).unwrap(), password: None, } } pub fn password(mut self, password: String) -> ClientHandler { self.password = Some(password); self } fn set_pwd(&self, rb: RequestBuilder) -> RequestBuilder { match &self.password { Some(p) => rb.bearer_auth(p), None => rb, } } fn build_get(&self, url: &str) -> RequestBuilder { let rb = self.client.get(self.base_url.join(url).unwrap()); self.set_pwd(rb) } fn build_post(&self, url: &str) -> RequestBuilder { let rb = self.client.post(self.base_url.join(url).unwrap()); self.set_pwd(rb) } // // get jobs for client #[api_route("GET")] async fn get_personal_jobs(&self, url_param: Option) -> VecDisplay {} // // send something to server #[api_route("POST")] async fn report(&self, payload: &[messaging::Reportable]) -> messaging::Empty {} // // download file #[api_route("GET")] async fn dl(&self, url_param: Option) -> Vec {} // // request download #[api_route("POST")] async fn dlr(&self, url_param: Option) -> messaging::DownloadInfo {} //##########// Admin area //##########// /// client listing #[api_route("GET")] async fn get_agents(&self, url_param: Option) -> VecDisplay {} // // get all available jobs #[api_route("GET")] async fn get_jobs(&self, url_param: Option) -> VecDisplay {} // // create and upload job #[api_route("POST")] async fn upload_jobs(&self, payload: &[models::JobMeta]) -> messaging::Empty {} // // delete something #[api_route("GET")] async fn del(&self, url_param: Option) -> i32 {} // // set jobs for any client #[api_route("POST")] async fn set_jobs(&self, url_param: Option, payload: &[String]) -> VecDisplay {} // // get jobs for any client #[api_route("GET")] async fn get_agent_jobs(&self, url_param: Option) -> VecDisplay {} }