This commit is contained in:
Frank Denis 2018-05-19 01:53:46 +02:00
parent 8a72005ca2
commit 66f68b8fac
2 changed files with 31 additions and 22 deletions

View file

@ -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) {

View file

@ -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<u8>, local_addr: SocketAddr, remote_addr: SocketAddr) -> Box<Future<Item = Response, Error = ()>> {
fn proxy(
query: Vec<u8>,
local_addr: SocketAddr,
remote_addr: SocketAddr,
) -> Box<Future<Item = Response, Error = ()>> {
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<Future<Item = _, Error = _>>;
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<Future<Item = _, Error = _>>;
}
Box::new(Self::proxy(query, local_addr, server_addr))
});
Box::new(fut)
}
}