From 4afc1fad3770a6519cd42a6d5cdce8d2033b04fa Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Tue, 30 Aug 2022 10:17:38 +0200 Subject: [PATCH] Start to serve config --- Cargo.lock | 18 +++++++++++ base/src/lib.rs | 6 ++-- tcp_relay_server/Cargo.toml | 4 ++- tcp_relay_server/src/args.rs | 23 ++++++++++++++ tcp_relay_server/src/lib.rs | 2 +- tcp_relay_server/src/main.rs | 56 +++++++++++++++++++++------------- tcp_relay_server/src/server.rs | 3 -- 7 files changed, 84 insertions(+), 28 deletions(-) create mode 100644 tcp_relay_server/src/args.rs delete mode 100644 tcp_relay_server/src/server.rs diff --git a/Cargo.lock b/Cargo.lock index 2903f98..c6b4732 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -181,6 +181,21 @@ dependencies = [ "syn", ] +[[package]] +name = "actix-web-httpauth" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dda62cf04bc3a9ad2ea8f314f721951cfdb4cdacec4e984d20e77c7bb170991" +dependencies = [ + "actix-utils", + "actix-web", + "base64", + "futures-core", + "futures-util", + "log", + "pin-project-lite", +] + [[package]] name = "adler" version = "1.0.2" @@ -507,6 +522,7 @@ dependencies = [ "futures-task", "pin-project-lite", "pin-utils", + "slab", ] [[package]] @@ -1046,6 +1062,8 @@ name = "tcp_relay_server" version = "0.1.0" dependencies = [ "actix-web", + "actix-web-httpauth", + "base", "clap", "env_logger", "log", diff --git a/base/src/lib.rs b/base/src/lib.rs index d71e623..20584d5 100644 --- a/base/src/lib.rs +++ b/base/src/lib.rs @@ -1,5 +1,7 @@ #[derive(serde::Serialize, serde::Deserialize)] pub struct RelayedPort { - pub id: u64, + pub id: usize, pub port: u16, -} \ No newline at end of file +} + +pub type RemoteConfig = Vec; \ No newline at end of file diff --git a/tcp_relay_server/Cargo.toml b/tcp_relay_server/Cargo.toml index 08513c5..8cc61b1 100644 --- a/tcp_relay_server/Cargo.toml +++ b/tcp_relay_server/Cargo.toml @@ -4,7 +4,9 @@ version = "0.1.0" edition = "2021" [dependencies] +base = { path = "../base" } clap = { version = "3.2.18", features = ["derive", "env"] } log = "0.4.17" env_logger = "0.9.0" -actix-web = "4" \ No newline at end of file +actix-web = "4" +actix-web-httpauth = "0.8.0" \ No newline at end of file diff --git a/tcp_relay_server/src/args.rs b/tcp_relay_server/src/args.rs new file mode 100644 index 0000000..9f80c24 --- /dev/null +++ b/tcp_relay_server/src/args.rs @@ -0,0 +1,23 @@ +use clap::Parser; + +/// TCP relay server +#[derive(Parser, Debug, Clone)] +#[clap(author, version, about, long_about = None)] +pub struct Args { + /// Access tokens + #[clap(short, long)] + pub tokens: Vec, + + /// Forwarded ports + #[clap(short, long)] + pub ports: Vec, + + /// HTTP server listen address + #[clap(short, long, default_value = "0.0.0.0:8000")] + pub listen_address: String, + + /// Increment ports on client. Useful for debugging and running both client and server + /// on the same machine + #[clap(short, long, default_value_t = 0)] + pub increment_ports: u16, +} \ No newline at end of file diff --git a/tcp_relay_server/src/lib.rs b/tcp_relay_server/src/lib.rs index bfe15ae..6e10f4a 100644 --- a/tcp_relay_server/src/lib.rs +++ b/tcp_relay_server/src/lib.rs @@ -1 +1 @@ -pub mod server; \ No newline at end of file +pub mod args; diff --git a/tcp_relay_server/src/main.rs b/tcp_relay_server/src/main.rs index 0f93b45..281c429 100644 --- a/tcp_relay_server/src/main.rs +++ b/tcp_relay_server/src/main.rs @@ -1,30 +1,39 @@ use std::sync::Arc; -use actix_web::{App, HttpServer, web}; +use actix_web::{App, Error, HttpResponse, HttpServer, middleware, Responder, web}; +use actix_web::dev::ServiceRequest; +use actix_web::error::ErrorUnauthorized; +use actix_web::web::Data; +use actix_web_httpauth::extractors::bearer::BearerAuth; +use actix_web_httpauth::middleware::HttpAuthentication; use clap::Parser; -use tcp_relay_server::server::*; +use base::RelayedPort; +use tcp_relay_server::args::Args; -/// TCP relay server -#[derive(Parser, Debug, Clone)] -#[clap(author, version, about, long_about = None)] -struct Args { - /// Access tokens - #[clap(short, long)] - tokens: Vec, +async fn auth_validator( + req: ServiceRequest, + creds: BearerAuth, +) -> Result { + let args: &Data> = req.app_data().unwrap(); + if args.tokens.iter().any(|t| t == creds.token()) { + Ok(req) + } else { + Err((ErrorUnauthorized("invalid token"), req)) + } +} - /// Forwarded ports - #[clap(short, long)] - ports: Vec, +pub async fn hello_route() -> &'static str { + "TCP relay. Hello world!" +} - /// HTTP server listen address - #[clap(short, long, default_value = "0.0.0.0:8000")] - listen_address: String, - - /// Increment ports on client. Useful for debugging and running both client and server - /// on the same machine - #[clap(short, long, default_value_t = 0)] - increment_ports: usize, +pub async fn config_route(data: Data>) -> impl Responder { + HttpResponse::Ok().json( + data.ports.iter() + .enumerate() + .map(|(id, port)| RelayedPort { id, port: port + data.increment_ports }) + .collect::>() + ) } #[actix_web::main] @@ -46,9 +55,14 @@ async fn main() -> std::io::Result<()> { log::info!("Starting relay on http://{}", args.listen_address); - HttpServer::new(|| { + let args_clone = args.clone(); + HttpServer::new(move || { App::new() + .wrap(middleware::Logger::default()) + .wrap(HttpAuthentication::bearer(auth_validator)) + .app_data(Data::new(args_clone.clone())) .route("/", web::get().to(hello_route)) + .route("/config", web::get().to(config_route)) }) .bind(&args.listen_address)? .run() diff --git a/tcp_relay_server/src/server.rs b/tcp_relay_server/src/server.rs deleted file mode 100644 index a991dd3..0000000 --- a/tcp_relay_server/src/server.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub async fn hello_route() -> &'static str { - "TCP relay. Hello world!" -} \ No newline at end of file