Rustfmt cleanup

This commit is contained in:
QuantumEntangledAndy committed 2020-07-13 12:12:17 +07:00
1 parent c1151acecb
commit 60810fc0f7
3 files changed
+60 -41

No files matched your search

+7 -3
View File
@@ -28,7 +28,11 @@ pub struct Config {
#[serde(default = "default_certificate")]
pub certificate: Option<String>,
#[validate(regex(path = "RE_TLS_CLIENT_AUTH", message = "Incorrect stream format", code = "format"))]
#[validate(regex(
path = "RE_TLS_CLIENT_AUTH",
message = "Incorrect stream format",
code = "format"
))]
#[serde(default = "default_tls_client_auth")]
pub tls_client_auth: String,
@@ -112,8 +116,8 @@ fn default_users() -> Vec<UserConfig> {
vec![]
}
fn validate_username(name: &str) -> Result<(), ValidationError> {
let reserved_names = vec!("anyone", "anonymous");
fn validate_username(name: &str) -> Result<(), ValidationError> {
let reserved_names = vec!["anyone", "anonymous"];
if reserved_names.contains(&name) {
return Err(ValidationError::new("This is a reserved username"));
}
+33 -25
View File
@@ -6,14 +6,18 @@ use gstreamer::prelude::Cast;
use gstreamer::{Bin, Structure};
use gstreamer_app::AppSrc;
//use gstreamer_rtsp::RTSPLowerTrans;
use gstreamer_rtsp_server::prelude::*;
use gio::{TlsAuthenticationMode, TlsCertificate};
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 gstreamer_rtsp_server::prelude::*;
use gstreamer_rtsp_server::{
RTSPAuth, RTSPMediaFactory, RTSPServer as GstRTSPServer, RTSPToken,
RTSP_PERM_MEDIA_FACTORY_ACCESS, RTSP_PERM_MEDIA_FACTORY_CONSTRUCT,
RTSP_TOKEN_MEDIA_FACTORY_ROLE,
};
use log::debug;
use std::fs;
use std::io;
use std::io::Write;
use std::fs;
use gio::{TlsCertificate,TlsAuthenticationMode};
type Result<T> = std::result::Result<T, ()>;
@@ -35,7 +39,12 @@ impl RtspServer {
}
}
pub fn add_stream(&self, paths: &[&str], stream_format: &StreamFormat, permitted_users: &[String]) -> Result<MaybeAppSrc> {
pub fn add_stream(
&self,
paths: &[&str],
stream_format: &StreamFormat,
permitted_users: &[String],
) -> Result<MaybeAppSrc> {
let mounts = self
.server
.get_mount_points()
@@ -49,7 +58,11 @@ impl RtspServer {
let factory = RTSPMediaFactory::new();
debug!("Permitting {} to access {}", permitted_users.join(", "), paths.join(", "));
debug!(
"Permitting {} to access {}",
permitted_users.join(", "),
paths.join(", ")
);
self.add_permitted_roles(&factory, permitted_users);
//factory.set_protocols(RTSPLowerTrans::TCP);
@@ -93,15 +106,13 @@ impl RtspServer {
pub fn add_permitted_roles(&self, factory: &RTSPMediaFactory, permitted_roles: &[String]) {
for permitted_role in permitted_roles {
factory.add_role_from_structure(
&Structure::new(
permitted_role.as_str(),
&[
(*RTSP_PERM_MEDIA_FACTORY_ACCESS, &true),
(*RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, &true),
]
)
);
factory.add_role_from_structure(&Structure::new(
permitted_role.as_str(),
&[
(*RTSP_PERM_MEDIA_FACTORY_ACCESS, &true),
(*RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, &true),
],
));
}
// During auth, first it binds anonymously. At this point it checks
// RTSP_PERM_MEDIA_FACTORY_ACCESS to see if anyone can connect
@@ -117,15 +128,11 @@ impl RtspServer {
// FYI: If no RTSP_PERM_MEDIA_FACTORY_ACCESS then server returns 404 not found
// If yes RTSP_PERM_MEDIA_FACTORY_ACCESS but no RTSP_PERM_MEDIA_FACTORY_CONSTRUCT
// server returns 401 not authourised
if ! permitted_roles.contains(&"anonymous".to_string()) {
factory.add_role_from_structure(
&Structure::new(
"anonymous",
&[
(*RTSP_PERM_MEDIA_FACTORY_ACCESS, &true),
]
)
);
if !permitted_roles.contains(&"anonymous".to_string()) {
factory.add_role_from_structure(&Structure::new(
"anonymous",
&[(*RTSP_PERM_MEDIA_FACTORY_ACCESS, &true)],
));
}
}
@@ -156,7 +163,8 @@ impl RtspServer {
// 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");
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);
+20 -13
View File
@@ -1,23 +1,23 @@
use env_logger::Env;
use err_derive::Error;
use gio::TlsAuthenticationMode;
use log::*;
use neolink::bc_protocol::BcCamera;
use neolink::gst::{MaybeAppSrc, RtspServer, StreamFormat};
use neolink::Never;
use std::collections::HashSet;
use std::fs;
use std::io::Write;
use std::sync::Arc;
use std::time::Duration;
use std::collections::HashSet;
use structopt::StructOpt;
use validator::Validate;
use gio::TlsAuthenticationMode;
mod cmdline;
mod config;
use cmdline::Opt;
use config::{UserConfig, CameraConfig, Config};
use config::{CameraConfig, Config, UserConfig};
#[derive(Debug, Error)]
pub enum Error {
@@ -77,13 +77,17 @@ fn main() -> Result<(), Error> {
&*format!("/{}", arc_cam.name),
&*format!("/{}/mainStream", arc_cam.name),
];
let mut output = rtsp.add_stream(paths, &stream_format, &permitted_user).unwrap();
let mut output = rtsp
.add_stream(paths, &stream_format, &permitted_user)
.unwrap();
let main_camera = arc_cam.clone();
s.spawn(move |_| camera_loop(&*main_camera, "mainStream", &mut output));
}
if ["both", "subStream"].iter().any(|&e| e == arc_cam.stream) {
let paths = &[&*format!("/{}/subStream", arc_cam.name)];
let mut output = rtsp.add_stream(paths, &substream_format, &permitted_user).unwrap();
let mut output = rtsp
.add_stream(paths, &substream_format, &permitted_user)
.unwrap();
let sub_camera = arc_cam.clone();
s.spawn(move |_| camera_loop(&*sub_camera, "subStream", &mut output));
}
@@ -146,7 +150,8 @@ fn set_up_tls(config: &Config, rtsp: &RtspServer) {
_ => unreachable!(),
};
if let Some(cert_path) = &config.certificate {
rtsp.set_tls(&cert_path, tls_client_auth).expect("Failed to set up TLS");
rtsp.set_tls(&cert_path, tls_client_auth)
.expect("Failed to set up TLS");
}
}
@@ -156,22 +161,24 @@ fn set_up_users(users: &Vec<UserConfig>, rtsp: &RtspServer) {
for user in users {
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))
},
let user_pass = match (name, pass) {
(Some(name), Some(pass)) => Some((&name as &str, &pass as &str)),
(Some(_), None) | (None, Some(_)) => {
warn!("Username and password must be supplied together - ignoring [[users]] entry");
None
},
}
_ => None,
};
credentials.push(user_pass);
}
rtsp.set_credentials(&credentials).expect("Failed to set up users.");
rtsp.set_credentials(&credentials)
.expect("Failed to set up users.");
}
fn get_permitted_users(users: &Vec<UserConfig>, current_permitted_users: &Vec<String>) -> Vec<String> {
fn get_permitted_users(
users: &Vec<UserConfig>,
current_permitted_users: &Vec<String>,
) -> Vec<String> {
// This is required to handle the special case of "anyone"
// ===Special set up of "anyone"===
// If in the camera config there is the user "anyone"