4-update-check
plazmoid 4 years ago
parent 4b6ce80790
commit b711eff13d
  1. 11
      bin/u-agent/src/main.rs
  2. 22
      bin/u-agent/src/network.rs
  3. 44
      bin/u-server/src/main.rs
  4. 1
      lib/u_lib/Cargo.toml
  5. 9
      lib/u_lib/src/config.rs
  6. 16
      lib/u_lib/src/contracts/client.rs
  7. 47
      lib/u_lib/src/contracts/messaging.rs
  8. 28
      lib/u_lib/src/contracts/mod.rs
  9. 3
      lib/u_lib/src/lib.rs

@ -8,26 +8,25 @@
// самоуничтожение
//mod jobs;
use std::process::Command;
//use std::process::Command;
use std::thread::sleep;
use std::time::Duration;
mod network;
use network::ClientHandler;
#[tokio::main]
async fn main() {
//daemonize();
let instance = ClientHandler::new();
instance.init().await;
instance.list().await;
/*loop {
loop {
instance.list().await;
sleep(Duration::from_secs(2));
/*Command::new("touch")
.arg("/tmp/win2")
.output()
.unwrap();*/
}*/
}
}

@ -8,8 +8,6 @@ use reqwest::{
Url,
Response
};
use uuid::Uuid;
use std::collections::HashMap;
pub struct ClientHandler {
@ -24,10 +22,7 @@ impl ClientHandler {
client: Client::new(),
cli_info: ClientInfo::gather(),
base_url: Url::parse(
&format!("http://{}:{}",
MASTER_SERVER,
MASTER_PORT
)
&format!("http://{}:{}", MASTER_SERVER, MASTER_PORT)
).unwrap()
}
}
@ -35,10 +30,7 @@ impl ClientHandler {
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()
))
.json(&self.cli_info.as_message())
.send()
.await
.unwrap();
@ -47,7 +39,6 @@ impl ClientHandler {
response
.json::<Message<RawMsg>>()
.await
.unwrap()
);
}
@ -58,11 +49,12 @@ impl ClientHandler {
.await
.unwrap();
let clients = response
.json::<Message<CliStorage>>()
.json::<Message<Vec<ClientInfo>>>()
.await
.unwrap();
for cli in clients.item {
println!("{}: {:?}", cli.0, cli.1);
println!("***********");
for cli in clients.item.iter() {
println!("{:?}", cli);
}
}
}
}

@ -2,13 +2,11 @@ use warp::{
Filter,
Rejection,
Reply,
body,
http::StatusCode
body
};
use std::{
collections::HashMap,
sync::Arc,
marker::Send
sync::Arc
};
use env_logger;
use tokio::sync::Mutex;
@ -20,34 +18,28 @@ use u_lib::{
contracts::*
};
use serde::Deserialize;
type SharedStorage = Arc<Mutex<CliStorage>>;
fn get_content()
-> impl Filter<Extract = (Message<ClientInfo>,), Error = Rejection> + Clone {
fn get_content() -> impl Filter<
Extract = (Message<'static, ClientInfo>,),
Error = Rejection
> + Clone {
body::content_length_limit(1024*64).and(body::json::<Message<ClientInfo>>())
}
async fn add_client(msg: Message<ClientInfo>, db: SharedStorage) ->
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())
)
&RawMsg("Already exist".to_string()).into_message()
))
} else {
let id = new_cli.id.clone();
clients.insert(new_cli.id.clone(), new_cli);
clients.insert(new_cli.id.clone(), new_cli.into_owned());
Ok(warp::reply::json(
&Message::new(
&id,
&RawMsg("Added".to_string())
))
&RawMsg("Added".to_string()).into_message()
)
)
}
}
@ -55,8 +47,12 @@ Result<impl Reply, Rejection> {
async fn listing(db: SharedStorage) ->
Result<impl Reply, Rejection> {
let clients = db.lock().await;
let mut result: Vec<ClientInfo> = Vec::with_capacity(clients.len());
for cli in clients.values() {
result.push(cli.clone());
}
Ok(warp::reply::json(
&Message::new(&Uuid::nil(), clients)
&Message::new_owned(result)
))
}
@ -69,9 +65,6 @@ async fn main() {
);
let db = warp::any().map(move || Arc::clone(&base_db));
let hello = warp::get().and(warp::path::param())
.map(|p: String| format!("Hello, {}", p));
let new_client = warp::post()
.and(warp::path("new"))
.and(get_content())
@ -83,10 +76,9 @@ async fn main() {
.and(db.clone())
.and_then(listing);
let routes = hello
.or(new_client)
let routes = new_client
.or(ls)
.with(warp::log("warp"));
warp::serve(routes)
.run((MASTER_SERVER.octets(), MASTER_PORT)).await;
.run(([0,0,0,0], MASTER_PORT)).await;
}

@ -11,3 +11,4 @@ serde = { version = "1.0.114", features = ["derive"] }
uuid = { version = "^0.8.1", features = ["serde", "v4"] }
nix = "0.17"
libc = "^0.2"
lazy_static = "1.4.0"

@ -1,4 +1,9 @@
use std::net::Ipv4Addr;
use uuid::Uuid;
pub const MASTER_SERVER: Ipv4Addr = Ipv4Addr::LOCALHOST;
pub const MASTER_PORT: u16 = 63714;
pub const MASTER_SERVER: Ipv4Addr = Ipv4Addr::new(3,9,16,40); //Ipv4Addr::LOCALHOST;
pub const MASTER_PORT: u16 = 63714;
lazy_static! {
pub static ref UID: Uuid = Uuid::new_v4();
}

@ -2,9 +2,18 @@ use serde::{
Deserialize,
Serialize
};
use std::{
collections::HashMap
};
use uuid::Uuid;
use super::{
ToMsg
};
use crate::UID;
pub type CliStorage = HashMap<Uuid, ClientInfo>;
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ClientInfo {
pub local_ip: String,
pub hostname: String,
@ -19,11 +28,12 @@ impl ClientInfo {
ClientInfo {
local_ip: String::from("1.2.3.4"),
hostname: String::from("polokonzerva"),
username: String::from("plazmoid (lol what a stupid name)"),
username: String::from("plazmoid"),
os: String::from("pinux"),
platform: String::from("x86_64"),
id: Uuid::new_v4()
id: *UID
}
}
}
impl ToMsg for ClientInfo {}

@ -3,28 +3,51 @@ use serde::{
Serialize,
Deserialize
};
use super::ClientInfo;
use std::collections::HashMap;
use std::{
borrow::Cow
};
use crate::UID;
pub trait ToMsg
where Self: Clone {
fn as_message<'m>(&'m self) -> Message<'m, Self>
where Cow<'m, Self>: From<&'m Self> {
Message::new(self)
}
pub type CliStorage = HashMap<Uuid, ClientInfo>;
fn into_message(self) -> Message<'static, Self> {
Message::new_owned(self)
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Message<I> {
pub struct Message<'cow, I>
where I: Clone {
pub id: Uuid,
pub item: I
pub item: Cow<'cow, I>
}
impl<I> Message<I> {
pub fn new(id: &Uuid, item: I) -> Self {
impl<'cow, I> Message<'cow, I>
where I: Clone {
pub fn new<C>(item: C) -> Self
where C: Into<Cow<'cow, I>> {
Self {
id: id.clone(),
item
id: *UID,
item: item.into()
}
}
// crutch
// because Vec is stored as &[] (wtf?)
pub fn new_owned(item: I) -> Self {
Self {
id: *UID,
item: Cow::Owned(item)
}
}
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RawMsg(pub String);
#[derive(Serialize, Deserialize, Debug)]
pub struct Empty;
impl ToMsg for RawMsg {}

@ -4,4 +4,30 @@ pub mod messaging;
pub use {
client::*,
messaging::*
};
};
use std::{
borrow::Cow
};
macro_rules! to_cow {
($type:ty) => {
impl<'cow> From<$type> for Cow<'cow, $type> {
#[inline]
fn from(obj: $type) -> Cow<'cow, $type> {
Cow::Owned(obj)
}
}
impl<'cow> From<&'cow $type> for Cow<'cow, $type> {
#[inline]
fn from(obj: &'cow $type) -> Cow<'cow, $type> {
Cow::Borrowed(obj)
}
}
}
}
to_cow!(ClientInfo);
to_cow!(RawMsg);

@ -4,3 +4,6 @@ pub mod utils;
pub use utils::*;
pub use config::*;
#[macro_use]
extern crate lazy_static;

Loading…
Cancel
Save