generated from PlexSheep/rs-base
feat(c3): add time limit
cargo devel CI / cargo CI (push) Successful in 1m32s
Details
cargo devel CI / cargo CI (push) Successful in 1m32s
Details
This commit is contained in:
parent
f3129b1d50
commit
1f6363c053
|
@ -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
|
||||
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue