diff --git a/windows/crates/ps-ctl/src/main.rs b/windows/crates/ps-ctl/src/main.rs index eaa31cd..0912ae7 100644 --- a/windows/crates/ps-ctl/src/main.rs +++ b/windows/crates/ps-ctl/src/main.rs @@ -647,6 +647,11 @@ async fn proxy(ctl: &Ctl, verb: Option) -> Result<()> { println!("{}", if s["proxy"]["tun"] == true { "on" } else { "off" }); } OnOff::On => { + ctl.edit(|c| { + c["proxy"]["tun"]["enable"] = true.into(); + Ok(()) + }) + .await?; ctl.call(Request::Start { unit: "proxy-suite-tun".into() }).await?; } OnOff::Off => { diff --git a/windows/crates/ps-service/src/api.rs b/windows/crates/ps-service/src/api.rs index 423e97f..f908112 100644 --- a/windows/crates/ps-service/src/api.rs +++ b/windows/crates/ps-service/src/api.rs @@ -174,6 +174,17 @@ async fn connection(mut conn: Conn, shared: Shared) { } }; let peer = conn.peer(); + // Every request, streamed or not, is authorized here before anything is done for it. + let denied = { + let s = shared.lock().await; + allowed(s.cfg(), &peer, scope_of(&req)).err() + }; + if let Some(e) = denied { + if conn.write_json(&Response::err(e)).await.is_err() { + return; + } + continue; + } if let Request::Logs { unit, lines, follow: true } = &req { follow_logs(&mut conn, &shared, unit.clone(), *lines).await; return; @@ -208,13 +219,8 @@ async fn follow_logs(conn: &mut Conn, shared: &Shared, unit: Option, lin } } +/// Does what `req` asks; the caller has authorized it (`connection`). pub async fn handle(req: Request, peer: &Peer, shared: &Shared) -> Response { - { - let s = shared.lock().await; - if let Err(e) = allowed(s.cfg(), peer, scope_of(&req)) { - return Response::err(e); - } - } match req { Request::Status => Response::ok(shared.lock().await.status()), @@ -525,6 +531,12 @@ mod tests { assert_eq!(back.proxy.listener.auth.password.as_deref(), Some("pw")); } + #[test] + fn following_logs_needs_the_secrets_scope_too() { + assert_eq!(scope_of(&Request::Logs { unit: None, lines: 10, follow: true }), Some(Scope::Secrets)); + assert_eq!(scope_of(&Request::Logs { unit: None, lines: 10, follow: false }), Some(Scope::Secrets)); + } + #[test] fn scopes_follow_user_control() { let mut cfg = Config::default(); diff --git a/windows/crates/ps-service/src/paths.rs b/windows/crates/ps-service/src/paths.rs index fd7f5b4..9afcb2b 100644 --- a/windows/crates/ps-service/src/paths.rs +++ b/windows/crates/ps-service/src/paths.rs @@ -71,8 +71,23 @@ impl Paths { /// lets anyone else in is moved aside, never adopted: ProgramData lets any user make /// folders, and the service runs what is under `bin\` as SYSTEM. pub fn create_all(&self) -> std::io::Result<()> { + // Once per process: a second check of a root this process made (and may be writing + // its log into) has nothing to learn, and moving it aside then would lose the log. + static CHECKED: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); + let first = { + let mut checked = CHECKED.lock().unwrap(); + let first = !checked.contains(&self.root); + if first { + checked.push(self.root.clone()); + } + first + }; #[cfg(windows)] - secure::ensure_root(&self.root)?; + if first { + secure::ensure_root(&self.root)?; + } + #[cfg(not(windows))] + let _ = first; #[cfg(not(windows))] std::fs::create_dir_all(&self.root)?; for dir in [self.subscriptions(), self.rule_sets(), self.cache(), self.run(), self.logs(), self.bin()] { diff --git a/windows/crates/ps-service/src/winsvc.rs b/windows/crates/ps-service/src/winsvc.rs index 13aede6..27f7cd8 100644 --- a/windows/crates/ps-service/src/winsvc.rs +++ b/windows/crates/ps-service/src/winsvc.rs @@ -25,7 +25,7 @@ pub fn install_dir() -> PathBuf { } /// The files of a release that `install` copies. -const RELEASE_FILES: [&str; 5] = ["proxy-suite-service.exe", "proxy-ctl.exe", "README.md", "THIRD-PARTY.md", "LICENSE"]; +const RELEASE_FILES: [&str; 7] = ["proxy-suite-service.exe", "proxy-ctl.exe", "README.md", "THIRD-PARTY.md", "LICENSE", "install.cmd", "uninstall.cmd"]; fn copy_release(from: &Path, to: &Path) -> Result<()> { std::fs::create_dir_all(to)?; @@ -124,8 +124,16 @@ pub fn install() -> Result<()> { if let Err(e) = path::add(&to) { println!("warning: could not add {} to PATH: {e:#}", to.display()); } - service.start::<&str>(&[]).context("starting the service")?; - println!("{DISPLAY} is installed in {} and started.", to.display()); + // A moment for the manager to settle the change just made (Wine's otherwise answers + // ERROR_SERVICE_DATABASE_LOCKED); a start that still fails leaves a working install. + std::thread::sleep(Duration::from_secs(1)); + match service.start::<&str>(&[]) { + Ok(()) => println!("{DISPLAY} is installed in {} and started.", to.display()), + Err(e) => { + println!("{DISPLAY} is installed in {}, but did not start: {e}", to.display()); + println!("Start it with: sc start {NAME} (its log: %ProgramData%\\ProxySuite\\logs\\service.log)"); + } + } println!("Open a new terminal and try: proxy-ctl status"); Ok(()) } @@ -148,10 +156,17 @@ pub fn uninstall() -> Result<()> { let dir = install_dir(); let _ = path::remove(&dir); if dir.exists() { - // This program may be one of the files: they go a moment after it exits. + // This program may be one of the files: they go a moment after it exits. Raw, as cmd + // does its own quote parsing; from outside the folder, which a working directory holds. + use std::os::windows::process::CommandExt; + const CREATE_NO_WINDOW: u32 = 0x0800_0000; let _ = std::process::Command::new("cmd") - .args(["/c", "ping -n 3 127.0.0.1 >nul & rmdir /s /q"]) - .arg(&dir) + .raw_arg(format!("/c ping -n 3 127.0.0.1 >nul & rmdir /s /q \"{}\"", dir.display())) + .current_dir(std::env::var_os("SystemRoot").unwrap_or_else(|| "C:\\Windows".into())) + .creation_flags(CREATE_NO_WINDOW) + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) .spawn(); println!("Removing {}.", dir.display()); } diff --git a/windows/tools/build-tg-ws-proxy.sh b/windows/tools/build-tg-ws-proxy.sh index e2e6818..e93b3e5 100755 --- a/windows/tools/build-tg-ws-proxy.sh +++ b/windows/tools/build-tg-ws-proxy.sh @@ -5,8 +5,8 @@ # # tools/build-tg-ws-proxy.sh OUT_DIR (needs curl, git, unzip, pip) set -euo pipefail +OUT=$(realpath -m "${1:?output directory}") cd "$(dirname "$0")" -OUT=${1:?output directory} PY_VERSION=3.12.10 PY_URL=https://www.python.org/ftp/python/$PY_VERSION/python-$PY_VERSION-embed-amd64.zip PY_SHA256=4acbed6dd1c744b0376e3b1cf57ce906f9dc9e95e68824584c8099a63025a3c3 @@ -25,7 +25,7 @@ pip download -q --require-hashes --no-deps --only-binary=:all: --platform win_am mkdir -p "$OUT/python/Lib/site-packages" for w in "$WORK"/wheels/*.whl; do unzip -q -o "$w" -d "$OUT/python/Lib/site-packages"; done -git clone -q --depth 1 --branch "$TG_TAG" https://github.com/Flowseal/tg-ws-proxy "$WORK/src" +git -c advice.detachedHead=false clone -q --depth 1 --branch "$TG_TAG" https://github.com/Flowseal/tg-ws-proxy "$WORK/src" [ "$(git -C "$WORK/src" rev-parse HEAD)" = "$TG_COMMIT" ] || { echo "tag $TG_TAG moved off $TG_COMMIT"; exit 1; } cp -r "$WORK/src/proxy" "$WORK/src/utils" "$WORK/src/LICENSE" "$OUT/app/"