From f73fc5539bbbadd8683ab5ab5363d161c5071ca8 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sat, 10 Feb 2018 16:39:07 +0100 Subject: [PATCH] Rewrite without async/await --- Cargo.toml | 2 +- src/main.rs | 91 ++++++++++++++++++++++++++++------------------------- 2 files changed, 50 insertions(+), 43 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e855f3b..f836e31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ categories = ["asynchronous", "network-programming","command-line-utilities"] [dependencies] base64 = "~0.9" clap = "~2" -futures-await = "~0.1" +futures = "~0.1" hyper = "~0.11" tokio = "~0.1" tokio-io = "~0.1" diff --git a/src/main.rs b/src/main.rs index f7b08f7..fa5ce41 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ extern crate base64; extern crate clap; -extern crate futures_await as futures; +extern crate futures; extern crate hyper; extern crate tokio; extern crate tokio_io; @@ -128,52 +128,59 @@ impl DoH { Box::new(future::ok(response)) } - #[async] - fn proxy(query: Vec) -> Result { + fn proxy(query: Vec) -> impl Future { let local_addr = LOCAL_BIND_ADDRESS.parse().unwrap(); let socket = UdpSocket::bind(&local_addr).unwrap(); - let remote_addr = SERVER_ADDRESS.parse().unwrap(); - let (socket, _) = await!(socket.send_dgram(query, &remote_addr)).map_err(|_| ())?; - let mut packet = vec![0; MAX_DNS_RESPONSE_LEN]; - let (_socket, mut packet, len, server_addr) = - await!(socket.recv_dgram(packet)).map_err(|_| ())?; - if len < MIN_DNS_PACKET_LEN || server_addr != remote_addr { - return Err(()); - } - packet.truncate(len); - let min_ttl = dns::min_ttl(&packet, MIN_TTL, MAX_TTL, ERR_TTL).map_err(|_| {})?; - Ok((packet, min_ttl)).map(|(body, ttl)| { - let body_len = body.len(); - let mut response = Response::new(); - response.set_body(body); - response - .with_header(ContentLength(body_len as u64)) - .with_header(ContentType( - "application/dns-udpwireformat".parse().unwrap(), - )) - .with_header(CacheControl(vec![CacheDirective::MaxAge(ttl)])) - }) + let remote_addr: SocketAddr = SERVER_ADDRESS.parse().unwrap(); + let fut = socket + .send_dgram(query, &remote_addr) + .map_err(|_| ()) + .and_then(move |(socket, _)| { + let packet = vec![0; MAX_DNS_RESPONSE_LEN]; + socket.recv_dgram(packet).map_err(|_| {}) + }) + .and_then(move |(_socket, mut packet, len, server_addr)| { + if len < MIN_DNS_PACKET_LEN || server_addr != remote_addr { + return future::err(()); + } + packet.truncate(len); + let ttl = match dns::min_ttl(&packet, MIN_TTL, MAX_TTL, ERR_TTL) { + Err(_) => return future::err(()), + Ok(min_ttl) => min_ttl, + }; + let packet_len = packet.len(); + let mut response = Response::new(); + response.set_body(packet); + let response = response + .with_header(ContentLength(packet_len as u64)) + .with_header(ContentType( + "application/dns-udpwireformat".parse().unwrap(), + )) + .with_header(CacheControl(vec![CacheDirective::MaxAge(ttl)])); + future::ok(response) + }); + fut } - #[async] - fn read_body_and_proxy(&self, body: Body) -> Result { - let query = await!({ - let mut sum_size = 0; - body.and_then(move |chunk| { - sum_size += chunk.len(); - if sum_size > MAX_DNS_QUESTION_LEN { - Err(hyper::error::Error::TooLarge) - } else { - Ok(chunk) + fn read_body_and_proxy(&self, body: Body) -> impl Future { + let mut sum_size = 0; + let fut = body.and_then(move |chunk| { + sum_size += chunk.len(); + if sum_size > MAX_DNS_QUESTION_LEN { + Err(hyper::error::Error::TooLarge) + } else { + Ok(chunk) + } + }).concat2() + .map_err(move |_err| ()) + .map(move |chunk| chunk.to_vec()) + .and_then(move |query| { + if query.len() < MIN_DNS_PACKET_LEN { + return Box::new(future::err(())) as Box>; } - }).concat2() - .map_err(|_err| ()) - .map(|chunk| chunk.to_vec()) - })?; - if query.len() < MIN_DNS_PACKET_LEN { - return Err(()); - } - await!(Self::proxy(query)) + Box::new(Self::proxy(query)) + }); + fut } }