diff options
author | FxQnLr <[email protected]> | 2024-06-17 11:19:13 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2024-06-17 11:19:13 +0200 |
commit | b375657e660b199127a76683980a5d210a572ab7 (patch) | |
tree | fbed775283917537c1d2d11bdbf38a0fa7c05a02 /src | |
parent | 31a57425a76cae121c5d8ef5b0f2442ca6a9ee61 (diff) | |
parent | 0877850a63791772dd19a0af9a0c7c87940aead2 (diff) | |
download | webol-cli-main.tar webol-cli-main.tar.gz webol-cli-main.zip |
0.3.0
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 22 | ||||
-rw-r--r-- | src/main.rs | 15 | ||||
-rw-r--r-- | src/requests/device.rs | 7 | ||||
-rw-r--r-- | src/requests/start.rs | 8 |
4 files changed, 39 insertions, 13 deletions
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; | |||
2 | 2 | ||
3 | #[derive(Deserialize)] | 3 | #[derive(Deserialize)] |
4 | pub struct Config { | 4 | pub struct Config { |
5 | pub apikey: String, | ||
6 | pub server: String, | 5 | pub server: String, |
6 | pub auth: Auth, | ||
7 | } | ||
8 | |||
9 | #[derive(Deserialize)] | ||
10 | pub struct Auth { | ||
11 | pub method: Method, | ||
12 | pub secret: String, | ||
13 | } | ||
14 | |||
15 | #[derive(PartialEq, Eq, Deserialize)] | ||
16 | pub enum Method { | ||
17 | None, | ||
18 | Key, | ||
7 | } | 19 | } |
8 | 20 | ||
9 | impl Config { | 21 | impl Config { |
@@ -12,9 +24,15 @@ impl Config { | |||
12 | 24 | ||
13 | let builder = config::Config::builder(); | 25 | let builder = config::Config::builder(); |
14 | 26 | ||
27 | let builder = builder | ||
28 | .set_default("auth.method", "none")? | ||
29 | .set_default("auth.secret", "")?; | ||
30 | |||
15 | let builder = if let Some(conf) = config_dir { | 31 | let builder = if let Some(conf) = config_dir { |
16 | let dir = conf.to_string_lossy(); | 32 | let dir = conf.to_string_lossy(); |
17 | builder.add_source(config::File::with_name(format!("{dir}/webol-cli").as_str()).required(false)) | 33 | builder.add_source( |
34 | config::File::with_name(format!("{dir}/webol-cli").as_str()).required(false), | ||
35 | ) | ||
18 | } else { | 36 | } else { |
19 | println!("!No config dir found"); | 37 | println!("!No config dir found"); |
20 | builder | 38 | 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}; | |||
3 | use crate::config::Config; | 3 | use crate::config::Config; |
4 | use clap::{Command, CommandFactory, Parser, Subcommand}; | 4 | use clap::{Command, CommandFactory, Parser, Subcommand}; |
5 | use clap_complete::{generate, Generator, Shell}; | 5 | use clap_complete::{generate, Generator, Shell}; |
6 | use config::Method; | ||
6 | use error::Error; | 7 | use error::Error; |
7 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; | 8 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; |
8 | use requests::{device, start::start}; | 9 | use requests::{device, start::start}; |
@@ -117,13 +118,21 @@ fn default_headers(config: &Config) -> Result<HeaderMap, Error> { | |||
117 | let mut map = HeaderMap::new(); | 118 | let mut map = HeaderMap::new(); |
118 | map.append("Accept-Content", HeaderValue::from_str("application/json")?); | 119 | map.append("Accept-Content", HeaderValue::from_str("application/json")?); |
119 | map.append("Content-Type", HeaderValue::from_str("application/json")?); | 120 | map.append("Content-Type", HeaderValue::from_str("application/json")?); |
120 | map.append("Authorization", HeaderValue::from_str(&config.apikey)?); | 121 | if config.auth.method != Method::None { |
122 | map.append("Authorization", HeaderValue::from_str(&config.auth.secret)?); | ||
123 | |||
124 | } | ||
121 | 125 | ||
122 | Ok(map) | 126 | Ok(map) |
123 | } | 127 | } |
124 | 128 | ||
125 | fn format_url(config: &Config, path: &str, protocol: &Protocols) -> String { | 129 | fn format_url(config: &Config, path: &str, protocol: &Protocols, id: Option<&str>) -> String { |
126 | format!("{}://{}/{}", protocol, config.server, path) | 130 | if let Some(id) = id { |
131 | format!("{}://{}/{}/{}", protocol, config.server, path, id) | ||
132 | |||
133 | } else { | ||
134 | format!("{}://{}/{}", protocol, config.server, path) | ||
135 | } | ||
127 | } | 136 | } |
128 | 137 | ||
129 | async fn check_success(res: Response) -> Result<String, Error> { | 138 | async fn check_success(res: Response) -> Result<String, Error> { |
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( | |||
7 | broadcast_addr: String, | 7 | broadcast_addr: String, |
8 | ip: String, | 8 | ip: String, |
9 | ) -> Result<(), Error> { | 9 | ) -> Result<(), Error> { |
10 | let url = format_url(config, "device", &Protocols::Http); | 10 | let url = format_url(config, "device", &Protocols::Http, None); |
11 | println!("{url}"); | 11 | println!("{url}"); |
12 | let res = reqwest::Client::new() | 12 | let res = reqwest::Client::new() |
13 | .put(url) | 13 | .put(url) |
@@ -25,9 +25,8 @@ pub async fn put( | |||
25 | 25 | ||
26 | pub async fn get(config: &Config, id: String) -> Result<(), Error> { | 26 | pub async fn get(config: &Config, id: String) -> Result<(), Error> { |
27 | let res = reqwest::Client::new() | 27 | let res = reqwest::Client::new() |
28 | .get(format_url(config, "device", &Protocols::Http)) | 28 | .get(format_url(config, "device", &Protocols::Http, Some(&id))) |
29 | .headers(default_headers(config)?) | 29 | .headers(default_headers(config)?) |
30 | .body(format!(r#"{{"id": "{id}"}}"#)) | ||
31 | .send() | 30 | .send() |
32 | .await?; | 31 | .await?; |
33 | 32 | ||
@@ -44,7 +43,7 @@ pub async fn post( | |||
44 | ip: String, | 43 | ip: String, |
45 | ) -> Result<(), Error> { | 44 | ) -> Result<(), Error> { |
46 | let res = reqwest::Client::new() | 45 | let res = reqwest::Client::new() |
47 | .post(format_url(config, "device", &Protocols::Http)) | 46 | .post(format_url(config, "device", &Protocols::Http, None)) |
48 | .headers(default_headers(config)?) | 47 | .headers(default_headers(config)?) |
49 | .body(format!( | 48 | .body(format!( |
50 | r#"{{"id": "{id}", "mac": "{mac}", "broadcast_addr": "{broadcast_addr}", "ip": "{ip}"}}"#, | 49 | 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> | |||
17 | let send_start = MultiProgress::new(); | 17 | let send_start = MultiProgress::new(); |
18 | let overview = add_pb(&send_start, OVERVIEW_STYLE, format!(") start {id}")); | 18 | let overview = add_pb(&send_start, OVERVIEW_STYLE, format!(") start {id}")); |
19 | 19 | ||
20 | let url = format_url(config, "start", &Protocols::Http); | 20 | let url = format_url(config, "start", &Protocols::Http, Some(&id)); |
21 | let connect = add_pb(&send_start, DEFAULT_STYLE, format!("connect to {url}")); | 21 | let connect = add_pb(&send_start, DEFAULT_STYLE, format!("connect to {url}")); |
22 | let res = reqwest::Client::new() | 22 | let res = reqwest::Client::new() |
23 | .post(url) | 23 | .post(url) |
24 | .headers(default_headers(config)?) | 24 | .headers(default_headers(config)?) |
25 | .body(format!(r#"{{"id": "{id}", "ping": {ping}}}"#)) | 25 | .body(format!(r#"{{"ping": {ping}}}"#)) |
26 | .send() | 26 | .send() |
27 | .await?; | 27 | .await?; |
28 | finish_pb(&connect, "connected, got response".to_string(), DONE_STYLE); | 28 | finish_pb(&connect, "connected, got response".to_string(), DONE_STYLE); |
@@ -71,8 +71,8 @@ async fn status_socket( | |||
71 | let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); | 71 | let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); |
72 | 72 | ||
73 | let request = Request::builder() | 73 | let request = Request::builder() |
74 | .uri(format_url(config, "status", &Protocols::Websocket)) | 74 | .uri(format_url(config, "status", &Protocols::Websocket, None)) |
75 | .header("Authorization", &config.apikey) | 75 | .header("Authorization", &config.auth.secret) |
76 | .header("sec-websocket-key", "") | 76 | .header("sec-websocket-key", "") |
77 | .header("host", &config.server) | 77 | .header("host", &config.server) |
78 | .header("upgrade", "websocket") | 78 | .header("upgrade", "websocket") |