Serve command #27
+5
-1
@@ -1,3 +1,4 @@
|
|||||||
|
use std::net::IpAddr;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use clap::{Args, Parser, Subcommand};
|
use clap::{Args, Parser, Subcommand};
|
||||||
@@ -50,5 +51,8 @@ pub enum Commands {
|
|||||||
/// Generate completions for your current shell
|
/// Generate completions for your current shell
|
||||||
ShellCompletions,
|
ShellCompletions,
|
||||||
/// Serve config files over tftp
|
/// Serve config files over tftp
|
||||||
Serve,
|
Serve {
|
||||||
|
#[arg(long, default_value = "0.0.0.0")]
|
||||||
|
ip: IpAddr,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -107,11 +107,11 @@ async fn main() -> Result<(), Error> {
|
|||||||
"crete",
|
"crete",
|
||||||
&mut std::io::stdout(),
|
&mut std::io::stdout(),
|
||||||
),
|
),
|
||||||
Commands::Serve => {
|
Commands::Serve { ip } => {
|
||||||
let re = Regex::new(r"talos/(?<cluster>.+)/(?<node>.+)\.ya?ml")
|
let re = Regex::new(r"talos/(?<cluster>.+)/(?<node>.+)\.ya?ml")
|
||||||
.expect("regex should be valid");
|
.expect("regex should be valid");
|
||||||
|
|
||||||
tftp::serve(move |filename, _| {
|
tftp::serve(ip, move |filename, _| {
|
||||||
let filename = filename.to_owned();
|
let filename = filename.to_owned();
|
||||||
let re = re.clone();
|
let re = re.clone();
|
||||||
|
|
||||||
|
|||||||
+5
-4
@@ -1,5 +1,5 @@
|
|||||||
use std::io;
|
use std::io;
|
||||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4};
|
use std::net::{IpAddr, SocketAddr};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use socket_pktinfo::{PktInfo, PktInfoUdpSocket};
|
use socket_pktinfo::{PktInfo, PktInfoUdpSocket};
|
||||||
@@ -150,7 +150,7 @@ where
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn serve<F, Fut>(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(&str, IpAddr) -> Fut + Clone + Send + 'static,
|
||||||
Fut: Future<Output = Option<Vec<u8>>> + Send + 'static,
|
Fut: Future<Output = Option<Vec<u8>>> + Send + 'static,
|
||||||
@@ -158,12 +158,13 @@ where
|
|||||||
let socket = PktInfoUdpSocket::new(Domain::IPV4)?;
|
let socket = PktInfoUdpSocket::new(Domain::IPV4)?;
|
||||||
socket.set_nonblocking(true)?;
|
socket.set_nonblocking(true)?;
|
||||||
|
|
||||||
socket.bind(&SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 69).into())?;
|
let addr: SocketAddr = (ip, 69).into();
|
||||||
|
socket.bind(&addr.into())?;
|
||||||
|
|
||||||
let async_fd = AsyncFd::new(socket)?;
|
let async_fd = AsyncFd::new(socket)?;
|
||||||
let mut buf = [0u8; PACKET_SIZE];
|
let mut buf = [0u8; PACKET_SIZE];
|
||||||
|
|
||||||
println!("Server started");
|
println!("Tftp server started on {addr}");
|
||||||
loop {
|
loop {
|
||||||
let mut guard = async_fd.readable().await?;
|
let mut guard = async_fd.readable().await?;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user