diff --git a/solutions/3.py b/solutions/3.py index c5bcd8a..304b947 100644 --- a/solutions/3.py +++ b/solutions/3.py @@ -1,5 +1,6 @@ from io import TextIOWrapper import socket +import time REMOTE = "127.0.0.1" PORT = 1337 @@ -50,10 +51,15 @@ def main() -> int: print(f"# We won: '{response}'") break + elif "slow" in response: + print(f"# We are too slow, it's futile") + break + else: print(f"! unknown response: '{response}'") s.close() return 1 + s.close() return 0 diff --git a/src/challenge/c3.rs b/src/challenge/c3.rs index ba3bb5f..3ee28aa 100644 --- a/src/challenge/c3.rs +++ b/src/challenge/c3.rs @@ -8,6 +8,7 @@ use rand::distributions::{Distribution, Standard}; use rand::{random, Rng}; use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; use tokio::net::{TcpListener, TcpStream}; +use tokio::time::Instant; use super::Challenge; use crate::config::Config; @@ -90,11 +91,22 @@ impl C3 { let (stream_read, mut stream_write) = stream.split(); let mut rdbuf = BufReader::new(stream_read); + let mut last_reset = Instant::now(); + loop { if correct >= NEEDED_CORRECT { Self::win(vault, &mut stream, &addr).await?; break; } + if last_reset.elapsed().as_millis() > NEEDED_CORRECT as u128 * 200 { + correct = 0; + debug!("{addr} was too slow, resetting"); + stream_write + .write_all("You're too slow!\n".to_string().as_bytes()) + .await?; + last_reset = Instant::now(); + continue; + } let (question, solution) = Self::mk_question(); debug!("sending question to {addr}: '{question}'"); stream_write.write_all((question + "\n").as_bytes()).await?; @@ -112,6 +124,7 @@ impl C3 { .write_all("wrong!\n".to_string().as_bytes()) .await?; correct = 0; + last_reset = Instant::now(); } buf.clear() }