Files
proxy-suite-flake/windows/crates/ps-core/src/components.rs
T
faumarayandClaude Opus 5.5 7565e62571 windows: the app sets the system proxy; ps-core lint fixes
- The app points the signed-in user's system proxy (WinINet: browsers
  and most programs follow it) at the local proxy while it runs and
  proxy.systemProxy.enable asks; the user's own settings are kept and put
  back when the proxy stops, the service goes, the option is turned off
  or the app quits; one left behind by a killed app is cleared. A switch
  for it on the overview. Elsewhere than Windows it does nothing.
- ps-core: clippy's collapsible ifs, comparisons without owned values, a
  range loop without indexing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 02:07:02 +03:00

123 lines
4.3 KiB
Rust

//! The components: the backend programs the service runs, pinned by version and hash
//! (`windows/components.json`). Each is fetched from its project's own Windows release and
//! verified before anything from it runs.
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Manifest {
pub components: BTreeMap<String, Component>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Component {
pub version: String,
pub license: String,
pub homepage: String,
/// Built with the release, not fetched from a source of its own.
#[serde(default)]
pub bundled: bool,
pub sources: Vec<Source>,
}
/// One archive and what to take from it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Source {
pub url: String,
/// The archive's SHA-256, where the archive is stable (a release asset).
#[serde(default)]
pub sha256: Option<String>,
/// The leading path every taken entry has, removed.
#[serde(default)]
pub strip: String,
/// Where below the component's directory the entries go.
#[serde(default)]
pub dest: String,
/// Only these entries (exact paths, or directories ending in `/`).
#[serde(default)]
pub include: Option<Vec<String>>,
/// Not these.
#[serde(default)]
pub exclude: Vec<String>,
/// Exactly these entries, each with its SHA-256: for an archive that is not byte-stable
/// (a commit snapshot), what is verified is what is taken.
#[serde(default)]
pub files: Option<BTreeMap<String, String>>,
}
/// The manifest this build pins.
pub fn manifest() -> Manifest {
serde_json::from_str(include_str!("../../../components.json")).expect("components.json is valid")
}
fn matches(list: &[String], rel: &str) -> bool {
list.iter().any(|p| if p.ends_with('/') { rel.starts_with(p.as_str()) } else { rel == p })
}
/// A relative path made of plain names only: no root, no drive, no `..`, no `.`.
pub fn safe_relative(rel: &str) -> bool {
!rel.is_empty()
&& !rel.starts_with(['/', '\\'])
&& !rel.contains(':')
&& rel.split(['/', '\\']).all(|part| !part.is_empty() && part != "." && part != "..")
}
impl Source {
/// Where an archive entry goes below the component's directory, or `None` to leave it.
pub fn place(&self, entry: &str) -> Option<String> {
if entry.ends_with('/') {
return None;
}
let rel = entry.strip_prefix(self.strip.as_str())?;
if let Some(files) = &self.files
&& !files.contains_key(rel)
{
return None;
}
if let Some(include) = &self.include
&& !matches(include, rel)
{
return None;
}
if matches(&self.exclude, rel) {
return None;
}
let out = format!("{}{rel}", self.dest);
safe_relative(&out).then_some(out)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_manifest_reads_and_places_entries() {
let m = manifest();
let sb = &m.components["sing-box"].sources[0];
assert_eq!(sb.place("sing-box-1.14.4-windows-amd64/sing-box.exe").as_deref(), Some("sing-box.exe"));
assert_eq!(sb.place("other/sing-box.exe"), None);
let z = &m.components["zapret"].sources[0];
assert_eq!(z.place("zapret-discord-youtube-1.10.4/bin/winws.exe").as_deref(), Some("bin/winws.exe"));
assert_eq!(z.place("zapret-discord-youtube-1.10.4/service.bat"), None);
assert_eq!(z.place("zapret-discord-youtube-1.10.4/utils/targets.txt"), None);
let k = &m.components["zapret2"].sources[1];
assert_eq!(k.place(&format!("{}nfqws2.conf", k.strip)).as_deref(), Some("keenetic/nfqws2.conf"));
assert!(m.components["tg-ws-proxy"].bundled);
}
#[test]
fn archive_paths_cannot_leave_the_directory() {
for bad in ["../x", "a/../b", "/etc/x", "C:/x", "a\\..\\b", "", "a//b"] {
assert!(!safe_relative(bad), "{bad}");
}
assert!(safe_relative("bin/winws.exe"));
let s = Source { url: String::new(), sha256: None, strip: "p/".into(), dest: String::new(), include: None, exclude: vec![], files: None };
assert_eq!(s.place("p/../../evil.exe"), None);
}
}