Compare commits
No commits in common. "636579945e1a9b2ac59922ca4ae915893f3981c8" and "bac8d2277f68b3f31fcb153fe76a583f4d79497e" have entirely different histories.
636579945e
...
bac8d2277f
4 changed files with 8 additions and 63 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
@ -22,8 +22,6 @@ name = "camellya-patch"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ilhook",
|
||||
"regex",
|
||||
"widestring",
|
||||
"windows",
|
||||
]
|
||||
|
||||
|
@ -89,9 +87,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.11.1"
|
||||
version = "1.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
|
||||
checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
|
@ -153,12 +151,6 @@ version = "1.0.13"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
|
||||
|
||||
[[package]]
|
||||
name = "widestring"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7219d36b6eac893fa81e84ebe06485e7dcbb616177469b142df14f1f4deb1311"
|
||||
|
||||
[[package]]
|
||||
name = "windows"
|
||||
version = "0.58.0"
|
||||
|
|
|
@ -16,8 +16,6 @@ enable-sdk = []
|
|||
|
||||
[dependencies]
|
||||
ilhook = "2.1.1"
|
||||
regex = "1.11.1"
|
||||
widestring = "1.1.0"
|
||||
windows = { version = "0.58.0", features = [
|
||||
"Win32_Foundation",
|
||||
"Win32_System_LibraryLoader",
|
||||
|
|
28
src/lib.rs
28
src/lib.rs
|
@ -1,9 +1,8 @@
|
|||
use std::sync::OnceLock;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use ilhook::x64::Registers;
|
||||
use windows::core::{PCSTR, PCWSTR};
|
||||
use windows::core::{PCSTR, PCWSTR, w};
|
||||
use windows::Win32::Foundation::HINSTANCE;
|
||||
use windows::Win32::System::Console;
|
||||
use windows::Win32::System::LibraryLoader::GetModuleHandleA;
|
||||
|
@ -12,13 +11,8 @@ use windows::Win32::System::SystemServices::DLL_PROCESS_ATTACH;
|
|||
use interceptor::Interceptor;
|
||||
use offsets::CONFIG;
|
||||
|
||||
use crate::replacer::{GenericReplacer, Replacer};
|
||||
|
||||
mod interceptor;
|
||||
mod offsets;
|
||||
mod replacer;
|
||||
|
||||
static CFG_SERVER_REPLACER: OnceLock<GenericReplacer> = OnceLock::new();
|
||||
|
||||
fn thread_func() {
|
||||
unsafe { Console::AllocConsole() }.unwrap();
|
||||
|
@ -70,23 +64,13 @@ fn thread_func() {
|
|||
unsafe extern "win64" fn on_kurohttp_get(reg: *mut Registers, _: usize) {
|
||||
let wstr = *((*reg).rcx as *const usize) as *mut u16;
|
||||
let url = PCWSTR::from_raw(wstr).to_string().unwrap();
|
||||
|
||||
println!("HTTP GET: {url}");
|
||||
|
||||
let replacer = CFG_SERVER_REPLACER.get_or_init(|| {
|
||||
GenericReplacer {
|
||||
regex: regex::Regex::new(r#"^(?:https|http)://.*/([a-zA-Z0-9]{32}/index\.json)$"#).unwrap(),
|
||||
replacement: std::env::var("CFG_SERVER_URL").unwrap_or("127.0.0.1:10001".to_string()),
|
||||
scheme: std::env::var("CFG_SERVER_SCHEME").unwrap_or("http".to_string()),
|
||||
}
|
||||
});
|
||||
|
||||
if let Some(result) = replacer.replace(url.as_str()) {
|
||||
println!("Redirecting to: {result}");
|
||||
// TODO: Track https://doc.rust-lang.org/nightly/unstable-book/library-features/str-from-utf16-endian.html to replace widestring when stabilized
|
||||
let new_url = widestring::U16String::from_str(result.as_str());
|
||||
let new_wstr = PCWSTR::from_raw(new_url.as_ptr());
|
||||
if url.ends_with("/index.json") {
|
||||
println!("index.json requested, redirecting");
|
||||
let new_wstr = w!("http://127.0.0.1:10001/index.json");
|
||||
std::ptr::copy_nonoverlapping(new_wstr.as_ptr(), wstr, new_wstr.as_wide().len() + 2);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
unsafe extern "win64" fn fpakfile_check_replacement(
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
pub(crate) trait Replacer {
|
||||
fn replace(&self, original: &str) -> Option<String>;
|
||||
}
|
||||
|
||||
pub(crate) struct GenericReplacer {
|
||||
pub(crate) regex: regex::Regex,
|
||||
pub(crate) replacement: String,
|
||||
pub(crate) scheme: String,
|
||||
}
|
||||
|
||||
impl Replacer for GenericReplacer {
|
||||
fn replace(&self, original: &str) -> Option<String> {
|
||||
// Prepare output array
|
||||
let mut results: Vec<String> = vec![];
|
||||
// Perform the capture over input
|
||||
for (_, [path]) in self.regex.captures_iter(original).map(|c| c.extract()) {
|
||||
results.push(format!("{}://{}/{}", self.scheme, self.replacement, path));
|
||||
}
|
||||
// We are supposed to only parse one entry from text
|
||||
if 1 == results.len() {
|
||||
return Some(results.remove(0));
|
||||
} else if results.is_empty() {
|
||||
println!("No valid url match found so returning original url");
|
||||
} else {
|
||||
println!("Invalid number of entries parsed, expected 1, obtained {:?}", results.len());
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue