diff --git a/Cargo.toml b/Cargo.toml index ede90c2..9cb212d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,8 @@ members = [ "bin/u_agent", "bin/u_panel", + "bin/u_panel/be", + "bin/u_panel/fe", "bin/u_run", "bin/u_server", "lib/u_lib", diff --git a/Makefile.toml b/Makefile.toml index bddcb4d..45c2842 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -66,3 +66,6 @@ docker run --rm \ [tasks.test] dependencies = ["unit", "integration"] + +[tasks.deploy] +script = './scripts/deploy.sh' diff --git a/bin/u_agent/src/lib.rs b/bin/u_agent/src/lib.rs index ad83999..a0b56f1 100644 --- a/bin/u_agent/src/lib.rs +++ b/bin/u_agent/src/lib.rs @@ -60,7 +60,7 @@ pub async fn process_request(job_requests: Vec, client: &ClientHand } } -async fn error_reporting(client: Arc) { +async fn error_reporting(client: Arc) -> ! { loop { let err = ErrChan::recv(); debug!("Error encountered: {:?}", err); @@ -100,10 +100,9 @@ pub async fn run_forever() { env_logger::init(); let arg_ip = env::args().nth(1); let client = Arc::new(ClientHandler::new(arg_ip.as_deref())); - let _cli = client.clone(); panic::set_hook(Box::new(|panic_info| { ErrChan::send(UError::Panic(panic_info.to_string())) })); - tokio::spawn(error_reporting(_cli)); + tokio::spawn(error_reporting(client.clone())); do_stuff(client).await; } diff --git a/bin/u_panel/Cargo.toml b/bin/u_panel/Cargo.toml index 1746585..511cdbf 100644 --- a/bin/u_panel/Cargo.toml +++ b/bin/u_panel/Cargo.toml @@ -11,10 +11,8 @@ structopt = "0.3.21" log = "^0.4" env_logger = "0.7.1" uuid = "0.6.5" -reqwest = { version = "0.11", features = ["json"] } -openssl = "*" -u_lib = { version = "*", path = "../../lib/u_lib" } serde_json = "1.0.4" serde = { version = "1.0.114", features = ["derive"] } -actix-web = "3.3.2" tokio = "1.11.0" +be = { version = "*", path = "./be" } +u_lib = { version = "*", path = "../../lib/u_lib" } \ No newline at end of file diff --git a/bin/u_panel/be/Cargo.toml b/bin/u_panel/be/Cargo.toml new file mode 100644 index 0000000..8831379 --- /dev/null +++ b/bin/u_panel/be/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "be" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +actix-web = "3.3.2" +u_lib = { version = "*", path = "../../../lib/u_lib" } \ No newline at end of file diff --git a/bin/u_panel/src/server/app.rs b/bin/u_panel/be/src/lib.rs similarity index 100% rename from bin/u_panel/src/server/app.rs rename to bin/u_panel/be/src/lib.rs diff --git a/bin/u_panel/fe/Cargo.toml b/bin/u_panel/fe/Cargo.toml new file mode 100644 index 0000000..f84fa64 --- /dev/null +++ b/bin/u_panel/fe/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "fe" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +u_lib = { version = "*", path = "../../../lib/u_lib" } +wasm-bindgen = "0.2.78" +yew = "0.18.0" +yew-router = "0.15.0" diff --git a/bin/u_panel/fe/index.html b/bin/u_panel/fe/index.html new file mode 100644 index 0000000..de24c98 --- /dev/null +++ b/bin/u_panel/fe/index.html @@ -0,0 +1,9 @@ + + + + + + Yew App + + + \ No newline at end of file diff --git a/bin/u_panel/fe/src/main.rs b/bin/u_panel/fe/src/main.rs new file mode 100644 index 0000000..c7e8f03 --- /dev/null +++ b/bin/u_panel/fe/src/main.rs @@ -0,0 +1,51 @@ +use wasm_bindgen::prelude::*; +use yew::prelude::*; +enum Msg { + AddOne, +} +struct Model { + // `ComponentLink` is like a reference to a component. + // It can be used to send messages to the component + link: ComponentLink, + value: i64, +} + +impl Component for Model { + type Message = Msg; + type Properties = (); + + fn create(_props: Self::Properties, link: ComponentLink) -> Self { + Self { link, value: 0 } + } + + fn update(&mut self, msg: Self::Message) -> ShouldRender { + match msg { + Msg::AddOne => { + self.value += 1; + // the value has changed so we need to + // re-render for it to appear on the page + true + } + } + } + + fn change(&mut self, _props: Self::Properties) -> ShouldRender { + // Should only return "true" if new properties are different to + // previously received properties. + // This component has no properties so we will always return "false". + false + } + + fn view(&self) -> Html { + html! { +
+ +

{ self.value }

+
+ } + } +} + +pub fn main() { + yew::start_app::(); +} diff --git a/bin/u_panel/src/argparse.rs b/bin/u_panel/src/argparse.rs index 9267de4..03cfb2b 100644 --- a/bin/u_panel/src/argparse.rs +++ b/bin/u_panel/src/argparse.rs @@ -131,7 +131,7 @@ pub async fn process_cmd(args: Args) -> UResult<()> { JobMapALD::List { uid } => printer.print(cli_handler.get_agent_jobs(uid).await), JobMapALD::Delete { uid } => printer.print(cli_handler.del(Some(uid)).await), }, - Cmd::Server => crate::server::serve().unwrap(), + Cmd::Server => be::serve().unwrap(), } Ok(()) } diff --git a/bin/u_panel/src/main.rs b/bin/u_panel/src/main.rs index 828a67f..0ab0a09 100644 --- a/bin/u_panel/src/main.rs +++ b/bin/u_panel/src/main.rs @@ -1,5 +1,4 @@ mod argparse; -mod server; use argparse::{process_cmd, Args}; use std::process; diff --git a/bin/u_panel/src/server/mod.rs b/bin/u_panel/src/server/mod.rs deleted file mode 100644 index a4fad6b..0000000 --- a/bin/u_panel/src/server/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod app; - -pub use app::serve; diff --git a/integration/tests/tests.rs b/integration/tests/tests.rs index 35a4789..32fcc9f 100644 --- a/integration/tests/tests.rs +++ b/integration/tests/tests.rs @@ -3,19 +3,19 @@ mod fixtures; mod helpers; use std::env; +use u_lib::config::MASTER_PORT; #[macro_use] extern crate rstest; -// TODO: huita -#[ignore] #[tokio::test] async fn test_non_auth_connection_dropped() { let env_server = env::var("U_SERVER").unwrap(); - match reqwest::get(format!("https://{}", env_server)).await { + match reqwest::get(format!("https://{}:{}", env_server, MASTER_PORT)).await { Err(e) => { - dbg!(e.to_string()); - assert!(e.is_request()) + assert!(e + .to_string() + .contains("unable to get local issuer certificate")) } _ => panic!("no error occured on foreign client connection"), } diff --git a/lib/u_lib/Cargo.toml b/lib/u_lib/Cargo.toml index 6d66582..a2afaf6 100644 --- a/lib/u_lib/Cargo.toml +++ b/lib/u_lib/Cargo.toml @@ -13,11 +13,7 @@ uuid = { version = "0.6.5", features = ["serde", "v4"] } nix = "0.17" libc = "^0.2" lazy_static = "1.4.0" -tokio = { version = "1.2.0", features = ["rt-multi-thread", "sync", "macros", "process", "time"] } -reqwest = { version = "0.11", features = ["json", "native-tls"] } -openssl = "*" futures = "0.3.5" -guess_host_triple = "0.1.2" thiserror = "*" log = "*" mockall = "0.9.1" @@ -29,11 +25,14 @@ once_cell = "1.7.2" shlex = "1.0.0" u_api_proc_macro = { version = "*", path = "../u_api_proc_macro" } crossbeam = "0.8.1" -backtrace = "0.3.61" +backtrace = "0.3.61" +diesel = { version = "1.4.5", features = ["postgres", "uuid"] } -[dependencies.diesel] -version = "1.4.5" -features = ["postgres", "uuid"] +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +reqwest = { version = "0.11", features = ["json", "native-tls"] } +tokio = { version = "1.2.0", features = ["rt-multi-thread", "sync", "macros", "process", "time"] } +guess_host_triple = "0.1.2" +openssl = "*" [dev-dependencies] test-case = "1.1.0" diff --git a/lib/u_lib/src/config.rs b/lib/u_lib/src/config.rs index 1fd815c..ed9052d 100644 --- a/lib/u_lib/src/config.rs +++ b/lib/u_lib/src/config.rs @@ -1,7 +1,7 @@ use lazy_static::lazy_static; use uuid::Uuid; -pub const MASTER_SERVER: &str = "127.0.0.1"; //Ipv4Addr::new(3,9,16,40) +pub const MASTER_SERVER: &str = "ortem.xyz"; //Ipv4Addr::new(3,9,16,40) pub const MASTER_PORT: u16 = 63714; lazy_static! { diff --git a/lib/u_lib/src/errors/variants.rs b/lib/u_lib/src/errors/variants.rs index 2b2570d..0819a96 100644 --- a/lib/u_lib/src/errors/variants.rs +++ b/lib/u_lib/src/errors/variants.rs @@ -1,5 +1,6 @@ use backtrace::Backtrace as CrateBacktrace; use diesel::result::Error as DslError; +#[cfg(not(target_arch = "wasm32"))] use reqwest::Error as ReqError; use serde::{Deserialize, Serialize}; use std::fmt; @@ -72,12 +73,14 @@ impl UError { } } +#[cfg(not(target_arch = "wasm32"))] impl From for UErrorBt { fn from(e: ReqError) -> Self { UError::from(e).into_bt() } } +#[cfg(not(target_arch = "wasm32"))] impl From for UError { fn from(e: ReqError) -> Self { UError::NetError(e.to_string(), String::new()) diff --git a/lib/u_lib/src/lib.rs b/lib/u_lib/src/lib.rs index c614b47..1f37843 100644 --- a/lib/u_lib/src/lib.rs +++ b/lib/u_lib/src/lib.rs @@ -1,17 +1,32 @@ #![allow(non_upper_case_globals)] -pub mod api; -pub mod builder; -pub mod cache; -pub mod config; -pub mod datatypes; -pub mod errors; -pub mod executor; -pub mod messaging; -pub mod models; -pub mod utils; +#[cfg(not(target_arch = "wasm32"))] +#[path = "."] +pub mod exports { + pub mod api; + pub mod builder; + pub mod cache; + pub mod config; + pub mod datatypes; + pub mod errors; + pub mod executor; + pub mod messaging; + pub mod models; + pub mod utils; +} + +#[cfg(target_arch = "wasm32")] +#[path = "."] +pub mod exports { + pub mod config; + pub mod errors; + pub mod messaging; + pub mod models; + pub mod utils; +} pub use config::UID; pub use errors::{UError, UErrorBt, ULocalError, ULocalResult, UResult}; +pub use exports::*; pub mod schema_exports { pub use crate::models::{Agentstate, Jobstate, Jobtype}; diff --git a/lib/u_lib/src/models/agent.rs b/lib/u_lib/src/models/agent.rs index daa49a0..7c238a8 100644 --- a/lib/u_lib/src/models/agent.rs +++ b/lib/u_lib/src/models/agent.rs @@ -4,12 +4,10 @@ use serde::{Deserialize, Serialize}; use std::{fmt, time::SystemTime}; use strum::Display; -use crate::{ - builder::NamedJobBuilder, messaging::Reportable, models::schema::*, unwrap_enum, - utils::systime_to_string, UID, -}; +#[cfg(not(target_arch = "wasm32"))] +use crate::builder::NamedJobBuilder; +use crate::{messaging::Reportable, models::schema::*, unwrap_enum, utils::systime_to_string, UID}; -use guess_host_triple::guess_host_triple; use uuid::Uuid; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, DbEnum, Display)] @@ -65,6 +63,7 @@ impl fmt::Display for Agent { } } +#[cfg(not(target_arch = "wasm32"))] impl Agent { pub fn with_id(uid: Uuid) -> Self { Self { @@ -73,6 +72,7 @@ impl Agent { } } + #[cfg(unix)] pub async fn gather() -> Self { let mut builder = NamedJobBuilder::from_shell(vec![ ("hostname", "hostname"), @@ -91,13 +91,14 @@ impl Agent { hostname: decoder(builder.pop("hostname")), is_root: &decoder(builder.pop("is_root")) == "0", username: decoder(builder.pop("username")), - platform: guess_host_triple().unwrap_or("unknown").to_string(), + platform: guess_host_triple::guess_host_triple() + .unwrap_or("unknown") + .to_string(), ..Default::default() } } pub async fn run() -> Reportable { - #[cfg(unix)] Reportable::Agent(Agent::gather().await) } } diff --git a/lib/u_lib/src/models/jobs/assigned.rs b/lib/u_lib/src/models/jobs/assigned.rs index 1b0b3a9..3182796 100644 --- a/lib/u_lib/src/models/jobs/assigned.rs +++ b/lib/u_lib/src/models/jobs/assigned.rs @@ -1,16 +1,16 @@ use super::JobState; +#[cfg(not(target_arch = "wasm32"))] +use crate::{cache::JobCache, utils::TempFile}; use crate::{ - cache::JobCache, errors::UError, messaging::Reportable, models::schema::*, - utils::{systime_to_string, ProcOutput, TempFile}, + utils::{systime_to_string, ProcOutput}, UID, }; use diesel::{Identifiable, Insertable, Queryable}; use serde::{Deserialize, Serialize}; use std::{fmt, time::SystemTime}; -use tokio::process::Command; use uuid::Uuid; #[derive( @@ -75,8 +75,10 @@ impl Default for AssignedJob { } } +#[cfg(not(target_arch = "wasm32"))] impl AssignedJob { pub async fn run(mut self) -> Reportable { + use tokio::process::Command; let (argv, _payload) = { let meta = JobCache::get(&self.job_id).unwrap(); if let Some(ref payload) = meta.payload { diff --git a/lib/u_lib/src/models/jobs/meta.rs b/lib/u_lib/src/models/jobs/meta.rs index 2a12e06..939d256 100644 --- a/lib/u_lib/src/models/jobs/meta.rs +++ b/lib/u_lib/src/models/jobs/meta.rs @@ -1,7 +1,6 @@ use super::JobType; use crate::{models::schema::*, utils::Stripped, UError, UResult}; use diesel::{Identifiable, Insertable, Queryable}; -use guess_host_triple::guess_host_triple; use serde::{Deserialize, Serialize}; use std::fmt; use std::str::from_utf8; @@ -61,7 +60,12 @@ impl Default for JobMeta { alias: None, argv: String::new(), exec_type: JobType::Shell, - platform: guess_host_triple().unwrap_or("unknown").to_string(), + #[cfg(not(target_arch = "wasm32"))] + platform: guess_host_triple::guess_host_triple() + .unwrap_or("unknown") + .to_string(), + #[cfg(target_arch = "wasm32")] + platform: "unknown".to_string(), payload: None, } } diff --git a/lib/u_lib/src/utils/misc.rs b/lib/u_lib/src/utils/misc.rs index 3025f24..8981bd3 100644 --- a/lib/u_lib/src/utils/misc.rs +++ b/lib/u_lib/src/utils/misc.rs @@ -1,9 +1,3 @@ -use nix::{ - sys::signal::{signal, SigHandler, Signal}, - unistd::{chdir, close as fdclose, fork, getppid, setsid, ForkResult}, -}; -use std::process::exit; - pub trait OneOrVec { fn into_vec(self) -> Vec; } @@ -31,38 +25,6 @@ macro_rules! unwrap_enum { }; } -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(); - } -} - pub fn init_env() { let envs = [".env", ".env.private"]; for envfile in &envs { diff --git a/lib/u_lib/src/utils/mod.rs b/lib/u_lib/src/utils/mod.rs index 41cf1a2..139fd5a 100644 --- a/lib/u_lib/src/utils/mod.rs +++ b/lib/u_lib/src/utils/mod.rs @@ -4,7 +4,10 @@ mod fmt; mod misc; mod proc_output; mod storage; +#[cfg(not(target_arch = "wasm32"))] mod tempfile; +#[cfg(unix)] +mod unix; mod vec_display; pub use combined_result::*; @@ -13,5 +16,9 @@ pub use fmt::*; pub use misc::*; pub use proc_output::*; pub use storage::*; +#[cfg(not(target_arch = "wasm32"))] pub use tempfile::*; pub use vec_display::*; + +#[cfg(unix)] +pub use unix::*; diff --git a/lib/u_lib/src/utils/unix.rs b/lib/u_lib/src/utils/unix.rs new file mode 100644 index 0000000..a4e4133 --- /dev/null +++ b/lib/u_lib/src/utils/unix.rs @@ -0,0 +1,37 @@ +use nix::{ + sys::signal::{signal, SigHandler, Signal}, + unistd::{chdir, close as fdclose, fork, getppid, setsid, ForkResult}, +}; +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(); + } +} diff --git a/scripts/deploy.sh b/scripts/deploy.sh index aa04c86..0804965 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -9,7 +9,8 @@ RSYNC="rsync -arzh --progress" ssh $SERVER mkdir -p $REMOTE_DIR/{release,deploy} $RSYNC $ROOTDIR/release/u_server $REMOTE_PATH/release/u_server -$RSYNC --exclude="*.sh" $ROOTDIR/certs/ $REMOTE_PATH/certs +$RSYNC $ROOTDIR/certs/server.{crt,key} $REMOTE_PATH/certs +$RSYNC $ROOTDIR/certs/ca.crt $REMOTE_PATH/certs $RSYNC $ROOTDIR/migrations/ $REMOTE_PATH/migrations $RSYNC $ROOTDIR/.env* $REMOTE_PATH/ $RSYNC $ROOTDIR/integration/docker-compose.yml $REMOTE_PATH/deploy/