windows: zapret2 on winws2 with nfqws2-keenetic's strategies
ps-core::zapret2 reads nfqws2-keenetic's nfqws2.conf and patches it as upstream's sources.nix does, expands the hostlist markers as zapret2's list.sh does (autohostlist with its detector settings, the configured domains), fills circular's unset detector settings from autoHostlist, merges the extra ports into the profiles that tell traffic by protocol, and gives winws2 WinDivert port filters in place of the queue's rules. The service runs it with the learned list in a directory labelled Low integrity, where winws2 can still write once it drops to Low. winws2 verifies the whole command line under Wine. detect.lua, z2k and the cutoff probe are not there yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9b0d7021f9
commit
bceb754e65
6 files changed
+516
-4
No files matched your search
@@ -173,8 +173,16 @@ impl Service {
|
||||
Ok(())
|
||||
}
|
||||
ps_core::config::ZapretEngine::Zapret2 => {
|
||||
self.sup.stop(crate::zapret::UNIT).await;
|
||||
anyhow::bail!("zapret2 is not supported on Windows yet; use engine \"zapret-discord-youtube\"")
|
||||
let program = self.comps.program("zapret2", "winws2")?;
|
||||
let spec = crate::zapret::zapret2_unit(&cfg, self.paths(), program)?;
|
||||
if cfg.zapret.tcp_timestamps && !self.tcp_timestamps_on {
|
||||
match crate::zapret::enable_tcp_timestamps().await {
|
||||
Ok(()) => self.tcp_timestamps_on = true,
|
||||
Err(e) => self.sup.note(crate::zapret::UNIT, format!("proxy-suite: warning: {e}")),
|
||||
}
|
||||
}
|
||||
self.sup.ensure(spec);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,3 +125,78 @@ pub async fn enable_tcp_timestamps() -> Result<()> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The unit for zapret2: winws2 with nfqws2-keenetic's strategies (ps_core::zapret2), its
|
||||
/// lists in the service's `zapret2\\` directory, the learned one where winws2 can still write
|
||||
/// once it runs at Low integrity.
|
||||
pub fn zapret2_unit(cfg: &Config, paths: &Paths, program: PathBuf) -> Result<UnitSpec> {
|
||||
use ps_core::zapret2;
|
||||
let z = &cfg.zapret.zapret2;
|
||||
let component = paths.bin().join("zapret2");
|
||||
let conf = std::fs::read_to_string(component.join("keenetic").join("nfqws2.conf")).context("reading nfqws2-keenetic's nfqws2.conf")?;
|
||||
let source = zapret2::keenetic(&conf, &with_sep(&component.join("keenetic"))).map_err(anyhow::Error::msg)?;
|
||||
|
||||
let state = paths.root.join("zapret2");
|
||||
let learned = state.join("learned");
|
||||
std::fs::create_dir_all(&learned)?;
|
||||
#[cfg(windows)]
|
||||
low_integrity(&learned)?;
|
||||
let user = state.join("zapret-hosts-user.txt");
|
||||
let exclude = state.join("zapret-hosts-user-exclude.txt");
|
||||
let auto = learned.join("zapret-hosts-auto.txt");
|
||||
// nfqws2 refuses a list it cannot open; an empty one is fine.
|
||||
for f in [&user, &exclude, &auto] {
|
||||
if !f.exists() {
|
||||
std::fs::write(f, b"")?;
|
||||
}
|
||||
}
|
||||
let p = zapret2::Paths {
|
||||
lua: with_sep(&component.join("lua")),
|
||||
user: user.to_string_lossy().into_owned(),
|
||||
exclude: exclude.to_string_lossy().into_owned(),
|
||||
auto: auto.to_string_lossy().into_owned(),
|
||||
};
|
||||
let argv = zapret2::args(z, &source, &p, cfg.debug.zapret2.packets).map_err(anyhow::Error::msg)?;
|
||||
let generation = {
|
||||
use sha2::{Digest, Sha256};
|
||||
let mut h = Sha256::new();
|
||||
for a in &argv {
|
||||
h.update(a.as_bytes());
|
||||
h.update(b"\n");
|
||||
}
|
||||
h.finalize().iter().map(|b| format!("{b:02x}")).collect()
|
||||
};
|
||||
Ok(UnitSpec { name: UNIT.into(), program, args: argv, env: Vec::new(), cwd: Some(component), generation })
|
||||
}
|
||||
|
||||
/// A Low mandatory label on `dir`, inherited below it: what a Low-integrity process (winws2,
|
||||
/// once it runs) may still write to. Its DACL stays as it is.
|
||||
#[cfg(windows)]
|
||||
fn low_integrity(dir: &Path) -> Result<()> {
|
||||
use std::ffi::c_void;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use std::ptr::null_mut;
|
||||
use windows_sys::Win32::Foundation::{ERROR_SUCCESS, LocalFree};
|
||||
use windows_sys::Win32::Security::Authorization::{
|
||||
ConvertStringSecurityDescriptorToSecurityDescriptorW, SDDL_REVISION_1, SE_FILE_OBJECT, SetNamedSecurityInfoW,
|
||||
};
|
||||
use windows_sys::Win32::Security::{ACL, GetSecurityDescriptorSacl, LABEL_SECURITY_INFORMATION};
|
||||
let sddl: Vec<u16> = "S:(ML;OICI;NW;;;LW)".encode_utf16().chain(Some(0)).collect();
|
||||
let path: Vec<u16> = dir.as_os_str().encode_wide().chain(Some(0)).collect();
|
||||
let mut sd: *mut c_void = null_mut();
|
||||
// SAFETY: NUL-terminated inputs; the SACL points into sd, freed below.
|
||||
unsafe {
|
||||
if ConvertStringSecurityDescriptorToSecurityDescriptorW(sddl.as_ptr(), SDDL_REVISION_1, &mut sd, null_mut()) == 0 {
|
||||
return Err(std::io::Error::last_os_error().into());
|
||||
}
|
||||
let (mut present, mut defaulted) = (0, 0);
|
||||
let mut sacl: *mut ACL = null_mut();
|
||||
GetSecurityDescriptorSacl(sd, &mut present, &mut sacl, &mut defaulted);
|
||||
let rc = SetNamedSecurityInfoW(path.as_ptr() as *mut u16, SE_FILE_OBJECT, LABEL_SECURITY_INFORMATION, null_mut(), null_mut(), null_mut(), sacl);
|
||||
LocalFree(sd);
|
||||
if rc != ERROR_SUCCESS {
|
||||
return Err(std::io::Error::from_raw_os_error(rc as i32).into());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user