print nodes working

This commit is contained in:
ghe0 2024-08-15 01:11:38 +03:00
parent 68c1978742
commit 701c2c6010
6 changed files with 1461 additions and 51 deletions

1405
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -4,9 +4,14 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
ed25519-dalek = "2.1.1" ed25519-dalek = { version = "2.1.1", features = ["rand_core", "serde"] }
hex = "0.4.3"
once_cell = "1.19.0" once_cell = "1.19.0"
prost = "0.13.1" prost = "0.13.1"
rand = "0.8.5"
salvo = "0.70.0"
tabled = "0.16.0"
tokio = { version = "1.39.2", features = ["macros"] }
tonic = "0.12.1" tonic = "0.12.1"
[build-dependencies] [build-dependencies]

@ -1,51 +1,68 @@
#![allow(dead_code)] #![allow(dead_code)]
use ed25519_dalek::{SigningKey, VerifyingKey};
use once_cell::sync::Lazy;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Mutex; use std::sync::Mutex;
use ed25519_dalek::{VerifyingKey, SigningKey};
use once_cell::sync::Lazy;
use std::time::SystemTime; use std::time::SystemTime;
use tabled::{Table, Tabled};
#[derive(Clone)] #[derive(Clone)]
pub struct NodeInfo { pub struct NodeInfo {
pubkey: VerifyingKey, pub pubkey: VerifyingKey,
updated_at: SystemTime, pub updated_at: SystemTime,
} }
static NODES: Lazy<Mutex<HashMap<String, NodeInfo>>> = Lazy::new(|| { static NODES: Lazy<Mutex<HashMap<String, NodeInfo>>> = Lazy::new(|| Mutex::new(HashMap::new()));
Mutex::new(HashMap::new())
});
static KEYS: Lazy<Mutex<HashMap<VerifyingKey, SigningKey>>> = Lazy::new(|| { static KEYS: Lazy<Mutex<HashMap<VerifyingKey, SigningKey>>> =
Mutex::new(HashMap::new()) Lazy::new(|| Mutex::new(HashMap::new()));
});
pub fn add_key(pubkey: VerifyingKey, privkey: SigningKey) { pub fn add_key(pubkey: VerifyingKey, privkey: SigningKey) {
let mut map = KEYS.lock().unwrap(); let mut keys = KEYS.lock().unwrap();
map.insert(pubkey, privkey); keys.insert(pubkey, privkey);
} }
pub fn remove_key(pubkey: &VerifyingKey) { pub fn remove_key(pubkey: &VerifyingKey) {
let mut map = KEYS.lock().unwrap(); let mut keys = KEYS.lock().unwrap();
map.remove(pubkey); keys.remove(pubkey);
} }
pub fn get_privkey(pubkey: &VerifyingKey) -> Option<SigningKey> { pub fn get_privkey(pubkey: &VerifyingKey) -> Option<SigningKey> {
let map = KEYS.lock().unwrap(); let keys = KEYS.lock().unwrap();
map.get(pubkey).cloned() keys.get(pubkey).cloned()
} }
pub fn add_node(ip: String, info: NodeInfo) { pub fn add_node(ip: String, info: NodeInfo) {
let mut map = NODES.lock().unwrap(); let mut nodes = NODES.lock().unwrap();
map.insert(ip, info); nodes.insert(ip, info);
} }
pub fn remove_node(ip: &str) { pub fn remove_node(ip: &str) {
let mut map = NODES.lock().unwrap(); let mut nodes = NODES.lock().unwrap();
map.remove(ip); nodes.remove(ip);
} }
pub fn get_pubkey(ip: &str) -> Option<NodeInfo> { pub fn get_pubkey(ip: &str) -> Option<NodeInfo> {
let map = NODES.lock().unwrap(); let nodes = NODES.lock().unwrap();
map.get(ip).cloned() nodes.get(ip).cloned()
} }
pub fn get_nodes_as_html_tabe() -> String {
#[derive(Tabled)]
struct OutputRow {
ip: String,
pubkey: String,
age: u64,
}
let mut output = vec![];
for (ip, node_info) in NODES.lock().unwrap().iter() {
let ip = ip.clone();
let pubkey = hex::encode(node_info.pubkey.as_bytes());
let age = std::time::SystemTime::now()
.duration_since(node_info.updated_at)
.unwrap_or(std::time::Duration::ZERO)
.as_secs();
output.push(OutputRow { ip, pubkey, age });
}
Table::new(output).to_string()
}

14
src/grpc.rs Normal file

@ -0,0 +1,14 @@
use crate::database;
use crate::database::NodeInfo;
use rand::rngs::OsRng;
pub fn add_node(ip: String) {
let mut csprng = OsRng;
database::add_node(
ip,
NodeInfo {
pubkey: ed25519_dalek::SigningKey::generate(&mut csprng).verifying_key(),
updated_at: std::time::SystemTime::now(),
},
)
}

14
src/http_server.rs Normal file

@ -0,0 +1,14 @@
use salvo::prelude::*;
use crate::database::get_nodes_as_html_tabe;
#[handler]
async fn homepage() -> String {
get_nodes_as_html_tabe()
}
pub async fn start() {
let acceptor = TcpListener::new("0.0.0.0:5800").bind().await;
let router = Router::new().get(homepage);
println!("{:?}", router);
Server::new(acceptor).serve(router).await;
}

@ -1,4 +1,11 @@
mod grpc;
mod http_server;
mod database; mod database;
fn main() {
println!("Hello, world!"); #[tokio::main]
async fn main() {
grpc::add_node("1.1.1.1".to_string());
grpc::add_node("1.2.3.4".to_string());
grpc::add_node("2.2.2.2".to_string());
crate::http_server::start().await;
} }