From 352dd535e0386d899e816ac5f597e583d1ade768 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Wed, 10 Apr 2024 12:06:45 +0200 Subject: Closes #8. 0.4.0 seems to work --- src/config.rs | 22 ++++++++++++++++++++-- src/main.rs | 15 ++++++++++++--- src/requests/device.rs | 7 +++---- src/requests/start.rs | 8 ++++---- 4 files changed, 39 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/config.rs b/src/config.rs index 769269c..01ab097 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,8 +2,20 @@ use serde::Deserialize; #[derive(Deserialize)] pub struct Config { - pub apikey: String, pub server: String, + pub auth: Auth, +} + +#[derive(Deserialize)] +pub struct Auth { + pub method: Method, + pub secret: String, +} + +#[derive(PartialEq, Eq, Deserialize)] +pub enum Method { + None, + Key, } impl Config { @@ -12,9 +24,15 @@ impl Config { let builder = config::Config::builder(); + let builder = builder + .set_default("auth.method", "none")? + .set_default("auth.secret", "")?; + let builder = if let Some(conf) = config_dir { let dir = conf.to_string_lossy(); - builder.add_source(config::File::with_name(format!("{dir}/webol-cli").as_str()).required(false)) + builder.add_source( + config::File::with_name(format!("{dir}/webol-cli").as_str()).required(false), + ) } else { println!("!No config dir found"); builder diff --git a/src/main.rs b/src/main.rs index 5a0931d..2726a5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use std::{fmt::Display, time::Duration}; use crate::config::Config; use clap::{Command, CommandFactory, Parser, Subcommand}; use clap_complete::{generate, Generator, Shell}; +use config::Method; use error::Error; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use requests::{device, start::start}; @@ -117,13 +118,21 @@ fn default_headers(config: &Config) -> Result { let mut map = HeaderMap::new(); map.append("Accept-Content", HeaderValue::from_str("application/json")?); map.append("Content-Type", HeaderValue::from_str("application/json")?); - map.append("Authorization", HeaderValue::from_str(&config.apikey)?); + if config.auth.method != Method::None { + map.append("Authorization", HeaderValue::from_str(&config.auth.secret)?); + + } Ok(map) } -fn format_url(config: &Config, path: &str, protocol: &Protocols) -> String { - format!("{}://{}/{}", protocol, config.server, path) +fn format_url(config: &Config, path: &str, protocol: &Protocols, id: Option<&str>) -> String { + if let Some(id) = id { + format!("{}://{}/{}/{}", protocol, config.server, path, id) + + } else { + format!("{}://{}/{}", protocol, config.server, path) + } } async fn check_success(res: Response) -> Result { diff --git a/src/requests/device.rs b/src/requests/device.rs index 7583406..2606579 100644 --- a/src/requests/device.rs +++ b/src/requests/device.rs @@ -7,7 +7,7 @@ pub async fn put( broadcast_addr: String, ip: String, ) -> Result<(), Error> { - let url = format_url(config, "device", &Protocols::Http); + let url = format_url(config, "device", &Protocols::Http, None); println!("{url}"); let res = reqwest::Client::new() .put(url) @@ -25,9 +25,8 @@ pub async fn put( pub async fn get(config: &Config, id: String) -> Result<(), Error> { let res = reqwest::Client::new() - .get(format_url(config, "device", &Protocols::Http)) + .get(format_url(config, "device", &Protocols::Http, Some(&id))) .headers(default_headers(config)?) - .body(format!(r#"{{"id": "{id}"}}"#)) .send() .await?; @@ -44,7 +43,7 @@ pub async fn post( ip: String, ) -> Result<(), Error> { let res = reqwest::Client::new() - .post(format_url(config, "device", &Protocols::Http)) + .post(format_url(config, "device", &Protocols::Http, None)) .headers(default_headers(config)?) .body(format!( r#"{{"id": "{id}", "mac": "{mac}", "broadcast_addr": "{broadcast_addr}", "ip": "{ip}"}}"#, diff --git a/src/requests/start.rs b/src/requests/start.rs index 3afbe3a..1ec3ce8 100644 --- a/src/requests/start.rs +++ b/src/requests/start.rs @@ -17,12 +17,12 @@ pub async fn start(config: &Config, id: String, ping: bool) -> Result<(), Error> let send_start = MultiProgress::new(); let overview = add_pb(&send_start, OVERVIEW_STYLE, format!(") start {id}")); - let url = format_url(config, "start", &Protocols::Http); + let url = format_url(config, "start", &Protocols::Http, Some(&id)); let connect = add_pb(&send_start, DEFAULT_STYLE, format!("connect to {url}")); let res = reqwest::Client::new() .post(url) .headers(default_headers(config)?) - .body(format!(r#"{{"id": "{id}", "ping": {ping}}}"#)) + .body(format!(r#"{{"ping": {ping}}}"#)) .send() .await?; finish_pb(&connect, "connected, got response".to_string(), DONE_STYLE); @@ -71,8 +71,8 @@ async fn status_socket( let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); let request = Request::builder() - .uri(format_url(config, "status", &Protocols::Websocket)) - .header("Authorization", &config.apikey) + .uri(format_url(config, "status", &Protocols::Websocket, None)) + .header("Authorization", &config.auth.secret) .header("sec-websocket-key", "") .header("host", &config.server) .header("upgrade", "websocket") -- cgit v1.2.3