You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.3 KiB

use crate::messaging;
//#[allow(non_upper_case_globals)]
4 years ago
use crate::{
config::{MASTER_PORT, MASTER_SERVER},
messaging::{AsMsg, BaseMessage},
models,
utils::{opt_to_string, VecDisplay},
UError, UResult,
};
3 years ago
use reqwest::{Certificate, Client, Identity, RequestBuilder, Url};
use std::env;
use u_api_proc_macro::api_route;
use uuid::Uuid;
4 years ago
3 years ago
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<String>,
}
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());
3 years ago
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 {
3 years ago
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<Uuid>) -> VecDisplay<models::AssignedJob> {}
//
// send something to server
#[api_route("POST")]
async fn report<M: AsMsg>(&self, payload: &M) -> messaging::Empty {}
//
// download file
#[api_route("GET")]
async fn dl(&self, url_param: Option<Uuid>) -> Vec<u8> {}
//
// request download
#[api_route("POST")]
async fn dlr(&self, url_param: Option<String>) -> messaging::DownloadInfo {}
//##########// Admin area //##########//
/// client listing
#[api_route("GET")]
async fn get_agents(&self, url_param: Option<Uuid>) -> VecDisplay<models::Agent> {}
//
// get all available jobs
#[api_route("GET")]
async fn get_jobs(&self, url_param: Option<Uuid>) -> VecDisplay<models::JobMeta> {}
//
// 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<Uuid>) -> i32 {}
//
3 years ago
// set jobs for any client
#[api_route("POST")]
async fn set_jobs(&self, url_param: Option<Uuid>, payload: &[String]) -> VecDisplay<Uuid> {}
3 years ago
//
// get jobs for any client
#[api_route("GET")]
async fn get_agent_jobs(&self, url_param: Option<Uuid>) -> VecDisplay<models::AssignedJob> {}
}