Add proxy list, git submodule for intercept

This commit is contained in:
Michael Mikovsky
2025-08-12 17:48:34 -06:00
parent 4757240463
commit 8b2a596b9a
9 changed files with 3373 additions and 23 deletions
+130
View File
@@ -0,0 +1,130 @@
#[allow(dead_code)]
mod syscall_intercept;
mod syscalls;
use libc::exit;
use std::{
cell::Cell,
io::{BufWriter, Write},
net::TcpStream,
sync::Mutex,
};
use syscall_intercept::*;
static mut STREAM: Option<Mutex<TcpStream>> = None;
static mut WRITER: Option<Mutex<BufWriter<TcpStream>>> = None;
#[ctor::ctor]
fn start() {
unsafe {
STREAM = Some({
match TcpStream::connect("127.0.0.1:1234") {
Ok(stream) => Mutex::new(stream),
Err(e) => {
eprintln!("Failed to connect to server: {}", e);
exit(1);
}
}
});
WRITER = Some(Mutex::new(BufWriter::new(
#[allow(static_mut_refs)]
STREAM
.as_ref()
.unwrap()
.lock()
.unwrap()
.try_clone()
.unwrap(),
)));
}
unsafe { set_hook_fn(hook) };
}
// fn print_direct(str: &str)
thread_local! {
/// A flag indicating whether the current thread is in an intercept context.
static INTERCEPTED: Cell<bool> = Cell::new(false);
}
// lazy_static! {
// }
struct InterceptGuard;
impl InterceptGuard {
fn try_lock() -> Option<Self> {
INTERCEPTED.with(|x| {
if x.get() {
None
} else {
x.set(true);
Some(InterceptGuard)
}
})
}
}
impl Drop for InterceptGuard {
fn drop(&mut self) {
INTERCEPTED.with(|x| x.set(false));
}
}
extern "C" fn hook(
syscall_num: isize,
arg0: isize,
arg1: isize,
arg2: isize,
arg3: isize,
arg4: isize,
arg5: isize,
_result: &mut isize,
) -> InterceptResult {
// detect and avoid recursive interception
let _guard = match InterceptGuard::try_lock() {
Some(g) => g,
None => return InterceptResult::Forward,
};
if !syscalls::syscall_should_proxy(syscall_num) {
return InterceptResult::Forward;
}
unsafe {
unset_hook_fn();
}
// Return if is print
// if syscall_num == libc::SYS_write as _ && arg0 == 1 {
// return InterceptResult::Forward;
// }
// let args = [arg0, arg1, arg2, arg3, arg4, arg5];
// let desc = if let Some(desc) = syscalls::get_syscall_desc(syscall_num, args) {
// "test"
// } else {
// "test"
// };
unsafe {
#[allow(static_mut_refs)]
if let Some(client) = WRITER.as_ref() {
client
.lock()
.unwrap()
.write_all(&format!("Data: {:?}\n", syscall_num).as_bytes())
.unwrap();
client.lock().unwrap().flush().unwrap();
}
}
unsafe {
set_hook_fn(hook);
}
InterceptResult::Forward
}