feat: added tcp

sorry Judah
This commit is contained in:
Andrew Rioux
2025-02-12 17:49:31 -05:00
parent e388b2eefa
commit f9ff9f266a
37 changed files with 1939 additions and 902 deletions

View File

@@ -3,14 +3,14 @@ use std::{
sync::{Arc, RwLock},
};
use axum::routing::{get, post, Router};
use axum::routing::{Router, get, post};
use sqlx::SqlitePool;
use tokio::task::JoinHandle;
pub mod error;
pub struct BeaconListenerHandle {
join_handle: JoinHandle<()>
join_handle: JoinHandle<()>,
}
impl BeaconListenerHandle {
@@ -34,7 +34,10 @@ impl std::ops::Deref for BeaconListenerMap {
}
}
pub async fn start_all_listeners(beacon_listener_map: BeaconListenerMap, db: SqlitePool) -> Result<(), crate::error::Error> {
pub async fn start_all_listeners(
beacon_listener_map: BeaconListenerMap,
db: SqlitePool,
) -> Result<(), crate::error::Error> {
let listener_ids = sqlx::query!("SELECT listener_id FROM beacon_listener")
.fetch_all(&db)
.await?;
@@ -42,7 +45,12 @@ pub async fn start_all_listeners(beacon_listener_map: BeaconListenerMap, db: Sql
tracing::info!("Starting {} listener(s)...", listener_ids.len());
for listener in listener_ids {
start_listener(beacon_listener_map.clone(), listener.listener_id, db.clone()).await?;
start_listener(
beacon_listener_map.clone(),
listener.listener_id,
db.clone(),
)
.await?;
}
Ok(())
@@ -50,7 +58,7 @@ pub async fn start_all_listeners(beacon_listener_map: BeaconListenerMap, db: Sql
#[derive(Clone)]
struct ListenerState {
db: SqlitePool
db: SqlitePool,
}
struct Listener {
@@ -59,41 +67,59 @@ struct Listener {
public_ip: String,
domain_name: String,
certificate: Vec<u8>,
privkey: Vec<u8>
privkey: Vec<u8>,
}
pub async fn start_listener(beacon_listener_map: BeaconListenerMap, listener_id: i64, db: SqlitePool) -> Result<(), crate::error::Error> {
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 {
return Err(crate::error::Error::Generic("Could not acquire write lock on beacon listener map".to_string()));
return Err(crate::error::Error::Generic(
"Could not acquire write lock on beacon listener map".to_string(),
));
};
if blm_handle.get(&listener_id).is_some() {
return Err(crate::error::Error::Generic("Beacon listener already started".to_string()));
return Err(crate::error::Error::Generic(
"Beacon listener already started".to_string(),
));
}
}
let listener = sqlx::query_as!(Listener, "SELECT * FROM beacon_listener WHERE listener_id = ?", listener_id)
.fetch_one(&db)
.await?;
let listener = sqlx::query_as!(
Listener,
"SELECT * FROM beacon_listener WHERE listener_id = ?",
listener_id
)
.fetch_one(&db)
.await?;
let app: Router<()> = Router::new()
.route("/register_beacon", post(|| async {
tracing::info!("Beacon attempting to register");
}))
.route("/test", get(|| async {
tracing::info!("Hello");
"hi there"
}))
.with_state(ListenerState {
db
});
.route(
"/register_beacon",
post(|| async {
tracing::info!("Beacon attempting to register");
}),
)
.route(
"/test",
get(|| async {
tracing::info!("Hello");
"hi there"
}),
)
.with_state(ListenerState { db });
let hidden_app = Router::new().nest("/hidden_sparse", app);
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}")));
return Err(crate::error::Error::Generic(format!(
"Could not parse private key: {e}"
)));
}
};
let cert = rustls::pki_types::CertificateDer::from(listener.certificate.clone());
@@ -105,17 +131,20 @@ pub async fn start_listener(beacon_listener_map: BeaconListenerMap, listener_id:
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);
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,
axum_server::tls_rustls::RustlsConfig::from_config(
Arc::new(tls_config)
)
axum_server::tls_rustls::RustlsConfig::from_config(Arc::new(tls_config)),
)
.serve(hidden_app.into_make_service())
.await;
.serve(hidden_app.into_make_service())
.await;
if let Err(e) = res {
tracing::error!("error running sparse listener: {e:?}");
@@ -123,12 +152,12 @@ pub async fn start_listener(beacon_listener_map: BeaconListenerMap, listener_id:
});
let Ok(mut blm_handle) = beacon_listener_map.write() else {
return Err(crate::error::Error::Generic("Could not acquire write lock on beacon listener map".to_string()));
return Err(crate::error::Error::Generic(
"Could not acquire write lock on beacon listener map".to_string(),
));
};
blm_handle.insert(listener_id, BeaconListenerHandle {
join_handle
});
blm_handle.insert(listener_id, BeaconListenerHandle { join_handle });
Ok(())
}