RoadSign/src/main.rs

95 lines
2.6 KiB
Rust
Raw Normal View History

2024-01-13 04:35:59 +00:00
mod config;
mod proxies;
2024-02-12 16:34:54 +00:00
mod sideload;
2024-01-15 15:22:53 +00:00
pub mod warden;
2024-01-14 09:55:14 +00:00
2024-02-12 16:01:39 +00:00
use actix_web::{App, HttpServer, web};
2024-02-12 17:04:43 +00:00
use actix_web_httpauth::extractors::AuthenticationError;
use actix_web_httpauth::headers::www_authenticate::basic::Basic;
use actix_web_httpauth::middleware::HttpAuthentication;
2024-02-12 16:01:39 +00:00
use awc::Client;
2024-01-14 09:55:14 +00:00
use lazy_static::lazy_static;
use proxies::RoadInstance;
use tokio::sync::Mutex;
2024-01-13 06:46:26 +00:00
use tracing::{error, info, Level};
2024-01-15 15:22:53 +00:00
use crate::proxies::route;
2024-01-14 09:55:14 +00:00
lazy_static! {
2024-01-15 15:22:53 +00:00
static ref ROAD: Mutex<RoadInstance> = Mutex::new(RoadInstance::new());
2024-01-14 09:55:14 +00:00
}
2024-01-13 04:35:59 +00:00
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
2024-01-13 06:46:26 +00:00
// Setting up logging
tracing_subscriber::fmt()
.with_max_level(Level::DEBUG)
.init();
// Prepare all the stuff
info!("Loading proxy regions...");
match proxies::loader::scan_regions(
config::C
.read()
2024-01-14 09:55:14 +00:00
.await
2024-01-13 06:46:26 +00:00
.get_string("regions")
.unwrap_or("./regions".to_string()),
) {
Err(_) => error!("Loading proxy regions... failed"),
Ok((regions, count)) => {
2024-01-14 09:55:14 +00:00
ROAD.lock().await.regions = regions;
2024-01-13 06:46:26 +00:00
info!(count, "Loading proxy regions... done")
}
};
2024-01-13 04:35:59 +00:00
// Proxies
2024-02-12 16:01:39 +00:00
let proxies_server = HttpServer::new(|| {
App::new()
.app_data(web::Data::new(Client::default()))
.route("/", web::to(route::handle))
}).bind(
config::C
.read()
2024-01-14 09:55:14 +00:00
.await
.get_string("listen.proxies")
2024-02-12 16:01:39 +00:00
.unwrap_or("0.0.0.0:80".to_string())
)?.run();
2024-01-13 04:35:59 +00:00
2024-02-12 16:34:54 +00:00
// Sideload
let sideload_server = HttpServer::new(|| {
2024-02-12 17:04:43 +00:00
App::new()
.wrap(HttpAuthentication::basic(|req, credentials| async move {
let password = config::C
.read()
.await
.get_string("secret")
.unwrap_or("".to_string());
if credentials.password().unwrap_or("") != password {
Err((AuthenticationError::new(Basic::new()).into(), req))
} else {
Ok(req)
}
}))
.service(sideload::service())
2024-02-12 16:34:54 +00:00
}).bind(
config::C
.read()
.await
.get_string("listen.sideload")
.unwrap_or("0.0.0.0:81".to_string())
)?.run();
2024-01-15 15:22:53 +00:00
// Process manager
{
let mut app = ROAD.lock().await;
{
let reg = app.regions.clone();
app.warden.scan(reg);
}
app.warden.start().await;
}
2024-02-12 16:34:54 +00:00
tokio::try_join!(proxies_server, sideload_server)?;
2024-01-13 04:35:59 +00:00
Ok(())
}