From c4e3745d2770b8ec27fe761e30451a210bf872bd Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Wed, 1 Jul 2020 11:34:19 +0700 Subject: [PATCH] Added validation to the config file --- Cargo.toml | 4 ++++ src/config.rs | 11 ++++++++++- src/main.rs | 15 ++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ef79e2f..05645c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,10 +17,12 @@ gstreamer = "0.15" gstreamer-app = "0.15" gstreamer-rtsp = "0.15" gstreamer-rtsp-server = "0.15" +lazy_static = "1.4" log = { version = "0.4" } indoc = "0.3" md5 = "0.7" nom = "5.1" +regex = "1" serde = { version = "1.0", features = ["derive"] } socket2 = "0.3" structopt = "0.3" @@ -28,3 +30,5 @@ toml = "0.5" yaserde = "0.3.16" yaserde_derive = "0.3.16" xml-rs = "0.8" +validator = "0.10" +validator_derive = "0.10" diff --git a/src/config.rs b/src/config.rs index 99f7510..7090573 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,13 @@ use serde::Deserialize; use std::net::SocketAddr; use std::time::Duration; +use validator::Validate; +use regex::Regex; + +lazy_static! { + static ref RE_STREAM_FORM: Regex = Regex::new(r"^([hH]26[45]|[ \t]*[!].*)$").unwrap(); + static ref RE_STREAM_SRC: Regex = Regex::new(r"^(mainStream|subStream)$").unwrap(); +} #[derive(Debug, Deserialize)] pub struct Config { @@ -10,7 +17,7 @@ pub struct Config { pub bind_addr: String, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Validate)] pub struct CameraConfig { pub name: String, @@ -22,9 +29,11 @@ pub struct CameraConfig { pub timeout: Option, + #[validate(regex(path = "RE_STREAM_FORM", message = "Incorrect stream format", code = "format"))] #[serde(default = "default_format")] pub format: String, + #[validate(regex(path = "RE_STREAM_SRC", message = "Incorrect stream source", code = "stream"))] #[serde(default = "default_stream")] pub stream: String, } diff --git a/src/main.rs b/src/main.rs index b13986f..5c32582 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,17 @@ +#[macro_use] extern crate validator_derive; +#[macro_use] extern crate lazy_static; + use env_logger::Env; use err_derive::Error; use log::*; use neolink::bc_protocol::BcCamera; -use neolink::gst::{MaybeAppSrc, RtspServer}; +use neolink::gst::{MaybeAppSrc, RtspServer, StreamFormat}; use neolink::Never; use std::fs; use std::io::Write; use std::time::Duration; use structopt::StructOpt; +use validator::Validate; mod cmdline; mod config; @@ -23,6 +27,8 @@ pub enum Error { ProtocolError(#[error(source)] neolink::Error), #[error(display = "I/O error")] IoError(#[error(source)] std::io::Error), + #[error(display = "Validation error")] + ValidationError(#[error(source)] validator::ValidationErrors), } fn main() -> Result<(), Error> { @@ -37,6 +43,13 @@ fn main() -> Result<(), Error> { let opt = Opt::from_args(); let config: Config = toml::from_str(&fs::read_to_string(opt.config)?)?; + for camera in &config.cameras { + match camera.validate() { + Ok(_) => (), + Err(e) => return Err(Error::ValidationError(e)), + }; + } + let rtsp = &RtspServer::new(); crossbeam::scope(|s| {