mirror of https://github.com/Revertron/Alfis
Implemented DNS on blockchain. Beautified a lot of code, fixed some things.
parent
4b5e5112da
commit
d135204af7
@ -0,0 +1,54 @@
|
||||
use crate::Context;
|
||||
use std::sync::{Mutex, Arc};
|
||||
use crate::dns::filter::DnsFilter;
|
||||
use crate::dns::protocol::{DnsPacket, QueryType, DnsRecord, DnsQuestion};
|
||||
|
||||
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> {
|
||||
let data = self.context.lock().unwrap().blockchain.get_domain_info(qname);
|
||||
match data {
|
||||
None => { println!("Not found info for domain {}", &qname); }
|
||||
Some(data) => {
|
||||
let records: Vec<DnsRecord> = match serde_json::from_str(&data) {
|
||||
Err(_) => { return None; }
|
||||
Ok(records) => { records }
|
||||
};
|
||||
let mut answers: Vec<DnsRecord> = Vec::new();
|
||||
for mut record in records {
|
||||
if record.get_querytype() == qtype {
|
||||
match &mut record {
|
||||
// TODO make it for all types of records
|
||||
DnsRecord::A { domain, .. } | DnsRecord::AAAA { domain, .. } if domain == "@" => {
|
||||
*domain = String::from(qname);
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
|
||||
answers.push(record);
|
||||
}
|
||||
}
|
||||
if !answers.is_empty() {
|
||||
// Create DnsPacket
|
||||
let mut packet = DnsPacket::new();
|
||||
packet.questions.push(DnsQuestion::new(String::from(qname), qtype));
|
||||
for answer in answers {
|
||||
packet.answers.push(answer);
|
||||
}
|
||||
return Some(packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
pub mod transaction;
|
||||
pub mod block;
|
||||
pub mod blockchain;
|
||||
pub mod filter;
|
||||
|
||||
pub use transaction::Transaction;
|
||||
pub use block::Block;
|
||||
|
@ -0,0 +1,16 @@
|
||||
use crate::dns::protocol::{QueryType, DnsPacket};
|
||||
|
||||
pub trait DnsFilter {
|
||||
fn lookup(&self, qname: &str, qtype: QueryType) -> Option<DnsPacket>;
|
||||
}
|
||||
|
||||
pub struct DummyFilter {
|
||||
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
impl DnsFilter for DummyFilter {
|
||||
fn lookup(&self, qname: &str, qtype: QueryType) -> Option<DnsPacket> {
|
||||
None
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::Bytes;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Settings {
|
||||
pub origin: String,
|
||||
pub version: u32,
|
||||
pub key_file: String,
|
||||
pub listen: String,
|
||||
pub public: bool,
|
||||
pub peers: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub dns: Dns
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn new<S: Into<String>>(settings: S) -> serde_json::Result<Settings> {
|
||||
serde_json::from_str(&settings.into())
|
||||
}
|
||||
|
||||
pub fn load(file_name: &str) -> Option<Settings> {
|
||||
match File::open(file_name) {
|
||||
Ok(mut file) => {
|
||||
let mut text = String::new();
|
||||
file.read_to_string(&mut text).unwrap();
|
||||
let loaded = serde_json::from_str(&text);
|
||||
return if loaded.is_ok() {
|
||||
Some(loaded.unwrap())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
},
|
||||
Err(..) => None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_origin(&self) -> Bytes {
|
||||
if self.origin.eq("") {
|
||||
return Bytes::zero32();
|
||||
}
|
||||
let origin = crate::from_hex(&self.origin).expect("Wrong origin in settings");
|
||||
Bytes::from_bytes(origin.as_slice())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Dns {
|
||||
pub port: u16,
|
||||
pub forwarders: Vec<String>
|
||||
}
|
||||
|
||||
impl Default for Dns {
|
||||
fn default() -> Self {
|
||||
Dns { port: 53, forwarders: Vec::new() }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue