2
0
mirror of https://github.com/Revertron/Alfis synced 2024-11-11 01:10:36 +00:00
Alfis/src/blockchain/filter.rs

209 lines
9.1 KiB
Rust
Raw Normal View History

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};
#[allow(unused_imports)]
2021-02-20 15:28:10 +00:00
use log::{trace, debug, info, warn, error};
use crate::blockchain::transaction::DomainData;
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";
impl DnsFilter for BlockchainFilter {
fn lookup(&self, qname: &str, qtype: QueryType) -> Option<DnsPacket> {
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();
let serial = self.context.lock().unwrap().chain.get_soa_serial();
if self.get_zone_response(parts[0], serial, &mut packet) {
2021-04-15 10:33:47 +00:00
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
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();
match data {
None => {
2021-05-02 10:55:51 +00:00
if self.context.lock().unwrap().chain.is_available_zone(&zone) {
2021-04-13 21:19:47 +00:00
trace!("Not found data for domain {}", &search);
// 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;
let serial = self.context.lock().unwrap().chain.get_soa_serial();
BlockchainFilter::add_soa_record(zone, serial, &mut packet);
//trace!("Returning packet: {:?}", &packet);
return Some(packet);
}
}
Some(data) => {
2021-04-13 21:19:47 +00:00
trace!("Found data for domain {}", &search);
let mut data: DomainData = match serde_json::from_str(&data) {
Err(_) => { return None; }
Ok(data) => { data }
};
let mut answers: Vec<DnsRecord> = Vec::new();
2021-04-02 01:24:53 +00:00
let a_record = qtype == QueryType::A || qtype == QueryType::AAAA;
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) {
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 == "@" => {
*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-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 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);
return if !answers.is_empty() {
// Create DnsPacket
let mut packet = DnsPacket::new();
2021-03-30 17:10:26 +00:00
packet.header.authoritative_answer = true;
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)
});
//trace!("Returning packet: {:?}", &packet);
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;
packet.questions.push(DnsQuestion::new(String::from(qname), qtype));
let serial = self.context.lock().unwrap().chain.get_soa_serial();
BlockchainFilter::add_soa_record(zone, serial, &mut packet);
//trace!("Returning packet: {:?}", &packet);
Some(packet)
}
}
}
None
}
2021-04-13 16:46:48 +00:00
}
impl BlockchainFilter {
fn add_soa_record(zone: String, serial: u32, packet: &mut DnsPacket) {
2021-04-13 16:46:48 +00:00
packet.authorities.push(DnsRecord::SOA {
domain: zone,
m_name: String::from(NAME_SERVER),
r_name: String::from(SERVER_ADMIN),
serial,
2021-04-13 16:46:48 +00:00
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, serial: u32, mut packet: &mut DnsPacket) -> bool {
2021-05-02 10:55:51 +00:00
let have_zone = self.context.lock().unwrap().chain.is_available_zone(zone);
2021-04-15 10:33:47 +00:00
if have_zone {
BlockchainFilter::add_soa_record(zone.to_owned(), serial, &mut packet);
2021-04-15 10:33:47 +00:00
}
have_zone
}
2021-04-13 16:46:48 +00:00
}
#[cfg(test)]
mod tests {
// TODO write tests for this filter
}