From 4b6ce80790d6647cef1efb323b82c9d227dacbea Mon Sep 17 00:00:00 2001 From: plazmoid Date: Sun, 26 Jul 2020 01:28:49 +0500 Subject: [PATCH] fukken compiled --- Cargo.toml | 5 +- bin/u-agent/Cargo.toml | 9 ++- bin/u-agent/src/main.rs | 71 ++++++----------------- bin/u-agent/src/network.rs | 68 ++++++++++++++++++++++ bin/u-server/Cargo.toml | 16 ++++++ bin/u-server/src/main.rs | 85 +++++++++++++++++++++++++--- lib/u_lib/Cargo.toml | 5 +- lib/u_lib/src/config.rs | 4 ++ lib/u_lib/src/contracts/client.rs | 29 ++++++++++ lib/u_lib/src/contracts/messaging.rs | 30 ++++++++++ lib/u_lib/src/contracts/mod.rs | 7 +++ lib/u_lib/src/lib.rs | 14 ++--- lib/u_lib/src/utils.rs | 47 +++++++++++++++ 13 files changed, 311 insertions(+), 79 deletions(-) create mode 100644 bin/u-agent/src/network.rs create mode 100644 bin/u-server/Cargo.toml create mode 100644 lib/u_lib/src/config.rs create mode 100644 lib/u_lib/src/contracts/client.rs create mode 100644 lib/u_lib/src/contracts/messaging.rs create mode 100644 lib/u_lib/src/contracts/mod.rs create mode 100644 lib/u_lib/src/utils.rs diff --git a/Cargo.toml b/Cargo.toml index 7ea1ae1..824cc9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,4 +2,7 @@ members = [ "bin/*", "lib/u_lib" -] \ No newline at end of file +] + +[profile.release] +panic = "abort" diff --git a/bin/u-agent/Cargo.toml b/bin/u-agent/Cargo.toml index 1345c84..f092dcb 100644 --- a/bin/u-agent/Cargo.toml +++ b/bin/u-agent/Cargo.toml @@ -7,14 +7,13 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -nix = "0.17" -libc = "^0.2" +tokio = { version = "*", features = ["macros"] } sysinfo = "0.10.5" #lazy_static = "1.4.0" log = "^0.4" env_logger = "0.7.1" cron = "0.6.0" +uuid = "0.8.1" -[profile.release] -panic = 'abort' -# strip \ No newline at end of file +reqwest = { version = "0.10.7", features = ["json"] } +u_lib = { version = "*", path = "../../lib/u_lib" } \ No newline at end of file diff --git a/bin/u-agent/src/main.rs b/bin/u-agent/src/main.rs index 5e980f8..835e528 100644 --- a/bin/u-agent/src/main.rs +++ b/bin/u-agent/src/main.rs @@ -7,66 +7,27 @@ // проверка ssh ключей и распространение через known_hosts // самоуничтожение -use std::process::exit; +//mod jobs; +use std::process::Command; +use std::thread::sleep; +use std::time::Duration; -use nix::{ - unistd::{ - getppid, - setsid, - fork, - ForkResult, - close as fdclose, - chdir - }, - sys::signal::{ - signal, - Signal, - SigHandler - } -}; +mod network; +use network::ClientHandler; -mod jobs; -fn setsig(sig: Signal, hnd: SigHandler) { - unsafe { - signal(sig, hnd).unwrap(); - } -} - -fn daemonize() { - if getppid().as_raw() != 1 { - setsig(Signal::SIGTTOU, SigHandler::SigIgn); - setsig(Signal::SIGTTIN, SigHandler::SigIgn); - setsig(Signal::SIGTSTP, SigHandler::SigIgn); - } - for fd in 0..=2 { - match fdclose(fd) { _ => () } - } - match chdir("/") { _ => () }; - - match fork() { - Ok(ForkResult::Parent {..}) => { - exit(0); - }, - Ok(ForkResult::Child) => { - match setsid() { _ => () } - } - Err(_) => { - exit(255) - } - } -} +#[tokio::main] +async fn main() { + //daemonize(); + let instance = ClientHandler::new(); + instance.init().await; + instance.list().await; + /*loop { -fn main() { - use std::process::Command; - use std::thread::sleep; - use std::time::Duration; - daemonize(); - loop { sleep(Duration::from_secs(2)); - Command::new("touch") + /*Command::new("touch") .arg("/tmp/win2") .output() - .unwrap(); - } + .unwrap();*/ + }*/ } diff --git a/bin/u-agent/src/network.rs b/bin/u-agent/src/network.rs new file mode 100644 index 0000000..2f1fd1e --- /dev/null +++ b/bin/u-agent/src/network.rs @@ -0,0 +1,68 @@ +use u_lib::{ + MASTER_SERVER, + MASTER_PORT, + contracts::* +}; +use reqwest::{ + Client, + Url, + Response +}; +use uuid::Uuid; +use std::collections::HashMap; + + +pub struct ClientHandler { + base_url: Url, + client: Client, + cli_info: ClientInfo +} + +impl ClientHandler { + pub fn new() -> Self { + Self { + client: Client::new(), + cli_info: ClientInfo::gather(), + base_url: Url::parse( + &format!("http://{}:{}", + MASTER_SERVER, + MASTER_PORT + ) + ).unwrap() + } + } + + pub async fn init(&self) { + let response: Response = self.client + .post(self.base_url.join("/new").unwrap()) + .json(&Message::new( + &self.cli_info.id, + self.cli_info.clone() + )) + .send() + .await + .unwrap(); + println!("{}: {:?}", + response.status(), + response + .json::>() + .await + .unwrap() + ); + } + + pub async fn list(&self) { + let response: Response = self.client + .get(self.base_url.join("/ls").unwrap()) + .send() + .await + .unwrap(); + let clients = response + .json::>() + .await + .unwrap(); + for cli in clients.item { + println!("{}: {:?}", cli.0, cli.1); + } + } +} \ No newline at end of file diff --git a/bin/u-server/Cargo.toml b/bin/u-server/Cargo.toml new file mode 100644 index 0000000..98b6946 --- /dev/null +++ b/bin/u-server/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "u-server" +version = "0.1.0" +authors = ["plazmoid "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +warp = "*" +serde = { version = "1.0.114", features = ["derive"] } +tokio = { version = "*", features = ["macros"] } +log = "*" +env_logger = "*" +uuid = "0.8.1" +u_lib = { version = "*", path = "../../lib/u_lib" } \ No newline at end of file diff --git a/bin/u-server/src/main.rs b/bin/u-server/src/main.rs index ae5c180..f721963 100644 --- a/bin/u-server/src/main.rs +++ b/bin/u-server/src/main.rs @@ -1,23 +1,92 @@ use warp::{ - Filter + Filter, + Rejection, + Reply, + body, + http::StatusCode +}; +use std::{ + collections::HashMap, + sync::Arc, + marker::Send }; use env_logger; use tokio::sync::Mutex; -use std::collections::HashMap; +use uuid::Uuid; + +use u_lib::{ + MASTER_SERVER, + MASTER_PORT, + contracts::* +}; + +use serde::Deserialize; -use u_lib::Client; +type SharedStorage = Arc>; + +fn get_content() +-> impl Filter,), Error = Rejection> + Clone { + body::content_length_limit(1024*64).and(body::json::>()) +} + +async fn add_client(msg: Message, db: SharedStorage) -> +Result { + let new_cli = msg.item; + let mut clients = db.lock().await; + if clients.contains_key(&new_cli.id) { + Ok(warp::reply::json( + &Message::new( + &new_cli.id, + &RawMsg("Already exist".to_string()) + ) + )) + } else { + let id = new_cli.id.clone(); + clients.insert(new_cli.id.clone(), new_cli); + Ok(warp::reply::json( + &Message::new( + &id, + &RawMsg("Added".to_string()) + )) + ) + } +} + +async fn listing(db: SharedStorage) -> +Result { + let clients = db.lock().await; + Ok(warp::reply::json( + &Message::new(&Uuid::nil(), clients) + )) +} -type Storage = Mutex>; #[tokio::main] async fn main() { env_logger::init(); - let db: Storage = Mutex::new(HashMap::::new()); + let base_db: SharedStorage = Arc::new( + Mutex::new(HashMap::::new()) + ); + let db = warp::any().map(move || Arc::clone(&base_db)); + let hello = warp::get().and(warp::path::param()) - .map(|p: String| format!("Hello, {} ({})", p, LOL)); + .map(|p: String| format!("Hello, {}", p)); + + let new_client = warp::post() + .and(warp::path("new")) + .and(get_content()) + .and(db.clone()) + .and_then(add_client); + + let ls = warp::get() + .and(warp::path("ls")) + .and(db.clone()) + .and_then(listing); - //let post = warp::post().and(warp::path("new")) let routes = hello + .or(new_client) + .or(ls) .with(warp::log("warp")); - warp::serve(routes).run(([127,0,0,1], 63714)).await; + warp::serve(routes) + .run((MASTER_SERVER.octets(), MASTER_PORT)).await; } diff --git a/lib/u_lib/Cargo.toml b/lib/u_lib/Cargo.toml index ec899e6..2ada440 100644 --- a/lib/u_lib/Cargo.toml +++ b/lib/u_lib/Cargo.toml @@ -7,4 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -serde = { version = "1.0.114", features = ["derive"] } \ No newline at end of file +serde = { version = "1.0.114", features = ["derive"] } +uuid = { version = "^0.8.1", features = ["serde", "v4"] } +nix = "0.17" +libc = "^0.2" diff --git a/lib/u_lib/src/config.rs b/lib/u_lib/src/config.rs new file mode 100644 index 0000000..1f46e96 --- /dev/null +++ b/lib/u_lib/src/config.rs @@ -0,0 +1,4 @@ +use std::net::Ipv4Addr; + +pub const MASTER_SERVER: Ipv4Addr = Ipv4Addr::LOCALHOST; +pub const MASTER_PORT: u16 = 63714; \ No newline at end of file diff --git a/lib/u_lib/src/contracts/client.rs b/lib/u_lib/src/contracts/client.rs new file mode 100644 index 0000000..1ab2a6a --- /dev/null +++ b/lib/u_lib/src/contracts/client.rs @@ -0,0 +1,29 @@ +use serde::{ + Deserialize, + Serialize +}; +use uuid::Uuid; + +#[derive(Serialize, Deserialize, Debug)] +pub struct ClientInfo { + pub local_ip: String, + pub hostname: String, + pub username: String, + pub os: String, + pub platform: String, + pub id: Uuid, +} + +impl ClientInfo { + pub fn gather() -> Self { + ClientInfo { + local_ip: String::from("1.2.3.4"), + hostname: String::from("polokonzerva"), + username: String::from("plazmoid (lol what a stupid name)"), + os: String::from("pinux"), + platform: String::from("x86_64"), + id: Uuid::new_v4() + } + } +} + diff --git a/lib/u_lib/src/contracts/messaging.rs b/lib/u_lib/src/contracts/messaging.rs new file mode 100644 index 0000000..18c1aa1 --- /dev/null +++ b/lib/u_lib/src/contracts/messaging.rs @@ -0,0 +1,30 @@ +use uuid::Uuid; +use serde::{ + Serialize, + Deserialize +}; +use super::ClientInfo; +use std::collections::HashMap; + +pub type CliStorage = HashMap; + +#[derive(Serialize, Deserialize, Debug)] +pub struct Message { + pub id: Uuid, + pub item: I +} + +impl Message { + pub fn new(id: &Uuid, item: I) -> Self { + Self { + id: id.clone(), + item + } + } +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct RawMsg(pub String); + +#[derive(Serialize, Deserialize, Debug)] +pub struct Empty; \ No newline at end of file diff --git a/lib/u_lib/src/contracts/mod.rs b/lib/u_lib/src/contracts/mod.rs new file mode 100644 index 0000000..01122a4 --- /dev/null +++ b/lib/u_lib/src/contracts/mod.rs @@ -0,0 +1,7 @@ +pub mod client; +pub mod messaging; + +pub use { + client::*, + messaging::* +}; \ No newline at end of file diff --git a/lib/u_lib/src/lib.rs b/lib/u_lib/src/lib.rs index 7033448..8c40c85 100644 --- a/lib/u_lib/src/lib.rs +++ b/lib/u_lib/src/lib.rs @@ -1,10 +1,6 @@ -use serde::Deserialize; +pub mod config; +pub mod contracts; +pub mod utils; -#[derive(Deserialize)] -pub struct Client { - pub ip: String, - pub hostname: String, - pub username: String, - pub os: String, - pub platform: String -} \ No newline at end of file +pub use utils::*; +pub use config::*; diff --git a/lib/u_lib/src/utils.rs b/lib/u_lib/src/utils.rs new file mode 100644 index 0000000..90c9284 --- /dev/null +++ b/lib/u_lib/src/utils.rs @@ -0,0 +1,47 @@ +use nix::{ + unistd::{ + getppid, + setsid, + fork, + ForkResult, + close as fdclose, + chdir + }, + sys::signal::{ + signal, + Signal, + SigHandler + } +}; +use std::process::exit; + + +pub fn daemonize() { + if getppid().as_raw() != 1 { + setsig(Signal::SIGTTOU, SigHandler::SigIgn); + setsig(Signal::SIGTTIN, SigHandler::SigIgn); + setsig(Signal::SIGTSTP, SigHandler::SigIgn); + } + for fd in 0..=2 { + match fdclose(fd) { _ => () } + } + match chdir("/") { _ => () }; + + match fork() { + Ok(ForkResult::Parent {..}) => { + exit(0); + }, + Ok(ForkResult::Child) => { + match setsid() { _ => () } + } + Err(_) => { + exit(255) + } + } +} + +pub fn setsig(sig: Signal, hnd: SigHandler) { + unsafe { + signal(sig, hnd).unwrap(); + } +} \ No newline at end of file