mirror of
https://github.com/NotAShelf/mpvrc.git
synced 2026-04-17 08:19:51 +00:00
read authentication token from the environment
This commit is contained in:
parent
37ffb95add
commit
ea085ba5fa
2 changed files with 19 additions and 7 deletions
|
|
@ -216,7 +216,6 @@ async fn main() -> io::Result<()> {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// I don't like this either, but it looks cleaner than a multi-line
|
// I don't like this either, but it looks cleaner than a multi-line
|
||||||
// print macro just cramped in here.
|
// print macro just cramped in here.
|
||||||
let commands = vec![
|
let commands = vec![
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,9 @@ use native_tls::{Identity, TlsAcceptor as NativeTlsAcceptor};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
use tokio_native_tls::TlsAcceptor;
|
use tokio_native_tls::TlsAcceptor;
|
||||||
use tracing::{info, debug, error};
|
use tracing::{debug, error, info};
|
||||||
|
|
||||||
use mrc::{set_property, get_property, playlist_clear, playlist_next, playlist_prev, quit, seek};
|
use mrc::{get_property, playlist_clear, playlist_next, playlist_prev, quit, seek, set_property};
|
||||||
|
|
||||||
const AUTH_TOKEN: &str = "your_secure_token";
|
|
||||||
|
|
||||||
async fn handle_connection(
|
async fn handle_connection(
|
||||||
stream: tokio::net::TcpStream,
|
stream: tokio::net::TcpStream,
|
||||||
|
|
@ -24,13 +22,28 @@ async fn handle_connection(
|
||||||
debug!("Received request:\n{}", request);
|
debug!("Received request:\n{}", request);
|
||||||
|
|
||||||
let headers = request.split("\r\n").collect::<Vec<&str>>();
|
let headers = request.split("\r\n").collect::<Vec<&str>>();
|
||||||
let token_line = headers.iter().find(|&&line| line.starts_with("Authorization:"));
|
let token_line = headers
|
||||||
|
.iter()
|
||||||
|
.find(|&&line| line.starts_with("Authorization:"));
|
||||||
let token = match token_line {
|
let token = match token_line {
|
||||||
Some(line) => line.split(" ").nth(1).unwrap_or_default(),
|
Some(line) => line.split(" ").nth(1).unwrap_or_default(),
|
||||||
None => "",
|
None => "",
|
||||||
};
|
};
|
||||||
|
|
||||||
if token != AUTH_TOKEN {
|
let auth_token = match env::var("AUTH_TOKEN") {
|
||||||
|
Ok(token) => token,
|
||||||
|
Err(_) => {
|
||||||
|
error!("Authentication token is not set. Connection cannot be accepted.");
|
||||||
|
stream.write_all(b"Authentication token not set\n").await?;
|
||||||
|
|
||||||
|
// You know what? I do not care to panic when the token is missing.
|
||||||
|
// Sure, start the server and hell even accept the connection. Auth
|
||||||
|
// will be refused if token is incorrect, so we can just continue here.
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if token != auth_token {
|
||||||
stream.write_all(b"Authentication failed\n").await?;
|
stream.write_all(b"Authentication failed\n").await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue