Added a restriction for IANA domain zones. Users cannot mine them from now on.

This commit is contained in:
Revertron
2021-03-11 01:41:19 +01:00
parent 5d57473122
commit 186f9cb05b
8 changed files with 112 additions and 10 deletions
+2
View File
@@ -1,2 +1,4 @@
/target /target
**/*.rs.bk **/*.rs.bk
/iana-tlds.txt
/iana-hashes.txt
+2
View File
@@ -42,6 +42,8 @@ winapi = { version = "0.3.7", features = ["impl-default", "wincon"]}
thread-priority = "0.2.1" thread-priority = "0.2.1"
[build-dependencies] [build-dependencies]
minreq = { version = "2.3.1", features = ["punycode", "https-rustls"] }
rust-crypto = "^0.2"
winres = "0.1" winres = "0.1"
[dev-dependencies] [dev-dependencies]
+58
View File
@@ -1,9 +1,67 @@
extern crate winres; extern crate winres;
use std::fs::File;
use std::path::Path;
use std::io::Write;
use crypto::digest::Digest;
use crypto::sha2::Sha256;
fn main() { fn main() {
if cfg!(target_os = "windows") { if cfg!(target_os = "windows") {
let mut res = winres::WindowsResource::new(); let mut res = winres::WindowsResource::new();
res.set_icon("globe_icon.ico"); res.set_icon("globe_icon.ico");
res.compile().unwrap(); res.compile().unwrap();
} }
download_iana_zones("iana-tlds.txt", "iana-hashes.txt");
}
fn download_iana_zones(zones_name: &str, hashes_name: &str) {
let response = minreq::get("https://data.iana.org/TLD/tlds-alpha-by-domain.txt").send().expect("Could not make request!");
let response = response.as_str().expect("Response is not a valid UTF-8!").to_lowercase();
let list: Vec<_> = response.split("\n").collect();
let mut zones = String::new();
let mut hashes = String::new();
for string in list {
if !string.starts_with("#") && !string.is_empty() {
zones.push_str(string);
zones.push('\n');
hashes.push_str(&hash_identity(string));
hashes.push('\n');
}
}
match File::create(Path::new(zones_name)) {
Ok(mut file) => {
file.write_all(zones.trim().as_bytes()).expect("Error saving TLDs file!");
}
Err(e) => { println!("Error opening TLDs file!\n{}", e); }
}
match File::create(Path::new(hashes_name)) {
Ok(mut file) => {
file.write_all(hashes.trim().as_bytes()).expect("Error saving TLD-hashes file!");
}
Err(e) => { println!("Error opening TLD-hashes file!\n{}", e); }
}
}
fn hash_identity(identity: &str) -> String {
let mut buf: [u8; 32] = [0; 32];
let mut digest = Sha256::new();
digest.input_str(identity);
digest.result(&mut buf);
to_hex(&buf)
}
/// Convert bytes array to HEX format
pub fn to_hex(buf: &[u8]) -> String {
let mut result = String::new();
for x in buf.iter() {
result.push_str(&format!("{:01$X}", x, 2));
}
result
} }
+3 -2
View File
@@ -1,4 +1,4 @@
use crate::{Chain, Bus, Keystore, Settings}; use crate::{Chain, Bus, Keystore, Settings, Iana};
use crate::event::Event; use crate::event::Event;
#[allow(unused_imports)] #[allow(unused_imports)]
use log::{trace, debug, info, warn, error}; use log::{trace, debug, info, warn, error};
@@ -7,13 +7,14 @@ pub struct Context {
pub settings: Settings, pub settings: Settings,
pub keystore: Keystore, pub keystore: Keystore,
pub chain: Chain, pub chain: Chain,
pub iana: Iana,
pub bus: Bus<Event>, pub bus: Bus<Event>,
} }
impl Context { impl Context {
/// Creating an essential context to work with /// Creating an essential context to work with
pub fn new(settings: Settings, keystore: Keystore, chain: Chain) -> Context { pub fn new(settings: Settings, keystore: Keystore, chain: Chain) -> Context {
Context { settings, keystore, chain, bus: Bus::new() } Context { settings, keystore, chain, iana: Iana::new(), bus: Bus::new() }
} }
/// Load keystore and return Context /// Load keystore and return Context
+28
View File
@@ -0,0 +1,28 @@
use std::collections::HashSet;
pub struct Iana {
zones: HashSet<String>,
hashes: HashSet<String>
}
impl Iana {
pub fn new() -> Self {
let zones: HashSet<_> = include_str!("../iana-tlds.txt")
.split("\n")
.map(String::from)
.collect();
let hashes: HashSet<_> = include_str!("../iana-hashes.txt")
.split("\n")
.map(String::from)
.collect();
Self { zones, hashes }
}
pub fn has_zone(&self, zone: &str) -> bool {
self.zones.contains(zone)
}
pub fn has_hash(&self, hash: &str) -> bool {
self.hashes.contains(hash)
}
}
+2
View File
@@ -8,6 +8,7 @@ pub use crate::p2p::Network;
pub use crate::settings::Settings; pub use crate::settings::Settings;
pub use crate::bytes::Bytes; pub use crate::bytes::Bytes;
pub use crate::keys::Keystore; pub use crate::keys::Keystore;
pub use crate::iana::Iana;
pub use crate::simplebus::*; pub use crate::simplebus::*;
pub use crate::utils::*; pub use crate::utils::*;
@@ -23,4 +24,5 @@ pub mod dns;
pub mod dns_utils; pub mod dns_utils;
pub mod settings; pub mod settings;
pub mod bytes; pub mod bytes;
pub mod iana;
+6
View File
@@ -355,6 +355,12 @@ fn handle_message(context: Arc<Mutex<Context>>, message: Message, peers: &mut Pe
}; };
let peer = peers.get_mut_peer(token).unwrap(); let peer = peers.get_mut_peer(token).unwrap();
peer.set_received_block(block.index); peer.set_received_block(block.index);
if let Some(transaction) = &block.transaction {
if context.lock().unwrap().iana.has_hash(&transaction.identity.to_string()) {
// This peer has mined some of the forbidden zones
return State::Banned;
}
}
let context = context.clone(); let context = context.clone();
let peers_count = peers.get_peers_active_count(); let peers_count = peers.get_peers_active_count();
thread::spawn(move || { thread::spawn(move || {
+11 -8
View File
@@ -164,11 +164,11 @@ pub fn run_interface(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>) {
}; };
match transaction { match transaction {
None => { None => {
create_domain(miner.clone(), name, records, &keystore); create_domain(context.clone(), miner.clone(), name, records, &keystore);
} }
Some(transaction) => { Some(transaction) => {
if transaction.pub_key == keystore.get_public() { if transaction.pub_key == keystore.get_public() {
create_domain(miner.clone(), name, records, &keystore); create_domain(context.clone(), miner.clone(), name, records, &keystore);
} else { } else {
warn!("Tried to mine not owned domain!"); warn!("Tried to mine not owned domain!");
let _ = web_view.eval(&format!("showWarning('{}');", "You cannot change domain that you don't own!")); let _ = web_view.eval(&format!("showWarning('{}');", "You cannot change domain that you don't own!"));
@@ -185,7 +185,7 @@ pub fn run_interface(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>) {
TransferDomain { .. } => {} TransferDomain { .. } => {}
CheckZone { name } => { CheckZone { name } => {
let name = name.to_lowercase(); let name = name.to_lowercase();
if !check_domain(&name, false) { if !check_domain(&name, false) || context.lock().unwrap().iana.has_zone(&name) {
web_view.eval("zoneAvailable(false)").expect("Error evaluating!"); web_view.eval("zoneAvailable(false)").expect("Error evaluating!");
} else { } else {
let c = context.lock().unwrap(); let c = context.lock().unwrap();
@@ -202,11 +202,11 @@ pub fn run_interface(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>) {
}; };
match transaction { match transaction {
None => { None => {
create_domain(miner.clone(), name, data, &keystore); create_domain(context.clone(), miner.clone(), name, data, &keystore);
} }
Some(transaction) => { Some(transaction) => {
if transaction.pub_key == keystore.get_public() { if transaction.pub_key == keystore.get_public() {
create_domain(miner.clone(), name, data, &keystore); create_domain(context.clone(), miner.clone(), name, data, &keystore);
} else { } else {
warn!("Tried to mine not owned domain!"); warn!("Tried to mine not owned domain!");
let _ = web_view.eval(&format!("showWarning('{}');", "You cannot change domain that you don't own!")); let _ = web_view.eval(&format!("showWarning('{}');", "You cannot change domain that you don't own!"));
@@ -253,14 +253,17 @@ pub fn run_interface(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>) {
interface.exit(); interface.exit();
} }
fn create_domain<S: Into<String>>(miner: Arc<Mutex<Miner>>, name: S, data: S, keystore: &Keystore) { fn create_domain<S: Into<String>>(context: Arc<Mutex<Context>>, miner: Arc<Mutex<Miner>>, name: S, data: S, keystore: &Keystore) {
let name = name.into(); let name = name.into();
info!("Generating domain or zone {}", name); info!("Generating domain or zone {}", name);
if context.lock().unwrap().iana.has_zone(&name) {
error!("Unable to mine IANA zone {}!", &name);
return;
}
//let tags_vector: Vec<String> = tags.into().trim().split(",").map(|s| s.trim()).map(String::from).collect(); //let tags_vector: Vec<String> = tags.into().trim().split(",").map(|s| s.trim()).map(String::from).collect();
let transaction = Transaction::from_str(name.into(), "dns".into(), data.into(), keystore.get_public().clone()); let transaction = Transaction::from_str(name.into(), "dns".into(), data.into(), keystore.get_public().clone());
let block = Block::new(Some(transaction), keystore.get_public(), Bytes::default()); let block = Block::new(Some(transaction), keystore.get_public(), Bytes::default());
let mut miner_guard = miner.lock().unwrap(); miner.lock().unwrap().add_block(block);
miner_guard.add_block(block);
} }
#[derive(Deserialize)] #[derive(Deserialize)]