diff --git a/src/server/mod.rs b/src/server/mod.rs index 0543407..7fc22ec 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -10,7 +10,7 @@ use libpt::log::{debug, error, info, trace, warn}; use rustls::pki_types::{CertificateDer, PrivateKeyDer}; use rustls_pemfile::{certs, private_key}; use tokio::{ - io::{AsyncReadExt, AsyncWriteExt}, + io::{copy, sink, AsyncReadExt, AsyncWriteExt}, net::{TcpListener, TcpStream}, time::{self}, }; @@ -21,7 +21,7 @@ use crate::common::conf::Config; pub mod errors; use errors::*; -const BUF_SIZE: usize = 64; +const BUF_SIZE: usize = 512; pub struct Server { cfg: Config, @@ -148,21 +148,25 @@ impl Server { mut stream: tokio_rustls::server::TlsStream, addr: SocketAddr, ) -> Result<()> { + let mut buf = [0; BUF_SIZE]; + let mut pings = 0; debug!("new peer: {:?}", addr); - loop { - stream - .write_all( - &b"HTTP/1.0 200 ok\r\n\ - Connection: close\r\n\ - Content-length: 12\r\n\ - \r\n\ - Hello world!"[..], - ) - .await?; + while stream.read(&mut buf).await? > 0 { + let request = self.decode(&buf)?; + debug!("< {:?}\n{}", addr, request); + if request == "ping" { + pings += 1; + } + if pings > 20 { + stream.write_all(b"You win!").await?; + stream.flush().await?; + stream.shutdown().await?; + break; + } + stream.write_all(b"pong").await?; stream.flush().await?; // we should wait, so that we don't spam the client std::thread::sleep(self.cfg.delay); - break; } debug!("disconnected peer: {:?}", addr); Ok(())