mirror of
https://github.com/euzu/tuliprox.git
synced 2026-10-11 18:32:24 +02:00
User Session handling for fast frequent requests
This commit is contained in:
@@ -1,17 +1,34 @@
|
||||
use crate::model::api_proxy::ProxyUserCredentials;
|
||||
use std::str;
|
||||
use crate::api::api_utils::{create_token_for_provider};
|
||||
use crate::utils::constants::{CONSTANTS, HLS_PREFIX};
|
||||
use crate::utils::crypto_utils::{deobfuscate_text, obfuscate_text};
|
||||
use crate::utils::hash_utils;
|
||||
use std::str;
|
||||
|
||||
fn create_hls_session_token_and_url(secret: &[u8], session_token: u32, stream_url: &str) -> Option<String> {
|
||||
let token = hash_utils::u32_to_base64(session_token);
|
||||
if let Ok(cookie_value) = obfuscate_text(secret, &format!("{token}@{stream_url}")) {
|
||||
return Some(cookie_value);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn get_hls_session_token_and_url_from_token(secret: &[u8], token: &str) -> Option<(Option<u32>, String)> {
|
||||
if let Ok(decrypted) = deobfuscate_text(secret, token) {
|
||||
let (session_token, stream_url) = decrypted.split_once('@')?;
|
||||
return Some((hash_utils::base64_to_u32(session_token), stream_url.to_owned()));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
pub struct RewriteHlsProps<'a> {
|
||||
pub secret: &'a [u8;16],
|
||||
pub secret: &'a [u8; 16],
|
||||
pub base_url: &'a str,
|
||||
pub content: &'a str,
|
||||
pub hls_url: String,
|
||||
pub virtual_id: u32,
|
||||
pub input_id: u16,
|
||||
pub provider_name: String,
|
||||
pub user_token: String,
|
||||
pub user_token: u32,
|
||||
}
|
||||
|
||||
fn rewrite_hls_url(input: &str, replacement: &str) -> String {
|
||||
@@ -34,7 +51,7 @@ fn rewrite_uri_attrib(line: &str, props: &RewriteHlsProps) -> String {
|
||||
if let Some(caps) = CONSTANTS.re_hls_uri.captures(line) {
|
||||
let uri = &caps[1];
|
||||
let target_url = &rewrite_hls_url(&props.hls_url, uri);
|
||||
if let Some(token) = create_token_for_provider(props.secret, &props.user_token, props.virtual_id, &props.provider_name, target_url) {
|
||||
if let Some(token) = create_hls_session_token_and_url(props.secret, props.user_token, target_url) {
|
||||
return CONSTANTS.re_hls_uri.replace(line, format!(r#"URI="{token}""#)).to_string();
|
||||
}
|
||||
}
|
||||
@@ -59,7 +76,7 @@ pub fn rewrite_hls(user: &ProxyUserCredentials, props: &RewriteHlsProps) -> Stri
|
||||
} else {
|
||||
rewrite_hls_url(&props.hls_url, line)
|
||||
};
|
||||
if let Some(token) = create_token_for_provider(props.secret, &props.user_token, props.virtual_id, &props.provider_name, &target_url) {
|
||||
if let Some(token) = create_hls_session_token_and_url(props.secret, props.user_token, &target_url) {
|
||||
let url = format!(
|
||||
"{}/{HLS_PREFIX}/{}/{}/{}/{}/{}",
|
||||
props.base_url,
|
||||
|
||||
Reference in New Issue
Block a user