diff --git a/Cargo.lock b/Cargo.lock index 63fd8071d25002d9689a56f86f3f660c8d05b521..4c92e46ba70a4d5454d50ee745c425f5f2cf0cf8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1376,7 +1376,7 @@ checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" [[package]] name = "sms-gateway" -version = "0.3.0" +version = "0.3.2" dependencies = [ "actix-rt", "actix-web", diff --git a/Cargo.toml b/Cargo.toml index afb77dab54b3b3ebd7145c6bee21ee7e2ac8bed1..b75359a7f5ebeca423601222e0acc80d81625cac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "sms-gateway" description = "A http frontend for SMS Server Tools 3" authors = ["David Huss <david.huss@hfbk-hamburg.de>"] -version = "0.3.1" +version = "0.4.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/build_and_deploy.sh b/build_and_deploy.sh index da6f36316ebae026284eefe18ee7519d79a8ac8a..6cb1d60ed2daa3d8e96b25eca95ef1d471200bb2 100755 --- a/build_and_deploy.sh +++ b/build_and_deploy.sh @@ -34,6 +34,8 @@ function build_and_deploy { echo -------------------------------------------------- echo "Deploying Binary $1 to Host $TARGET_HOST:$TARGET_PATH$1"; rsync ${SOURCE_PATH}$1 $TARGET_HOST:$TARGET_PATH$1 + # chmod +x the binary + ssh -p ${TARGET_PORT} -t ${TARGET_HOST} "chmod +x $TARGET_PATH$1" echo } @@ -45,6 +47,6 @@ do done # Finally restart the service -# ssh -p ${TARGET_PORT} -t ${TARGET_HOST} ${TARGET_PATH}$1 +ssh -p ${TARGET_PORT} -t ${TARGET_HOST} "systemctl restart sms-gateway" echo "done" \ No newline at end of file diff --git a/examples/sms-gateway.service b/examples/sms-gateway.service index 66c73b6318c2813d92e9a07752a4d31a2e41811a..a4fe51f78c32a4579722752fadd53797b9dd3bd4 100644 --- a/examples/sms-gateway.service +++ b/examples/sms-gateway.service @@ -1,7 +1,7 @@ [Unit] Description=sms-gateway web-frontend DefaultDependencies=no -After=network.target +After=network.target smstools.target [Service] Type=simple diff --git a/src/bin/eventhandler.rs b/src/bin/eventhandler.rs index 6facb14ca4affdcbdcc6850ee9aca12d8ab31aa7..eeac18f215c104e340a090ede9f0d15d810a26aa 100644 --- a/src/bin/eventhandler.rs +++ b/src/bin/eventhandler.rs @@ -1,6 +1,7 @@ use std::process::exit; use std::env::args; use std::collections::HashMap; +use std::path::PathBuf; use reqwest; pub use log::{info, warn, debug, error}; @@ -16,51 +17,67 @@ const BINARY_NAME: &str = env!("CARGO_BIN_NAME"); +fn help() { + info!("Error: Insufficient number of arguments! Expected two or three arguments."); + exit(1); +} + + fn main() -> Result<(), Box<dyn std::error::Error>> { info!("Started {} {}", PACKAGE_NAME, BINARY_NAME); - if args().len() != 3 { - error!("Error: Insufficient number of arguments! Expected two arguments."); - exit(1); - } - let kind = args().nth(1).unwrap().to_ascii_lowercase(); - let content = args().nth(2).unwrap(); - match &kind[..] { - "received" => { - info!("Received sms event of type \"{}\": {}", kind, content); - }, - "sent" => { - info!("Received sms event of type \"{}\": {}", kind, content); - }, - "failed" => { - info!("Received sms event of type \"{}\": {}", kind, content); - }, - _ => { - error!("Error: Unhandled event type: {}", &kind[..]); - exit(1); - } - } + let args: Vec<String> = args().collect(); - let url = format!("http://{host}:{port}/eventhandler", host=CONFIG.server.host, port=CONFIG.server.port); - info!("Attempting to send Event to url: \"{}\"", url); + match args.len() { + 2 | 3 => { + let kind = args[0].to_ascii_lowercase(); + let path = PathBuf::from(&args[1]); - let mut map = HashMap::new(); - map.insert("kind", kind); - map.insert("sms", content.replace("\\n", "\n")); + let content = std::fs::read_to_string(&path)?; - debug!("Json Payload to be sent: {:#?}", &map); + match &kind[..] { + "received" => { + info!("Received sms event of type \"{}\": {}", kind, content); + }, + "sent" => { + info!("Received sms event of type \"{}\": {}", kind, content); + }, + "failed" => { + info!("Received sms event of type \"{}\": {}", kind, content); + }, + _ => { + error!("Error: Unhandled event type: {}", &kind[..]); + exit(1); + } + } - let client = reqwest::blocking::Client::new(); - let response = client.post(url) - .json(&map) - .send()?; + let url = format!("http://{host}:{port}/eventhandler", host=CONFIG.server.host, port=CONFIG.server.port); + info!("Attempting to send Event to url: \"{}\"", url); - if response.status() != 200 { - error!("{:#?}", response.json::<ErrorResponse>()); - } else { - info!("OK {}", response.status()); - } + let mut map = HashMap::new(); + map.insert("kind", kind); + map.insert("sms", content.replace("\\n", "\n")); + + debug!("Json Payload to be sent: {:#?}", &map); + + let client = reqwest::blocking::Client::new(); + let response = client.post(url) + .json(&map) + .send()?; + + if response.status() != 200 { + error!("{:#?}", response.json::<ErrorResponse>()); + } else { + info!("OK {}", response.status()); + } - Ok(()) + Ok(()) + }, + _ => { + error!("Error: Insufficient number of arguments! Expected two or three arguments."); + help(); + Ok(()) + } + } } \ No newline at end of file diff --git a/src/errors.rs b/src/errors.rs index 1d7cf6dc1229fe568122836a11753062c1c69786..f7feb489001d627cf107733322dfccda3ee12831 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -19,9 +19,9 @@ pub enum SmsError { UnknownEventKind(String), #[error("Failed to deserialize SMS from string: '{0}'")] DeserializationError(String), - #[error("The provided number '{0}' was not in the whitelist")] + #[error("The provided number '{0}' was not on the whitelist")] NotInWhiteList(String), - #[error("The provided number '{0}' was on the blacklist")] + #[error("The provided number '{0}' as it matched at least one entry of the blacklist")] InBlackList(String), } diff --git a/src/sms.rs b/src/sms.rs index c794238fde37f446d39a4701ce035b3c015f4516..b83fe5ec6e938255dc9856dcf96bfcb382a7b324 100644 --- a/src/sms.rs +++ b/src/sms.rs @@ -116,7 +116,7 @@ impl Sms { // If this is the case, return an Error if in_blacklist { - warn!("Refused to send SMS to \"{}\" as it was blacklisted", self.number); + warn!("Refused to send SMS to \"{}\" as it matched at least one entry of the blacklist", self.number); return Err(SmsError::InBlackList(self.number.clone())); } }