first commit
This commit is contained in:
commit
c30064b531
93 files changed
+26367
No files matched your search
+246
@@ -0,0 +1,246 @@
|
||||
//! Registry of active stream clients (TS, HLS, recordings) shown in the panel.
|
||||
|
||||
use crate::db::now;
|
||||
use serde::Serialize;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::{AtomicI64, AtomicU64, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct ClientEntry {
|
||||
pub id: u64,
|
||||
pub kind: String,
|
||||
pub channel_id: i64,
|
||||
pub channel_name: String,
|
||||
pub ip: String,
|
||||
pub user_agent: String,
|
||||
pub who: String,
|
||||
pub profile: String,
|
||||
pub started: i64,
|
||||
pub bytes: AtomicU64,
|
||||
pub last_seen: AtomicI64,
|
||||
#[serde(skip)]
|
||||
key: String,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Clients {
|
||||
map: Mutex<HashMap<u64, Arc<ClientEntry>>>,
|
||||
next: AtomicU64,
|
||||
/// Total bytes sent to stream clients since start (for throughput charts).
|
||||
sent: AtomicU64,
|
||||
/// Total bytes written by recordings since start.
|
||||
recorded: AtomicU64,
|
||||
}
|
||||
|
||||
pub struct ClientGuard {
|
||||
clients: Arc<ClientEntry>,
|
||||
owner: Arc<Clients>,
|
||||
}
|
||||
|
||||
impl ClientGuard {
|
||||
pub fn add_bytes(&self, n: u64) {
|
||||
self.clients.bytes.fetch_add(n, Ordering::Relaxed);
|
||||
self.owner.count(&self.clients.kind, n);
|
||||
self.clients.last_seen.store(now(), Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ClientGuard {
|
||||
fn drop(&mut self) {
|
||||
self.owner.map.lock().unwrap().remove(&self.clients.id);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NewClient<'a> {
|
||||
pub kind: &'a str,
|
||||
pub channel_id: i64,
|
||||
pub channel_name: &'a str,
|
||||
pub ip: &'a str,
|
||||
pub user_agent: &'a str,
|
||||
pub who: &'a str,
|
||||
pub profile: &'a str,
|
||||
}
|
||||
|
||||
impl Clients {
|
||||
fn count(&self, kind: &str, n: u64) {
|
||||
if kind == "recording" {
|
||||
self.recorded.fetch_add(n, Ordering::Relaxed);
|
||||
} else {
|
||||
self.sent.fetch_add(n, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
/// (bytes sent to clients, bytes recorded) since start.
|
||||
pub fn totals(&self) -> (u64, u64) {
|
||||
(
|
||||
self.sent.load(Ordering::Relaxed),
|
||||
self.recorded.load(Ordering::Relaxed),
|
||||
)
|
||||
}
|
||||
|
||||
fn make(&self, c: &NewClient, key: String) -> Arc<ClientEntry> {
|
||||
let id = self.next.fetch_add(1, Ordering::Relaxed) + 1;
|
||||
Arc::new(ClientEntry {
|
||||
id,
|
||||
kind: c.kind.into(),
|
||||
channel_id: c.channel_id,
|
||||
channel_name: c.channel_name.into(),
|
||||
ip: c.ip.into(),
|
||||
user_agent: c.user_agent.chars().take(160).collect(),
|
||||
who: c.who.into(),
|
||||
profile: c.profile.into(),
|
||||
started: now(),
|
||||
bytes: AtomicU64::new(0),
|
||||
last_seen: AtomicI64::new(now()),
|
||||
key,
|
||||
})
|
||||
}
|
||||
|
||||
/// Client whose lifetime is bound to a guard (TS, recording).
|
||||
pub fn register(self: &Arc<Self>, c: NewClient) -> ClientGuard {
|
||||
let e = self.make(&c, String::new());
|
||||
self.map.lock().unwrap().insert(e.id, e.clone());
|
||||
ClientGuard {
|
||||
clients: e,
|
||||
owner: self.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// HLS client identified by (channel, profile, IP, UA); expires when idle.
|
||||
pub fn touch_hls(&self, c: NewClient, bytes: u64) {
|
||||
let key = format!("{}|{}|{}|{}", c.channel_id, c.profile, c.ip, c.user_agent);
|
||||
self.sent.fetch_add(bytes, Ordering::Relaxed);
|
||||
let mut map = self.map.lock().unwrap();
|
||||
if let Some(e) = map.values().find(|e| e.kind == "hls" && e.key == key) {
|
||||
e.bytes.fetch_add(bytes, Ordering::Relaxed);
|
||||
e.last_seen.store(now(), Ordering::Relaxed);
|
||||
return;
|
||||
}
|
||||
let e = self.make(&c, key);
|
||||
e.bytes.fetch_add(bytes, Ordering::Relaxed);
|
||||
map.insert(e.id, e);
|
||||
}
|
||||
|
||||
pub fn reap(&self, hls_idle: i64) {
|
||||
let t = now();
|
||||
self.map
|
||||
.lock()
|
||||
.unwrap()
|
||||
.retain(|_, e| e.kind != "hls" || t - e.last_seen.load(Ordering::Relaxed) <= hls_idle);
|
||||
}
|
||||
|
||||
pub fn list(&self) -> Vec<Arc<ClientEntry>> {
|
||||
let mut v: Vec<_> = self.map.lock().unwrap().values().cloned().collect();
|
||||
v.sort_by_key(|e| e.started);
|
||||
v
|
||||
}
|
||||
}
|
||||
|
||||
/// Friendly device/app name derived from a User-Agent (or HTSP client name).
|
||||
pub fn device_name(ua: &str) -> String {
|
||||
let u = ua.trim();
|
||||
if u.is_empty() {
|
||||
return "unknown".into();
|
||||
}
|
||||
let l = u.to_lowercase();
|
||||
let pick = |name: &str| -> String {
|
||||
// version after the product name when present ("Kodi/21.0", "VLC/3.0.20")
|
||||
let ver = l
|
||||
.find(&name.to_lowercase())
|
||||
.map(|i| &u[i + name.len()..])
|
||||
.and_then(|rest| {
|
||||
let rest = rest.trim_start_matches(['/', ' ']);
|
||||
let v: String = rest
|
||||
.chars()
|
||||
.take_while(|c| c.is_ascii_digit() || *c == '.')
|
||||
.collect();
|
||||
(!v.is_empty()).then_some(v)
|
||||
});
|
||||
match ver {
|
||||
Some(v) => format!("{name} {v}"),
|
||||
None => name.to_string(),
|
||||
}
|
||||
};
|
||||
let apps = [
|
||||
("kodi", "Kodi"),
|
||||
("tivimate", "TiviMate"),
|
||||
("ott navigator", "OTT Navigator"),
|
||||
("iptv smarters", "IPTV Smarters"),
|
||||
("tvhclient", "TVHClient"),
|
||||
("vlc", "VLC"),
|
||||
("mpv", "mpv"),
|
||||
("ffmpeg", "ffmpeg"),
|
||||
("lavf", "ffmpeg"),
|
||||
("exoplayer", "ExoPlayer"),
|
||||
("stagefright", "Android media player"),
|
||||
("bravia", "Sony Bravia"),
|
||||
("smarttv", "Smart TV"),
|
||||
("tizen", "Samsung Tizen"),
|
||||
("web0s", "LG webOS"),
|
||||
("roku", "Roku"),
|
||||
("appletv", "Apple TV"),
|
||||
("gstreamer", "GStreamer"),
|
||||
("plex", "Plex"),
|
||||
("jellyfin", "Jellyfin"),
|
||||
("emby", "Emby"),
|
||||
("curl", "curl"),
|
||||
("wget", "wget"),
|
||||
("python", "Python"),
|
||||
("okhttp", "Android app"),
|
||||
("dalvik", "Android app"),
|
||||
("cfnetwork", "iOS/macOS app"),
|
||||
];
|
||||
for (needle, name) in apps {
|
||||
if l.contains(needle) {
|
||||
return pick(name);
|
||||
}
|
||||
}
|
||||
if l.contains("mozilla") || l.contains("safari") || l.contains("firefox") {
|
||||
let browser = if l.contains("edg/") {
|
||||
"Edge"
|
||||
} else if l.contains("opr/") {
|
||||
"Opera"
|
||||
} else if l.contains("firefox") {
|
||||
"Firefox"
|
||||
} else if l.contains("chrome") || l.contains("crios") {
|
||||
"Chrome"
|
||||
} else if l.contains("safari") {
|
||||
"Safari"
|
||||
} else {
|
||||
"browser"
|
||||
};
|
||||
let os = if l.contains("android") {
|
||||
" · Android"
|
||||
} else if l.contains("iphone") || l.contains("ipad") {
|
||||
" · iOS"
|
||||
} else if l.contains("windows") {
|
||||
" · Windows"
|
||||
} else if l.contains("mac os") {
|
||||
" · macOS"
|
||||
} else if l.contains("linux") {
|
||||
" · Linux"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
return format!("{browser}{os}");
|
||||
}
|
||||
u.split(['/', ' ', '(']).next().unwrap_or(u).to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn device_names() {
|
||||
assert_eq!(device_name("Kodi/21.0 (Linux; Android 12)"), "Kodi 21.0");
|
||||
assert_eq!(device_name("VLC/3.0.20 LibVLC/3.0.20"), "VLC 3.0.20");
|
||||
assert!(device_name("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36").starts_with("Chrome"));
|
||||
assert_eq!(
|
||||
device_name("Dalvik/2.1.0 (Linux; U; Android 10; BRAVIA 4K UR2 Build/QTG2.200305.002)"),
|
||||
"Sony Bravia"
|
||||
);
|
||||
assert_eq!(device_name(""), "unknown");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user