diff --git a/src/client/attribute.rs b/src/client/attribute.rs deleted file mode 100644 index 094fcf7..0000000 --- a/src/client/attribute.rs +++ /dev/null @@ -1,20 +0,0 @@ -pub struct Attribute { - value: String, - count: u128, -} - -impl Attribute { - pub fn new(value: &str) -> Attribute { - Attribute { - value: String::from(value), - count: 0, - } - } - pub fn count(&mut self) -> u128 { - return self.count; - } - pub fn incr(&mut self) { - self.count += 1; - } -} - diff --git a/src/client/db.rs b/src/client/db.rs deleted file mode 100644 index 553b236..0000000 --- a/src/client/db.rs +++ /dev/null @@ -1,38 +0,0 @@ -//mod attribute; - -use std::collections::HashMap; - -use crate::attribute::Attribute; - -pub struct Database { - db: HashMap, -} - -impl Database { - pub fn new() -> Database { - Database { - db: HashMap::new(), - } - } - pub fn write(&mut self, path: &str, value: &str) -> bool { - let attr = self.db.get_mut(&path.to_string()); - match attr { - Some(attr) => { attr.incr(); }, - None => { - let mut iattr = Attribute::new(&value); - iattr.incr(); - self.db.insert(path.to_string(), iattr); - }, - } - return true; - } - pub fn get_count(&mut self, path: &str, _value: &str) -> u128 { - let attr = self.db.get_mut(&path.to_string()); - match attr { - Some(attr) => { return attr.count(); }, - None => { - return 0; - }, - }; - } -} diff --git a/src/client/db_read.rs b/src/client/db_read.rs deleted file mode 100644 index f23342c..0000000 --- a/src/client/db_read.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::db::Database; - -pub fn read(db: &mut Database, path: &str, value: &str) { - println!("path:{}", path); - println!("value:{}", value); - let cnt = db.get_count(path, value); - println!("{}", cnt); -} diff --git a/src/client/db_write.rs b/src/client/db_write.rs deleted file mode 100644 index 036a8e8..0000000 --- a/src/client/db_write.rs +++ /dev/null @@ -1,7 +0,0 @@ -use crate::db::Database; - -pub fn write(db: &mut Database, path: &str, value: &str) { - println!("path:{}", path); - println!("value:{}", value); - db.write(path, value); -} diff --git a/src/client/main.rs b/src/client/main.rs index a91d25d..1a8f45c 100644 --- a/src/client/main.rs +++ b/src/client/main.rs @@ -1,22 +1,13 @@ extern crate rustyline; -extern crate dirs; +// extern crate dirs; extern crate regex; //extern crate clap; -mod db_write; -mod db_read; -mod attribute; -mod db; - use rustyline::error::ReadlineError; use rustyline::Editor; use regex::Regex; //use clap::App; -//use attribute::Attribute; - - - fn main() { // App::new("Sighting DB") // .version("0.0.1") @@ -26,18 +17,16 @@ fn main() { println!("Sigthing DB - count attributes at scale\n(c) Devo Inc 2019 - Written by Sebastien Tricaud"); - let mut db = db::Database::new(); - // let mut histfile = String::from(""); - match dirs::home_dir() { - Some(mut dir) => { - dir = dir.join(".sightingdb_history"); - // histfile = dir.to_string_lossy().to_string(); - } - None => { - // histfile = String::from(".sightingdb_history"); - } - } + // match dirs::home_dir() { + // Some(mut dir) => { + // dir = dir.join(".sightingdb_history"); + // // histfile = dir.to_string_lossy().to_string(); + // } + // None => { + // // histfile = String::from(".sightingdb_history"); + // } + // } let rw_re = Regex::new(r"^(r|w)\s(\S+)\s(.*)").unwrap(); @@ -55,10 +44,10 @@ fn main() { let value = caps.get(3).map_or("", |m| m.as_str()); if db_action == "r" { - db_read::read(&mut db, path, value); + println!("read namespace={}; value={}", path, value); } if db_action == "w" { - db_write::write(&mut db, path, value); + println!("write namespace={}; value={}", path, value); } }, Err(ReadlineError::Interrupted) => { diff --git a/src/daemon/db.rs b/src/daemon/db.rs index 420b3ec..cff4cd3 100644 --- a/src/daemon/db.rs +++ b/src/daemon/db.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use serde::{Deserialize, Serialize}; +use serde::{Serialize}; use regex::Regex; use crate::attribute::Attribute; @@ -33,15 +33,14 @@ impl Database { pub fn write(&mut self, path: &str, value: &str, timestamp: i64, write_consensus: bool) -> u128 { let valuestable = self.hashtable.get_mut(&path.to_string()); let mut new_value_to_path = false; - let mut retval = 0; - let mut consensus_count = 0; + let retval; match valuestable { Some(valuestable) => { //let mut valuestable = self.hashtable.get_mut(&path.to_string()).unwrap(); let attr = valuestable.get(&value.to_string()); match attr { - Some(attr) => { + Some(_attr) => { let iattr = valuestable.get_mut(&value.to_string()).unwrap(); if timestamp > 0 { iattr.incr_from_timestamp(timestamp); @@ -95,11 +94,10 @@ impl Database { return retval; } pub fn new_consensus(&mut self, path: &str, value: &str, consensus_count: u128) -> u128 { - let count: u32; let valuestable = self.hashtable.get_mut(&path.to_string()).unwrap(); let attr = valuestable.get_mut(&value.to_string()); match attr { - Some(attr) => { + Some(_attr) => { let iattr = valuestable.get_mut(&value.to_string()).unwrap(); iattr.set_consensus(consensus_count); return iattr.consensus; @@ -110,7 +108,6 @@ impl Database { }; } pub fn get_count(&mut self, path: &str, value: &str) -> u128 { - let count: u128; let valuestable = self.hashtable.get_mut(&path.to_string()).unwrap(); let attr = valuestable.get_mut(&value.to_string()); match attr { @@ -128,8 +125,8 @@ impl Database { let attr = valuestable.get_mut(&value.to_string()); match attr { Some(attr) => { - if (attr.ttl > 0) { - println!("{:?}", attr); + if attr.ttl > 0 { + println!("FIXME, IMPLEMENT TTL. {:?}", attr); } attr.consensus = consensus_count; diff --git a/src/daemon/main.rs b/src/daemon/main.rs index ba4f757..7cd5ecd 100644 --- a/src/daemon/main.rs +++ b/src/daemon/main.rs @@ -18,7 +18,7 @@ use daemonize::Daemonize; use ansi_term::Color::Red; use ini::Ini; -use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder, Error}; +use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder}; use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod}; use qstring::QString; @@ -97,7 +97,6 @@ fn read(data: web::Data>>, _req: HttpRequest) -> impl Res // fn write(db: web::Data>, _req: HttpRequest) -> impl Responder { fn write(data: web::Data>>, _req: HttpRequest) -> HttpResponse { let sharedstate = &mut *data.lock().unwrap(); - let mut could_write = false; // println!("{:?}", _req.path()); let (_, path) = _req.path().split_at(3); // We remove '/w/' @@ -108,7 +107,7 @@ fn write(data: web::Data>>, _req: HttpRequest) -> HttpRes Some(v) => { let timestamp = query_string.get("timestamp").unwrap_or("0"); let timestamp_i = timestamp.parse::().unwrap_or(0); - could_write = sighting_writer::write(&mut sharedstate.db, path, v, timestamp_i); + let could_write = sighting_writer::write(&mut sharedstate.db, path, v, timestamp_i); if could_write { return HttpResponse::Ok().json(Message{message: String::from("ok")}); } else { @@ -179,7 +178,7 @@ fn read_bulk_with_stats(data: web::Data>>, postdata: web: fn write_bulk(data: web::Data>>, postdata: web::Json, _req: HttpRequest) -> impl Responder { let sharedstate = &mut *data.lock().unwrap(); let mut could_write = false; - + for i in &postdata.items { if i.value.len() > 0 { // There is no need to write a value that does not exists let timestamp = i.timestamp.unwrap_or(0); @@ -276,7 +275,7 @@ fn main() { // } let configarg = matches.value_of("config"); - let mut configstr = String::from(""); + let configstr; match configarg { Some(_configstr) => { configstr = _configstr.to_string(); }, None => { diff --git a/src/daemon/sighting_configure.rs b/src/daemon/sighting_configure.rs index 0e3f150..9d5ee0c 100644 --- a/src/daemon/sighting_configure.rs +++ b/src/daemon/sighting_configure.rs @@ -1,40 +1,40 @@ -use std::collections::HashMap; +// use std::collections::HashMap; -#[derive(PartialEq, Eq)] -pub enum ConfigStorage { - IN_MEMORY, - ON_DISK, -} +// #[derive(PartialEq, Eq)] +// pub enum ConfigStorage { +// IN_MEMORY, +// ON_DISK, +// } -pub struct Configuration { - storage: HashMap, -} +// pub struct Configuration { +// storage: HashMap, +// } -impl Configuration { - pub fn new() -> Configuration { - Configuration { - storage: HashMap::new(), - } - } - pub fn set_storage(&mut self, storage: ConfigStorage, path: String) { - self.storage.insert(path, storage); - } - pub fn get_storage(&mut self, path: String) -> String { - let storageopt = self.storage.get_mut(&path); - match storageopt { - Some(storageopt) => { - match storageopt { - ConfigStorage::IN_MEMORY => { return String::from("IN_MEMORY"); }, - ConfigStorage::ON_DISK => { return String::from("ON_DISK"); }, - } - }, - None => { - return String::from("IN_MEMORY"); - } - } - } +// impl Configuration { +// pub fn new() -> Configuration { +// Configuration { +// storage: HashMap::new(), +// } +// } +// pub fn set_storage(&mut self, storage: ConfigStorage, path: String) { +// self.storage.insert(path, storage); +// } +// pub fn get_storage(&mut self, path: String) -> String { +// let storageopt = self.storage.get_mut(&path); +// match storageopt { +// Some(storageopt) => { +// match storageopt { +// ConfigStorage::IN_MEMORY => { return String::from("IN_MEMORY"); }, +// ConfigStorage::ON_DISK => { return String::from("ON_DISK"); }, +// } +// }, +// None => { +// return String::from("IN_MEMORY"); +// } +// } +// } -} +// } // pub fn set(path: &str, value: &str) { // println!("Configuring path {}", path); diff --git a/src/daemon/sighting_reader.rs b/src/daemon/sighting_reader.rs index 4d27611..28b80d6 100644 --- a/src/daemon/sighting_reader.rs +++ b/src/daemon/sighting_reader.rs @@ -1,21 +1,17 @@ use crate::db::Database; -use base64::{decode_config, URL_SAFE_NO_PAD}; -use serde::{Deserialize, Serialize}; +use serde::{Serialize}; #[derive(Serialize)] pub struct Message { message: String } -#[derive(Deserialize)] -struct NotFound { - error: String, - path: String, - value: String -} - -//{"error":"Path not found","path":"security_intelligence","value":"MTAuNTIuNjAuNjk"} -//{"value":"MTAuNTIuNjAuNjk","first_seen":1582161107,"last_seen":1582161107,"count":1,"tags":"","ttl":0} +// #[derive(Deserialize)] +// struct NotFound { +// error: String, +// path: String, +// value: String +// } pub fn read(db: &mut Database, path: &str, value: &str, with_stats: bool) -> String { @@ -36,7 +32,11 @@ pub fn read(db: &mut Database, path: &str, value: &str, with_stats: bool) -> Str return attr; } -// Our internal reading does not trigger shadow sightings -pub fn read_internal(db: &mut Database, path: &str, value: &str, with_stats: bool) -> String { - return db.get_attr(path, value, with_stats, 0); -} +// Our internal reading does not trigger shadow sightings. +// USELESS FOR NOW, but will need to reactivate once we have the possibility to skip shadow if we want to +// pub fn read_internal(db: &mut Database, path: &str, value: &str, with_stats: bool) -> String { +// let consensus = db.get_count(&"_all".to_string(), value); +// let attr = db.get_attr(path, value, with_stats, consensus); + +// return attr; +// }