From 66f68b8fac42e7bc3d82bfed024638541c1de6d2 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sat, 19 May 2018 01:53:46 +0200 Subject: [PATCH] Format --- src/dns.rs | 6 ++++-- src/main.rs | 47 +++++++++++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/dns.rs b/src/dns.rs index 64360a0..bf77067 100644 --- a/src/dns.rs +++ b/src/dns.rs @@ -101,8 +101,10 @@ pub fn min_ttl( } let qtype = u16::from(packet[offset]) << 8 | u16::from(packet[offset + 1]); let qclass = u16::from(packet[offset + 2]) << 8 | u16::from(packet[offset + 3]); - let ttl = u32::from(packet[offset + 4]) << 24 | u32::from(packet[offset + 5]) << 16 - | u32::from(packet[offset + 6]) << 8 | u32::from(packet[offset + 7]); + let ttl = u32::from(packet[offset + 4]) << 24 + | u32::from(packet[offset + 5]) << 16 + | u32::from(packet[offset + 6]) << 8 + | u32::from(packet[offset + 7]); let rdlen = (u16::from(packet[offset + 8]) << 8 | u16::from(packet[offset + 9])) as usize; offset += 10; if !(qtype == DNS_TYPE_OPT && qclass == DNS_CLASS_IN) { diff --git a/src/main.rs b/src/main.rs index d60813c..3ac9356 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,13 +14,13 @@ mod dns; use clap::{App, Arg}; use futures::future; use futures::prelude::*; -use hyper::{Body, Method, StatusCode}; use hyper::header::{CacheControl, CacheDirective, ContentLength, ContentType}; use hyper::server::{Http, Request, Response, Service}; +use hyper::{Body, Method, StatusCode}; use std::cell::RefCell; use std::net::SocketAddr; -use std::time::Duration; use std::rc::Rc; +use std::time::Duration; use tokio::executor::current_thread; use tokio::net::{TcpListener, UdpSocket}; use tokio_timer::Timer; @@ -68,7 +68,8 @@ impl Service for DoH { (*count).saturating_add(1); } let clients_count_inner = self.clients_count.clone(); - let fut = self.handle_client(req) + let fut = self + .handle_client(req) .then(move |fut| { (*clients_count_inner).borrow_mut().saturating_sub(1); fut @@ -77,7 +78,8 @@ impl Service for DoH { eprintln!("server error: {:?}", err); err }); - let timed = self.timers + let timed = self + .timers .timeout(fut.map_err(|_| {}), self.timeout) .map_err(|_| hyper::Error::Timeout); Box::new(timed) @@ -126,7 +128,11 @@ impl DoH { Box::new(future::ok(response)) } - fn proxy(query: Vec, local_addr: SocketAddr, remote_addr: SocketAddr) -> Box> { + fn proxy( + query: Vec, + local_addr: SocketAddr, + remote_addr: SocketAddr, + ) -> Box> { let socket = UdpSocket::bind(&local_addr).unwrap(); let fut = socket .send_dgram(query, &remote_addr) @@ -162,22 +168,23 @@ impl DoH { let mut sum_size = 0; let local_addr = self.local_bind_address.clone(); let server_addr = self.server_address.clone(); - 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>; + 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) } - Box::new(Self::proxy(query, local_addr, server_addr)) - }); + }).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>; + } + Box::new(Self::proxy(query, local_addr, server_addr)) + }); Box::new(fut) } }