windows: the service, its API and proxy-ctl
ps-ipc: line-delimited JSON over a named pipe whose ACL admits SYSTEM, Administrators and (with userControl) the interactive user; the caller is identified by impersonation, never by what it claims. A Unix socket stands in off Windows. ps-service (proxy-suite-service.exe): a Windows service, or --console. Keeps the configuration with revisions, runtime state and generated secrets; renders, checks with sing-box check before replacing what runs, and supervises the backends (restart with backoff, start limit, logs in a ring and a file, a Job Object on Windows). Enforces scopes, admin-only edits and secret redaction that round-trips. Fetches subscriptions and rule sets through the proxy or directly with upstream's redirect rules, on timers. ps-ctl (proxy-ctl.exe): status, restart, logs [-f], config get/set/ validate, and proxy on/off, outbounds, pin, mode, subs, rules, rulesets, tun and config. An end-to-end test runs it all offline: a sing-box Shadowsocks server, the service, proxy-ctl adding the outbound, a page fetched through the local proxy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
918cdc9a75
commit
41c8ed4406
25 files changed
+5411
-7
No files matched your search
@@ -0,0 +1,115 @@
|
||||
//! 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,
|
||||
/// Fetch and install components (all wanted, or the named ones).
|
||||
ComponentsInstall { names: Vec<String> },
|
||||
}
|
||||
|
||||
#[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}"#);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user