Files
proxy-suite-flake/windows/crates/ps-ipc/src/lib.rs
T
faumarayandClaude Opus 5.5 803e18db9d windows: zapret2's 16 KB cutoff probe
Upstream's cutoff.nix and cutoff-probe.template.sh: z2k-detect (built from
z2k's pinned source for Windows, in zapret2-extras) finds the networks this
line cuts after ~16 KB, and with z2k's own strategies a whitelisted name per
network; z2k-tcp16.lua inside winws2 reads the maps (Z2K_TCP16_*, their
digest in the environment so a new map restarts winws2); what no name fixes
goes to the zapret-cutoff rule-set for the proxy (cutoff.proxyFallback).

Upstream keeps zapret2 off the probe with an nft cgroup rule. Here
z2k-detect is patched to dial from a local port range
(Z2K_TCP16_LOCAL_PORTS, 44000-47999, below Windows' dynamic range) and
winws2 gets --wf-raw-filter leaving that range alone; under the TUN the
probe and the service's own lookups of the line's network go direct.

Rounds as upstream's timer: 5 min after start, every 10 min, probing when
the line's network (ipinfo.io and ifconfig.co agreeing) changed or a day
passed. Request::ZapretCutoff, proxy-ctl zapret cutoff [probe], a card on
the DPI page. Verified live on a line that cuts (25 networks, a name for
each), the Windows z2k-detect under Wine, winws2 --dry-run and --wf-save
with the filter. gui-shot's scroll takes a count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 23:12:43 +03:00

168 lines
6.3 KiB
Rust

//! The service's API: what proxy-ctl and the app ask, what it answers, and how the two
//! reach each other. One JSON request per line, answered by one JSON response per line (a
//! log follow answers with many, up to a final one).
//!
//! On Windows the transport is the named pipe `\\.\pipe\proxy-suite`, whose security
//! descriptor admits SYSTEM, Administrators and (with `userControl`) the interactive user.
//! Elsewhere it is a Unix socket, so the whole stack runs on Linux for development.
pub mod transport;
use serde::{Deserialize, Serialize};
use serde_json::Value;
/// The pipe on Windows.
pub const PIPE_NAME: &str = r"\\.\pipe\proxy-suite";
/// Who is asking, as the transport established it (never as the client claims).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Peer {
/// An administrator (elevated on Windows, root elsewhere): every scope, and the
/// admin-only options.
pub admin: bool,
/// The account, for the log.
pub user: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "op", rename_all = "kebab-case")]
pub enum Request {
/// Units, their state, the proxy's mode.
Status,
/// The configuration. Secrets (links, passwords) need the `secrets` scope.
ConfigGet,
/// Replaces the whole configuration, if it is valid and `base` (a revision from
/// `config-get`) is still current.
ConfigSet { config: Value, base: Option<u64> },
/// Checks a configuration without taking it.
ConfigValidate { config: Value },
/// Runtime state: pins, disabled outbounds, the route mode.
StateGet,
Pin { tag: Option<String>, group: Option<String> },
Disable { tag: String, disabled: bool },
Mode { mode: Option<String> },
/// What the proxy runs: tags, sources, groups, the pick.
Outbounds,
/// The backend configs as rendered last. Secrets scope.
RenderedConfig,
Restart { units: Vec<String> },
Start { unit: String },
Stop { unit: String },
/// The last `lines` of a unit's log (every unit's, interleaved, without one); `follow`
/// keeps the answer open with each new line.
Logs { unit: Option<String>, lines: usize, follow: bool },
/// Refetch subscriptions (all, or one).
SubsUpdate { tag: Option<String> },
/// Refetch rule sets and geodata.
RuleSetsUpdate,
/// Latency of outbounds through the Clash API.
Test { tags: Vec<String> },
/// Components: what is installed, what the manifest wants.
Components,
/// The presets the installed zapret release has, and the one configured.
ZapretPresets,
/// tg-ws-proxy's tg:// links, which carry its secret. Secrets scope.
TgLinks,
/// Fetch and install components (all wanted, or the named ones).
ComponentsInstall { names: Vec<String> },
/// whitelist-bypass's creators and joiners, and their state.
WlList,
/// The call a creator made and keeps rejoining.
WlLink { name: String },
/// A creator's login: a cookies export, or (DION, Bitrix) the JSON of its email, password
/// and portal.
WlAuth { name: String, login: String },
/// The call link a joiner joins.
WlJoin { name: String, link: String },
/// Drops the call a creator rejoins: it makes a new one.
WlNew { name: String },
/// autoProxy: the routes it learned, its exits, what waits.
AutoList,
/// Probes a destination (a host, or host/path) through the exits as the prober does:
/// through `exits` (all, without), or whether `via` carries what direct reaches.
AutoProbe {
target: String,
#[serde(default)]
exits: Option<Vec<String>>,
#[serde(default)]
via: Option<String>,
#[serde(default)]
keep_going: bool,
},
/// Hosts to probe now.
AutoLearn { hosts: Vec<String> },
/// Forgets a domain's route and verdicts; without one, everything learned.
AutoForget {
#[serde(default)]
domain: Option<String>,
},
/// AmneziaWG's profiles: system-wide ones with their tunnel's state, outbounds by kind.
AwgList,
/// Starts or stops a system-wide profile's tunnel.
AwgSet { name: String, on: bool },
/// The last lines of a system-wide profile's tunnel log. Secrets scope, as logs are.
AwgLog { name: String, lines: usize },
/// The 16 KB cutoff probe: the networks this line cuts, and the names that get through;
/// `probe` runs it now.
ZapretCutoff {
#[serde(default)]
probe: bool,
},
/// This version, and the newest release if it is newer.
UpdateCheck,
/// Fetches the newest release's setup program, checks it, and runs it: the service stops,
/// is replaced, and starts again, and the app with it. Services scope.
UpdateInstall,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Response {
pub ok: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
#[serde(default, skip_serializing_if = "Value::is_null")]
pub data: Value,
/// More responses follow (a log follow).
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub more: bool,
}
impl Response {
pub fn ok(data: Value) -> Self {
Self { ok: true, error: None, data, more: false }
}
pub fn err(message: impl Into<String>) -> Self {
Self { ok: false, error: Some(message.into()), data: Value::Null, more: false }
}
pub fn partial(data: Value) -> Self {
Self { ok: true, error: None, data, more: true }
}
}
#[derive(Debug, thiserror::Error)]
pub enum IpcError {
#[error("cannot reach the Proxy Suite service ({0}); is it running?")]
Connect(std::io::Error),
#[error("the service closed the connection")]
Closed,
#[error("{0}")]
Io(#[from] std::io::Error),
#[error("bad message: {0}")]
Json(#[from] serde_json::Error),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn requests_read_as_upstream_verbs() {
let r: Request = serde_json::from_str(r#"{"op":"pin","tag":"nl","group":null}"#).unwrap();
assert_eq!(r, Request::Pin { tag: Some("nl".into()), group: None });
let s = serde_json::to_string(&Request::Logs { unit: None, lines: 50, follow: true }).unwrap();
assert_eq!(s, r#"{"op":"logs","unit":null,"lines":50,"follow":true}"#);
}
}