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 --- Cargo.lock | 103 +++++++++++++++++-------------------------------- Cargo.toml | 2 +- src/config.rs | 22 ++++++++++- src/main.rs | 15 +++++-- src/requests/device.rs | 7 ++-- src/requests/start.rs | 8 ++-- 6 files changed, 76 insertions(+), 81 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25f23fe..56afa07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -527,29 +527,23 @@ checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" [[package]] name = "h2" -version = "0.3.21" +version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91fc23aa11be92976ef4729127f1a74adf36d8436f7816b185d18df956790833" +checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8" dependencies = [ "bytes", "fnv", "futures-core", "futures-sink", "futures-util", - "http 0.2.10", - "indexmap 1.9.3", + "http 0.2.12", + "indexmap", "slab", "tokio", "tokio-util", "tracing", ] -[[package]] -name = "hashbrown" -version = "0.12.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" - [[package]] name = "hashbrown" version = "0.13.2" @@ -576,9 +570,9 @@ checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" [[package]] name = "http" -version = "0.2.10" +version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f95b9abcae896730d42b78e09c155ed4ddf82c07b4de772c64aee5b2d8b7c150" +checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1" dependencies = [ "bytes", "fnv", @@ -598,12 +592,12 @@ dependencies = [ [[package]] name = "http-body" -version = "0.4.5" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" dependencies = [ "bytes", - "http 0.2.10", + "http 0.2.12", "pin-project-lite", ] @@ -621,22 +615,22 @@ checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" [[package]] name = "hyper" -version = "0.14.27" +version = "0.14.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffb1cfd654a8219eaef89881fdb3bb3b1cdc5fa75ded05d6933b2b382e395468" +checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" dependencies = [ "bytes", "futures-channel", "futures-core", "futures-util", "h2", - "http 0.2.10", + "http 0.2.12", "http-body", "httparse", "httpdate", "itoa", "pin-project-lite", - "socket2 0.4.10", + "socket2", "tokio", "tower-service", "tracing", @@ -666,16 +660,6 @@ dependencies = [ "unicode-normalization", ] -[[package]] -name = "indexmap" -version = "1.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" -dependencies = [ - "autocfg", - "hashbrown 0.12.3", -] - [[package]] name = "indexmap" version = "2.2.3" @@ -1095,9 +1079,9 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.11.22" +version = "0.11.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "046cd98826c46c2ac8ddecae268eb5c2e58628688a5fc7a2643704a73faba95b" +checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62" dependencies = [ "base64", "bytes", @@ -1105,7 +1089,7 @@ dependencies = [ "futures-core", "futures-util", "h2", - "http 0.2.10", + "http 0.2.12", "http-body", "hyper", "hyper-tls", @@ -1117,9 +1101,11 @@ dependencies = [ "once_cell", "percent-encoding", "pin-project-lite", + "rustls-pemfile", "serde", "serde_json", "serde_urlencoded", + "sync_wrapper", "system-configuration", "tokio", "tokio-native-tls", @@ -1172,6 +1158,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "rustls-pemfile" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" +dependencies = [ + "base64", +] + [[package]] name = "ryu" version = "1.0.15" @@ -1293,16 +1288,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "socket2" -version = "0.4.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d" -dependencies = [ - "libc", - "winapi", -] - [[package]] name = "socket2" version = "0.5.5" @@ -1330,6 +1315,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + [[package]] name = "system-configuration" version = "0.5.1" @@ -1420,7 +1411,7 @@ dependencies = [ "mio", "num_cpus", "pin-project-lite", - "socket2 0.5.5", + "socket2", "tokio-macros", "windows-sys 0.48.0", ] @@ -1499,7 +1490,7 @@ version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2c1b5fd4128cc8d3e0cb74d4ed9a9cc7c7284becd4df68f5f940e1ad123606f6" dependencies = [ - "indexmap 2.2.3", + "indexmap", "serde", "serde_spanned", "toml_datetime", @@ -1729,7 +1720,7 @@ dependencies = [ [[package]] name = "webol-cli" -version = "0.2.0" +version = "0.3.0" dependencies = [ "anyhow", "clap", @@ -1746,28 +1737,6 @@ dependencies = [ "tokio-tungstenite", ] -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - [[package]] name = "windows-sys" version = "0.45.0" diff --git a/Cargo.toml b/Cargo.toml index 36b08c5..8709e56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "webol-cli" -version = "0.2.0" +version = "0.3.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 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