cleanup, fix fmt and lint, fix tests, fix signature checking

This commit is contained in:
sad 2024-10-04 17:27:08 +00:00
parent 61db822921
commit 1f29c0e62c
3 changed files with 34 additions and 31 deletions

View File

@ -1,7 +1,8 @@
use clap::Parser; use clap::Parser;
#[derive(Parser, Debug)] #[derive(Parser, Debug, Clone)]
#[command(author = "perp")] #[command(author = "perp and sad")]
// CLI flags/arguments // CLI flags/arguments
pub struct Cli { pub struct Cli {
#[arg( #[arg(

View File

@ -44,27 +44,23 @@ pub fn parse_signatures(file_path: &str) -> Result<Vec<Signature>> {
fn unescape_string(s: &str) -> Result<Vec<u8>> { fn unescape_string(s: &str) -> Result<Vec<u8>> {
let mut result = Vec::new(); let mut result = Vec::new();
let mut chars = s.chars(); let mut chars = s.chars().peekable();
while let Some(c) = chars.next() { while let Some(c) = chars.next() {
if c == '\\' { if c == '\\' {
match chars.next() { match chars.next() {
Some('x') => { Some('x') => {
let hex = chars.next().and_then(|c1| { let hex: String = chars.by_ref().take(2).collect();
chars.next().map(|c2| format!("{}{}", c1, c2)) if hex.len() == 2 {
}).unwrap_or_else(|| {
result.push(b'\\');
result.push(b'x');
String::new()
});
if !hex.is_empty() {
if let Ok(byte) = u8::from_str_radix(&hex, 16) { if let Ok(byte) = u8::from_str_radix(&hex, 16) {
result.push(byte); result.push(byte);
} else { } else {
result.push(b'\\'); result.extend(b"\\x");
result.push(b'x');
result.extend(hex.bytes()); result.extend(hex.bytes());
} }
} else {
result.extend(b"\\x");
result.extend(hex.bytes());
} }
}, },
Some('0') => result.push(0), Some('0') => result.push(0),

View File

@ -1,6 +1,7 @@
use clap::Parser; use clap::Parser;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use tokio::net::TcpListener; use tokio::net::TcpListener;
use tokio::io::AsyncWriteExt;
use tracing::{debug, error, info, Level}; use tracing::{debug, error, info, Level};
mod cli; mod cli;
@ -29,23 +30,22 @@ async fn main() -> anyhow::Result<()> {
return Err(e); return Err(e);
} }
}; };
debug!("Read {} signatures", signatures.len()); //debug!("Read {} signatures", signatures.len());
// Bind listener
let listener = TcpListener::bind(&cli.listen).await?; let listener = TcpListener::bind(&cli.listen).await?;
info!("Started listener on {}", cli.listen); info!("Started listener on {}", cli.listen);
loop { loop {
// Accept connection // Accept connection
let (stream, address) = listener.accept().await?; let (mut stream, address) = listener.accept().await?;
if cli.debug { if cli.debug {
debug!("Accepted connection from {}", address); debug!("Accepted connection from {}", address);
} else if cli.verbose { } else if cli.verbose {
info!("Accepted connection from {}", address); info!("Accepted connection from {}", address);
} }
// Clone signatures
let sigs = signatures.clone(); let sigs = signatures.clone();
let cli_clone = cli.clone();
// Spawn async thread // Spawn async thread
tokio::spawn(async move { tokio::spawn(async move {
@ -53,23 +53,29 @@ async fn main() -> anyhow::Result<()> {
let signature = sigs.choose(&mut rand::thread_rng()); let signature = sigs.choose(&mut rand::thread_rng());
if let Some(sig) = signature { if let Some(sig) = signature {
// Generate payload
let payload = generate_payload(sig); let payload = generate_payload(sig);
// Write payload // Write payload
if let Err(e) = stream.try_write(&payload) { match stream.write_all(&payload).await {
error!("Failed to write payload to {}: {}", address, e); Ok(()) => {
return; if cli_clone.debug {
}
if cli.debug {
debug!( debug!(
"Sent payload to {}: {:?}", "Sent payload to {}: {:?} ({} bytes)",
address, address,
String::from_utf8_lossy(&payload) String::from_utf8_lossy(&payload),
payload.len()
); );
} else if cli.verbose { } else if cli_clone.verbose {
info!("Sent payload to {}", address); info!("Sent payload ({} bytes) to {}", payload.len(), address);
}
}
Err(e) => {
if e.kind() == std::io::ErrorKind::ConnectionReset {
debug!("Connection reset by peer: {}", address);
} else {
error!("Failed to write payload to {}: {}", address, e);
}
}
} }
} else { } else {
debug!("No signature available"); debug!("No signature available");