Multiple listeners

This commit is contained in:
2024-02-13 20:32:13 +08:00
parent 3fbe1db1ef
commit 12add73ecb
13 changed files with 126 additions and 75 deletions

View File

@@ -1,21 +1,20 @@
extern crate core;
mod config;
mod proxies;
mod sideload;
pub mod warden;
mod tls;
mod warden;
mod server;
pub mod tls;
use std::error;
use actix_web::{App, HttpServer, web};
use actix_web::middleware::Logger;
use actix_web_httpauth::extractors::AuthenticationError;
use actix_web_httpauth::headers::www_authenticate::basic::Basic;
use actix_web_httpauth::middleware::HttpAuthentication;
use awc::Client;
use lazy_static::lazy_static;
use proxies::RoadInstance;
use tokio::sync::Mutex;
use tokio::task::JoinSet;
use tracing::{error, info, Level};
use crate::proxies::route;
use crate::proxies::server::build_proxies;
use crate::sideload::server::build_sideload;
lazy_static! {
static ref ROAD: Mutex<RoadInstance> = Mutex::new(RoadInstance::new());
@@ -43,50 +42,15 @@ async fn main() -> Result<(), Box<dyn error::Error>> {
}
};
let mut server_set = JoinSet::new();
// Proxies
let proxies_server = HttpServer::new(|| {
App::new()
.wrap(Logger::default())
.app_data(web::Data::new(Client::default()))
.route("/", web::to(route::handle))
}).bind_rustls_0_22(
config::CFG
.read()
.await
.get_string("listen.proxies_tls")?,
tls::use_rustls().await?,
)?.bind(
config::CFG
.read()
.await
.get_string("listen.proxies")?
)?.run();
for server in build_proxies().await? {
server_set.spawn(server);
}
// Sideload
let sideload_server = HttpServer::new(|| {
App::new()
.wrap(HttpAuthentication::basic(|req, credentials| async move {
let password = match config::CFG
.read()
.await
.get_string("secret") {
Ok(val) => val,
Err(_) => return Err((AuthenticationError::new(Basic::new()).into(), req))
};
if credentials.password().unwrap_or("") != password {
Err((AuthenticationError::new(Basic::new()).into(), req))
} else {
Ok(req)
}
}))
.service(sideload::service())
}).bind(
config::CFG
.read()
.await
.get_string("listen.sideload")
.unwrap_or("0.0.0.0:81".to_string())
)?.workers(1).run();
server_set.spawn(build_sideload().await?);
// Process manager
{
@@ -98,7 +62,8 @@ async fn main() -> Result<(), Box<dyn error::Error>> {
app.warden.start().await;
}
tokio::try_join!(proxies_server, sideload_server)?;
// Wait for web servers
server_set.join_next().await;
Ok(())
}

View File

@@ -17,6 +17,7 @@ pub mod loader;
pub mod metrics;
pub mod responder;
pub mod route;
pub mod server;
#[derive(Debug, Display)]
pub enum ProxyError {

39
src/proxies/server.rs Normal file
View File

@@ -0,0 +1,39 @@
use std::error;
use actix_web::{App, HttpServer, web};
use actix_web::dev::Server;
use actix_web::middleware::Logger;
use awc::Client;
use crate::config::CFG;
use crate::proxies::route;
use crate::server::ServerBindConfig;
use crate::tls::{load_certificates, use_rustls};
pub async fn build_proxies() -> Result<Vec<Server>, Box<dyn error::Error>> {
load_certificates().await?;
let cfg = CFG
.read()
.await
.get::<Vec<ServerBindConfig>>("proxies.bind")?;
let mut tasks = Vec::new();
for item in cfg {
tasks.push(build_single_proxy(item)?);
}
Ok(tasks)
}
pub fn build_single_proxy(cfg: ServerBindConfig) -> Result<Server, Box<dyn error::Error>> {
let server = HttpServer::new(|| {
App::new()
.wrap(Logger::default())
.app_data(web::Data::new(Client::default()))
.route("/", web::to(route::handle))
});
if cfg.tls {
Ok(server.bind_rustls_0_22(cfg.addr, use_rustls()?)?.run())
} else {
Ok(server.bind(cfg.addr)?.run())
}
}

7
src/server.rs Normal file
View File

@@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct ServerBindConfig {
pub addr: String,
pub tls: bool,
}

View File

@@ -4,6 +4,7 @@ use crate::sideload::regions::list_region;
mod overview;
mod regions;
pub mod server;
static ROOT: &str = "";

35
src/sideload/server.rs Normal file
View File

@@ -0,0 +1,35 @@
use std::error;
use actix_web::dev::Server;
use actix_web::{App, HttpServer};
use actix_web_httpauth::extractors::AuthenticationError;
use actix_web_httpauth::headers::www_authenticate::basic::Basic;
use actix_web_httpauth::middleware::HttpAuthentication;
use crate::sideload;
pub async fn build_sideload() -> Result<Server, Box<dyn error::Error>> {
Ok(
HttpServer::new(|| {
App::new()
.wrap(HttpAuthentication::basic(|req, credentials| async move {
let password = match crate::config::CFG
.read()
.await
.get_string("secret") {
Ok(val) => val,
Err(_) => return Err((AuthenticationError::new(Basic::new()).into(), req))
};
if credentials.password().unwrap_or("") != password {
Err((AuthenticationError::new(Basic::new()).into(), req))
} else {
Ok(req)
}
}))
.service(sideload::service())
}).bind(
crate::config::CFG
.read()
.await
.get_string("sideload.bind_addr")?
)?.workers(1).run()
)
}

View File

@@ -67,9 +67,7 @@ pub async fn load_certificates() -> Result<(), ConfigError> {
Ok(())
}
pub async fn use_rustls() -> Result<rustls::ServerConfig, ConfigError> {
load_certificates().await?;
pub fn use_rustls() -> Result<rustls::ServerConfig, ConfigError> {
Ok(
rustls::ServerConfig::builder()
.with_no_client_auth()