use std::{
str::FromStr,
net::SocketAddr,
};
use tokio::net::TcpListener;
use hyper::{
Body,
Method,
Request,
Response,
StatusCode,
server::{
conn::Http,
accept::from_stream,
},
service::{make_service_fn, service_fn},
};
use ciph::salsa::{Psk, Acceptor, Randomness, TcpListenAcceptor};
async fn echo(req: Request
) -> Result, hyper::Error> {
match (req.method(), req.uri().path()) {
(&Method::POST, "/") => Ok(Response::new(req.into_body())),
_ => {
let mut not_found = Response::default();
*not_found.status_mut() = StatusCode::NOT_FOUND;
Ok(not_found)
},
}
}
#[tokio::main]
async fn main() -> Result<(), Box> {
const PSK_B64: &str = include_str!("example.psk");
let psk = Psk::from_str(PSK_B64)?;
let acceptor = Acceptor::new(psk, Randomness::Entropy);
let args: Vec = std::env::args()
.collect();
assert!(args.len() >= 2);
let listen_addr = args[1].to_owned();
let addr = SocketAddr::from_str(listen_addr.as_str())?;
let listener = TcpListener::bind(&addr).await?;
let acceptor = TcpListenAcceptor::new(acceptor, listener);
let incoming = from_stream(acceptor);
let service = make_service_fn(|_| async {
Ok::<_, hyper::Error>(service_fn(echo))
});
let server = hyper::server::Builder::new(incoming, Http::new())
.serve(service);
server.await?;
Ok(())
}