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