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:
faumarayandClaude Opus 5.5 committed 2026-10-26 23:13:19 +03:00
1 parent 918cdc9a75
commit 41c8ed4406
25 files changed
+5411 -7

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
//! Where the service keeps everything.
//!
//! `%ProgramData%\ProxySuite` on Windows, or `--state-dir` (any platform, for development
//! and tests):
//!
//! ```text
//! config.json the configuration, written only by the service
//! state.json pins, disabled outbounds, the route mode
//! secrets.json what the service generated: the Clash API secret, the hop login
//! subscriptions/ each subscription's links as fetched last
//! rule-sets/ downloaded geodata and rule sets
//! cache/ sing-box's fake IP cache
//! run/ the rendered backend configs
//! logs/ one log per unit
//! bin/ the components (sing-box, xray, winws, …)
//! ```
use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
pub struct Paths {
pub root: PathBuf,
}
impl Paths {
pub fn new(root: impl Into<PathBuf>) -> Self {
Self { root: root.into() }
}
/// `%ProgramData%\ProxySuite`, or `/var/lib/proxy-suite-windows` elsewhere.
pub fn platform_default() -> Self {
if cfg!(windows) {
let base = std::env::var_os("ProgramData").map(PathBuf::from).unwrap_or_else(|| PathBuf::from(r"C:\ProgramData"));
Self::new(base.join("ProxySuite"))
} else {
Self::new("/var/lib/proxy-suite-windows")
}
}
pub fn config(&self) -> PathBuf {
self.root.join("config.json")
}
pub fn state(&self) -> PathBuf {
self.root.join("state.json")
}
pub fn secrets(&self) -> PathBuf {
self.root.join("secrets.json")
}
pub fn subscriptions(&self) -> PathBuf {
self.root.join("subscriptions")
}
pub fn rule_sets(&self) -> PathBuf {
self.root.join("rule-sets")
}
pub fn cache(&self) -> PathBuf {
self.root.join("cache")
}
pub fn run(&self) -> PathBuf {
self.root.join("run")
}
pub fn logs(&self) -> PathBuf {
self.root.join("logs")
}
pub fn bin(&self) -> PathBuf {
self.root.join("bin")
}
pub fn create_all(&self) -> std::io::Result<()> {
for dir in [self.root.clone(), self.subscriptions(), self.rule_sets(), self.cache(), self.run(), self.logs(), self.bin()] {
std::fs::create_dir_all(dir)?;
}
Ok(())
}
}
/// Writes `data` to `path` through a temporary file and a rename, so a reader never sees half
/// of it and a crash leaves the old one.
pub fn write_atomic(path: &Path, data: &[u8]) -> std::io::Result<()> {
let tmp = path.with_extension("tmp");
std::fs::write(&tmp, data)?;
std::fs::rename(&tmp, path)
}
/// Whether `path` holds exactly `data` already.
pub fn same_content(path: &Path, data: &[u8]) -> bool {
std::fs::read(path).is_ok_and(|old| old == data)
}