Files
proxy-suite-flake/windows/crates/ps-ipc/src/lib.rs
T
faumarayandClaude Opus 5.5 5cf3a237e6 windows: AmneziaWG as a system-wide VPN
Each profile without asOutbound is a tunnel service of its own,
AmneziaWGTunnel$<interface>, as amneziawg-windows's client installs one:
amneziawg-go on Wintun, its routes and DNS, and for one peer that takes
everything its firewall blocking what would leave outside the tunnel.
It runs awg-tunnel.exe, amneziawg-windows built from its pinned commit
with a main of ours (its Run(), its log in a file) instead of its
tunnel.dll, beside Wintun's signed DLL (tools/build-amneziawg.sh).

The service writes each profile's .conf below its state root, registers
the tunnel (unrestricted service SID, which its firewall exempts it by),
starts the autostart ones with itself, and removes the tunnels of
profiles gone, and all of them on uninstall. proxy-ctl awg
list|on|off|log, and the app's AmneziaWG page: profiles (a system-wide
one switched on and off, its log), one added from a vpn:// link or a
.conf, or removed.

tools/lib.sh: clone_commit, and fetch_pinned, which fetches in pieces a
download that stalls (some networks cut TLS after about 16 KB, as
wintun.net's here).

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

157 lines
5.8 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 },
}
#[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}"#);
}
}