2021-02-19 15:41:43 +00:00
|
|
|
use crate::Context;
|
|
|
|
use std::sync::{Mutex, Arc};
|
|
|
|
use crate::dns::filter::DnsFilter;
|
2021-02-26 20:00:08 +00:00
|
|
|
use crate::dns::protocol::{DnsPacket, QueryType, DnsRecord, DnsQuestion, ResultCode};
|
2021-02-21 20:56:56 +00:00
|
|
|
#[allow(unused_imports)]
|
2021-02-20 15:28:10 +00:00
|
|
|
use log::{trace, debug, info, warn, error};
|
2021-02-19 15:41:43 +00:00
|
|
|
|
|
|
|
pub struct BlockchainFilter {
|
|
|
|
context: Arc<Mutex<Context>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockchainFilter {
|
|
|
|
pub fn new(context: Arc<Mutex<Context>>) -> Self {
|
|
|
|
BlockchainFilter { context }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DnsFilter for BlockchainFilter {
|
|
|
|
fn lookup(&self, qname: &str, qtype: QueryType) -> Option<DnsPacket> {
|
2021-02-21 20:56:56 +00:00
|
|
|
let search;
|
|
|
|
let subdomain;
|
2021-02-21 11:29:09 +00:00
|
|
|
let parts: Vec<&str> = qname.rsplitn(3, ".").collect();
|
|
|
|
match parts.len() {
|
|
|
|
1 => { return None; }
|
|
|
|
2 => {
|
|
|
|
search = format!("{}.{}", parts[1], parts[0]);
|
|
|
|
subdomain = String::new();
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
search = format!("{}.{}", parts[1], parts[0]);
|
|
|
|
subdomain = String::from(parts[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!("Searching domain {} and record {}", &search, &subdomain);
|
|
|
|
|
2021-03-10 21:21:50 +00:00
|
|
|
let data = self.context.lock().unwrap().chain.get_domain_info(&search);
|
2021-02-19 15:41:43 +00:00
|
|
|
match data {
|
2021-02-26 20:00:08 +00:00
|
|
|
None => {
|
|
|
|
debug!("Not found data for domain {}", &search);
|
2021-03-10 21:21:50 +00:00
|
|
|
if self.context.lock().unwrap().chain.is_zone_in_blockchain(parts[0]) {
|
2021-02-26 20:00:08 +00:00
|
|
|
// Create DnsPacket
|
|
|
|
let mut packet = DnsPacket::new();
|
|
|
|
packet.questions.push(DnsQuestion::new(String::from(qname), qtype));
|
|
|
|
packet.header.rescode = ResultCode::SERVFAIL;
|
|
|
|
trace!("Returning packet: {:?}", &packet);
|
|
|
|
return Some(packet);
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 15:41:43 +00:00
|
|
|
Some(data) => {
|
2021-02-21 11:29:09 +00:00
|
|
|
info!("Found data for domain {}", &search);
|
|
|
|
let mut records: Vec<DnsRecord> = match serde_json::from_str(&data) {
|
2021-02-19 15:41:43 +00:00
|
|
|
Err(_) => { return None; }
|
|
|
|
Ok(records) => { records }
|
|
|
|
};
|
|
|
|
let mut answers: Vec<DnsRecord> = Vec::new();
|
2021-02-21 11:29:09 +00:00
|
|
|
for mut record in records.iter_mut() {
|
2021-02-19 15:41:43 +00:00
|
|
|
if record.get_querytype() == qtype {
|
|
|
|
match &mut record {
|
2021-02-21 11:29:09 +00:00
|
|
|
DnsRecord::A { domain, .. }
|
|
|
|
| DnsRecord::AAAA { domain, .. }
|
|
|
|
| DnsRecord::NS { domain, .. }
|
|
|
|
| DnsRecord::CNAME { domain, .. }
|
|
|
|
| DnsRecord::SRV { domain, .. }
|
|
|
|
| DnsRecord::MX { domain, .. }
|
|
|
|
| DnsRecord::UNKNOWN { domain, .. }
|
|
|
|
| DnsRecord::SOA { domain, .. }
|
|
|
|
| DnsRecord::TXT { domain, .. } if domain == "@" => {
|
2021-02-19 15:41:43 +00:00
|
|
|
*domain = String::from(qname);
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
2021-02-21 11:29:09 +00:00
|
|
|
match record.get_domain() {
|
|
|
|
None => {}
|
|
|
|
Some(domain) => {
|
|
|
|
if domain == search {
|
|
|
|
answers.push(record.clone());
|
|
|
|
} else if domain == subdomain {
|
|
|
|
match &mut record {
|
|
|
|
DnsRecord::A { domain, .. }
|
|
|
|
| DnsRecord::AAAA { domain, .. }
|
|
|
|
| DnsRecord::NS { domain, .. }
|
|
|
|
| DnsRecord::CNAME { domain, .. }
|
|
|
|
| DnsRecord::SRV { domain, .. }
|
|
|
|
| DnsRecord::MX { domain, .. }
|
|
|
|
| DnsRecord::UNKNOWN { domain, .. }
|
|
|
|
| DnsRecord::SOA { domain, .. }
|
|
|
|
| DnsRecord::TXT { domain, .. } => {
|
|
|
|
*domain = String::from(qname);
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
answers.push(record.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 15:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-21 11:29:09 +00:00
|
|
|
if answers.is_empty() {
|
|
|
|
// If there are no records found we search for *.domain.ltd record
|
|
|
|
for mut record in records {
|
|
|
|
if record.get_querytype() == qtype {
|
|
|
|
match record.get_domain() {
|
|
|
|
None => {}
|
|
|
|
Some(domain) => {
|
|
|
|
if domain == search {
|
|
|
|
answers.push(record.clone());
|
|
|
|
} else if domain == "*" {
|
|
|
|
match &mut record {
|
|
|
|
DnsRecord::A { domain, .. }
|
|
|
|
| DnsRecord::AAAA { domain, .. }
|
|
|
|
| DnsRecord::NS { domain, .. }
|
|
|
|
| DnsRecord::CNAME { domain, .. }
|
|
|
|
| DnsRecord::SRV { domain, .. }
|
|
|
|
| DnsRecord::MX { domain, .. }
|
|
|
|
| DnsRecord::UNKNOWN { domain, .. }
|
|
|
|
| DnsRecord::SOA { domain, .. }
|
|
|
|
| DnsRecord::TXT { domain, .. } => {
|
|
|
|
*domain = String::from(qname);
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
answers.push(record.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 20:00:08 +00:00
|
|
|
return if !answers.is_empty() {
|
2021-02-19 15:41:43 +00:00
|
|
|
// Create DnsPacket
|
|
|
|
let mut packet = DnsPacket::new();
|
|
|
|
packet.questions.push(DnsQuestion::new(String::from(qname), qtype));
|
|
|
|
for answer in answers {
|
|
|
|
packet.answers.push(answer);
|
|
|
|
}
|
2021-02-26 20:00:08 +00:00
|
|
|
trace!("Returning packet: {:?}", &packet);
|
|
|
|
Some(packet)
|
|
|
|
} else {
|
|
|
|
// Create DnsPacket
|
|
|
|
let mut packet = DnsPacket::new();
|
|
|
|
packet.questions.push(DnsQuestion::new(String::from(qname), qtype));
|
|
|
|
packet.header.rescode = ResultCode::SERVFAIL;
|
|
|
|
trace!("Returning packet: {:?}", &packet);
|
|
|
|
Some(packet)
|
2021-02-19 15:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|