feat: added beacon installer generation, download

This commit is contained in:
Andrew Rioux
2025-02-02 02:37:53 -05:00
parent b416f35b63
commit 0576c4fd3b
22 changed files with 554 additions and 87 deletions

View File

@@ -4,6 +4,7 @@ pub enum Error {
Sqlx(sqlx::Error),
TokioJoin(tokio::task::JoinError),
Io(std::io::Error),
Rustls(rustls::Error),
}
impl std::fmt::Display for Error {
@@ -21,6 +22,9 @@ impl std::fmt::Display for Error {
Error::Io(err) => {
write!(f, "io error: {err:?}")
}
Error::Rustls(err) => {
write!(f, "rustls error: {err:?}")
}
}
}
}
@@ -31,6 +35,7 @@ impl std::error::Error for Error {
Error::Sqlx(err) => Some(err),
Error::TokioJoin(err) => Some(err),
Error::Io(err) => Some(err),
Error::Rustls(err) => Some(err),
_ => None,
}
}
@@ -61,3 +66,9 @@ impl From<std::io::Error> for Error {
Self::Io(err)
}
}
impl From<rustls::Error> for Error {
fn from(err: rustls::Error) -> Self {
Self::Rustls(err)
}
}

View File

@@ -4,7 +4,6 @@ use std::{
};
use axum::routing::{get, post, Router};
use axum_server::tls_rustls::RustlsConfig;
use sqlx::SqlitePool;
use tokio::task::JoinHandle;
@@ -36,8 +35,6 @@ impl std::ops::Deref for BeaconListenerMap {
}
pub async fn start_all_listeners(beacon_listener_map: BeaconListenerMap, db: SqlitePool) -> Result<(), crate::error::Error> {
tracing::debug!("Typeid: {:?}", std::any::TypeId::of::<BeaconListenerMap>());
let listener_ids = sqlx::query!("SELECT listener_id FROM beacon_listener")
.fetch_all(&db)
.await?;
@@ -56,6 +53,15 @@ struct ListenerState {
db: SqlitePool
}
struct Listener {
listener_id: i64,
port: i64,
public_ip: String,
domain_name: String,
certificate: Vec<u8>,
privkey: Vec<u8>
}
pub async fn start_listener(beacon_listener_map: BeaconListenerMap, listener_id: i64, db: SqlitePool) -> Result<(), crate::error::Error> {
{
let Ok(blm_handle) = beacon_listener_map.read() else {
@@ -66,7 +72,7 @@ pub async fn start_listener(beacon_listener_map: BeaconListenerMap, listener_id:
return Err(crate::error::Error::Generic("Beacon listener already started".to_string()));
}
}
let listener = sqlx::query!("SELECT * FROM beacon_listener WHERE listener_id = ?", listener_id)
let listener = sqlx::query_as!(Listener, "SELECT * FROM beacon_listener WHERE listener_id = ?", listener_id)
.fetch_one(&db)
.await?;
@@ -84,17 +90,30 @@ pub async fn start_listener(beacon_listener_map: BeaconListenerMap, listener_id:
let hidden_app = Router::new().nest("/hidden_sparse", app);
let tls_config = RustlsConfig::from_pem(
listener.certificate.as_bytes().to_vec(),
listener.privkey.as_bytes().to_vec()
).await?;
let keypair = match rustls::pki_types::PrivateKeyDer::try_from(listener.privkey.clone()) {
Ok(pk) => pk,
Err(e) => {
return Err(crate::error::Error::Generic(format!("Could not parse private key: {e}")));
}
};
let cert = rustls::pki_types::CertificateDer::from(listener.certificate.clone());
let mut tls_config = rustls::ServerConfig::builder()
.with_no_client_auth()
.with_single_cert(vec![cert], keypair)?;
tls_config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
let addr = std::net::SocketAddr::from(([0, 0, 0, 0], listener.port as u16));
tracing::debug!("Starting listener {}, {}, on port {}", listener_id, listener.domain_name, listener.port);
let join_handle = tokio::task::spawn(async move {
let res = axum_server::tls_rustls::bind_rustls(addr, tls_config)
let res = axum_server::tls_rustls::bind_rustls(
addr,
axum_server::tls_rustls::RustlsConfig::from_config(
Arc::new(tls_config)
)
)
.serve(hidden_app.into_make_service())
.await;