diff --git a/windows/.gitignore b/windows/.gitignore index 2f7896d..73b9c10 100644 --- a/windows/.gitignore +++ b/windows/.gitignore @@ -1 +1,2 @@ target/ +dist/ diff --git a/windows/Cargo.lock b/windows/Cargo.lock index 191163d..c64140b 100644 --- a/windows/Cargo.lock +++ b/windows/Cargo.lock @@ -879,7 +879,7 @@ dependencies = [ [[package]] name = "ps-core" -version = "0.1.0" +version = "0.1.0-pre.1" dependencies = [ "base64 0.22.1", "indexmap", @@ -893,7 +893,7 @@ dependencies = [ [[package]] name = "ps-ctl" -version = "0.1.0" +version = "0.1.0-pre.1" dependencies = [ "anyhow", "clap", @@ -905,7 +905,7 @@ dependencies = [ [[package]] name = "ps-ipc" -version = "0.1.0" +version = "0.1.0-pre.1" dependencies = [ "serde", "serde_json", @@ -916,7 +916,7 @@ dependencies = [ [[package]] name = "ps-service" -version = "0.1.0" +version = "0.1.0-pre.1" dependencies = [ "anyhow", "clap", diff --git a/windows/Cargo.toml b/windows/Cargo.toml index 511d68e..b4428be 100644 --- a/windows/Cargo.toml +++ b/windows/Cargo.toml @@ -3,7 +3,7 @@ resolver = "2" members = ["crates/ps-core", "crates/ps-ipc", "crates/ps-service", "crates/ps-ctl"] [workspace.package] -version = "0.1.0" +version = "0.1.0-pre.1" edition = "2024" license = "MIT" repository = "https://git.faumaray.com/faumaray/proxy-suite-flake" @@ -31,3 +31,8 @@ sha2 = "0.10" zip = { version = "2", default-features = false, features = ["deflate"] } ps-core = { path = "crates/ps-core" } ps-ipc = { path = "crates/ps-ipc" } + +[profile.release] +lto = "thin" +strip = true +codegen-units = 4 diff --git a/windows/README.md b/windows/README.md new file mode 100644 index 0000000..1268cb5 --- /dev/null +++ b/windows/README.md @@ -0,0 +1,150 @@ +# Proxy Suite for Windows + +A native Windows port of [proxy-suite-flake](https://github.com/FUFSoB/proxy-suite-flake): the +same configuration (upstream's `services.proxy-suite` options, same names and defaults), run +by a Windows service instead of NixOS and systemd, controlled with `proxy-ctl` as upstream's is. + +> [!WARNING] +> **Pre-release.** Built and tested on Linux and under Wine, **never yet run on real Windows.** +> Read [what has been verified](#what-has-been-verified) before relying on it, and please +> report what happens on yours. + +> [!IMPORTANT] +> For study and research only, as upstream: learning how proxies, tunnels and traffic +> routing work. No warranty. + +## What it does (this release) + +| | | +|---|---| +| Local proxy | SOCKS5/HTTP on `127.0.0.1:1080` through sing-box (or the hybrid sing-box + XRay backend) | +| Outbounds | links (vless/REALITY/TLS, vmess, trojan, shadowsocks, hysteria2, tuic, anytls, naive, socks, http), raw JSON, subscriptions, groups (failover, urltest, selector), chains, pins, route modes, routing rules by site, address, geodata, rule set or **program** | +| zapret | Flowseal's [zapret-discord-youtube](https://github.com/Flowseal/zapret-discord-youtube) release: `winws.exe` with any of its `general*.bat` presets, your lists, the game filter | +| zapret2 | `winws2.exe` from bol-van's [zapret-win-bundle](https://github.com/bol-van/zapret-win-bundle) with [nfqws2-keenetic](https://github.com/nfqws/nfqws2-keenetic)'s strategies and nfqws2's own learning of blocked sites | +| tg-ws-proxy | Flowseal's [tg-ws-proxy](https://github.com/Flowseal/tg-ws-proxy), the headless proxy, on a bundled Python | +| TUN | sing-box's TUN (wintun), `proxy-ctl proxy tun on` — **see the caveats** | + +Not in this release, still to come: the desktop app (GPUI, with a tray icon), AmneziaWG, +WARP, Tor, the SSH tunnel, whitelist-bypass, the kill switch, server inbounds, the XRay-only +backend, zapret2's extended detection and z2k strategies. + +## What has been verified + +| Part | How | +|---|---| +| Share-link and subscription parsing | upstream's tests, ported; ~750 links through upstream's Python and this, identical results | +| sing-box and XRay configs | rendered for many setups and accepted by `sing-box check` and `xray run -test` | +| Service, API, `proxy-ctl` | end to end on Linux: a real sing-box Shadowsocks server, the service, `proxy-ctl` adding the outbound, a page fetched through the local proxy | +| Windows build: service, named pipe, its ACL, caller identification, component hashes, supervisor | under Wine (`tools/wine-e2e.sh`) | +| zapret arguments | **every** preset of the release, both game filters, checked by the release's own `winws.exe --dry-run` under Wine (`tools/winws-dry-run.sh`) | +| zapret2 arguments | checked by `winws2.exe --dry-run` under Wine | +| tg-ws-proxy | runs and listens under Wine | + +**Not verified: anything that needs real Windows.** Traffic through sing-box on Windows (Wine +lacks an API it needs), the TUN adapter, WinDivert actually rewriting packets, the Windows +service under the Service Control Manager, `install`/`uninstall`, the system PATH change. + +## Install + +1. Unzip `ProxySuite--windows-x64.zip` anywhere. +2. Run `install.cmd` (it asks for administrator rights). It copies the program to + `C:\Program Files\ProxySuite`, registers the **ProxySuite** service (starts at boot), adds + `proxy-ctl` to PATH and starts it. +3. Open a **new** terminal: `proxy-ctl status`. + +To upgrade, unzip the new release and run its `install.cmd`. To remove, run `uninstall.cmd`; +the configuration and state stay in `%ProgramData%\ProxySuite` until you delete it. + +Windows Defender and other antivirus software may flag WinDivert (zapret's driver): it is a +packet-filtering driver, which is what it is for. Remove other DPI-bypass tools first: +Flowseal's own `zapret` service (its `service.bat` → Remove Services) or GoodbyeDPI — the +service refuses to start zapret while one runs, and says which. + +## First run + +```powershell +# A proxy: +proxy-ctl proxy outbounds add "vless://...#My Server" +proxy-ctl proxy on +proxy-ctl status +# Then point apps (or Windows: Settings → Network → Proxy) at 127.0.0.1:1080. + +# Or a subscription: +proxy-ctl proxy subs add https://provider.example/sub/token + +# zapret, Flowseal's presets: +proxy-ctl zapret on +proxy-ctl zapret preset "general (ALT9)" +proxy-ctl zapret game-filter all + +# zapret2 instead: +proxy-ctl config get > config.json # set "zapret": { "enable": true, "engine": "zapret2" } +proxy-ctl config set config.json + +# Telegram's WebSocket proxy: +proxy-ctl config get > config.json # set "tgWsProxy": { "enable": true } +proxy-ctl config set config.json +proxy-ctl logs proxy-suite-tg-ws-proxy # its tg:// link and secret + +proxy-ctl logs -f # everything, following +proxy-ctl --help +``` + +## The configuration + +`proxy-ctl config get` prints it, `proxy-ctl config set ` replaces it (checked first; a +bad one changes nothing), `proxy-ctl config validate ` only checks. It is upstream's +option tree as JSON, so [upstream's option reference](../docs/options/index.md) describes it: +`services.proxy-suite.proxy.listener.port` is `{"proxy": {"listener": {"port": …}}}`. +Options that only mean something on Linux (TProxy, fwmarks, routing tables, packages) are not +there; the Windows-only ones are documented in `crates/ps-core/src/config/`: + +- `proxy.singBox.testPort`; `proxy.systemProxy` (applied by the desktop app, which is not in this release) +- `proxy.routing.rules.*.processNames` / `processPaths` (and `name`, `priority`, `disabled`) +- `perAppRouting.profiles.*`: programs held to an outbound, by name or path +- `zapret.tcpTimestamps`, `geodata` as download locations, `tor.controlPort` +- inline `config` text where upstream only takes a file (AmneziaWG, WARP) + +## Who may change what + +The service runs as LocalSystem. Its settings are meant to be edited by whoever uses the +machine, so — **unlike upstream, where changes need root** — `userControl` defaults to on +for the signed-in user, with every scope. On a shared computer, set +`userControl.scopes` (or turn `userControl.enable` off) as an administrator. + +Whatever the scopes, these always need an administrator: anything that makes the service run +a program, pass arguments to one, or read or write a file of the caller's choosing — every +`*File` option, raw `singBoxJson`/`xrayJson`, `sshProxy.extraArgs`, `tor.extraConfig`, +`nfqwsArgs`, zapret2's `profiles` and `blobs`, `allowConfigHooks`, and `userControl` itself — +and installing components. Secrets (links, passwords) are hidden from callers without the +`secrets` scope, and survive a round trip through `config get`/`config set` unchanged. + +## Caveats + +- **TUN and zapret together**: upstream keeps zapret's packets off the TUN interface, which + this release does not do yet. Use one or the other. +- zapret2 learns with nfqws2's own detector, not upstream's extended one; z2k's strategies are + not there yet. +- The global TUN restarts the local proxy when it is turned on or off (one sing-box does both). + +## Components + +The release carries every program it runs, each pinned by version and hash +([`components.json`](components.json)), and the service checks each against the hashes +recorded at install before running it. `proxy-ctl components` lists them; +`proxy-ctl components install [name…]` fetches and verifies them again (administrator). +See [`THIRD-PARTY.md`](THIRD-PARTY.md) for their licenses and sources. + +## Building + +On Linux with Nix: `nix develop` here (this directory has its own `flake.nix`), then + +```sh +cargo test --workspace # everything, sing-box and xray from the shell +python3 -I tools/parity/links.py # link parsing against upstream's Python +tools/package.sh # the release zip, in dist/ +nix shell nixpkgs#wineWow64Packages.stable -c tools/winws-dry-run.sh dist/ProxySuite-*/components +``` + +The Windows programs are cross-compiled with `cargo xwin` (MSVC target). Design notes: +[`DESIGN.md`](DESIGN.md). diff --git a/windows/flake.nix b/windows/flake.nix index c89c260..131e050 100644 --- a/windows/flake.nix +++ b/windows/flake.nix @@ -47,6 +47,11 @@ xray python3 jq + zip + unzip + curl + git + python3Packages.pip ]; buildInputs = guiLibs; LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath guiLibs; diff --git a/windows/tools/install.cmd b/windows/tools/install.cmd new file mode 100644 index 0000000..94d3e70 --- /dev/null +++ b/windows/tools/install.cmd @@ -0,0 +1,9 @@ +@echo off +:: Proxy Suite: install, or upgrade, as an administrator. Copies the program into +:: %ProgramFiles%\ProxySuite, registers the service there and starts it. +net session >nul 2>&1 || ( + powershell -NoProfile -Command "Start-Process -Verb RunAs -FilePath '%~f0'" + exit /b +) +"%~dp0proxy-suite-service.exe" install +pause diff --git a/windows/tools/package.sh b/windows/tools/package.sh new file mode 100755 index 0000000..49dc5a4 --- /dev/null +++ b/windows/tools/package.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +# The release: ProxySuite--windows-x64.zip in dist/, with the programs, every +# component (fetched and verified), the install scripts, the README and the licenses. +# +# nix develop --command tools/package.sh (needs curl, git, unzip, zip, pip) +set -euo pipefail +cd "$(dirname "$0")/.." +VERSION=$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -1) +NAME=ProxySuite-$VERSION-windows-x64 +DIST=dist/$NAME +CACHE=${CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/proxy-suite-windows} +rm -rf "$DIST" "dist/$NAME.zip" +mkdir -p "$DIST/components" + +echo "== components" +tools/build-tg-ws-proxy.sh "$DIST/components/tg-ws-proxy" +cargo build -q --release -p ps-service +target/release/proxy-suite-service stage-components --to "$DIST/components" --cache "$CACHE" +rm -rf "$DIST"/components/*.old "$DIST"/components/*.staging + +echo "== programs" +cargo xwin build -q --release -p ps-service -p ps-ctl --target x86_64-pc-windows-msvc +cp target/x86_64-pc-windows-msvc/release/proxy-suite-service.exe target/x86_64-pc-windows-msvc/release/proxy-ctl.exe "$DIST/" +cp ../LICENSE README.md "$DIST/" +cp tools/install.cmd tools/uninstall.cmd "$DIST/" + +echo "== THIRD-PARTY.md" +{ + echo "# Third-party components" + echo + echo "Proxy Suite for Windows ($VERSION) is MIT-licensed (LICENSE). It carries, unchanged, the" + echo "programs below, each from its project's own release, pinned by version and hash in" + echo "components.json. Their licenses are theirs; where a license asks for its source, it is at" + echo "the address given." + echo + echo "| Component | Version | License | Source |" + echo "|---|---|---|---|" + jq -r '.components | to_entries[] | "| \(.key) | \(.value.version) | \(.value.license) | \(.value.homepage) |"' components.json + echo + echo "Inside them:" + echo + echo "- **WinDivert** (zapret, zapret2): LGPL-3.0 or GPL-2.0, https://github.com/basil00/WinDivert" + echo "- **cygwin1.dll** (zapret, zapret2): LGPL-3.0, https://cygwin.com" + echo "- **zapret2** (winws2.exe, its Lua) and **nfqws2-keenetic** (its strategies, lists, blobs): MIT, https://github.com/bol-van/zapret2, https://github.com/nfqws/nfqws2-keenetic" + echo "- **wintun** (inside sing-box): WireGuard's prebuilt-binary license, https://www.wintun.net" + echo "- **CPython $( sed 's/.*+py//' "$DIST/components/tg-ws-proxy/.version" )** (tg-ws-proxy): PSF License, https://www.python.org" + echo "- **Python packages** (tg-ws-proxy):" + sed -n 's/^\([A-Za-z0-9_.-]*\)==\([^ ]*\) .*/ - \1 \2, https:\/\/pypi.org\/project\/\1\/\2\//p' tools/tg-ws-proxy-requirements.txt + echo + echo "License files that came with them:" + echo + (cd "$DIST" && find components -iname 'LICENSE*' -o -iname 'COPYING*' | sort | sed 's/^/- /') +} > "$DIST/THIRD-PARTY.md" + +echo "== zip" +(cd dist && zip -qr "$NAME.zip" "$NAME") +(cd dist && sha256sum "$NAME.zip" | tee "$NAME.zip.sha256") +du -h "dist/$NAME.zip" diff --git a/windows/tools/uninstall.cmd b/windows/tools/uninstall.cmd new file mode 100644 index 0000000..281a24a --- /dev/null +++ b/windows/tools/uninstall.cmd @@ -0,0 +1,9 @@ +@echo off +:: Proxy Suite: remove the service and the program. The configuration and state stay in +:: %ProgramData%\ProxySuite. +net session >nul 2>&1 || ( + powershell -NoProfile -Command "Start-Process -Verb RunAs -FilePath '%~f0'" + exit /b +) +:: One line: cmd reads a batch file line by line, and this one is deleted meanwhile. +"%~dp0proxy-suite-service.exe" uninstall & pause & exit /b diff --git a/windows/tools/wine-install-test.sh b/windows/tools/wine-install-test.sh new file mode 100755 index 0000000..da782dc --- /dev/null +++ b/windows/tools/wine-install-test.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +# The release's install and uninstall, under Wine's Service Control Manager: copied into +# Program Files, registered, started, reachable over its pipe, removed. +# +# nix shell nixpkgs#wineWow64Packages.stable -c tools/wine-install-test.sh dist/ProxySuite--windows-x64 +set -euo pipefail +REL=$(realpath "${1:?the unpacked release directory}") +WORK=$(mktemp -d) +export WINEPREFIX=${WINEPREFIX:-$WORK/prefix} WINEDEBUG=-all +trap 'wineserver -k 2>/dev/null || true; rm -rf "$WORK"' EXIT +wineboot -i >/dev/null 2>&1 || true +for key in Tcpip Tcpip6; do + wine reg add "HKLM\\SYSTEM\\CurrentControlSet\\Services\\$key\\Parameters\\Interfaces" /f >/dev/null 2>&1 +done +cd "$REL" +echo "== install" +wine ./proxy-suite-service.exe install +PF="$WINEPREFIX/drive_c/Program Files/ProxySuite" +ls "$PF" | sed 's/^/ /' +# Wine's manager often fails the first start right after a create (real Windows does not), +# holding the service in a start that times out after ~50 s: install says so; once that is +# over, a second start works. +for _ in $(seq 40); do + st=$(wine sc query ProxySuite 2>/dev/null || true) + st=${st//$'\r'/} + echo " $(grep STATE <<<"$st" | grep -o '[A-Z_]*$' || true)" + grep -q RUNNING <<<"$st" && break + grep -q STOPPED <<<"$st" && { wine sc start ProxySuite >/dev/null 2>&1 || true; } + sleep 3 +done +echo "== status over the pipe" +for _ in $(seq 30); do wine "$PF/proxy-ctl.exe" status 2>/dev/null && break; sleep 1; done +wine "$PF/proxy-ctl.exe" components || { + echo "== service log"; tail -30 "$WINEPREFIX/drive_c/ProgramData/ProxySuite/logs/service.log"; exit 1; } +echo "== uninstall" +wine "$PF/proxy-suite-service.exe" uninstall +sleep 5 +[ ! -e "$PF/proxy-suite-service.exe" ] && echo "program files removed" || echo "program files still there"