Finally done fighting with Rust

This commit is contained in:
techmetx11 2024-04-28 20:19:38 +01:00
parent 2089efd753
commit a8435f8a29
No known key found for this signature in database
GPG Key ID: 20E0C88A0E7E5AF2
5 changed files with 807 additions and 482 deletions

1181
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -7,6 +7,7 @@ edition = "2021"
[dependencies] [dependencies]
regex = "1.10.4" regex = "1.10.4"
rquickjs = "0.6.0" rquickjs = {version = "0.6.0", features=["futures"]}
tokio = {version = "1.37.0", features=["net", "macros", "rt-multi-thread", "io-std", "io-util"]} tokio = {version = "1.37.0", features=["net", "macros", "rt-multi-thread", "io-std", "io-util"]}
tokio-curl = "0.1.11" reqwest = "0.12.4"
lazy-regex = "3.1.0"

View File

@ -1 +1,12 @@
use lazy_regex::{regex, Lazy};
use regex::Regex;
pub static DEFAULT_SOCK_PATH: &str = "/tmp/inv_sig_helper.sock"; pub static DEFAULT_SOCK_PATH: &str = "/tmp/inv_sig_helper.sock";
pub static TEST_YOUTUBE_VIDEO: &str = "https://www.youtube.com/watch?v=jNQXAC9IVRw";
pub static REGEX_PLAYER_ID: &Lazy<Regex> = regex!("\\/s\\/player\\/([0-9a-f]{8})");
pub static NSIG_FUNCTION_ARRAY: &Lazy<Regex> = regex!(
"\\.get\\(\"n\"\\)\\)&&\\([a-zA-Z0-9$_]=([a-zA-Z0-9$_]+)(?:\\[(\\d+)])?\\([a-zA-Z0-9$_]\\)"
);
pub static NSIG_FUNCTION_BODY: &Lazy<Regex> =
regex!("=\\s*function([\\S\\s]*?\\}\\s*return [\\w$]+?\\.join\\(\"\"\\)\\s*\\};)");

View File

@ -1,3 +1,8 @@
use std::sync::Arc;
use tokio::sync::Mutex;
use crate::consts::{REGEX_PLAYER_ID, TEST_YOUTUBE_VIDEO};
pub enum JobOpcode { pub enum JobOpcode {
ForceUpdate, ForceUpdate,
DecryptNSignature, DecryptNSignature,
@ -14,5 +19,61 @@ impl From<u8> for JobOpcode {
} }
} }
pub async fn process_fetch_update() {} pub struct PlayerInfo {
pub async fn process_decrypt_n_signature(sig: String) {} nsig_function_bytecode: Vec<u8>,
player_id: u32,
}
pub struct GlobalState {
player_info: Mutex<PlayerInfo>,
}
impl GlobalState {
pub fn new() -> GlobalState {
return GlobalState {
player_info: Mutex::new(PlayerInfo {
nsig_function_bytecode: Default::default(),
player_id: Default::default(),
}),
};
}
}
pub async fn process_fetch_update(state: Arc<GlobalState>) {
let global_state = state.clone();
let response = match reqwest::get(TEST_YOUTUBE_VIDEO).await {
Ok(req) => req.text().await.unwrap(),
Err(x) => {
println!("Could not fetch the test video: {}", x);
return;
}
};
let player_id_str = match REGEX_PLAYER_ID.captures(&response).unwrap().get(0) {
Some(result) => result.as_str(),
None => return,
};
let player_id: u32 = u32::from_str_radix(player_id_str, 16).unwrap();
let current_player_info = global_state.player_info.lock().await;
let current_player_id = current_player_info.player_id;
drop(current_player_info);
if player_id == current_player_id {
// Player is already up to date
return;
}
// Download the player script
let player_js_url: String = format!(
"https://www.youtube.com/s/player/{:08x}/player_ias.vflset/en_US/base.js",
player_id
);
let player_javascript = match reqwest::get(player_js_url).await {
Ok(req) => req.text().await.unwrap(),
Err(x) => {
println!("Could not fetch the player JS: {}", x);
return;
}
};
}
pub async fn process_decrypt_n_signature(state: Arc<GlobalState>, sig: String) {}

View File

@ -2,8 +2,8 @@ mod consts;
mod jobs; mod jobs;
use consts::DEFAULT_SOCK_PATH; use consts::DEFAULT_SOCK_PATH;
use jobs::{process_decrypt_n_signature, process_fetch_update, JobOpcode}; use jobs::{process_decrypt_n_signature, process_fetch_update, GlobalState, JobOpcode};
use std::env::args; use std::{env::args, sync::Arc};
use tokio::{ use tokio::{
io::{AsyncReadExt, BufReader}, io::{AsyncReadExt, BufReader},
net::{UnixListener, UnixStream}, net::{UnixListener, UnixStream},
@ -29,18 +29,22 @@ async fn main() {
None => DEFAULT_SOCK_PATH, None => DEFAULT_SOCK_PATH,
}; };
// have to please rust
let state: Arc<GlobalState> = Arc::new(GlobalState::new());
let socket = UnixListener::bind(socket_url).unwrap(); let socket = UnixListener::bind(socket_url).unwrap();
loop { loop {
let (socket, _addr) = socket.accept().await.unwrap(); let (socket, _addr) = socket.accept().await.unwrap();
tokio::spawn(async move { let cloned_state = state.clone();
process_socket(socket).await; tokio::spawn(async {
process_socket(cloned_state, socket).await;
}); });
} }
} }
async fn process_socket(socket: UnixStream) { async fn process_socket(state: Arc<GlobalState>, socket: UnixStream) {
let mut bufreader = BufReader::new(socket); let mut bufreader = BufReader::new(socket);
loop { loop {
@ -49,8 +53,9 @@ async fn process_socket(socket: UnixStream) {
match opcode { match opcode {
JobOpcode::ForceUpdate => { JobOpcode::ForceUpdate => {
tokio::spawn(async move { let cloned_state = state.clone();
process_fetch_update().await; tokio::spawn(async {
process_fetch_update(cloned_state).await;
}); });
} }
JobOpcode::DecryptNSignature => { JobOpcode::DecryptNSignature => {
@ -59,10 +64,10 @@ async fn process_socket(socket: UnixStream) {
break_fail!(bufreader.read_exact(&mut buf).await); break_fail!(bufreader.read_exact(&mut buf).await);
let _str = break_fail!(String::from_utf8(buf)); let str = break_fail!(String::from_utf8(buf));
let cloned_state = state.clone();
tokio::spawn(async move { tokio::spawn(async {
process_decrypt_n_signature(_str).await; process_decrypt_n_signature(cloned_state, str).await;
}); });
} }
_ => {} _ => {}