diff --git a/README.md b/README.md index a97a122..becce0d 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ You may also need to use `h264` format while using the SD stream. By default Neolink serves on all IP addresses on port 8554. You can modify this by changing the `bind` and the `bind_port` parameter. -Use can enable `rtsps` (TLS) by adding a `certificate = "/path/to/pem"` to the config. This pem should contain by the certificate and the key used for the server. If TLS is enabled all connections must use `rtsps`. Use can also control client side TLS with the config option `tls_client_auth = "none|request|require"`, in this case the client should present a certificate signed by the server's CA, this is disabled by default. +Use can enable `rtsps` (TLS) by adding a `certificate = "/path/to/pem"` to the config. This pem should contain by the certificate and the key used for the server. If TLS is enabled all connections must use `rtsps`. You can also control client side TLS with the config option `tls_client_auth = "none|request|require"`, in this case the client should present a certificate signed by the server's CA, this is disabled by default. Use can enable basic authentication by adding the configuration option `username = "someone"` and `password = "somepass"`. Both username and password must be set for this to work. diff --git a/src/config.rs b/src/config.rs index 2b155b6..e7ccfba 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,7 +24,7 @@ pub struct Config { pub bind_port: u16, #[serde(default = "default_certificate")] - pub certificate: String, + pub certificate: Option, #[validate(regex(path = "RE_TLS_CLIENT_AUTH", message = "Incorrect stream format", code = "format"))] #[serde(default = "default_tls_client_auth")] @@ -60,11 +60,13 @@ pub struct CameraConfig { #[derive(Debug, Deserialize, Validate, Clone)] pub struct UserConfig { + #[validate(required)] #[serde(alias = "username")] - pub name: String, + pub name: Option, + #[validate(required)] #[serde(alias = "password")] - pub pass: String, + pub pass: Option, } fn default_bind_addr() -> String { @@ -83,8 +85,8 @@ fn default_stream() -> String { "both".to_string() } -fn default_certificate() -> String { - "".to_string() +fn default_certificate() -> Option { + None } fn default_tls_client_auth() -> String { diff --git a/src/gst.rs b/src/gst.rs index 9bc8893..0c1b57b 100644 --- a/src/gst.rs +++ b/src/gst.rs @@ -9,7 +9,7 @@ use gstreamer_app::AppSrc; use gstreamer_rtsp_server::prelude::*; use gstreamer_rtsp::RTSPAuthMethod; use gstreamer_rtsp_server::{RTSPAuth, RTSPToken, RTSPMediaFactory, RTSP_TOKEN_MEDIA_FACTORY_ROLE, RTSP_PERM_MEDIA_FACTORY_ACCESS, RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, RTSPServer as GstRTSPServer}; -use log::{debug, info}; +use log::debug; use std::io; use std::io::Write; use std::fs; @@ -100,26 +100,18 @@ impl RtspServer { Ok(maybe_app_src) } - pub fn set_credentials(&self, credentials :&Vec<(&String, &String)>) -> Result<()> { - let auth = match self.server.get_auth() { - Some(x) => x, - None => RTSPAuth::new(), - }; + pub fn set_credentials(&self, credentials :&Vec>) -> Result<()> { + let auth = self.server.get_auth().unwrap_or_else(|| RTSPAuth::new()); auth.set_supported_methods(RTSPAuthMethod::Basic); - for credentials in credentials { - let (user, pass) = credentials; - if ! user.is_empty() && ! pass.is_empty() { - info!("Setting credentials for user {}", user); + for credential in credentials { + if let Some((user, pass)) = credential { + debug!("Setting credentials for user {}", user); debug!("Password is {}", pass); let token = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, user)]); let basic = RTSPAuth::make_basic(user, pass); auth.add_basic(basic.as_str(), &token); - - // Now we have basic auth we stop others from watching - let mut token = RTSPToken::new_empty(); - auth.set_default_token(Some(&mut token)); } } let mut un_authtoken = RTSPToken::new(&[(*RTSP_TOKEN_MEDIA_FACTORY_ROLE, &"unauth")]); @@ -130,20 +122,17 @@ impl RtspServer { } pub fn set_tls(&self, cert_file: &str, client_auth: TlsAuthenticationMode) -> Result<()> { - if ! cert_file.is_empty() { - info!("Setting up TLS using {}", cert_file); - let auth = match self.server.get_auth() { - Some(x) => x, - None => RTSPAuth::new(), - }; + debug!("Setting up TLS using {}", cert_file); + let auth = self.server.get_auth().unwrap_or_else(|| 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); + // We seperate reading the file and changing to a PEM so that we get different error messages. + let cert_contents = fs::read_to_string(cert_file).expect("TLS file not found"); + let cert = TlsCertificate::new_from_pem(&cert_contents).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)); - } + self.server.set_auth(Some(&auth)); Ok(()) } diff --git a/src/main.rs b/src/main.rs index d859f67..1922fc5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -137,16 +137,25 @@ fn set_up_tls(config: &Config, rtsp: &RtspServer) { let tls_client_auth = match &config.tls_client_auth as &str { "request" => TlsAuthenticationMode::Requested, "require" => TlsAuthenticationMode::Required, - "none"|_ => TlsAuthenticationMode::None, + "none" => TlsAuthenticationMode::None, + _ => unreachable!(), }; - rtsp.set_tls(&config.certificate, tls_client_auth).expect("Failed to set up TLS"); + if let Some(cert_path) = &config.certificate { + rtsp.set_tls(&cert_path, tls_client_auth).expect("Failed to set up TLS"); + } } fn set_up_users(users: &Vec, rtsp: &RtspServer) { // Setting up users let mut credentials = vec![]; for user in users { - credentials.push((&user.name, &user.pass)); + let name = &user.name; + let pass = &user.pass; + let user_pass = match (name, pass) { + (Some(name), Some(pass)) => Some((&name as &str, &pass as &str)), + _ => None, + }; + credentials.push(user_pass); } rtsp.set_credentials(&credentials).expect("Failed to set up users."); } @@ -159,8 +168,9 @@ fn get_permitted_users(users: &Vec, current_permitted_users: &Vec