//! 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 }, /// Checks a configuration without taking it. ConfigValidate { config: Value }, /// Runtime state: pins, disabled outbounds, the route mode. StateGet, Pin { tag: Option, group: Option }, Disable { tag: String, disabled: bool }, Mode { mode: Option }, /// What the proxy runs: tags, sources, groups, the pick. Outbounds, /// The backend configs as rendered last. Secrets scope. RenderedConfig, Restart { units: Vec }, 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, lines: usize, follow: bool }, /// Refetch subscriptions (all, or one). SubsUpdate { tag: Option }, /// Refetch rule sets and geodata. RuleSetsUpdate, /// Latency of outbounds through the Clash API. Test { tags: Vec }, /// 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 }, /// 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>, #[serde(default)] via: Option, #[serde(default)] keep_going: bool, }, /// Hosts to probe now. AutoLearn { hosts: Vec }, /// Forgets a domain's route and verdicts; without one, everything learned. AutoForget { #[serde(default)] domain: Option, }, /// 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, #[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) -> 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}"#); } }