-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathmain.rs
More file actions
54 lines (47 loc) · 1.45 KB
/
main.rs
File metadata and controls
54 lines (47 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#![no_std]
#![no_main]
use aya_ebpf::EbpfContext;
use aya_ebpf::macros::{cgroup_sock, map};
use aya_ebpf::maps::Array;
use aya_ebpf::programs::SockContext;
use aya_log_ebpf::debug;
use mitmproxy_linux_ebpf_common::{Action, INTERCEPT_CONF_LEN};
#[unsafe(no_mangle)]
static INTERFACE_ID: u32 = 0;
#[map]
static INTERCEPT_CONF: Array<Action> = Array::with_max_entries(INTERCEPT_CONF_LEN, 0);
#[cgroup_sock(sock_create)]
pub fn cgroup_sock_create(ctx: SockContext) -> i32 {
if should_intercept(&ctx) {
debug!(&ctx, "intercepting in sock_create");
let interface_id = unsafe { core::ptr::read_volatile(&INTERFACE_ID) };
unsafe {
(*ctx.sock).bound_dev_if = interface_id;
}
}
1
}
pub fn should_intercept(ctx: &SockContext) -> bool {
let command = ctx.command().ok();
let pid = ctx.pid();
let mut intercept = matches!(INTERCEPT_CONF.get(0), Some(Action::Exclude(_)));
for i in 0..INTERCEPT_CONF_LEN {
match INTERCEPT_CONF.get(i) {
Some(Action::Include(pattern)) => {
intercept = intercept || pattern.matches(command.as_ref(), pid);
}
Some(Action::Exclude(pattern)) => {
intercept = intercept && !pattern.matches(command.as_ref(), pid);
}
_ => {
break;
}
}
}
intercept
}
#[cfg(not(test))]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}