2021-02-19 15:41:43 +00:00
|
|
|
use crate::Context;
|
|
|
|
use std::sync::{Mutex, Arc};
|
|
|
|
use crate::dns::filter::DnsFilter;
|
2021-03-30 17:10:26 +00:00
|
|
|
use crate::dns::protocol::{DnsPacket, QueryType, DnsRecord, DnsQuestion, ResultCode, TransientTtl};
|
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-03-17 13:55:05 +00:00
|
|
|
use crate::blockchain::transaction::DomainData;
|
2021-03-30 17:10:26 +00:00
|
|
|
use chrono::Utc;
|
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 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 17:10:26 +00:00
|
|
|
const NAME_SERVER: & str = "ns.alfis.name";
|
|
|
|
const SERVER_ADMIN: & str = "admin.alfis.name";
|
|
|
|
|
2021-02-19 15:41:43 +00:00
|
|
|
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() {
|
2021-04-15 10:33:47 +00:00
|
|
|
1 => {
|
|
|
|
let mut packet = DnsPacket::new();
|
|
|
|
if self.get_zone_response(parts[0], &mut packet) {
|
|
|
|
return Some(packet);
|
|
|
|
}
|
|
|
|
return None;
|
|
|
|
}
|
2021-02-21 11:29:09 +00:00
|
|
|
2 => {
|
|
|
|
search = format!("{}.{}", parts[1], parts[0]);
|
|
|
|
subdomain = String::new();
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
search = format!("{}.{}", parts[1], parts[0]);
|
|
|
|
subdomain = String::from(parts[2]);
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 21:19:47 +00:00
|
|
|
trace!("Searching record type '{:?}', name '{}' for domain '{}'", &qtype, &subdomain, &search);
|
2021-02-21 11:29:09 +00:00
|
|
|
|
2021-03-10 21:21:50 +00:00
|
|
|
let data = self.context.lock().unwrap().chain.get_domain_info(&search);
|
2021-03-30 17:10:26 +00:00
|
|
|
let zone = parts[0].to_owned();
|
2021-02-19 15:41:43 +00:00
|
|
|
match data {
|
2021-02-26 20:00:08 +00:00
|
|
|
None => {
|
2021-04-28 10:20:26 +00:00
|
|
|
if self.context.lock().unwrap().chain.is_zone_in_blockchain(i64::MAX as u64, &zone) {
|
2021-04-13 21:19:47 +00:00
|
|
|
trace!("Not found data for domain {}", &search);
|
2021-02-26 20:00:08 +00:00
|
|
|
// Create DnsPacket
|
|
|
|
let mut packet = DnsPacket::new();
|
|
|
|
packet.questions.push(DnsQuestion::new(String::from(qname), qtype));
|
2021-03-30 17:10:26 +00:00
|
|
|
packet.header.rescode = ResultCode::NXDOMAIN;
|
|
|
|
packet.header.authoritative_answer = true;
|
2021-04-13 16:46:48 +00:00
|
|
|
BlockchainFilter::add_soa_record(zone, &mut packet);
|
2021-03-20 23:19:09 +00:00
|
|
|
//trace!("Returning packet: {:?}", &packet);
|
2021-02-26 20:00:08 +00:00
|
|
|
return Some(packet);
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 15:41:43 +00:00
|
|
|
Some(data) => {
|
2021-04-13 21:19:47 +00:00
|
|
|
trace!("Found data for domain {}", &search);
|
2021-03-17 13:55:05 +00:00
|
|
|
let mut data: DomainData = match serde_json::from_str(&data) {
|
2021-02-19 15:41:43 +00:00
|
|
|
Err(_) => { return None; }
|
2021-03-17 13:55:05 +00:00
|
|
|
Ok(data) => { data }
|
2021-02-19 15:41:43 +00:00
|
|
|
};
|
|
|
|
let mut answers: Vec<DnsRecord> = Vec::new();
|
2021-04-02 01:24:53 +00:00
|
|
|
let a_record = qtype == QueryType::A || qtype == QueryType::AAAA;
|
2021-03-17 13:55:05 +00:00
|
|
|
for mut record in data.records.iter_mut() {
|
2021-04-02 01:24:53 +00:00
|
|
|
if record.get_querytype() == qtype || (a_record && record.get_querytype() == QueryType::CNAME) {
|
2021-02-19 15:41:43 +00:00
|
|
|
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
|
2021-03-17 13:55:05 +00:00
|
|
|
for mut record in data.records {
|
2021-02-21 11:29:09 +00:00
|
|
|
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-03-29 09:10:48 +00:00
|
|
|
//debug!("Answers: {:?}", &answers);
|
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();
|
2021-03-30 17:10:26 +00:00
|
|
|
packet.header.authoritative_answer = true;
|
2021-02-19 15:41:43 +00:00
|
|
|
packet.questions.push(DnsQuestion::new(String::from(qname), qtype));
|
|
|
|
for answer in answers {
|
|
|
|
packet.answers.push(answer);
|
|
|
|
}
|
2021-03-30 17:10:26 +00:00
|
|
|
packet.authorities.push( DnsRecord::NS {
|
|
|
|
domain: zone,
|
|
|
|
host: String::from(NAME_SERVER),
|
|
|
|
ttl: TransientTtl(600)
|
|
|
|
});
|
2021-03-20 23:19:09 +00:00
|
|
|
//trace!("Returning packet: {:?}", &packet);
|
2021-02-26 20:00:08 +00:00
|
|
|
Some(packet)
|
|
|
|
} else {
|
|
|
|
// Create DnsPacket
|
|
|
|
let mut packet = DnsPacket::new();
|
2021-03-30 17:10:26 +00:00
|
|
|
packet.header.authoritative_answer = true;
|
2021-03-30 18:50:20 +00:00
|
|
|
packet.header.rescode = ResultCode::NOERROR;
|
2021-02-26 20:00:08 +00:00
|
|
|
packet.questions.push(DnsQuestion::new(String::from(qname), qtype));
|
2021-04-13 16:46:48 +00:00
|
|
|
BlockchainFilter::add_soa_record(zone, &mut packet);
|
2021-03-20 23:19:09 +00:00
|
|
|
//trace!("Returning packet: {:?}", &packet);
|
2021-02-26 20:00:08 +00:00
|
|
|
Some(packet)
|
2021-02-19 15:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2021-04-13 16:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockchainFilter {
|
|
|
|
fn add_soa_record(zone: String, packet: &mut DnsPacket) {
|
|
|
|
packet.authorities.push(DnsRecord::SOA {
|
|
|
|
domain: zone,
|
|
|
|
m_name: String::from(NAME_SERVER),
|
|
|
|
r_name: String::from(SERVER_ADMIN),
|
|
|
|
serial: Utc::now().timestamp() as u32,
|
|
|
|
refresh: 3600,
|
|
|
|
retry: 300,
|
|
|
|
expire: 604800,
|
|
|
|
minimum: 60,
|
|
|
|
ttl: TransientTtl(60),
|
|
|
|
});
|
|
|
|
}
|
2021-04-15 10:33:47 +00:00
|
|
|
|
|
|
|
fn get_zone_response(&self, zone: &str, mut packet: &mut DnsPacket) -> bool {
|
2021-04-26 19:49:01 +00:00
|
|
|
let have_zone = self.context.lock().unwrap().chain.is_zone_in_blockchain(i64::MAX as u64, zone);
|
2021-04-15 10:33:47 +00:00
|
|
|
if have_zone {
|
|
|
|
BlockchainFilter::add_soa_record(zone.to_owned(), &mut packet);
|
|
|
|
}
|
|
|
|
have_zone
|
|
|
|
}
|
2021-04-13 16:46:48 +00:00
|
|
|
}
|