fukken compiled

4-update-check
plazmoid 4 years ago
parent 046ea8d3a3
commit 4b6ce80790
  1. 3
      Cargo.toml
  2. 9
      bin/u-agent/Cargo.toml
  3. 71
      bin/u-agent/src/main.rs
  4. 68
      bin/u-agent/src/network.rs
  5. 16
      bin/u-server/Cargo.toml
  6. 85
      bin/u-server/src/main.rs
  7. 3
      lib/u_lib/Cargo.toml
  8. 4
      lib/u_lib/src/config.rs
  9. 29
      lib/u_lib/src/contracts/client.rs
  10. 30
      lib/u_lib/src/contracts/messaging.rs
  11. 7
      lib/u_lib/src/contracts/mod.rs
  12. 14
      lib/u_lib/src/lib.rs
  13. 47
      lib/u_lib/src/utils.rs

@ -3,3 +3,6 @@ members = [
"bin/*",
"lib/u_lib"
]
[profile.release]
panic = "abort"

@ -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
reqwest = { version = "0.10.7", features = ["json"] }
u_lib = { version = "*", path = "../../lib/u_lib" }

@ -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();*/
}*/
}

@ -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::<Message<RawMsg>>()
.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::<Message<CliStorage>>()
.await
.unwrap();
for cli in clients.item {
println!("{}: {:?}", cli.0, cli.1);
}
}
}

@ -0,0 +1,16 @@
[package]
name = "u-server"
version = "0.1.0"
authors = ["plazmoid <kronos44@mail.ru>"]
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" }

@ -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<Mutex<CliStorage>>;
fn get_content()
-> impl Filter<Extract = (Message<ClientInfo>,), Error = Rejection> + Clone {
body::content_length_limit(1024*64).and(body::json::<Message<ClientInfo>>())
}
async fn add_client(msg: Message<ClientInfo>, db: SharedStorage) ->
Result<impl Reply, Rejection> {
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<impl Reply, Rejection> {
let clients = db.lock().await;
Ok(warp::reply::json(
&Message::new(&Uuid::nil(), clients)
))
}
type Storage = Mutex<HashMap<String, Client>>;
#[tokio::main]
async fn main() {
env_logger::init();
let db: Storage = Mutex::new(HashMap::<String, Client>::new());
let base_db: SharedStorage = Arc::new(
Mutex::new(HashMap::<Uuid, ClientInfo>::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;
}

@ -8,3 +8,6 @@ edition = "2018"
[dependencies]
serde = { version = "1.0.114", features = ["derive"] }
uuid = { version = "^0.8.1", features = ["serde", "v4"] }
nix = "0.17"
libc = "^0.2"

@ -0,0 +1,4 @@
use std::net::Ipv4Addr;
pub const MASTER_SERVER: Ipv4Addr = Ipv4Addr::LOCALHOST;
pub const MASTER_PORT: u16 = 63714;

@ -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()
}
}
}

@ -0,0 +1,30 @@
use uuid::Uuid;
use serde::{
Serialize,
Deserialize
};
use super::ClientInfo;
use std::collections::HashMap;
pub type CliStorage = HashMap<Uuid, ClientInfo>;
#[derive(Serialize, Deserialize, Debug)]
pub struct Message<I> {
pub id: Uuid,
pub item: I
}
impl<I> Message<I> {
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;

@ -0,0 +1,7 @@
pub mod client;
pub mod messaging;
pub use {
client::*,
messaging::*
};

@ -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
}
pub use utils::*;
pub use config::*;

@ -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();
}
}
Loading…
Cancel
Save