summaryrefslogtreecommitdiffhomepage
path: root/src/platform/windows.rs
blob: 35ce956e5c8de9fb4dd3e6035fa2a386eda2f0f3 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2025 Dillution <[email protected]>.
//
// This file is part of DPIBreak.
//
// DPIBreak is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// DPIBreak is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License
// along with DPIBreak. If not, see <https://www.gnu.org/licenses/>.

use anyhow::Result;
use windivert::{
    WinDivert,
    layer::NetworkLayer
};
use std::sync::{atomic::Ordering, LazyLock};
use crate::{log::LogLevel, log_println, splash};

pub static WINDIVERT_HANDLE: LazyLock<WinDivert<NetworkLayer>> = LazyLock::new(|| {
    use windivert::*;

    const FILTER: &str = "outbound and tcp and tcp.DstPort == 443";

    match WinDivert::network(FILTER, 0, prelude::WinDivertFlags::new()) {
        Ok(h) => {
            log_println!(LogLevel::Info, "windivert: HANDLE constructed for {}", FILTER);

            h
        },
        Err(e) => { panic!("Err: {}", e); }
    }
});

pub fn bootstrap() -> Result<()> {
    Ok(())
}

pub fn cleanup() -> Result<()> {
    Ok(())
}

pub fn send_to_raw(pkt: &[u8]) -> Result<()> {
    use windivert::*;

    let mut p = unsafe { packet::WinDivertPacket::<NetworkLayer>::new(pkt.to_vec()) };

    p.address.set_outbound(true);
    p.address.set_ip_checksum(true);
    p.address.set_tcp_checksum(true);

    WINDIVERT_HANDLE.send(&p)?;

    Ok(())
}

pub fn run() -> Result<()> {
    use crate::{handle_packet, RUNNING, MESSAGE_AT_RUN};

    let mut buf = vec![0u8; 65536];

    splash!("{MESSAGE_AT_RUN}");

    while RUNNING.load(Ordering::SeqCst) {
        let pkt = WINDIVERT_HANDLE.recv(Some(&mut buf))?;

        handle_packet!(
            &pkt.data,
            handled => {},
            rejected => { WINDIVERT_HANDLE.send(&pkt)?; }
        );
    }

    Ok(())
}