mirror of
https://github.com/tun2proxy/tun2proxy.git
synced 2025-04-24 15:56:03 +00:00
dns query from remote server
This commit is contained in:
parent
04d4faff68
commit
60b9683fac
2 changed files with 29 additions and 4 deletions
19
src/dns.rs
19
src/dns.rs
|
@ -1,6 +1,9 @@
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use std::{net::IpAddr, str::FromStr};
|
use std::{
|
||||||
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||||
|
str::FromStr,
|
||||||
|
};
|
||||||
use trust_dns_proto::{
|
use trust_dns_proto::{
|
||||||
op::{Message, ResponseCode},
|
op::{Message, ResponseCode},
|
||||||
rr::{record_type::RecordType, Name, RData, Record},
|
rr::{record_type::RecordType, Name, RData, Record},
|
||||||
|
@ -90,3 +93,17 @@ pub fn parse_data_to_dns_message(data: &[u8], used_by_tcp: bool) -> Result<Messa
|
||||||
let message = Message::from_vec(data).map_err(|e| e.to_string())?;
|
let message = Message::from_vec(data).map_err(|e| e.to_string())?;
|
||||||
Ok(message)
|
Ok(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: use IpAddr::is_global() instead when it's stable
|
||||||
|
pub fn addr_is_private(addr: &SocketAddr) -> bool {
|
||||||
|
fn is_benchmarking(addr: &Ipv4Addr) -> bool {
|
||||||
|
addr.octets()[0] == 198 && (addr.octets()[1] & 0xfe) == 18
|
||||||
|
}
|
||||||
|
fn addr_v4_is_private(addr: &Ipv4Addr) -> bool {
|
||||||
|
is_benchmarking(addr) || addr.is_private() || addr.is_loopback() || addr.is_link_local()
|
||||||
|
}
|
||||||
|
match addr {
|
||||||
|
SocketAddr::V4(addr) => addr_v4_is_private(addr.ip()),
|
||||||
|
SocketAddr::V6(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::{error::Error, error::Result, virtdevice::VirtualTunDevice, NetworkInterface, Options};
|
use crate::{dns, error::Error, error::Result, virtdevice::VirtualTunDevice, NetworkInterface, Options};
|
||||||
use mio::{event::Event, net::TcpStream, net::UdpSocket, unix::SourceFd, Events, Interest, Poll, Token};
|
use mio::{event::Event, net::TcpStream, net::UdpSocket, unix::SourceFd, Events, Interest, Poll, Token};
|
||||||
use smoltcp::{
|
use smoltcp::{
|
||||||
iface::{Config, Interface, SocketHandle, SocketSet},
|
iface::{Config, Interface, SocketHandle, SocketSet},
|
||||||
|
@ -468,7 +468,15 @@ impl<'a> TunToProxy<'a> {
|
||||||
let (info, _first_packet, payload_offset, payload_size) = result?;
|
let (info, _first_packet, payload_offset, payload_size) = result?;
|
||||||
let origin_dst = SocketAddr::try_from(&info.dst)?;
|
let origin_dst = SocketAddr::try_from(&info.dst)?;
|
||||||
let connection_info = match &mut self.options.virtual_dns {
|
let connection_info = match &mut self.options.virtual_dns {
|
||||||
None => info,
|
None => {
|
||||||
|
let mut info = info;
|
||||||
|
let port = origin_dst.port();
|
||||||
|
if port == 53 && info.protocol == IpProtocol::Udp && dns::addr_is_private(&origin_dst) {
|
||||||
|
let dns_addr: SocketAddr = "8.8.8.8:53".parse()?; // TODO: Configurable
|
||||||
|
info.dst = Address::from(dns_addr);
|
||||||
|
}
|
||||||
|
info
|
||||||
|
}
|
||||||
Some(virtual_dns) => {
|
Some(virtual_dns) => {
|
||||||
let dst_ip = origin_dst.ip();
|
let dst_ip = origin_dst.ip();
|
||||||
virtual_dns.touch_ip(&dst_ip);
|
virtual_dns.touch_ip(&dst_ip);
|
||||||
|
@ -798,7 +806,7 @@ impl<'a> TunToProxy<'a> {
|
||||||
Ok(read_result) => read_result,
|
Ok(read_result) => read_result,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
if error.kind() != std::io::ErrorKind::WouldBlock {
|
if error.kind() != std::io::ErrorKind::WouldBlock {
|
||||||
log::error!("Read from proxy: {}", error);
|
log::error!("{} Read from proxy: {}", conn_info.dst, error);
|
||||||
}
|
}
|
||||||
vecbuf.len()
|
vecbuf.len()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue