feat: added the ability to respond to ARP requests

This commit is contained in:
Andrew Rioux
2023-09-26 01:57:10 -04:00
parent 0bda72491c
commit 0bb2871568
4 changed files with 219 additions and 45 deletions

View File

@@ -1,4 +1,9 @@
use std::{io::prelude::*, net::UdpSocket, thread, sync::{Arc, Mutex, Condvar}};
use std::{
io::prelude::*,
net::UdpSocket,
sync::{Arc, Condvar, Mutex},
thread,
};
use anyhow::{anyhow, Context};
use ed25519_dalek::{Keypair, Signer};
@@ -17,7 +22,7 @@ impl Msg<'_> {
0 => Some(Msg::Ready(bytes[1])),
1 => Some(Msg::Stdout(&bytes[1..])),
2 => Some(Msg::Stderr(&bytes[1..])),
_ => None
_ => None,
}
}
}
@@ -47,8 +52,12 @@ fn main() -> anyhow::Result<()> {
let mut buffer = [0u8; 1536];
loop {
let Ok(amount) = remote_listen.recv(&mut buffer[..]) else { continue; };
let Some(msg) = Msg::parse(&buffer[..amount]) else { continue; };
let Ok(amount) = remote_listen.recv(&mut buffer[..]) else {
continue;
};
let Some(msg) = Msg::parse(&buffer[..amount]) else {
continue;
};
match msg {
Msg::Ready(_) => {
@@ -59,12 +68,14 @@ fn main() -> anyhow::Result<()> {
};
*inuse = false;
cvar.notify_one();
},
}
Msg::Stderr(err) => {
let _ = stderr.write(err);
},
}
Msg::Stdout(out) => {
let Ok(mut stdout) = stdout_arc.lock() else { continue; };
let Ok(mut stdout) = stdout_arc.lock() else {
continue;
};
let _ = stdout.write(out);
}
}
@@ -92,13 +103,19 @@ fn main() -> anyhow::Result<()> {
}
{
let Ok(mut stdout) = stdout_arc.lock() else { continue; };
let Ok(_) = write!(&*stdout, "root@{}:{} # ", &target, &cwd) else { continue; };
let Ok(mut stdout) = stdout_arc.lock() else {
continue;
};
let Ok(_) = write!(&*stdout, "root@{}:{} # ", &target, &cwd) else {
continue;
};
let _ = stdout.flush();
}
let mut cmd = String::new();
let Ok(amount) = stdin.read_line(&mut cmd) else { continue; };
let Ok(amount) = stdin.read_line(&mut cmd) else {
continue;
};
let mut cmd = cmd.trim();
if amount == 0 {
@@ -113,8 +130,10 @@ fn main() -> anyhow::Result<()> {
};
match cmd.split(" ").collect::<Vec<_>>()[..] {
["exit"] => { break },
["cd", dir] => { cwd = dir.to_owned(); },
["exit"] => break,
["cd", dir] => {
cwd = dir.to_owned();
}
_ => {
let (lock, _) = &*stdout_inuse;
let Ok(mut inuse) = lock.lock() else {