Kinda get network working

This commit is contained in:
Michael Mikovsky
2025-08-12 22:24:00 -06:00
parent 8b2a596b9a
commit 856d7e467f
15 changed files with 3316 additions and 3219 deletions
+40 -43
View File
@@ -1,6 +1,9 @@
mod host;
mod syscall_exec;
#[allow(dead_code)]
mod syscall_intercept;
mod syscalls;
use syscall_lib::syscall_should_proxy;
use libc::exit;
use std::{
@@ -11,32 +14,22 @@ use std::{
};
use syscall_intercept::*;
static mut STREAM: Option<Mutex<TcpStream>> = None;
static mut WRITER: Option<Mutex<BufWriter<TcpStream>>> = None;
use crate::host::Host;
static mut HOST: Option<Mutex<Host>> = None;
#[ctor::ctor]
fn start() {
unsafe {
STREAM = Some({
match TcpStream::connect("127.0.0.1:1234") {
Ok(stream) => Mutex::new(stream),
HOST = Some({
match Host::new() {
Ok(host) => Mutex::new(host),
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) };
@@ -75,50 +68,54 @@ impl Drop for InterceptGuard {
}
extern "C" fn hook(
syscall_num: isize,
arg0: isize,
arg1: isize,
arg2: isize,
arg3: isize,
arg4: isize,
arg5: isize,
_result: &mut isize,
syscall_num: usize,
arg0: usize,
arg1: usize,
arg2: usize,
arg3: usize,
arg4: usize,
arg5: usize,
result: &mut isize,
) -> InterceptResult {
// detect and avoid recursive interception
let _guard = match InterceptGuard::try_lock() {
Some(g) => g,
None => return InterceptResult::Forward,
None => {
println!("Disabled!");
return InterceptResult::Forward;
}
};
if !syscalls::syscall_should_proxy(syscall_num) {
return InterceptResult::Forward;
}
// if syscall_should_proxy(syscall_num) {
// return InterceptResult::Forward;
// }
unsafe {
unset_hook_fn();
}
let args = [arg0, arg1, arg2, arg3, arg4, arg5];
let syscall = syscall_lib::create_syscall(syscall_num, args);
if !syscall_should_proxy(&syscall) {
return InterceptResult::Forward;
}
// 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"
// };
// if syscall_num == libc::SYS_close as _ && arg0 == 1 {
// return InterceptResult::Forward;
// }
// println!("{:?}", desc);
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();
if let Some(host) = HOST.as_ref() {
*result = host.lock().unwrap().execute(&syscall);
}
}
@@ -126,5 +123,5 @@ extern "C" fn hook(
set_hook_fn(hook);
}
InterceptResult::Forward
InterceptResult::Hook
}