generated from PlexSheep/rs-base
All checks were successful
cargo devel CI / cargo CI (push) Successful in 1m29s
49 lines
1.3 KiB
Rust
49 lines
1.3 KiB
Rust
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}");
|
|
};
|
|
});
|
|
}
|
|
}
|
|
}
|