feat: challenge 1
cargo devel CI / cargo CI (push) Successful in 1m29s Details

This commit is contained in:
Christoph J. Scherr 2024-09-05 17:18:08 +02:00
parent 11810b7d13
commit 98e4ce0b73
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
6 changed files with 158 additions and 2 deletions

View File

@ -12,3 +12,7 @@ repository = "https://git.cscherr.de/PlexSheep/wooly-vault"
[dependencies] [dependencies]
anyhow = "1.0.86"
async-trait = "0.1.82"
libpt = { version = "0.6.0", features = ["log"] }
tokio = { version = "1.40.0", features = ["macros", "net", "time", "io-util", "rt", "sync"] }

49
src/challenge/c1.rs Normal file
View File

@ -0,0 +1,49 @@
use async_trait::async_trait;
use libpt::log::{info, warn};
use tokio::io::AsyncWriteExt;
use tokio::net::TcpListener;
use super::Challenge;
use crate::has_won;
use crate::vault::{Config, VaultRef};
pub struct C1 {
config: Config,
vault: VaultRef,
}
impl C1 {}
#[async_trait]
impl Challenge for C1 {
fn new(config: Config, vault: VaultRef) -> Self {
Self { config, vault }
}
fn hint() -> String {
String::from("TCP connect to 1337")
}
async fn serve(self) -> anyhow::Result<()> {
info!("serving challenge 1");
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;
}
};
has_won(addr);
tokio::spawn(async move {
if let Err(e) = stream.write_all(vault.secret().as_bytes()).await {
warn!("could not write to peer {addr}: {e}");
};
if let Err(e) = stream.shutdown().await {
warn!("could end connection to peer {addr}: {e}");
};
});
}
}
}

24
src/challenge/mod.rs Normal file
View File

@ -0,0 +1,24 @@
use anyhow::anyhow;
use async_trait::async_trait;
use crate::vault::{Config, VaultRef};
use self::c1::C1;
pub mod c1;
#[async_trait]
pub trait Challenge
where
Self: Sized,
{
fn new(config: Config, vault: VaultRef) -> Self;
fn hint() -> String;
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)),
_ => Err(anyhow!("no challenge with that index exists")),
}
}
}

View File

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

View File

@ -1,3 +1,41 @@
fn main() { use anyhow::anyhow;
println!("Hello, world!"); use libpt::log::{debug, error};
use std::io::Read;
use wooly_vault::challenge::c1::C1;
use wooly_vault::challenge::Challenge;
use wooly_vault::vault::{Config, Vault};
#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
let _logger = libpt::log::Logger::builder()
.set_level(libpt::log::Level::TRACE)
.display_time(false)
.build()?;
println!("Input the secret and press enter");
let mut buf: String = String::new();
std::io::stdin().read_line(&mut buf)?;
let secret = buf.trim();
let conf = Config::default();
let v = Vault::new(secret);
println!("What challenge to serve?");
let i = select_challenge()?;
match i {
1 => {
C1::new(conf, v).serve().await?;
}
_ => return Err(anyhow!("no challenge with index {i} does currently exist")),
}
Ok(())
}
fn select_challenge() -> anyhow::Result<u16> {
let mut buf: String = String::new();
std::io::stdin().read_line(&mut buf)?;
Ok(buf.trim().parse()?)
} }

35
src/vault.rs Normal file
View File

@ -0,0 +1,35 @@
use std::net::SocketAddr;
use std::str::FromStr;
use std::sync::Arc;
pub type VaultRef = Arc<Vault>;
#[derive(Debug, Clone, PartialEq, Copy)]
pub struct Config {
pub addr: SocketAddr,
}
impl Default for Config {
fn default() -> Self {
Self {
addr: SocketAddr::from_str("127.0.0.1:1337").unwrap(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Vault {
secret: String,
}
impl Vault {
pub fn new(secret: &str) -> VaultRef {
let v = Self {
secret: secret.to_string(),
};
Arc::new(v)
}
pub fn secret(&self) -> &str {
&self.secret
}
}