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,90 @@
|
||||
//! sing-box's Clash API on loopback: what selectors pick, and outbound latency.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
use serde_json::Value;
|
||||
|
||||
pub struct Clash {
|
||||
base: String,
|
||||
secret: String,
|
||||
http: reqwest::Client,
|
||||
}
|
||||
|
||||
fn encode(segment: &str) -> String {
|
||||
let mut out = String::new();
|
||||
for b in segment.bytes() {
|
||||
if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~') {
|
||||
out.push(b as char);
|
||||
} else {
|
||||
out.push_str(&format!("%{b:02X}"));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
impl Clash {
|
||||
pub fn new(port: u16, secret: &str) -> Self {
|
||||
let http = reqwest::Client::builder()
|
||||
.no_proxy()
|
||||
.timeout(Duration::from_secs(20))
|
||||
.build()
|
||||
.expect("an HTTP client");
|
||||
Self { base: format!("http://127.0.0.1:{port}"), secret: secret.to_owned(), http }
|
||||
}
|
||||
|
||||
async fn get(&self, path: &str) -> Result<Value> {
|
||||
let resp = self.http.get(format!("{}{path}", self.base)).bearer_auth(&self.secret).send().await?;
|
||||
let status = resp.status();
|
||||
let body: Value = resp.json().await.unwrap_or(Value::Null);
|
||||
if !status.is_success() {
|
||||
bail!("{}", body["message"].as_str().unwrap_or(status.as_str()));
|
||||
}
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
/// Every proxy (outbound or group) with what a selector holds now.
|
||||
pub async fn proxies(&self) -> Result<Value> {
|
||||
self.get("/proxies").await.map(|v| v["proxies"].clone())
|
||||
}
|
||||
|
||||
/// What `group` (a selector or urltest) uses now.
|
||||
pub async fn current(&self, group: &str) -> Result<Option<String>> {
|
||||
let v = self.get(&format!("/proxies/{}", encode(group))).await?;
|
||||
Ok(v["now"].as_str().map(str::to_owned))
|
||||
}
|
||||
|
||||
/// Switches the selector `group` to `tag`, live.
|
||||
pub async fn select(&self, group: &str, tag: &str) -> Result<()> {
|
||||
let resp = self
|
||||
.http
|
||||
.put(format!("{}/proxies/{}", self.base, encode(group)))
|
||||
.bearer_auth(&self.secret)
|
||||
.json(&serde_json::json!({ "name": tag }))
|
||||
.send()
|
||||
.await
|
||||
.context("the Clash API")?;
|
||||
if !resp.status().is_success() {
|
||||
let body: Value = resp.json().await.unwrap_or(Value::Null);
|
||||
bail!("cannot switch {group} to {tag}: {}", body["message"].as_str().unwrap_or("refused"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The delay to `url` through `tag`, in milliseconds.
|
||||
pub async fn delay(&self, tag: &str, url: &str, timeout_ms: u32) -> Result<u32> {
|
||||
let resp = self
|
||||
.http
|
||||
.get(format!("{}/proxies/{}/delay", self.base, encode(tag)))
|
||||
.bearer_auth(&self.secret)
|
||||
.query(&[("url", url), ("timeout", &timeout_ms.to_string())])
|
||||
.send()
|
||||
.await?;
|
||||
let status = resp.status();
|
||||
let body: Value = resp.json().await.unwrap_or(Value::Null);
|
||||
if !status.is_success() {
|
||||
bail!("{}", body["message"].as_str().unwrap_or("timed out"));
|
||||
}
|
||||
body["delay"].as_u64().map(|d| d as u32).context("no delay in the answer")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user