Skip to content

Commit

Permalink
refactor: removed lockfile authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
iholston committed Sep 20, 2024
1 parent 2f101bd commit 174fcc6
Show file tree
Hide file tree
Showing 9 changed files with 711 additions and 292 deletions.
584 changes: 485 additions & 99 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ version = "2.1.1"
edition = "2021"

[dependencies]
lazy_static = "1.5.0"
reqwest = { version = "0.12.5", features = ["json", "rustls-tls", "blocking"] }
serde_derive = "1.0.205"
serde_json = "1.0.122"
base64 = "0.13.0"
image = "0.23.14"
reqwest = { version = "0.11.24", features = ["blocking"] }
tasklist = "0.2.13"
tray-icon = "0.11.3"
winit = "0.29.13"
regex = "1.10.6"
powershell_script = "1.1.0"
serde = { version = "1.0.205", features = ["derive"] }
strum = "0.26.3"
strum_macros = "0.26.4"


[build-dependencies]
winres = "0.1.12"
File renamed without changes.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use winres::WindowsResource;
fn main() -> std::io::Result<()> {
if std::env::var_os("CARGO_CFG_WINDOWS").is_some() {
WindowsResource::new()
.set_icon("resources/icon.ico")
.set_icon("assets/icon.ico")
.compile()?;
}
Ok(())
Expand Down
138 changes: 0 additions & 138 deletions src/accept.rs

This file was deleted.

87 changes: 87 additions & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use serde::{Deserialize, Serialize};
use lazy_static::lazy_static;

const LCU_PORT_KEY: &str = "--app-port=";
const LCU_TOKEN_KEY: &str = "--remoting-auth-token=";
const LCU_DIR_KEY: &str = "--install-directory=";
const LCU_COMMAND: &str = "Get-CimInstance Win32_Process -Filter \"name = 'LeagueClientUx.exe'\" | Select-Object -ExpandProperty CommandLine";

lazy_static! {
static ref PORT_REGEXP: regex::Regex = regex::Regex::new(r"--app-port=\d+").unwrap();
static ref TOKEN_REGEXP: regex::Regex = regex::Regex::new(r"--remoting-auth-token=\S+").unwrap();
static ref DIR_REGEXP: regex::Regex = regex::Regex::new(r#"--install-directory=(.*?)""#).unwrap();
static ref MAC_DIR_REGEXP: regex::Regex = regex::Regex::new(r"--install-directory=([^\s]+).*?--").unwrap();
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct CommandLineOutput {
pub auth_url: String,
pub token: String,
pub port: String,
pub dir: String,
}

#[cfg(target_os = "windows")]
pub fn get_commandline() -> CommandLineOutput {
use powershell_script::PsScriptBuilder;

let ps = PsScriptBuilder::new()
.no_profile(true)
.non_interactive(true)
.hidden(true)
.print_commands(false)
.build();

match ps.run(LCU_COMMAND) {
Ok(out) => {
let output = out.stdout();

if output.is_some() {
return match_stdout(&String::from(output.unwrap()));
}
}
Err(err) => println!("cmd error: {:?}", err),
}

CommandLineOutput {
..Default::default()
}
}

fn match_stdout(stdout: &str) -> CommandLineOutput {
let port = if let Some(port_match) = PORT_REGEXP.find(stdout) {
port_match.as_str().replace(LCU_PORT_KEY, "")
} else {
"0".to_string()
};

let token = if let Some(token_match) = TOKEN_REGEXP.find(stdout) {
token_match
.as_str()
.replace(LCU_TOKEN_KEY, "")
.replace(['\\', '\"'], "")
} else {
"".to_string()
};

let auth_url = make_auth_url(&token, &port);

let raw_dir = if let Some(dir_match) = DIR_REGEXP.find(stdout) {
dir_match.as_str().replace(LCU_DIR_KEY, "")
} else {
"".to_string()
};
let output_dir = raw_dir.replace('\"', "");
let dir = format!("{output_dir}/");

CommandLineOutput {
auth_url,
token,
port,
dir,
}
}

fn make_auth_url(token: &String, port: &String) -> String {
format!("https://riot:{token}@127.0.0.1:{port}")
}
45 changes: 45 additions & 0 deletions src/lcu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::time::Duration;
use lazy_static::lazy_static;

lazy_static! {
static ref CLIENT: reqwest::blocking::Client = {
reqwest::blocking::Client::builder()
.use_rustls_tls()
.danger_accept_invalid_certs(true)
.timeout(Duration::from_secs(2))
.no_proxy()
.build()
.unwrap()
};
}

pub fn make_client() -> &'static reqwest::blocking::Client {
&CLIENT
}

pub fn accept_match(endpoint: &str) {
let client = make_client();
let url = format!("{endpoint}/lol-matchmaking/v1/ready-check/accept");
let _ = client
.post(url)
.version(reqwest::Version::HTTP_2)
.header(reqwest::header::ACCEPT, "application/json")
.send();
}

pub fn get_phase(endpoint: &str) -> String {
let mut phase = String::from("Unknown");
let url = format!("{endpoint}/lol-gameflow/v1/gameflow-phase");
let client = make_client();
let response = client
.get(url)
.version(reqwest::Version::HTTP_2)
.header(reqwest::header::ACCEPT, "application/json")
.send()
.ok();
if let Some(response) = response {
phase = response.text().unwrap_or(phase);
phase = phase.trim_matches('"').to_string();
}
phase
}
48 changes: 43 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
#![windows_subsystem = "windows"]

mod accept;
mod cmd;
mod lcu;
mod tray;

use accept::Acceptor;
use tray::TrayApp;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time::Duration;

use crate::tray::TrayApp;

fn main() {
let mut app = TrayApp::new(Acceptor::new());
app.run();
let pause = Arc::new(AtomicBool::new(false));
let pause_clone = Arc::clone(&pause);
let terminate = Arc::new(AtomicBool::new(false));
let terminate_clone = Arc::clone(&terminate);

// Start Acceptor
let mut auth = cmd::get_commandline();
thread::spawn(move || {
while !terminate.load(Ordering::SeqCst) {
if !pause.load(Ordering::SeqCst) && !auth.auth_url.is_empty() {
match lcu::get_phase(&auth.auth_url).as_str() {
"Lobby" => {
thread::sleep(Duration::from_millis(1000));
continue;
}
"Matchmaking" => {
thread::sleep(Duration::from_millis(300));
continue;
}
"ReadyCheck" => {
lcu::accept_match(&auth.auth_url);
thread::sleep(Duration::from_millis(1000));
continue;
}
_ => {},
}
}
auth = cmd::get_commandline();
thread::sleep(Duration::from_millis(5000));
}
});

// Start Tray Icon
let mut icon = TrayApp::new();
icon.show(pause_clone, terminate_clone);
}
Loading

0 comments on commit 174fcc6

Please sign in to comment.