Add token authentication for controllers
This commit is contained in:
parent
1fb71c2325
commit
1c476dac64
3 changed files with 35 additions and 8 deletions
|
|
@ -118,10 +118,11 @@ async fn main() -> Result<(), std::io::Error> {
|
|||
let app_state = app_state::AppState::new(pool.clone(), this, settings.clone(), drivers).start();
|
||||
|
||||
log::info!("Starting main loops");
|
||||
let ws_token = settings.server.token.clone();
|
||||
|
||||
let _ = tokio::join!(
|
||||
tokio::spawn(run_relays_loop(app_state.clone(), settings.clone())),
|
||||
tokio::spawn(run_ws_loop(pool.clone(), app_state.clone(), url)),
|
||||
tokio::spawn(run_ws_loop(pool.clone(), app_state.clone(), url, ws_token)),
|
||||
);
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
mod handlers;
|
||||
|
||||
use crate::app_state;
|
||||
use actix::Addr;
|
||||
use emgauwa_common::constants;
|
||||
use emgauwa_common::constants::WEBSOCKET_RETRY_TIMEOUT;
|
||||
use emgauwa_common::errors::EmgauwaError;
|
||||
use emgauwa_common::types::ControllerWsAction;
|
||||
|
|
@ -8,16 +10,21 @@ use futures::{future, pin_mut, SinkExt, StreamExt};
|
|||
use sqlx::{Pool, Sqlite};
|
||||
use tokio::time;
|
||||
use tokio_tungstenite::connect_async;
|
||||
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
use crate::app_state;
|
||||
|
||||
use crate::app_state::AppState;
|
||||
use crate::ws::handlers::handle_message;
|
||||
|
||||
pub async fn run_ws_loop(pool: Pool<Sqlite>, app_state: Addr<AppState>, url: String) {
|
||||
pub async fn run_ws_loop(
|
||||
pool: Pool<Sqlite>,
|
||||
app_state: Addr<AppState>,
|
||||
url: String,
|
||||
ws_token: String,
|
||||
) {
|
||||
log::debug!("Spawned ws loop");
|
||||
loop {
|
||||
let run_result = run_websocket(pool.clone(), &app_state, &url).await;
|
||||
let run_result = run_websocket(pool.clone(), &app_state, &url, &ws_token).await;
|
||||
if let Err(err) = run_result {
|
||||
log::error!("Error running websocket: {}", err);
|
||||
}
|
||||
|
|
@ -34,9 +41,26 @@ async fn run_websocket(
|
|||
pool: Pool<Sqlite>,
|
||||
app_state: &Addr<AppState>,
|
||||
url: &str,
|
||||
ws_token: &str,
|
||||
) -> Result<(), EmgauwaError> {
|
||||
log::debug!("Trying to connect to {}", url);
|
||||
match connect_async(url).await {
|
||||
|
||||
let mut request = url.into_client_request().map_err(|err| {
|
||||
EmgauwaError::Other(format!(
|
||||
"Failed to create client request for websocket: {}",
|
||||
err
|
||||
))
|
||||
})?;
|
||||
|
||||
let header_value = ws_token.parse().map_err(|err| {
|
||||
EmgauwaError::Other(format!("Failed to set token header for websocket: {}", err))
|
||||
})?;
|
||||
|
||||
request
|
||||
.headers_mut()
|
||||
.insert(constants::CONTROLLER_WS_TOKEN_HEADER, header_value);
|
||||
|
||||
match connect_async(request).await {
|
||||
Ok(connection) => {
|
||||
log::info!("Websocket connected");
|
||||
let (ws_stream, _) = connection;
|
||||
|
|
@ -73,9 +97,7 @@ async fn read_app_state(
|
|||
app_state: Addr<AppState>,
|
||||
tx: futures_channel::mpsc::UnboundedSender<Message>,
|
||||
) -> Result<(), EmgauwaError> {
|
||||
let notifier = &*app_state
|
||||
.send(app_state::GetRelayNotifier {})
|
||||
.await?;
|
||||
let notifier = &*app_state.send(app_state::GetRelayNotifier {}).await?;
|
||||
|
||||
loop {
|
||||
notifier.notified().await;
|
||||
|
|
|
|||
Loading…
Reference in a new issue