From a192e9baca9a14beaa9f87c27a63cff96aa41c94 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sun, 25 Feb 2024 20:00:38 +0100 Subject: Closes #4. Auth on Websocket. Small stuff --- src/requests/device.rs | 23 ++++++++++------------- src/requests/start.rs | 24 ++++++++++++++++++------ 2 files changed, 28 insertions(+), 19 deletions(-) (limited to 'src/requests') diff --git a/src/requests/device.rs b/src/requests/device.rs index a612978..7583406 100644 --- a/src/requests/device.rs +++ b/src/requests/device.rs @@ -1,4 +1,4 @@ -use crate::{config::Config, default_headers, error::Error, format_url, Protocols}; +use crate::{check_success, config::Config, default_headers, error::Error, format_url, Protocols}; pub async fn put( config: &Config, @@ -16,11 +16,10 @@ pub async fn put( r#"{{"id": "{id}", "mac": "{mac}", "broadcast_addr": "{broadcast_addr}", "ip": "{ip}"}}"#, )) .send() - .await? - .text() - .await; + .await?; - println!("{res:?}"); + let body = check_success(res).await?; + println!("{body}"); Ok(()) } @@ -30,11 +29,10 @@ pub async fn get(config: &Config, id: String) -> Result<(), Error> { .headers(default_headers(config)?) .body(format!(r#"{{"id": "{id}"}}"#)) .send() - .await? - .text() - .await; + .await?; - println!("{res:?}"); + let body = check_success(res).await?; + println!("{body}"); Ok(()) } @@ -52,10 +50,9 @@ pub async fn post( r#"{{"id": "{id}", "mac": "{mac}", "broadcast_addr": "{broadcast_addr}", "ip": "{ip}"}}"#, )) .send() - .await? - .text() - .await; + .await?; - println!("{res:?}"); + let body = check_success(res).await?; + println!("{body}"); Ok(()) } diff --git a/src/requests/start.rs b/src/requests/start.rs index 7abbbe0..d07177e 100644 --- a/src/requests/start.rs +++ b/src/requests/start.rs @@ -2,7 +2,10 @@ use futures_util::{SinkExt, StreamExt}; use indicatif::{MultiProgress, ProgressBar}; use reqwest::StatusCode; use serde::Deserialize; -use tokio_tungstenite::{connect_async, tungstenite::Message}; +use tokio_tungstenite::{ + connect_async, + tungstenite::{http::Request, Message}, +}; use crate::{ add_pb, config::Config, default_headers, error::Error, finish_pb, format_url, ErrorResponse, @@ -66,17 +69,26 @@ async fn status_socket( id: String, ) -> Result { let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); - let (mut ws_stream, _response) = - connect_async(format_url(config, "status", &Protocols::Websocket)) - .await - .expect("Failed to connect"); + + let request = Request::builder() + .uri(format_url(config, "status", &Protocols::Websocket)) + .header("Authorization", &config.apikey) + .header("sec-websocket-key", "") + .header("host", &config.server) + .header("upgrade", "websocket") + .header("connection", "upgrade") + .header("sec-websocket-version", 13) + .body(()) + .unwrap(); + + let (mut ws_stream, _response) = connect_async(request).await?; finish_pb(&ws_pb, "connected to websocket".to_string(), DONE_STYLE); ws_stream.send(Message::Text(uuid.clone())).await.unwrap(); // Get ETA let eta_msg = ws_stream.next().await.unwrap().unwrap(); - let eta = get_eta(&eta_msg.into_text().unwrap(), &uuid)? + overview.elapsed().as_secs(); + let eta = get_eta(&eta_msg.into_text().unwrap(), &uuid)?; overview.set_message(format!("/{eta}) start {id}")); let msg_pb = add_pb(pb, DEFAULT_STYLE, "await message".to_string()); -- cgit v1.2.3