Skip to content

Commit

Permalink
Add Upstream trait (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mygod authored and madeye committed Mar 22, 2020
1 parent a88cb1a commit dfead7e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/relay/dnsrelay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ use crate::{
},
};

pub mod upstream;

async fn udp_lookup(qname: &Name, qtype: RecordType, server: &SocketAddr) -> io::Result<Message> {
let bind_addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0));
let mut socket = UdpSocket::bind(bind_addr).await?;
Expand Down
59 changes: 59 additions & 0 deletions src/relay/dnsrelay/upstream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::io::Result;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket};
use trust_dns_proto::op::Message;

pub trait Upstream {
async fn lookup(&self, request: &Message) -> Result<Message>;
}

struct UdpUpstream {
server: SocketAddr,
}

impl Upstream for UdpUpstream {
async fn lookup(&self, request: &Message) -> Result<Message> {
let socket = UdpSocket::bind(SocketAddr::new(match *self.server {
SocketAddr::V4(..) => IpAddr::v4(Ipv4Addr::new(0, 0, 0, 0)),
SocketAddr::V6(..) => IpAddr::v6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)),
}, 0)).await?;
socket.send_to(&request.to_vec()?, self.server).await?;
let mut response = vec![0; 512];
socket.recv_from(&mut response).await?;
Ok(Message::from_vec(&response)?)
}
}

struct Socks5UdpUpstream {
server: SocketAddr,
}

impl Upstream for Socks5UdpUpstream {
async fn lookup(&self, request: &Message) -> Result<Message> {
let socket = UdpSocket::bind(SocketAddr::new(IpAddr::v4(Ipv4Addr::new(0, 0, 0, 0)), 0)).await?;
unimplemented!()
}
}

struct TcpUpstream {
server: SocketAddr,
}

impl Upstream for TcpUpstream {
async fn lookup(&self, request: &Message) -> Result<Message> {
unimplemented!()
}
}

#[cfg(any(target_os = "linux", target_os = "android"))]
struct UnixSocketUpstream {
path: String,
}

#[cfg(any(target_os = "linux", target_os = "android"))]
impl Upstream for UnixSocketUpstream {
async fn lookup(&self, request: &Message) -> Result<Message> {
use tokio::net::UnixStream;
let socket = UnixStream::connect(path).await?;
unimplemented!()
}
}

0 comments on commit dfead7e

Please sign in to comment.