- ps-client: the service's client side, moved out of proxy-ctl: calls, configuration edits against the revision read (retried on a concurrent change), log following, and the edits themselves as functions on the configuration (outbounds, subscriptions, rules, zapret, tg-ws-proxy), now unit-tested. proxy-ctl does what it did, through it; a refused log follow is now an error instead of nothing. - ps-gui: proxy-suite, the desktop app on GPUI (gpui-kit 0.8.1); only a first window yet. Not in default-members: plain cargo build/test and the release workflow's tests leave GPUI out. - tools/gui-shot.sh: runs the app on Xvfb with Mesa's software Vulkan and screenshots it, to check the GUI on Linux without a display. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
130 lines
4.4 KiB
Rust
130 lines
4.4 KiB
Rust
//! The service's client side, shared by proxy-ctl and the app: requests, configuration
|
|
//! edits made against the revision they read, and the edits both make ([`edits`]).
|
|
|
|
pub mod edits;
|
|
|
|
use anyhow::{Result, bail};
|
|
use ps_ipc::transport::{self, Address};
|
|
use ps_ipc::{Request, Response};
|
|
use serde_json::Value;
|
|
|
|
/// What a configuration edit got back: the new revision, the validator's notes, and the
|
|
/// error applying it, when it was saved but did not apply.
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct Saved {
|
|
pub revision: Option<u64>,
|
|
pub issues: Vec<Issue>,
|
|
pub apply_error: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct Issue {
|
|
pub severity: String,
|
|
pub path: String,
|
|
pub message: String,
|
|
}
|
|
|
|
impl std::fmt::Display for Issue {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "{}: {}: {}", self.severity, self.path, self.message)
|
|
}
|
|
}
|
|
|
|
/// The validator's notes in a response's data.
|
|
pub fn issues(data: &Value) -> Vec<Issue> {
|
|
data["issues"]
|
|
.as_array()
|
|
.into_iter()
|
|
.flatten()
|
|
.map(|i| Issue {
|
|
severity: i["severity"].as_str().unwrap_or("").to_owned(),
|
|
path: i["path"].as_str().unwrap_or("").to_owned(),
|
|
message: i["message"].as_str().unwrap_or("").to_owned(),
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Client {
|
|
pub addr: Address,
|
|
}
|
|
|
|
impl Client {
|
|
pub fn new(addr: Address) -> Self {
|
|
Self { addr }
|
|
}
|
|
|
|
/// One request; a refusal is an error carrying the service's words and its notes.
|
|
pub async fn call(&self, req: Request) -> Result<Value> {
|
|
let resp: Response = transport::call(&self.addr, &req).await?;
|
|
if !resp.ok {
|
|
let mut msg = resp.error.unwrap_or_else(|| "failed".into());
|
|
for i in issues(&resp.data) {
|
|
msg.push_str(&format!("\n {i}"));
|
|
}
|
|
bail!(msg);
|
|
}
|
|
Ok(resp.data)
|
|
}
|
|
|
|
/// Reads the configuration, lets `edit` change it, and writes it back against the
|
|
/// revision read; a concurrent change is read again and `edit` made again.
|
|
pub async fn edit(&self, edit: impl Fn(&mut Value) -> Result<()>) -> Result<Saved> {
|
|
for _ in 0..3 {
|
|
let got = self.call(Request::ConfigGet).await?;
|
|
let mut config = got["config"].clone();
|
|
edit(&mut config)?;
|
|
match self.call(Request::ConfigSet { config, base: got["revision"].as_u64() }).await {
|
|
Err(e) if e.to_string().contains("changed since revision") => continue,
|
|
other => {
|
|
let r = other?;
|
|
return Ok(Saved {
|
|
revision: r["revision"].as_u64(),
|
|
issues: issues(&r),
|
|
apply_error: r["error"].as_str().map(str::to_owned),
|
|
});
|
|
}
|
|
}
|
|
}
|
|
bail!("the configuration kept changing; try again")
|
|
}
|
|
|
|
/// Log lines (every unit's without `unit`) as `(unit, line)`, the last `lines` and, with
|
|
/// `follow`, each new one, until `each` says stop.
|
|
pub async fn logs(&self, unit: Option<String>, lines: usize, follow: bool, mut each: impl FnMut(&str, &str) -> bool) -> Result<()> {
|
|
let mut take = |r: Response| {
|
|
let one = |l: &Value| (l["unit"].as_str().unwrap_or("").to_owned(), l["line"].as_str().unwrap_or("").to_owned());
|
|
match &r.data {
|
|
Value::Array(ls) => ls.iter().map(one).all(|(u, l)| each(&u, &l)),
|
|
d if d.is_object() => {
|
|
let (u, l) = one(d);
|
|
each(&u, &l)
|
|
}
|
|
_ => true,
|
|
}
|
|
};
|
|
let req = Request::Logs { unit, lines, follow };
|
|
if follow {
|
|
let mut refused = None;
|
|
transport::stream(&self.addr, &req, |r| {
|
|
if !r.ok {
|
|
refused = Some(r.error.unwrap_or_else(|| "failed".into()));
|
|
return false;
|
|
}
|
|
take(r)
|
|
})
|
|
.await?;
|
|
if let Some(e) = refused {
|
|
bail!(e);
|
|
}
|
|
} else {
|
|
let r = transport::call(&self.addr, &req).await?;
|
|
if !r.ok {
|
|
bail!(r.error.unwrap_or_else(|| "failed".into()));
|
|
}
|
|
take(r);
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|