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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
// 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, anyhow, Context};
use std::sync::{
atomic::{Ordering, AtomicBool},
OnceLock,
};
mod platform;
mod pkt;
mod tls;
mod log;
use log::LogLevel;
const PROJECT_NAME: &str = "DPIBreak";
const PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
const PKG_DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
const PKG_HOMEPAGE: &str = env!("CARGO_PKG_HOMEPAGE");
const MESSAGE_AT_RUN: &str = r#"DPIBreak is now running.
Press Ctrl+C or close this window to stop.
"#;
static RUNNING: AtomicBool = AtomicBool::new(true);
static DELAY_MS: OnceLock<u64> = OnceLock::new();
fn delay_ms() -> u64 {
*DELAY_MS.get().expect("DELAY_MS not initialized")
}
fn split_packet(pkt: &pkt::PktView, start: u32, end: Option<u32>,
out_buf: &mut Vec<u8>) -> Result<()> {
use etherparse::*;
let ip = &pkt.ip;
let tcp = &pkt.tcp;
let payload = tcp.payload();
let end = end.unwrap_or(payload.len().try_into()?);
if start > end || payload.len() < end as usize {
return Err(anyhow!("invalid index"));
}
let opts = tcp.options();
let mut tcp_hdr = tcp.to_header();
tcp_hdr.sequence_number += start;
// TODO: refactor this to reuse IP header with no copy
let builder = match ip {
IpSlice::Ipv4(hdr) =>
PacketBuilder::ip(IpHeaders::Ipv4(
hdr.header().to_header(),
hdr.extensions().to_header()
)),
IpSlice::Ipv6(hdr) =>
PacketBuilder::ip(IpHeaders::Ipv6(
hdr.header().to_header(),
Default::default()
))
}.tcp_header(tcp_hdr).options_raw(opts)?;
let payload = &payload[start as usize..end as usize];
out_buf.clear();
builder.write(out_buf, payload)?;
Ok(())
}
/// Return Ok(true) if packet is handled
fn handle_packet(pkt: &[u8], buf: &mut Vec::<u8>) -> Result<bool> {
use platform::send_to_raw;
#[cfg(target_os = "linux")]
let is_filtered = platform::IS_U32_SUPPORTED.load(Ordering::Relaxed);
#[cfg(windows)]
let is_filtered = false;
let view = pkt::PktView::from_raw(pkt)?;
if !is_filtered && !tls::is_client_hello(view.tcp.payload()) {
return Ok(false);
}
// TODO: if clienthello packet has been (unlikely) fragmented,
// we should find the second part and drop, reassemble it here.
split_packet(&view, 0, Some(1), buf)?;
send_to_raw(&buf)?;
std::thread::sleep(std::time::Duration::from_millis(delay_ms()));
split_packet(&view, 1, None, buf)?;
send_to_raw(&buf)?;
#[cfg(debug_assertions)]
log_println!(LogLevel::Debug, "packet is handled, len={}", pkt.len());
Ok(true)
}
#[macro_export]
macro_rules! handle_packet {
($bytes:expr, $buf:expr, handled => $on_handled:expr, rejected => $on_rejected:expr $(,)?) => {{
match handle_packet($bytes, $buf) {
Ok(true) => { $on_handled }
Ok(false) => { $on_rejected }
Err(e) => {
log_println!(LogLevel::Warning, "handle_packet: {e}");
$on_rejected
}
}
}};
}
fn take_value<T, I>(args: &mut I, arg_name: &str) -> Result<T>
where
T: std::str::FromStr,
T::Err: std::error::Error + Send + Sync + 'static,
I: Iterator<Item = String>,
{
let raw = args
.next()
.ok_or_else(|| anyhow!("argument: missing value after {}", arg_name))?;
raw.parse::<T>()
.with_context(|| format!("argument {}: invalid value '{}'", arg_name, raw))
}
fn usage() {
println!(
r#"Usage: dpibreak [OPTIONS]
Options:
--delay-ms <u64> (default: 0)
--queue-num <u16> (linux only, default: 1)
--loglevel <debug|info|warning|error> (default: warning)
--no-splash Do not print splash messages
-h, --help Show this help"#
);
}
fn parse_args_1() -> Result<()> {
let mut delay_ms: u64 = 0;
let mut no_splash: bool = false;
#[cfg(debug_assertions)]
let mut log_level: log::LogLevel = LogLevel::Debug;
#[cfg(not(debug_assertions))]
let mut log_level: log::LogLevel = LogLevel::Warning;
#[cfg(target_os = "linux")]
let mut queue_num: u16 = 1;
let mut args = std::env::args().skip(1); // program name
while let Some(arg) = args.next() {
let argv = arg.as_str();
match argv {
"-h" | "--help" => { usage(); std::process::exit(0); }
"--delay-ms" => { delay_ms = take_value(&mut args, argv)?; }
"--loglevel" => { log_level = take_value(&mut args, argv)?; }
"--no-splash" => { no_splash = true; }
#[cfg(target_os = "linux")]
"--queue-num" => { queue_num = take_value(&mut args, argv)?; }
_ => { return Err(anyhow!("argument: unknown: {}", arg)); }
}
}
DELAY_MS.set(delay_ms).map_err(|_| anyhow!("DELAY_MS already initialized"))?;
log::set_no_splash(no_splash).map_err(|e| anyhow!("{e}"))?;
log::set_log_level(log_level).map_err(|e| anyhow!("{e}"))?;
#[cfg(target_os = "linux")]
platform::QUEUE_NUM.set(queue_num).map_err(|_| anyhow!("QUEUE_NUM already initialized"))?;
Ok(())
}
fn parse_args() {
if let Err(e) = parse_args_1() {
log_println!(LogLevel::Error, "{e}");
usage();
std::process::exit(1);
}
}
fn trap_exit() -> Result<()> {
ctrlc::set_handler(|| {
RUNNING.store(false, Ordering::SeqCst);
}).context("handler: ")?;
Ok(())
}
/// Drop with calling platform::cleanup()
struct EnsureCleanup;
impl Drop for EnsureCleanup {
fn drop(&mut self) {
if let Err(e) = platform::cleanup() {
log_println!(LogLevel::Error, "cleanup failed: {e}");
}
}
}
fn main_0() -> Result<()> {
trap_exit()?;
parse_args();
splash!("{PROJECT_NAME} v{PKG_VERSION} - {PKG_DESCRIPTION}");
splash!("{PKG_HOMEPAGE}");
splash!("");
let _guard = EnsureCleanup;
platform::bootstrap()?;
platform::run()?;
Ok(())
}
fn main() {
let code = match main_0() {
Ok(()) => 0,
Err(e) => {
log_println!(LogLevel::Error, "{e}");
for (i, cause) in e.chain().skip(1).enumerate() {
log_println!(LogLevel::Error, "caused by[{i}]: {cause}");
}
1
}
};
std::process::exit(code);
}
|