Serve command #27

Merged
Dreaded_X merged 20 commits from feature/tftp_side into main 2026-10-21 05:15:47 +00:00
3 changed files with 39 additions and 45 deletions
Showing only changes of commit b9251ee5dc - Show all commits
+32 -35
View File
@@ -4,55 +4,52 @@ use ipxe::IPXE_DIR;
use minijinja::{Environment, context}; use minijinja::{Environment, context};
use regex::regex; use regex::regex;
// TODO: Make this a proper async function pub async fn get_file(
pub fn get_file(
filename: impl ToString, filename: impl ToString,
http_url: impl ToString, http_url: impl ToString,
) -> impl Future<Output = Option<(Vec<u8>, &'static str)>> { ) -> Option<(Vec<u8>, &'static str)> {
let re = regex!(r"^talos/(?<cluster>.+)/(?<node>.+)\.ya?ml$"); let re = regex!(r"^talos/(?<cluster>.+)/(?<node>.+)\.ya?ml$");
let filename: String = filename.to_string(); let filename: String = filename.to_string();
let re = re.clone(); let re = re.clone();
async move { const IPXE_PATTERN: &str = "ipxe/";
const IPXE_PATTERN: &str = "ipxe/"; if filename == format!("{IPXE_PATTERN}boot.ipxe") {
if filename == format!("{IPXE_PATTERN}boot.ipxe") { let mut env = Environment::new();
let mut env = Environment::new();
configure_environment(&mut env); configure_environment(&mut env);
let template = env let template = env
.template_from_str(include_str!("./boot.ipxe")) .template_from_str(include_str!("./boot.ipxe"))
.expect("Template should be valid"); .expect("Template should be valid");
let clusters = Cluster::get_all().await; let clusters = Cluster::get_all().await;
let content = template let content = template
.render(context! { .render(context! {
clusters => clusters.values().collect::<Vec<_>>(), clusters => clusters.values().collect::<Vec<_>>(),
httpUrl => http_url.to_string() httpUrl => http_url.to_string()
}) })
.unwrap(); .unwrap();
Some((content.into(), "text/plain; charset=utf-8")) Some((content.into(), "text/plain; charset=utf-8"))
} else if let Some(filename) = filename.strip_prefix(IPXE_PATTERN) { } else if let Some(filename) = filename.strip_prefix(IPXE_PATTERN) {
Some(( Some((
IPXE_DIR.get_file(filename)?.contents().to_owned(), IPXE_DIR.get_file(filename)?.contents().to_owned(),
"application/octet-stream", "application/octet-stream",
)) ))
} else if let Some(caps) = re.captures(&filename) { } else if let Some(caps) = re.captures(&filename) {
let cluster = &caps["cluster"]; let cluster = &caps["cluster"];
let node = &caps["node"]; let node = &caps["node"];
let clusters = Cluster::get_all().await; let clusters = Cluster::get_all().await;
let cluster = clusters.get(cluster)?; let cluster = clusters.get(cluster)?;
let node = cluster.node(node)?; let node = cluster.node(node)?;
let config = node.config(cluster).await; let config = node.config(cluster).await;
Some((config.into(), "application/yaml; charset=utf-8")) Some((config.into(), "application/yaml; charset=utf-8"))
} else { } else {
None None
}
} }
} }
+4 -7
View File
@@ -114,13 +114,10 @@ async fn main() -> Result<(), Error> {
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();
}); });
tftp::serve(ip, move |filename, ip| { tftp::serve(ip, move |filename, ip| async move {
let filename = filename.to_owned(); get_file(filename, format!("http://{ip}:{port}"))
async move { .await
get_file(filename, format!("http://{ip}:{port}")) .map(|(file, _)| file)
.await
.map(|(file, _)| file)
}
}) })
.await?; .await?;
} }
+3 -3
View File
@@ -37,7 +37,7 @@ pub async fn handle_connection<F, Fut>(
callback: F, callback: F,
) -> Result<(), Error> ) -> Result<(), Error>
where where
F: Fn(&str, IpAddr) -> Fut, F: Fn(String, IpAddr) -> Fut,
Fut: Future<Output = Option<Vec<u8>>>, Fut: Future<Output = Option<Vec<u8>>>,
{ {
let src = info.addr_src; let src = info.addr_src;
@@ -59,7 +59,7 @@ where
let filename = request.filename(); let filename = request.filename();
println!("{src}: {filename} [{:?}]", request.mode()); println!("{src}: {filename} [{:?}]", request.mode());
let Some(data) = callback(filename, info.addr_dst).await else { let Some(data) = callback(filename.to_string(), info.addr_dst).await else {
Packet::error(ErrorCode::FileNotFound, "Unknown file") Packet::error(ErrorCode::FileNotFound, "Unknown file")
.send_to(&socket, &src) .send_to(&socket, &src)
.await?; .await?;
@@ -152,7 +152,7 @@ where
pub async fn serve<F, Fut>(ip: IpAddr, callback: F) -> Result<!, Error> pub async fn serve<F, Fut>(ip: IpAddr, callback: F) -> Result<!, Error>
where where
F: Fn(&str, IpAddr) -> Fut + Clone + Send + 'static, F: Fn(String, IpAddr) -> Fut + Clone + Send + 'static,
Fut: Future<Output = Option<Vec<u8>>> + Send + 'static, Fut: Future<Output = Option<Vec<u8>>> + Send + 'static,
{ {
let socket = PktInfoUdpSocket::new(Domain::IPV4)?; let socket = PktInfoUdpSocket::new(Domain::IPV4)?;