feat: challenge 2 + better hints
cargo devel CI / cargo CI (push) Successful in 1m30s Details

This commit is contained in:
Christoph J. Scherr 2024-09-06 12:01:48 +02:00
parent 4fdcaa977f
commit 320625ecaf
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
5 changed files with 99 additions and 19 deletions

View File

@ -19,8 +19,8 @@ impl Challenge for C1 {
fn new(config: Config, vault: VaultRef) -> Self { fn new(config: Config, vault: VaultRef) -> Self {
Self { config, vault } Self { config, vault }
} }
fn hint() -> String { fn hints() -> Vec<String> {
String::from("TCP connect to 1337") vec![String::from("TCP connect to 1337")]
} }
async fn serve(self) -> anyhow::Result<()> { async fn serve(self) -> anyhow::Result<()> {
info!("serving challenge 1"); info!("serving challenge 1");
@ -35,7 +35,7 @@ impl Challenge for C1 {
continue; continue;
} }
}; };
has_won(addr); has_won(&addr);
tokio::spawn(async move { tokio::spawn(async move {
if let Err(e) = stream.write_all(vault.secret().as_bytes()).await { if let Err(e) = stream.write_all(vault.secret().as_bytes()).await {
warn!("could not write to peer {addr}: {e}"); warn!("could not write to peer {addr}: {e}");

80
src/challenge/c2.rs Normal file
View File

@ -0,0 +1,80 @@
use anyhow::Result;
use async_trait::async_trait;
use libpt::log::{info, warn};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use super::Challenge;
use crate::has_won;
use crate::vault::{Config, VaultRef};
pub struct C2 {
config: Config,
vault: VaultRef,
}
impl C2 {
async fn win(
vault: &VaultRef,
stream: &mut TcpStream,
addr: &std::net::SocketAddr,
) -> Result<()> {
has_won(addr);
if let Err(e) = stream.write_all(vault.secret().as_bytes()).await {
warn!("could not write to peer {addr}: {e}");
return Err(e.into());
};
if let Err(e) = stream.shutdown().await {
warn!("could end connection to peer {addr}: {e}");
return Err(e.into());
};
Ok(())
}
}
#[async_trait]
impl Challenge for C2 {
fn new(config: Config, vault: VaultRef) -> Self {
Self { config, vault }
}
fn hints() -> Vec<String> {
vec![String::from(
"TCP connect to 1337 and give me a special u16",
)]
}
async fn serve(self) -> anyhow::Result<()> {
info!("serving challenge 2");
let listener = TcpListener::bind(self.config.addr).await?;
loop {
let vault = self.vault.clone();
let (mut stream, addr) = match listener.accept().await {
Ok(s) => s,
Err(err) => {
warn!("could not accept tcp stream: {err:?}");
continue;
}
};
info!("new peer: {addr}");
tokio::spawn(async move {
let mut buf: u16;
loop {
match stream.read_u16().await {
Err(e) => {
warn!("could read from {addr}: {e}");
return;
}
Ok(u) => buf = u,
}
if buf == 1337 {
if let Err(e) = Self::win(&vault, &mut stream, &addr).await {
warn!("could not let {addr} win: {e}");
}
} else {
info!("peer wrote crap: {buf}");
}
}
});
}
}
}

View File

@ -3,9 +3,8 @@ use async_trait::async_trait;
use crate::vault::{Config, VaultRef}; use crate::vault::{Config, VaultRef};
use self::c1::C1;
pub mod c1; pub mod c1;
pub mod c2;
#[async_trait] #[async_trait]
pub trait Challenge pub trait Challenge
@ -13,12 +12,19 @@ where
Self: Sized, Self: Sized,
{ {
fn new(config: Config, vault: VaultRef) -> Self; fn new(config: Config, vault: VaultRef) -> Self;
fn hint() -> String; fn hints() -> Vec<String>;
async fn serve(self) -> anyhow::Result<()>; async fn serve(self) -> anyhow::Result<()>;
fn get_challenge(idx: u16, config: Config, vault: VaultRef) -> anyhow::Result<impl Challenge> { }
match idx {
1 => Ok(C1::new(config, vault)), pub async fn select_and_start(index: u16, config: Config, vault: VaultRef) -> anyhow::Result<()> {
_ => Err(anyhow!("no challenge with that index exists")), match index {
1 => c1::C1::new(config, vault).serve().await?,
2 => c2::C2::new(config, vault).serve().await?,
_ => {
return Err(anyhow!(
"no challenge with index {index} does currently exist"
))
} }
} }
Ok(())
} }

View File

@ -2,6 +2,6 @@ pub mod challenge;
pub mod vault; pub mod vault;
#[inline] #[inline]
pub(crate) fn has_won(addr: std::net::SocketAddr) { pub(crate) fn has_won(addr: &std::net::SocketAddr) {
libpt::log::info!("Sending the secret to {addr}") libpt::log::info!("Sending the secret to {addr}")
} }

View File

@ -1,7 +1,6 @@
use anyhow::anyhow; use anyhow::anyhow;
use wooly_vault::challenge::c1::C1; use wooly_vault::challenge::select_and_start;
use wooly_vault::challenge::Challenge;
use wooly_vault::vault::{Config, Vault}; use wooly_vault::vault::{Config, Vault};
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
@ -22,12 +21,7 @@ async fn main() -> anyhow::Result<()> {
println!("What challenge to serve?"); println!("What challenge to serve?");
let i = select_challenge()?; let i = select_challenge()?;
match i { select_and_start(i, conf, v).await?;
1 => {
C1::new(conf, v).serve().await?;
}
_ => return Err(anyhow!("no challenge with index {i} does currently exist")),
}
Ok(()) Ok(())
} }