feat(c3): add time limit
cargo devel CI / cargo CI (push) Successful in 1m32s Details

This commit is contained in:
Christoph J. Scherr 2024-09-06 20:01:47 +02:00
parent f3129b1d50
commit 1f6363c053
2 changed files with 19 additions and 0 deletions

View File

@ -1,5 +1,6 @@
from io import TextIOWrapper from io import TextIOWrapper
import socket import socket
import time
REMOTE = "127.0.0.1" REMOTE = "127.0.0.1"
PORT = 1337 PORT = 1337
@ -50,10 +51,15 @@ def main() -> int:
print(f"# We won: '{response}'") print(f"# We won: '{response}'")
break break
elif "slow" in response:
print(f"# We are too slow, it's futile")
break
else: else:
print(f"! unknown response: '{response}'") print(f"! unknown response: '{response}'")
s.close() s.close()
return 1 return 1
s.close() s.close()
return 0 return 0

View File

@ -8,6 +8,7 @@ use rand::distributions::{Distribution, Standard};
use rand::{random, Rng}; use rand::{random, Rng};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::{TcpListener, TcpStream}; use tokio::net::{TcpListener, TcpStream};
use tokio::time::Instant;
use super::Challenge; use super::Challenge;
use crate::config::Config; use crate::config::Config;
@ -90,11 +91,22 @@ impl C3 {
let (stream_read, mut stream_write) = stream.split(); let (stream_read, mut stream_write) = stream.split();
let mut rdbuf = BufReader::new(stream_read); let mut rdbuf = BufReader::new(stream_read);
let mut last_reset = Instant::now();
loop { loop {
if correct >= NEEDED_CORRECT { if correct >= NEEDED_CORRECT {
Self::win(vault, &mut stream, &addr).await?; Self::win(vault, &mut stream, &addr).await?;
break; 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(); let (question, solution) = Self::mk_question();
debug!("sending question to {addr}: '{question}'"); debug!("sending question to {addr}: '{question}'");
stream_write.write_all((question + "\n").as_bytes()).await?; stream_write.write_all((question + "\n").as_bytes()).await?;
@ -112,6 +124,7 @@ impl C3 {
.write_all("wrong!\n".to_string().as_bytes()) .write_all("wrong!\n".to_string().as_bytes())
.await?; .await?;
correct = 0; correct = 0;
last_reset = Instant::now();
} }
buf.clear() buf.clear()
} }