feat: host challenges on different ports #8
cargo devel CI / cargo CI (push) Has been cancelled Details

This commit is contained in:
Christoph J. Scherr 2024-09-08 04:17:17 +02:00
parent fb10ca1464
commit 5f29c42aeb
5 changed files with 38 additions and 15 deletions

View File

@ -3,6 +3,8 @@
//! This challenge is designed to be simple and straightforward, requiring the contestant to only
//! establish a TCP connection to the server to receive the secret.
use std::net::SocketAddr;
use async_trait::async_trait;
use libpt::log::warn;
use tokio::io::AsyncWriteExt;
@ -42,8 +44,8 @@ impl ChallengeLike for C1 {
}
}
async fn serve(&self) -> anyhow::Result<()> {
let listener = TcpListener::bind(self.config.addr).await?;
async fn serve(&self, addr: SocketAddr) -> anyhow::Result<()> {
let listener = TcpListener::bind(addr).await?;
loop {
let vault = self.vault.clone();

View File

@ -4,6 +4,8 @@
//! This challenge is designed to be simple and straightforward, but still requires some thinkering
//! for newbies, especially those that sill only think in text.
use std::net::SocketAddr;
use anyhow::Result;
use async_trait::async_trait;
use libpt::log::{info, warn};
@ -69,8 +71,8 @@ impl ChallengeLike for C2 {
}
}
async fn serve(&self) -> anyhow::Result<()> {
let listener = TcpListener::bind(self.config.addr).await?;
async fn serve(&self, addr: SocketAddr) -> anyhow::Result<()> {
let listener = TcpListener::bind(addr).await?;
loop {
let vault = self.vault.clone();

View File

@ -231,9 +231,9 @@ impl ChallengeLike for C3 {
}
}
async fn serve(&self) -> anyhow::Result<()> {
async fn serve(&self, addr: SocketAddr) -> anyhow::Result<()> {
info!("serving challenge 3");
let listener = TcpListener::bind(self.config.addr).await?;
let listener = TcpListener::bind(addr).await?;
loop {
let vault = self.vault.clone();

View File

@ -3,6 +3,8 @@
//! This module is the core of the Wooly Vault application, as it defines the interface that all
//! challenges must implement, and contains the challenge modules themselves.
use std::net::SocketAddr;
use async_trait::async_trait;
use libpt::log::{error, info};
use serde::{Deserialize, Serialize};
@ -120,7 +122,7 @@ where
/// # Errors
///
/// Will error when the challenge errors, for example when the network adress cannot be bound.
async fn serve(&self) -> anyhow::Result<()>;
async fn serve(&self, addr: SocketAddr) -> anyhow::Result<()>;
/// Serves a challenge and sets up the hint and monitoring service for the admin.
///
/// This method not only serves the challenge, but it also sets up a small webservice for the
@ -135,11 +137,11 @@ where
/// # Returns
///
/// A result indicating whether the challenge and the admin interface were successfully served.
async fn start(&self) -> anyhow::Result<()> {
async fn start(&self, addr: SocketAddr) -> anyhow::Result<()> {
let c = self.clone();
tokio::spawn(async move {
info!("starting C{}", Self::text().id());
if let Err(e) = c.serve().await {
if let Err(e) = c.serve(addr).await {
error!("challenge {} has crashed! {e:#?}", Self::text().title());
};
});

View File

@ -14,6 +14,8 @@
//! Wooly Vault is programmed asynchronously with [tokio] to be able to handle many contestants at
//! once if needed.
use std::net::SocketAddr;
use anyhow::anyhow;
use libpt::log::info;
@ -60,9 +62,21 @@ pub async fn select_and_start_single(
) -> anyhow::Result<()> {
info!("select+start");
match index {
1 => challenge::c1::C1::new(config, vault).start().await?,
2 => challenge::c2::C2::new(config, vault).start().await?,
3 => challenge::c3::C3::new(config, vault).start().await?,
1 => {
challenge::c1::C1::new(config.clone(), vault)
.start(config.addr)
.await?
}
2 => {
challenge::c2::C2::new(config.clone(), vault)
.start(config.addr)
.await?
}
3 => {
challenge::c3::C3::new(config.clone(), vault)
.start(config.addr)
.await?
}
_ => {
return Err(anyhow!(
"no challenge with index {index} does currently exist"
@ -74,13 +88,16 @@ pub async fn select_and_start_single(
// TODO: actually use different vaults for the challenges #7
pub async fn start_all(config: Config, vault: VaultRef) -> anyhow::Result<()> {
let ip = config.addr.ip();
let start_port = config.addr.port();
let c1 = challenge::c1::C1::new(config.clone(), vault.clone());
let c2 = challenge::c2::C2::new(config.clone(), vault.clone());
let c3 = challenge::c3::C3::new(config.clone(), vault.clone());
c1.start().await?;
c2.start().await?;
c3.start().await?;
c1.start(SocketAddr::new(ip, start_port)).await?;
c2.start(SocketAddr::new(ip, start_port + 1)).await?;
c3.start(SocketAddr::new(ip, start_port + 2)).await?;
meta::serve(
vec![