summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-04-10 12:06:45 +0200
committerFxQnLr <[email protected]>2024-04-10 12:06:45 +0200
commit352dd535e0386d899e816ac5f597e583d1ade768 (patch)
tree064522c02fb30b3804ee399fd2e77a3e10c3f289
parent23aa75439c3ee60e22ce183853516ca3cdd13ff4 (diff)
downloadwebol-cli-352dd535e0386d899e816ac5f597e583d1ade768.tar
webol-cli-352dd535e0386d899e816ac5f597e583d1ade768.tar.gz
webol-cli-352dd535e0386d899e816ac5f597e583d1ade768.zip
Closes #8. 0.4.0 seems to work
-rw-r--r--Cargo.lock103
-rw-r--r--Cargo.toml2
-rw-r--r--src/config.rs22
-rw-r--r--src/main.rs15
-rw-r--r--src/requests/device.rs7
-rw-r--r--src/requests/start.rs8
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,17 +527,17 @@ checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0"
527 527
528[[package]] 528[[package]]
529name = "h2" 529name = "h2"
530version = "0.3.21" 530version = "0.3.26"
531source = "registry+https://github.com/rust-lang/crates.io-index" 531source = "registry+https://github.com/rust-lang/crates.io-index"
532checksum = "91fc23aa11be92976ef4729127f1a74adf36d8436f7816b185d18df956790833" 532checksum = "81fe527a889e1532da5c525686d96d4c2e74cdd345badf8dfef9f6b39dd5f5e8"
533dependencies = [ 533dependencies = [
534 "bytes", 534 "bytes",
535 "fnv", 535 "fnv",
536 "futures-core", 536 "futures-core",
537 "futures-sink", 537 "futures-sink",
538 "futures-util", 538 "futures-util",
539 "http 0.2.10", 539 "http 0.2.12",
540 "indexmap 1.9.3", 540 "indexmap",
541 "slab", 541 "slab",
542 "tokio", 542 "tokio",
543 "tokio-util", 543 "tokio-util",
@@ -546,12 +546,6 @@ dependencies = [
546 546
547[[package]] 547[[package]]
548name = "hashbrown" 548name = "hashbrown"
549version = "0.12.3"
550source = "registry+https://github.com/rust-lang/crates.io-index"
551checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
552
553[[package]]
554name = "hashbrown"
555version = "0.13.2" 549version = "0.13.2"
556source = "registry+https://github.com/rust-lang/crates.io-index" 550source = "registry+https://github.com/rust-lang/crates.io-index"
557checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e" 551checksum = "43a3c133739dddd0d2990f9a4bdf8eb4b21ef50e4851ca85ab661199821d510e"
@@ -576,9 +570,9 @@ checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
576 570
577[[package]] 571[[package]]
578name = "http" 572name = "http"
579version = "0.2.10" 573version = "0.2.12"
580source = "registry+https://github.com/rust-lang/crates.io-index" 574source = "registry+https://github.com/rust-lang/crates.io-index"
581checksum = "f95b9abcae896730d42b78e09c155ed4ddf82c07b4de772c64aee5b2d8b7c150" 575checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1"
582dependencies = [ 576dependencies = [
583 "bytes", 577 "bytes",
584 "fnv", 578 "fnv",
@@ -598,12 +592,12 @@ dependencies = [
598 592
599[[package]] 593[[package]]
600name = "http-body" 594name = "http-body"
601version = "0.4.5" 595version = "0.4.6"
602source = "registry+https://github.com/rust-lang/crates.io-index" 596source = "registry+https://github.com/rust-lang/crates.io-index"
603checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" 597checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2"
604dependencies = [ 598dependencies = [
605 "bytes", 599 "bytes",
606 "http 0.2.10", 600 "http 0.2.12",
607 "pin-project-lite", 601 "pin-project-lite",
608] 602]
609 603
@@ -621,22 +615,22 @@ checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
621 615
622[[package]] 616[[package]]
623name = "hyper" 617name = "hyper"
624version = "0.14.27" 618version = "0.14.28"
625source = "registry+https://github.com/rust-lang/crates.io-index" 619source = "registry+https://github.com/rust-lang/crates.io-index"
626checksum = "ffb1cfd654a8219eaef89881fdb3bb3b1cdc5fa75ded05d6933b2b382e395468" 620checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80"
627dependencies = [ 621dependencies = [
628 "bytes", 622 "bytes",
629 "futures-channel", 623 "futures-channel",
630 "futures-core", 624 "futures-core",
631 "futures-util", 625 "futures-util",
632 "h2", 626 "h2",
633 "http 0.2.10", 627 "http 0.2.12",
634 "http-body", 628 "http-body",
635 "httparse", 629 "httparse",
636 "httpdate", 630 "httpdate",
637 "itoa", 631 "itoa",
638 "pin-project-lite", 632 "pin-project-lite",
639 "socket2 0.4.10", 633 "socket2",
640 "tokio", 634 "tokio",
641 "tower-service", 635 "tower-service",
642 "tracing", 636 "tracing",
@@ -668,16 +662,6 @@ dependencies = [
668 662
669[[package]] 663[[package]]
670name = "indexmap" 664name = "indexmap"
671version = "1.9.3"
672source = "registry+https://github.com/rust-lang/crates.io-index"
673checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
674dependencies = [
675 "autocfg",
676 "hashbrown 0.12.3",
677]
678
679[[package]]
680name = "indexmap"
681version = "2.2.3" 665version = "2.2.3"
682source = "registry+https://github.com/rust-lang/crates.io-index" 666source = "registry+https://github.com/rust-lang/crates.io-index"
683checksum = "233cf39063f058ea2caae4091bf4a3ef70a653afbc026f5c4a4135d114e3c177" 667checksum = "233cf39063f058ea2caae4091bf4a3ef70a653afbc026f5c4a4135d114e3c177"
@@ -1095,9 +1079,9 @@ dependencies = [
1095 1079
1096[[package]] 1080[[package]]
1097name = "reqwest" 1081name = "reqwest"
1098version = "0.11.22" 1082version = "0.11.27"
1099source = "registry+https://github.com/rust-lang/crates.io-index" 1083source = "registry+https://github.com/rust-lang/crates.io-index"
1100checksum = "046cd98826c46c2ac8ddecae268eb5c2e58628688a5fc7a2643704a73faba95b" 1084checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62"
1101dependencies = [ 1085dependencies = [
1102 "base64", 1086 "base64",
1103 "bytes", 1087 "bytes",
@@ -1105,7 +1089,7 @@ dependencies = [
1105 "futures-core", 1089 "futures-core",
1106 "futures-util", 1090 "futures-util",
1107 "h2", 1091 "h2",
1108 "http 0.2.10", 1092 "http 0.2.12",
1109 "http-body", 1093 "http-body",
1110 "hyper", 1094 "hyper",
1111 "hyper-tls", 1095 "hyper-tls",
@@ -1117,9 +1101,11 @@ dependencies = [
1117 "once_cell", 1101 "once_cell",
1118 "percent-encoding", 1102 "percent-encoding",
1119 "pin-project-lite", 1103 "pin-project-lite",
1104 "rustls-pemfile",
1120 "serde", 1105 "serde",
1121 "serde_json", 1106 "serde_json",
1122 "serde_urlencoded", 1107 "serde_urlencoded",
1108 "sync_wrapper",
1123 "system-configuration", 1109 "system-configuration",
1124 "tokio", 1110 "tokio",
1125 "tokio-native-tls", 1111 "tokio-native-tls",
@@ -1173,6 +1159,15 @@ dependencies = [
1173] 1159]
1174 1160
1175[[package]] 1161[[package]]
1162name = "rustls-pemfile"
1163version = "1.0.4"
1164source = "registry+https://github.com/rust-lang/crates.io-index"
1165checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c"
1166dependencies = [
1167 "base64",
1168]
1169
1170[[package]]
1176name = "ryu" 1171name = "ryu"
1177version = "1.0.15" 1172version = "1.0.15"
1178source = "registry+https://github.com/rust-lang/crates.io-index" 1173source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1295,16 +1290,6 @@ dependencies = [
1295 1290
1296[[package]] 1291[[package]]
1297name = "socket2" 1292name = "socket2"
1298version = "0.4.10"
1299source = "registry+https://github.com/rust-lang/crates.io-index"
1300checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d"
1301dependencies = [
1302 "libc",
1303 "winapi",
1304]
1305
1306[[package]]
1307name = "socket2"
1308version = "0.5.5" 1293version = "0.5.5"
1309source = "registry+https://github.com/rust-lang/crates.io-index" 1294source = "registry+https://github.com/rust-lang/crates.io-index"
1310checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9" 1295checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9"
@@ -1331,6 +1316,12 @@ dependencies = [
1331] 1316]
1332 1317
1333[[package]] 1318[[package]]
1319name = "sync_wrapper"
1320version = "0.1.2"
1321source = "registry+https://github.com/rust-lang/crates.io-index"
1322checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
1323
1324[[package]]
1334name = "system-configuration" 1325name = "system-configuration"
1335version = "0.5.1" 1326version = "0.5.1"
1336source = "registry+https://github.com/rust-lang/crates.io-index" 1327source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1420,7 +1411,7 @@ dependencies = [
1420 "mio", 1411 "mio",
1421 "num_cpus", 1412 "num_cpus",
1422 "pin-project-lite", 1413 "pin-project-lite",
1423 "socket2 0.5.5", 1414 "socket2",
1424 "tokio-macros", 1415 "tokio-macros",
1425 "windows-sys 0.48.0", 1416 "windows-sys 0.48.0",
1426] 1417]
@@ -1499,7 +1490,7 @@ version = "0.22.6"
1499source = "registry+https://github.com/rust-lang/crates.io-index" 1490source = "registry+https://github.com/rust-lang/crates.io-index"
1500checksum = "2c1b5fd4128cc8d3e0cb74d4ed9a9cc7c7284becd4df68f5f940e1ad123606f6" 1491checksum = "2c1b5fd4128cc8d3e0cb74d4ed9a9cc7c7284becd4df68f5f940e1ad123606f6"
1501dependencies = [ 1492dependencies = [
1502 "indexmap 2.2.3", 1493 "indexmap",
1503 "serde", 1494 "serde",
1504 "serde_spanned", 1495 "serde_spanned",
1505 "toml_datetime", 1496 "toml_datetime",
@@ -1729,7 +1720,7 @@ dependencies = [
1729 1720
1730[[package]] 1721[[package]]
1731name = "webol-cli" 1722name = "webol-cli"
1732version = "0.2.0" 1723version = "0.3.0"
1733dependencies = [ 1724dependencies = [
1734 "anyhow", 1725 "anyhow",
1735 "clap", 1726 "clap",
@@ -1747,28 +1738,6 @@ dependencies = [
1747] 1738]
1748 1739
1749[[package]] 1740[[package]]
1750name = "winapi"
1751version = "0.3.9"
1752source = "registry+https://github.com/rust-lang/crates.io-index"
1753checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
1754dependencies = [
1755 "winapi-i686-pc-windows-gnu",
1756 "winapi-x86_64-pc-windows-gnu",
1757]
1758
1759[[package]]
1760name = "winapi-i686-pc-windows-gnu"
1761version = "0.4.0"
1762source = "registry+https://github.com/rust-lang/crates.io-index"
1763checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
1764
1765[[package]]
1766name = "winapi-x86_64-pc-windows-gnu"
1767version = "0.4.0"
1768source = "registry+https://github.com/rust-lang/crates.io-index"
1769checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
1770
1771[[package]]
1772name = "windows-sys" 1741name = "windows-sys"
1773version = "0.45.0" 1742version = "0.45.0"
1774source = "registry+https://github.com/rust-lang/crates.io-index" 1743source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 36b08c5..8709e56 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
1[package] 1[package]
2name = "webol-cli" 2name = "webol-cli"
3version = "0.2.0" 3version = "0.3.0"
4edition = "2021" 4edition = "2021"
5 5
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 6# 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;
2 2
3#[derive(Deserialize)] 3#[derive(Deserialize)]
4pub struct Config { 4pub struct Config {
5 pub apikey: String,
6 pub server: String, 5 pub server: String,
6 pub auth: Auth,
7}
8
9#[derive(Deserialize)]
10pub struct Auth {
11 pub method: Method,
12 pub secret: String,
13}
14
15#[derive(PartialEq, Eq, Deserialize)]
16pub enum Method {
17 None,
18 Key,
7} 19}
8 20
9impl Config { 21impl 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};
3use crate::config::Config; 3use crate::config::Config;
4use clap::{Command, CommandFactory, Parser, Subcommand}; 4use clap::{Command, CommandFactory, Parser, Subcommand};
5use clap_complete::{generate, Generator, Shell}; 5use clap_complete::{generate, Generator, Shell};
6use config::Method;
6use error::Error; 7use error::Error;
7use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; 8use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
8use requests::{device, start::start}; 9use 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
125fn format_url(config: &Config, path: &str, protocol: &Protocols) -> String { 129fn 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
129async fn check_success(res: Response) -> Result<String, Error> { 138async 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
26pub async fn get(config: &Config, id: String) -> Result<(), Error> { 26pub 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")