parent
046ea8d3a3
commit
4b6ce80790
13 changed files with 311 additions and 79 deletions
@ -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; |
||||
|
||||
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>>()) |
||||
} |
||||
|
||||
use u_lib::Client; |
||||
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; |
||||
} |
||||
|
@ -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…
Reference in new issue