|
|
@ -1,11 +1,10 @@ |
|
|
|
use crate::{Response, StatusCode};
|
|
|
|
use async_std::{
|
|
|
|
io::prelude::*,
|
|
|
|
use tokio::{
|
|
|
|
prelude::*,
|
|
|
|
net::{TcpListener, TcpStream},
|
|
|
|
stream::StreamExt,
|
|
|
|
task,
|
|
|
|
};
|
|
|
|
use async_tls::TlsAcceptor;
|
|
|
|
use tokio_rustls::TlsAcceptor;
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use rustls::Certificate;
|
|
|
|
use std::{error::Error as StdError, net::SocketAddr, sync::Arc};
|
|
|
@ -53,12 +52,10 @@ where |
|
|
|
{
|
|
|
|
let cfg = Arc::new(cfg);
|
|
|
|
let listener = TcpListener::bind(&format!("{}:{}", host, port)).await?;
|
|
|
|
let mut incoming = listener.incoming();
|
|
|
|
let acceptor = Arc::new(TlsAcceptor::from(cfg.clone()));
|
|
|
|
while let Some(Ok(stream)) = incoming.next().await {
|
|
|
|
while let Ok((stream, addr)) = listener.accept().await {
|
|
|
|
let h = h.clone();
|
|
|
|
let acceptor = acceptor.clone();
|
|
|
|
let addr = stream.peer_addr().unwrap();
|
|
|
|
let port = port.clone();
|
|
|
|
|
|
|
|
task::spawn(handle_request(h, stream, acceptor, addr, port));
|
|
|
@ -83,7 +80,7 @@ async fn handle_request( |
|
|
|
if let Some(u_port) = url.port() {
|
|
|
|
if port != u_port {
|
|
|
|
let _ = write_header(
|
|
|
|
&mut stream,
|
|
|
|
stream,
|
|
|
|
StatusCode::ProxyRequestRefused,
|
|
|
|
"Cannot proxy to that URL",
|
|
|
|
)
|
|
|
@ -117,7 +114,7 @@ async fn handle_request( |
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn write_header<W: Write + Unpin>(
|
|
|
|
pub async fn write_header<W: AsyncWrite + Unpin>(
|
|
|
|
mut stream: W,
|
|
|
|
status: StatusCode,
|
|
|
|
meta: &str,
|
|
|
@ -129,7 +126,7 @@ pub async fn write_header<W: Write + Unpin>( |
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the URL requested by the client.
|
|
|
|
async fn parse_request<R: Read + Unpin>(mut stream: R) -> Result<Url> {
|
|
|
|
async fn parse_request<R: AsyncRead + Unpin>(mut stream: R) -> Result<Url> {
|
|
|
|
// Because requests are limited to 1024 bytes (plus 2 bytes for CRLF), we
|
|
|
|
// can use a fixed-sized buffer on the stack, avoiding allocations and
|
|
|
|
// copying, and stopping bad clients from making us use too much memory.
|
|
|
@ -167,7 +164,7 @@ async fn handle<T>( |
|
|
|
stream: &mut T,
|
|
|
|
addr: SocketAddr,
|
|
|
|
) where
|
|
|
|
T: Write + Unpin,
|
|
|
|
T: AsyncWrite + Unpin,
|
|
|
|
{
|
|
|
|
let u = req.url.clone();
|
|
|
|
match h.handle(req).await {
|
|
|
|