Start to serve config
This commit is contained in:
@ -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"
|
||||
actix-web = "4"
|
||||
actix-web-httpauth = "0.8.0"
|
23
tcp_relay_server/src/args.rs
Normal file
23
tcp_relay_server/src/args.rs
Normal file
@ -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<String>,
|
||||
|
||||
/// Forwarded ports
|
||||
#[clap(short, long)]
|
||||
pub ports: Vec<u16>,
|
||||
|
||||
/// 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,
|
||||
}
|
@ -1 +1 @@
|
||||
pub mod server;
|
||||
pub mod args;
|
||||
|
@ -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<String>,
|
||||
async fn auth_validator(
|
||||
req: ServiceRequest,
|
||||
creds: BearerAuth,
|
||||
) -> Result<ServiceRequest, (Error, ServiceRequest)> {
|
||||
let args: &Data<Arc<Args>> = 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<usize>,
|
||||
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<Arc<Args>>) -> impl Responder {
|
||||
HttpResponse::Ok().json(
|
||||
data.ports.iter()
|
||||
.enumerate()
|
||||
.map(|(id, port)| RelayedPort { id, port: port + data.increment_ports })
|
||||
.collect::<Vec<_>>()
|
||||
)
|
||||
}
|
||||
|
||||
#[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()
|
||||
|
@ -1,3 +0,0 @@
|
||||
pub async fn hello_route() -> &'static str {
|
||||
"TCP relay. Hello world!"
|
||||
}
|
Reference in New Issue
Block a user