diff --git a/Cargo.toml b/Cargo.toml index 05645c1..602c79d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,10 +13,11 @@ crossbeam = "0.7" env_logger = "*" err-derive = "0.2" glib = "*" +gio = "0.8" gstreamer = "0.15" gstreamer-app = "0.15" gstreamer-rtsp = "0.15" -gstreamer-rtsp-server = "0.15" +gstreamer-rtsp-server = { version = "0.15", features = ["v1_12"]} lazy_static = "1.4" log = { version = "0.4" } indoc = "0.3" diff --git a/src/config.rs b/src/config.rs index 576ea99..37a58aa 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,6 +8,7 @@ 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|both)$").unwrap(); + static ref RE_TLS_CLIENT_AUTH: Regex = Regex::new(r"^(none|request|require)$").unwrap(); } #[derive(Debug, Deserialize, Validate, Clone)] @@ -21,6 +22,13 @@ pub struct Config { #[validate(range(min = 0, max = 65535, message = "Invalid port", code = "bind_port"))] #[serde(default = "default_bind_port")] pub bind_port: u16, + + #[serde(default = "default_certificate")] + pub certificate: String, + + #[validate(regex(path = "RE_TLS_CLIENT_AUTH", message = "Incorrect stream format", code = "format"))] + #[serde(default = "default_tls_client_auth")] + pub tls_client_auth: String, } #[derive(Debug, Deserialize, Validate, Clone)] @@ -59,3 +67,11 @@ fn default_format() -> String { fn default_stream() -> String { "both".to_string() } + +fn default_certificate() -> String { + "".to_string() +} + +fn default_tls_client_auth() -> String { + "none".to_string() +} diff --git a/src/gst.rs b/src/gst.rs index 525ccfe..b1cf9ac 100644 --- a/src/gst.rs +++ b/src/gst.rs @@ -7,10 +7,13 @@ use gstreamer::Bin; use gstreamer_app::AppSrc; //use gstreamer_rtsp::RTSPLowerTrans; use gstreamer_rtsp_server::prelude::*; +use gstreamer_rtsp::RTSPAuthMethod; use gstreamer_rtsp_server::{RTSPAuth, RTSPMediaFactory, RTSPServer as GstRTSPServer}; -use log::debug; +use log::{debug, info}; use std::io; use std::io::Write; +use std::fs; +use gio::{TlsCertificate,TlsAuthenticationMode}; type Result = std::result::Result; @@ -96,7 +99,23 @@ impl RtspServer { }); self.server.set_auth(auth.as_ref()); + Ok(()) + } + pub fn set_tls(&self, cert_file: &str, client_auth: TlsAuthenticationMode) -> Result<()> { + info!("Setting up TLS using {}", cert_file); + let auth = match self.server.get_auth() { + Some(x) => x, + None => RTSPAuth::new(), + }; + + let cert = TlsCertificate::new_from_pem(&fs::read_to_string(cert_file).expect("TLS file not found")).expect("Not a valid TLS certificate"); + auth.set_tls_certificate(Some(&cert)); + auth.set_tls_authentication_mode(client_auth); + auth.set_supported_methods(RTSPAuthMethod::None); + + + self.server.set_auth(Some(&auth)); Ok(()) } diff --git a/src/main.rs b/src/main.rs index ec7f9f1..485bf17 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ use std::io::Write; use std::time::Duration; use structopt::StructOpt; use validator::Validate; +use gio::TlsAuthenticationMode; mod cmdline; mod config; @@ -76,12 +77,21 @@ fn main() -> Result<(), Error> { let rtsp = &RtspServer::new(); + if ! config.certificate.is_empty() { + let tls_client_auth = match &config.tls_client_auth as &str { + "request" => TlsAuthenticationMode::Requested, + "require" => TlsAuthenticationMode::Required, + "none"|_ => TlsAuthenticationMode::None, + }; + rtsp.set_tls(&config.certificate, tls_client_auth).expect("Failed to set up TLS"); + } + + crossbeam::scope(|s| { for camera in config.cameras { s.spawn(move |_| { // TODO handle these errors - let cam_format :&str = &camera.format; - let stream_format = match cam_format { + let stream_format = match &camera.format as &str { "h264"|"H264" => StreamFormat::H264, "h265"|"H265" => StreamFormat::H265, custom_format @ _ => StreamFormat::Custom(custom_format.to_string())