@onxdev/silentium 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +1194 -0
- package/Cargo.toml +37 -0
- package/LICENSE +21 -0
- package/README.md +314 -2
- package/npm/bin/silentium.js +45 -0
- package/npm/install.js +31 -0
- package/npm/lib.js +220 -0
- package/package.json +44 -4
- package/rust-toolchain.toml +2 -0
- package/src/cli.rs +146 -0
- package/src/detect.rs +248 -0
- package/src/discord.rs +273 -0
- package/src/main.rs +347 -0
- package/src/net.rs +371 -0
- package/src/ui.rs +414 -0
- package/src/update.rs +385 -0
package/src/net.rs
ADDED
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
use anyhow::Result;
|
|
2
|
+
|
|
3
|
+
pub const VOICE_PORT_START: u16 = 50000;
|
|
4
|
+
pub const VOICE_PORT_END: u16 = 65535;
|
|
5
|
+
#[allow(dead_code)]
|
|
6
|
+
pub const POLICY_PREFIX: &str = "Silentium";
|
|
7
|
+
|
|
8
|
+
#[derive(Debug, Clone)]
|
|
9
|
+
pub struct ShapeSpec {
|
|
10
|
+
pub rate_kbps: u64,
|
|
11
|
+
pub loss_percent: Option<u8>,
|
|
12
|
+
pub delay_ms: Option<u32>,
|
|
13
|
+
#[allow(dead_code)]
|
|
14
|
+
pub iface: Option<String>,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
pub fn apply(spec: &ShapeSpec) -> Result<()> {
|
|
18
|
+
#[cfg(windows)]
|
|
19
|
+
{
|
|
20
|
+
return windows::apply(spec);
|
|
21
|
+
}
|
|
22
|
+
#[cfg(target_os = "linux")]
|
|
23
|
+
{
|
|
24
|
+
linux::apply(spec)
|
|
25
|
+
}
|
|
26
|
+
#[cfg(not(any(windows, target_os = "linux")))]
|
|
27
|
+
{
|
|
28
|
+
let _ = spec;
|
|
29
|
+
anyhow::bail!("plataforma sem suporte a shaping nesta versão")
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
pub fn clear(iface: Option<String>) -> Result<()> {
|
|
34
|
+
#[cfg(windows)]
|
|
35
|
+
{
|
|
36
|
+
let _ = iface;
|
|
37
|
+
return windows::clear();
|
|
38
|
+
}
|
|
39
|
+
#[cfg(target_os = "linux")]
|
|
40
|
+
{
|
|
41
|
+
linux::clear(iface)
|
|
42
|
+
}
|
|
43
|
+
#[cfg(not(any(windows, target_os = "linux")))]
|
|
44
|
+
{
|
|
45
|
+
let _ = iface;
|
|
46
|
+
anyhow::bail!("plataforma sem suporte a shaping nesta versão")
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
#[cfg(windows)]
|
|
51
|
+
mod windows {
|
|
52
|
+
use super::{ShapeSpec, POLICY_PREFIX, VOICE_PORT_END, VOICE_PORT_START};
|
|
53
|
+
use anyhow::{bail, Context, Result};
|
|
54
|
+
use std::process::Command;
|
|
55
|
+
|
|
56
|
+
fn powershell(script: &str) -> Result<()> {
|
|
57
|
+
let out = Command::new("powershell")
|
|
58
|
+
.args(["-NoProfile", "-NonInteractive", "-Command", script])
|
|
59
|
+
.output()
|
|
60
|
+
.context("não consegui executar powershell")?;
|
|
61
|
+
if !out.status.success() {
|
|
62
|
+
let err = String::from_utf8_lossy(&out.stderr).trim().to_string();
|
|
63
|
+
let std_out = String::from_utf8_lossy(&out.stdout).trim().to_string();
|
|
64
|
+
bail!("powershell falhou: {} {}", err, std_out);
|
|
65
|
+
}
|
|
66
|
+
Ok(())
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const BLOCK_RULE: &str = "Silentium-block";
|
|
70
|
+
|
|
71
|
+
pub fn block(on: bool) -> Result<()> {
|
|
72
|
+
if !on {
|
|
73
|
+
let script = format!(
|
|
74
|
+
"Get-NetFirewallRule -DisplayName '{BLOCK_RULE}-*' -ErrorAction SilentlyContinue | Remove-NetFirewallRule"
|
|
75
|
+
);
|
|
76
|
+
powershell(&script)?;
|
|
77
|
+
println!("[+] bloqueio do Discord removido");
|
|
78
|
+
return Ok(());
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let script = r#"
|
|
82
|
+
$ErrorActionPreference = 'Stop'
|
|
83
|
+
$paths = New-Object System.Collections.Generic.List[string]
|
|
84
|
+
Get-Process Discord, DiscordPTB, DiscordCanary, DiscordDevelopment -ErrorAction SilentlyContinue |
|
|
85
|
+
ForEach-Object { if ($_.Path) { $paths.Add($_.Path) } }
|
|
86
|
+
$roots = @(
|
|
87
|
+
(Join-Path $env:LOCALAPPDATA 'Discord'),
|
|
88
|
+
(Join-Path $env:LOCALAPPDATA 'DiscordPTB'),
|
|
89
|
+
(Join-Path $env:LOCALAPPDATA 'DiscordCanary'),
|
|
90
|
+
(Join-Path $env:PROGRAMFILES 'Discord'),
|
|
91
|
+
(Join-Path ${env:PROGRAMFILES(x86)} 'Discord')
|
|
92
|
+
)
|
|
93
|
+
foreach ($root in $roots) {
|
|
94
|
+
if ($root -and (Test-Path $root)) {
|
|
95
|
+
Get-ChildItem -Path $root -Recurse -Filter 'Discord.exe' -ErrorAction SilentlyContinue |
|
|
96
|
+
ForEach-Object { $paths.Add($_.FullName) }
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
$paths = $paths | Sort-Object -Unique
|
|
100
|
+
if ($paths.Count -eq 0) { Write-Error 'Discord.exe não encontrado'; exit 2 }
|
|
101
|
+
Get-NetFirewallRule -DisplayName 'SILENTIUM_RULE-*' -ErrorAction SilentlyContinue | Remove-NetFirewallRule
|
|
102
|
+
$i = 0
|
|
103
|
+
foreach ($p in $paths) {
|
|
104
|
+
New-NetFirewallRule -DisplayName ("SILENTIUM_RULE-{0}" -f $i) -Direction Outbound `
|
|
105
|
+
-Program $p -Action Block -Profile Any -Enabled True | Out-Null
|
|
106
|
+
$i++
|
|
107
|
+
}
|
|
108
|
+
Write-Output ("regras: {0}" -f $paths.Count)
|
|
109
|
+
"#;
|
|
110
|
+
let script = script.replace("SILENTIUM_RULE", BLOCK_RULE);
|
|
111
|
+
powershell(&script).context("aplicando regras de firewall (rode como Administrador)")?;
|
|
112
|
+
println!("[+] internet do Discord cortada — só os processos dele, resto do PC intacto");
|
|
113
|
+
Ok(())
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
pub fn apply(spec: &ShapeSpec) -> Result<()> {
|
|
117
|
+
if spec.loss_percent.is_some() || spec.delay_ms.is_some() {
|
|
118
|
+
eprintln!(
|
|
119
|
+
"[!] no Windows a pilha nativa só limita banda; --loss/--delay são ignorados aqui"
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
let bits = spec.rate_kbps.saturating_mul(1000).max(1000);
|
|
123
|
+
|
|
124
|
+
for (name, port) in [
|
|
125
|
+
(format!("{POLICY_PREFIX}-out"), "IPDstPort"),
|
|
126
|
+
(format!("{POLICY_PREFIX}-in"), "IPSrcPort"),
|
|
127
|
+
] {
|
|
128
|
+
let script = format!(
|
|
129
|
+
"New-NetQosPolicy -Name '{name}' -PolicyStore ActiveStore \
|
|
130
|
+
-IPProtocolMatchCondition UDP \
|
|
131
|
+
-{port}StartMatchCondition {VOICE_PORT_START} \
|
|
132
|
+
-{port}EndMatchCondition {VOICE_PORT_END} \
|
|
133
|
+
-ThrottleRateActionBitsPerSecond {bits} -ErrorAction Stop"
|
|
134
|
+
);
|
|
135
|
+
powershell(&script)
|
|
136
|
+
.with_context(|| format!("aplicando política {name} (rode como Administrador)"))?;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
println!(
|
|
140
|
+
"[+] QoS ativa: UDP {VOICE_PORT_START}-{VOICE_PORT_END} limitado a {} kbps (envio e recebimento)",
|
|
141
|
+
spec.rate_kbps
|
|
142
|
+
);
|
|
143
|
+
Ok(())
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
pub fn clear() -> Result<()> {
|
|
147
|
+
for name in [
|
|
148
|
+
format!("{POLICY_PREFIX}-out"),
|
|
149
|
+
format!("{POLICY_PREFIX}-in"),
|
|
150
|
+
] {
|
|
151
|
+
let script = format!(
|
|
152
|
+
"Remove-NetQosPolicy -Name '{name}' -PolicyStore ActiveStore -Confirm:$false -ErrorAction SilentlyContinue"
|
|
153
|
+
);
|
|
154
|
+
powershell(&script)?;
|
|
155
|
+
}
|
|
156
|
+
println!("[+] políticas {POLICY_PREFIX}-* removidas");
|
|
157
|
+
Ok(())
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
#[cfg(target_os = "linux")]
|
|
162
|
+
mod linux {
|
|
163
|
+
use super::{ShapeSpec, VOICE_PORT_END, VOICE_PORT_START};
|
|
164
|
+
use anyhow::{bail, Context, Result};
|
|
165
|
+
use std::process::Command;
|
|
166
|
+
|
|
167
|
+
const MARK: &str = "0x0d15";
|
|
168
|
+
|
|
169
|
+
fn run(program: &str, args: &[&str]) -> Result<()> {
|
|
170
|
+
let out = Command::new(program)
|
|
171
|
+
.args(args)
|
|
172
|
+
.output()
|
|
173
|
+
.with_context(|| format!("executando {program}"))?;
|
|
174
|
+
if !out.status.success() {
|
|
175
|
+
bail!(
|
|
176
|
+
"{program} {} falhou: {}",
|
|
177
|
+
args.join(" "),
|
|
178
|
+
String::from_utf8_lossy(&out.stderr).trim()
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
Ok(())
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
fn detect_iface() -> Result<String> {
|
|
185
|
+
let out = Command::new("ip")
|
|
186
|
+
.args(["route", "get", "1.1.1.1"])
|
|
187
|
+
.output()
|
|
188
|
+
.context("ip route: iproute2 instalado?")?;
|
|
189
|
+
let text = String::from_utf8_lossy(&out.stdout).to_string();
|
|
190
|
+
let mut words = text.split_whitespace();
|
|
191
|
+
while let Some(word) = words.next() {
|
|
192
|
+
if word == "dev" {
|
|
193
|
+
if let Some(iface) = words.next() {
|
|
194
|
+
return Ok(iface.to_string());
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
bail!("não consegui detectar a interface de saída");
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
fn ports() -> String {
|
|
202
|
+
format!("{VOICE_PORT_START}:{VOICE_PORT_END}")
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
fn ensure_mark(dir: &str) -> Result<()> {
|
|
206
|
+
let ports = ports();
|
|
207
|
+
let check = [
|
|
208
|
+
"-t",
|
|
209
|
+
"mangle",
|
|
210
|
+
"-C",
|
|
211
|
+
"OUTPUT",
|
|
212
|
+
"-p",
|
|
213
|
+
"udp",
|
|
214
|
+
dir,
|
|
215
|
+
&ports,
|
|
216
|
+
"-j",
|
|
217
|
+
"MARK",
|
|
218
|
+
"--set-mark",
|
|
219
|
+
MARK,
|
|
220
|
+
];
|
|
221
|
+
if run("iptables", &check).is_ok() {
|
|
222
|
+
return Ok(());
|
|
223
|
+
}
|
|
224
|
+
let add = [
|
|
225
|
+
"-t",
|
|
226
|
+
"mangle",
|
|
227
|
+
"-A",
|
|
228
|
+
"OUTPUT",
|
|
229
|
+
"-p",
|
|
230
|
+
"udp",
|
|
231
|
+
dir,
|
|
232
|
+
&ports,
|
|
233
|
+
"-j",
|
|
234
|
+
"MARK",
|
|
235
|
+
"--set-mark",
|
|
236
|
+
MARK,
|
|
237
|
+
];
|
|
238
|
+
run("iptables", &add)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
pub fn block() -> Result<()> {
|
|
242
|
+
let ports = ports();
|
|
243
|
+
for dir in ["--dport", "--sport"] {
|
|
244
|
+
run(
|
|
245
|
+
"iptables",
|
|
246
|
+
&["-C", "OUTPUT", "-p", "udp", dir, &ports, "-j", "DROP"],
|
|
247
|
+
)
|
|
248
|
+
.or_else(|_| {
|
|
249
|
+
run(
|
|
250
|
+
"iptables",
|
|
251
|
+
&["-A", "OUTPUT", "-p", "udp", dir, &ports, "-j", "DROP"],
|
|
252
|
+
)
|
|
253
|
+
})?;
|
|
254
|
+
}
|
|
255
|
+
println!("[+] UDP de voz bloqueado (Linux não isola por processo; uso fica na faixa 50000-65535)");
|
|
256
|
+
Ok(())
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
pub fn unblock() -> Result<()> {
|
|
260
|
+
let ports = ports();
|
|
261
|
+
for dir in ["--dport", "--sport"] {
|
|
262
|
+
let _ = run(
|
|
263
|
+
"iptables",
|
|
264
|
+
&["-D", "OUTPUT", "-p", "udp", dir, &ports, "-j", "DROP"],
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
println!("[+] bloqueio removido");
|
|
268
|
+
Ok(())
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
pub fn apply(spec: &ShapeSpec) -> Result<()> {
|
|
272
|
+
let iface = match &spec.iface {
|
|
273
|
+
Some(name) => name.clone(),
|
|
274
|
+
None => detect_iface()?,
|
|
275
|
+
};
|
|
276
|
+
ensure_mark("--dport")?;
|
|
277
|
+
ensure_mark("--sport")?;
|
|
278
|
+
|
|
279
|
+
let mut netem = format!("rate {}kbit", spec.rate_kbps.max(1));
|
|
280
|
+
if let Some(loss) = spec.loss_percent {
|
|
281
|
+
netem.push_str(&format!(" loss {loss}%"));
|
|
282
|
+
}
|
|
283
|
+
if let Some(delay) = spec.delay_ms {
|
|
284
|
+
netem.push_str(&format!(" delay {delay}ms 200ms distribution normal"));
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
let _ = run("tc", &["qdisc", "del", "dev", &iface, "root"]);
|
|
288
|
+
run(
|
|
289
|
+
"tc",
|
|
290
|
+
&[
|
|
291
|
+
"qdisc", "add", "dev", &iface, "root", "handle", "1:", "prio",
|
|
292
|
+
],
|
|
293
|
+
)?;
|
|
294
|
+
run(
|
|
295
|
+
"tc",
|
|
296
|
+
&[
|
|
297
|
+
"qdisc", "add", "dev", &iface, "parent", "1:3", "handle", "30:", "netem", &netem,
|
|
298
|
+
],
|
|
299
|
+
)?;
|
|
300
|
+
run(
|
|
301
|
+
"tc",
|
|
302
|
+
&[
|
|
303
|
+
"filter", "add", "dev", &iface, "parent", "1:0", "handle", MARK, "fw", "flowid",
|
|
304
|
+
"1:3",
|
|
305
|
+
],
|
|
306
|
+
)?;
|
|
307
|
+
|
|
308
|
+
println!("[+] netem em {iface}: {netem}");
|
|
309
|
+
Ok(())
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
pub fn clear(iface: Option<String>) -> Result<()> {
|
|
313
|
+
let iface = match iface {
|
|
314
|
+
Some(name) => name,
|
|
315
|
+
None => detect_iface()?,
|
|
316
|
+
};
|
|
317
|
+
let ports = ports();
|
|
318
|
+
let _ = run("tc", &["qdisc", "del", "dev", &iface, "root"]);
|
|
319
|
+
for dir in ["--dport", "--sport"] {
|
|
320
|
+
let _ = run(
|
|
321
|
+
"iptables",
|
|
322
|
+
&[
|
|
323
|
+
"-t",
|
|
324
|
+
"mangle",
|
|
325
|
+
"-D",
|
|
326
|
+
"OUTPUT",
|
|
327
|
+
"-p",
|
|
328
|
+
"udp",
|
|
329
|
+
dir,
|
|
330
|
+
&ports,
|
|
331
|
+
"-j",
|
|
332
|
+
"MARK",
|
|
333
|
+
"--set-mark",
|
|
334
|
+
MARK,
|
|
335
|
+
],
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
println!("[+] shaping removido de {iface}");
|
|
339
|
+
Ok(())
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
pub fn block() -> Result<()> {
|
|
344
|
+
#[cfg(windows)]
|
|
345
|
+
{
|
|
346
|
+
return windows::block(true);
|
|
347
|
+
}
|
|
348
|
+
#[cfg(target_os = "linux")]
|
|
349
|
+
{
|
|
350
|
+
linux::block()
|
|
351
|
+
}
|
|
352
|
+
#[cfg(not(any(windows, target_os = "linux")))]
|
|
353
|
+
{
|
|
354
|
+
anyhow::bail!("plataforma sem suporte a bloqueio por processo")
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
pub fn unblock() -> Result<()> {
|
|
359
|
+
#[cfg(windows)]
|
|
360
|
+
{
|
|
361
|
+
return windows::block(false);
|
|
362
|
+
}
|
|
363
|
+
#[cfg(target_os = "linux")]
|
|
364
|
+
{
|
|
365
|
+
linux::unblock()
|
|
366
|
+
}
|
|
367
|
+
#[cfg(not(any(windows, target_os = "linux")))]
|
|
368
|
+
{
|
|
369
|
+
anyhow::bail!("plataforma sem suporte a bloqueio por processo")
|
|
370
|
+
}
|
|
371
|
+
}
|