Add TLS
This commit is contained in:
4 files changed
+50
-4
No files matched your search
+2
-1
@@ -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"
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
+20
-1
@@ -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<T> = std::result::Result<T, ()>;
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
|
||||
|
||||
+12
-2
@@ -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())
|
||||
|
||||
Reference in new issue
Block a user