2024-01-14 10:39:22 +00:00
|
|
|
pub mod auth;
|
2024-01-13 04:35:59 +00:00
|
|
|
mod config;
|
|
|
|
mod proxies;
|
|
|
|
mod sideload;
|
|
|
|
|
2024-01-14 09:55:14 +00:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
|
|
|
use lazy_static::lazy_static;
|
2024-01-14 10:39:22 +00:00
|
|
|
use poem::{listener::TcpListener, EndpointExt, Route, Server};
|
2024-01-13 04:35:59 +00:00
|
|
|
use poem_openapi::OpenApiService;
|
2024-01-14 09:55:14 +00:00
|
|
|
use proxies::RoadInstance;
|
|
|
|
use tokio::sync::Mutex;
|
2024-01-13 06:46:26 +00:00
|
|
|
use tracing::{error, info, Level};
|
2024-01-13 04:35:59 +00:00
|
|
|
|
2024-01-14 09:55:14 +00:00
|
|
|
use crate::proxies::{metrics::RoadMetrics, route};
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref ROAD: Mutex<RoadInstance> = Mutex::new(RoadInstance {
|
|
|
|
regions: vec![],
|
|
|
|
metrics: RoadMetrics {
|
|
|
|
requests_count: 0,
|
|
|
|
failures_count: 0,
|
|
|
|
recent_successes: VecDeque::new(),
|
|
|
|
recent_errors: VecDeque::new(),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-01-13 18:01:56 +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
|
2024-01-13 04:35:59 +00:00
|
|
|
if std::env::var_os("RUST_LOG").is_none() {
|
|
|
|
std::env::set_var("RUST_LOG", "poem=debug");
|
|
|
|
}
|
2024-01-13 06:46:26 +00:00
|
|
|
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-01-13 18:01:56 +00:00
|
|
|
let proxies_server = Server::new(TcpListener::bind(
|
|
|
|
config::C
|
|
|
|
.read()
|
2024-01-14 09:55:14 +00:00
|
|
|
.await
|
2024-01-13 18:01:56 +00:00
|
|
|
.get_string("listen.proxies")
|
|
|
|
.unwrap_or("0.0.0.0:80".to_string()),
|
|
|
|
))
|
2024-01-14 09:55:14 +00:00
|
|
|
.run(route::handle);
|
2024-01-13 04:35:59 +00:00
|
|
|
|
|
|
|
// Sideload
|
|
|
|
let sideload = OpenApiService::new(sideload::SideloadApi, "Sideload API", "1.0")
|
|
|
|
.server("http://localhost:3000/cgi");
|
|
|
|
|
|
|
|
let sideload_server = Server::new(TcpListener::bind(
|
2024-01-13 06:46:26 +00:00
|
|
|
config::C
|
|
|
|
.read()
|
2024-01-14 09:55:14 +00:00
|
|
|
.await
|
2024-01-13 04:35:59 +00:00
|
|
|
.get_string("listen.sideload")
|
|
|
|
.unwrap_or("0.0.0.0:81".to_string()),
|
|
|
|
))
|
2024-01-13 18:01:56 +00:00
|
|
|
.run(
|
2024-01-14 10:39:22 +00:00
|
|
|
Route::new().nest("/cgi", sideload).with(auth::BasicAuth {
|
|
|
|
username: "RoadSign".to_string(),
|
|
|
|
password: config::C
|
|
|
|
.read()
|
|
|
|
.await
|
|
|
|
.get_string("secret")
|
|
|
|
.unwrap_or("password".to_string()),
|
|
|
|
}),
|
2024-01-13 18:01:56 +00:00
|
|
|
);
|
2024-01-13 04:35:59 +00:00
|
|
|
|
2024-01-13 18:01:56 +00:00
|
|
|
tokio::try_join!(proxies_server, sideload_server)?;
|
2024-01-13 04:35:59 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|