primitive tlsping
cargo devel CI / cargo CI (push) Successful in 2m28s Details

This commit is contained in:
Christoph J. Scherr 2024-01-24 11:23:15 +01:00
parent 7188f424ba
commit 4595f37f4c
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
1 changed files with 17 additions and 13 deletions

View File

@ -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<TcpStream>,
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(())