Skip to content

Commit

Permalink
support auth for RTMP/HTTP-FLV/HLS
Browse files Browse the repository at this point in the history
  • Loading branch information
harlanc authored and hailiang8 committed Feb 10, 2024
1 parent 1cad13a commit 70d8e48
Show file tree
Hide file tree
Showing 27 changed files with 1,247 additions and 92 deletions.
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ members = [
"library/codec/h264",
"library/logger",
"library/streamhub",
"library/common",
]
1 change: 1 addition & 0 deletions application/xiu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ tokio-metrics = { version = "0.2.0", default-features = false }

env_logger_extend = { path = "../../library/logger/" }
streamhub = { path = "../../library/streamhub/" }
commonlib = { path = "../../library/common/" }
rtmp = { path = "../../protocol/rtmp/" }
xrtsp = { path = "../../protocol/rtsp/" }
xwebrtc = { path = "../../protocol/webrtc/" }
Expand Down
33 changes: 32 additions & 1 deletion application/xiu/src/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
enabled = true
port = 1935
gop_num = 0
[rtmp.auth]
pull_enabled = true
push_enabled = true
# simple or md5
algorithm = "simple"
# pull streams from other server node.
[rtmp.pull]
enabled = false
Expand All @@ -28,35 +33,61 @@ on_unpublish = "http://localhost:3001/on_unpuslish"
on_play = "http://localhost:3001/on_play"
on_stop = "http://localhost:3001/on_stop"

[authsecret]
# used for md5 authentication
key = ""
# used for simple authentication
password = ""


##########################
# RTSP configurations #
##########################
[rtsp]
enabled = false
port = 445
[rtsp.auth]
pull_enabled = true
push_enabled = true
# simple or md5
algorithm = "simple"

##########################
# WebRTC configurations #
##########################
[webrtc]
enabled = false
port = 8083
[webrtc.auth]
pull_enabled = true
push_enabled = true
# simple or md5
algorithm = "simple"

##########################
# HTTPFLV configurations #
##########################
[httpflv]
enabled = false
port = 8081
[httpflv.auth]
pull_enabled = true
# simple or md5
algorithm = "simple"


##########################
# HLS configurations #
##########################
[hls]
enabled = false
port = 8080
need_record = true
need_record = false
[hls.auth]
pull_enabled = true
# simple or md5
algorithm = "simple"


##########################
# LOG configurations #
Expand Down
32 changes: 28 additions & 4 deletions application/xiu/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod errors;

use commonlib::auth::AuthAlgorithm;
use errors::ConfigError;
use serde_derive::Deserialize;
use std::fs;
Expand All @@ -14,6 +15,7 @@ pub struct Config {
pub hls: Option<HlsConfig>,
pub httpapi: Option<HttpApiConfig>,
pub httpnotify: Option<HttpNotifierConfig>,
pub authsecret: AuthSecretConfig,
pub log: Option<LogConfig>,
}

Expand All @@ -34,6 +36,7 @@ impl Config {
port: rtmp_port,
pull: None,
push: None,
auth: None,
});
}

Expand All @@ -42,6 +45,7 @@ impl Config {
rtsp_config = Some(RtspConfig {
enabled: true,
port: rtsp_port,
auth: None,
});
}

Expand All @@ -50,6 +54,7 @@ impl Config {
webrtc_config = Some(WebRTCConfig {
enabled: true,
port: webrtc_port,
auth: None,
});
}

Expand All @@ -58,6 +63,7 @@ impl Config {
httpflv_config = Some(HttpFlvConfig {
enabled: true,
port: httpflv_port,
auth: None,
});
}

Expand All @@ -67,6 +73,7 @@ impl Config {
enabled: true,
port: hls_port,
need_record: false,
auth: None,
});
}

Expand All @@ -83,6 +90,7 @@ impl Config {
hls: hls_config,
httpapi: None,
httpnotify: None,
authsecret: AuthSecretConfig::default(),
log: log_config,
}
}
Expand All @@ -95,6 +103,7 @@ pub struct RtmpConfig {
pub gop_num: Option<usize>,
pub pull: Option<RtmpPullConfig>,
pub push: Option<Vec<RtmpPushConfig>>,
pub auth: Option<AuthConfig>,
}
#[derive(Debug, Deserialize, Clone)]
pub struct RtmpPullConfig {
Expand All @@ -113,18 +122,21 @@ pub struct RtmpPushConfig {
pub struct RtspConfig {
pub enabled: bool,
pub port: usize,
pub auth: Option<AuthConfig>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct WebRTCConfig {
pub enabled: bool,
pub port: usize,
pub auth: Option<AuthConfig>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct HttpFlvConfig {
pub enabled: bool,
pub port: usize,
pub auth: Option<AuthConfig>,
}

#[derive(Debug, Deserialize, Clone)]
Expand All @@ -133,6 +145,7 @@ pub struct HlsConfig {
pub port: usize,
//record or not
pub need_record: bool,
pub auth: Option<AuthConfig>,
}

pub enum LogLevel {
Expand Down Expand Up @@ -170,6 +183,19 @@ pub struct HttpNotifierConfig {
pub on_stop: Option<String>,
}

#[derive(Debug, Deserialize, Clone, Default)]
pub struct AuthSecretConfig {
pub key: String,
pub password: String,
}

#[derive(Debug, Deserialize, Clone, Default)]
pub struct AuthConfig {
pub pull_enabled: bool,
pub push_enabled: Option<bool>,
pub algorithm: AuthAlgorithm,
}

pub fn load(cfg_path: &String) -> Result<Config, ConfigError> {
let content = fs::read_to_string(cfg_path)?;
let decoded_config = toml::from_str(&content[..]).unwrap();
Expand All @@ -184,15 +210,13 @@ fn test_toml_parse() {
Err(err) => println!("{}", err),
}

let str = fs::read_to_string(
"./src/config/config.toml",
);
let str = fs::read_to_string("./src/config/config.toml");

match str {
Ok(val) => {
println!("++++++{val}\n");
let decoded: Config = toml::from_str(&val[..]).unwrap();

println!("whole config: {:?}", decoded);
let rtmp = decoded.httpnotify;

if let Some(val) = rtmp {
Expand Down
43 changes: 39 additions & 4 deletions application/xiu/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use commonlib::auth::AuthType;
use rtmp::remuxer::RtmpRemuxer;

use crate::config::{AuthConfig, AuthSecretConfig};

use {
super::api,
super::config::Config,
//https://rustcc.cn/article?id=6dcbf032-0483-4980-8bfe-c64a7dfb33c7
anyhow::Result,
commonlib::auth::Auth,
hls::remuxer::HlsRemuxer,
hls::server as hls_server,
httpflv::server as httpflv_server,
Expand All @@ -27,6 +31,35 @@ impl Service {
Service { cfg }
}

fn gen_auth(auth_config: &Option<AuthConfig>, authsecret: &AuthSecretConfig) -> Option<Auth> {
if let Some(cfg) = auth_config {
let auth_type = if let Some(push_enabled) = cfg.push_enabled {
if push_enabled && cfg.pull_enabled {
AuthType::Both
} else if !push_enabled && !cfg.pull_enabled {
AuthType::None
} else if push_enabled && !cfg.pull_enabled {
AuthType::Push
} else {
AuthType::Pull
}
} else {
match cfg.pull_enabled {
true => AuthType::Pull,
false => AuthType::None,
}
};
Some(Auth::new(
authsecret.key.clone(),
authsecret.password.clone(),
cfg.algorithm.clone(),
auth_type,
))
} else {
None
}
}

pub async fn run(&mut self) -> Result<()> {
let notifier = if let Some(httpnotifier) = &self.cfg.httpnotify {
if !httpnotifier.enabled {
Expand Down Expand Up @@ -146,7 +179,8 @@ impl Service {
let listen_port = rtmp_cfg_value.port;
let address = format!("0.0.0.0:{listen_port}");

let mut rtmp_server = RtmpServer::new(address, producer, gop_num);
let auth = Self::gen_auth(&rtmp_cfg_value.auth, &self.cfg.authsecret);
let mut rtmp_server = RtmpServer::new(address, producer, gop_num, auth);
tokio::spawn(async move {
if let Err(err) = rtmp_server.run().await {
log::error!("rtmp server error: {}", err);
Expand Down Expand Up @@ -258,8 +292,9 @@ impl Service {
let port = httpflv_cfg_value.port;
let event_producer = stream_hub.get_hub_event_sender();

let auth = Self::gen_auth(&httpflv_cfg_value.auth, &self.cfg.authsecret);
tokio::spawn(async move {
if let Err(err) = httpflv_server::run(event_producer, port).await {
if let Err(err) = httpflv_server::run(event_producer, port, auth).await {
log::error!("httpflv server error: {}", err);
}
});
Expand Down Expand Up @@ -291,9 +326,9 @@ impl Service {
});

let port = hls_cfg_value.port;

let auth = Self::gen_auth(&hls_cfg_value.auth, &self.cfg.authsecret);
tokio::spawn(async move {
if let Err(err) = hls_server::run(port).await {
if let Err(err) = hls_server::run(port, auth).await {
log::error!("hls server error: {}", err);
}
});
Expand Down
Loading

0 comments on commit 70d8e48

Please sign in to comment.