Skip to content

Commit

Permalink
Use cached DNS records
Browse files Browse the repository at this point in the history
  • Loading branch information
hanneary committed Jul 25, 2024
1 parent c79f148 commit 2fbd370
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions data-plane/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ tower = { version = "0.4.13", features = ["util"] }
tower-http = { version = "0.5.0", features = ["catch-panic"] }
libc = "0.2.150"
serial_test = "3.0.0"
trust-dns-proto = "0.23.2"


[dev-dependencies]
Expand Down
48 changes: 42 additions & 6 deletions data-plane/src/dns/enclavedns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ use shared::server::egress::{cache_ip_for_allowlist, EgressDestinations};
use shared::server::get_vsock_client;
use shared::server::CID::Parent;
use shared::DNS_PROXY_VSOCK_PORT;
use std::net::Ipv4Addr;
use std::net::SocketAddr;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::UdpSocket;
use tokio::sync::{mpsc::Receiver, Semaphore};
use tokio::time::timeout;
use trust_dns_proto::op::{Message, MessageType, OpCode, ResponseCode};
use trust_dns_proto::rr::{DNSClass, Name, RData, Record, RecordType};
use trust_dns_proto::serialize::binary::{BinEncodable, BinEncoder};

/// Empty struct for the DNS proxy that runs in the data plane
pub struct EnclaveDnsProxy;
Expand Down Expand Up @@ -121,19 +126,50 @@ impl EnclaveDnsDriver {
}
}

fn test() -> Vec<u8> {
let mut message = Message::new();
message.set_id(1); // Set the message ID
message.set_message_type(MessageType::Response);
message.set_op_code(OpCode::Query);
message.set_authoritative(true);
message.set_recursion_desired(true);
message.set_response_code(ResponseCode::NoError);

// Create an answer
let mut record: Record = Record::new();
record.set_name(Name::from_str("jsonplaceholder.typicode.com").unwrap());
record.set_record_type(RecordType::A);
record.set_dns_class(DNSClass::IN);
record.set_ttl(300);
record.set_data(Some(RData::A(trust_dns_proto::rr::rdata::A(
Ipv4Addr::new(93, 184, 216, 34),
))));
message.add_answer(record);

// Encode the message to binary format
let mut buf = Vec::new();
{
let mut encoder = BinEncoder::new(&mut buf);
message.emit(&mut encoder).unwrap();
}
println!("{:?}", buf.len());

return buf;
}

/// Perform a DNS lookup using the proxy running on the Host
async fn perform_dns_lookup(
dns_packet: Bytes,
request_upper_bound: Duration,
allowed_destinations: EgressDestinations,
) -> Result<Bytes, DNSError> {
// Check domain is allowed before proxying lookup
check_dns_allowed_for_domain(&dns_packet.clone(), &allowed_destinations)?;
// Attempt DNS lookup wth a timeout, flatten timeout errors into a DNS Error
let dns_response =
timeout(request_upper_bound, Self::forward_dns_lookup(dns_packet)).await??;
cache_ip_for_allowlist(&dns_response.clone())?;
Ok(dns_response)
// check_dns_allowed_for_domain(&dns_packet.clone(), &allowed_destinations)?;
// // Attempt DNS lookup wth a timeout, flatten timeout errors into a DNS Error
// let dns_response =
// timeout(request_upper_bound, Self::forward_dns_lookup(dns_packet)).await??;
// cache_ip_for_allowlist(&dns_response.clone())?;
Ok(Self::test().into())
}

/// Takes a DNS lookup as `Bytes` and sends forwards it over VSock to the host process to be sent to
Expand Down

0 comments on commit 2fbd370

Please sign in to comment.