Files
proxy-suite-flake/windows/tools/wine-e2e.sh
T
2026-10-26 23:35:15 +03:00

75 lines
4.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# The Windows build, end to end, under Wine: proxy-suite-service.exe supervising the real
# sing-box.exe, proxy-ctl.exe adding an outbound over the named pipe, a page fetched through
# the local proxy from a Shadowsocks server on the Linux side.
#
# nix develop --command nix shell nixpkgs#wineWow64Packages.stable -c tools/wine-e2e.sh STAGE_DIR
#
# STAGE_DIR: components staged by `proxy-suite-service stage-components` (needs sing-box).
#
# Wine 11 lacks iphlpapi!InitializeIpInterfaceEntry, which sing-box calls as it starts: the
# test then stops there, having checked everything of ours up to it.
set -euo pipefail
cd "$(dirname "$0")/.."
STAGE=${1:?staged components directory}
WORK=$(mktemp -d)
export WINEPREFIX=${WINEPREFIX:-$WORK/prefix} WINEDEBUG=-all
trap 'wineserver -k 2>/dev/null || true; kill $(jobs -p) 2>/dev/null || true; rm -rf "$WORK"' EXIT
cargo xwin build -q -p ps-service -p ps-ctl --target x86_64-pc-windows-msvc
E=target/x86_64-pc-windows-msvc/debug
port() { python3 -c 'import socket;s=socket.socket();s.bind(("127.0.0.1",0));print(s.getsockname()[1])'; }
SS=$(port) WEB=$(port) PROXY=$(port) CLASH=$(port) TEST=$(port)
KEY=$(python3 -c "import base64;print(base64.b64encode(b'k'*16).decode())")
# The web server and the Shadowsocks server, on Linux, which knows web.test as loopback.
mkdir -p "$WORK/www" && echo -n hello-through-windows > "$WORK/www/index.html"
python3 -m http.server -b 127.0.0.1 -d "$WORK/www" "$WEB" >/dev/null 2>&1 &
cat > "$WORK/server.json" <<JSON
{"log":{"level":"warn"},
"dns":{"servers":[{"type":"hosts","tag":"hosts","predefined":{"web.test":["127.0.0.1"]}}]},
"inbounds":[{"type":"shadowsocks","listen":"127.0.0.1","listen_port":$SS,"method":"2022-blake3-aes-128-gcm","password":"$KEY"}],
"outbounds":[{"type":"direct","tag":"direct"}],
"route":{"default_domain_resolver":"hosts","final":"direct"}}
JSON
sing-box run -c "$WORK/server.json" >/dev/null 2>&1 &
PIPE='\\.\pipe\pse2e'
ROOT="$WINEPREFIX/drive_c/pse2e"
ctl() { wine "$E/proxy-ctl.exe" --socket "$PIPE" "$@"; }
start() {
wine "$E/proxy-suite-service.exe" --console --state-dir 'C:\pse2e' --socket "$PIPE" >>"$WORK/service.log" 2>&1 &
for _ in $(seq 40); do ctl status >/dev/null 2>&1 && return; sleep 0.5; done
echo "the service did not come up"; cat "$WORK/service.log"; exit 1
}
# The service makes its own state directory (it refuses one someone else made), then gets
# sing-box as the component manager installs it, and a configuration.
start
wineserver -k; sleep 1
# What Windows has and a fresh Wine prefix lacks: sing-box's network monitor reads it.
for key in Tcpip Tcpip6; do
wine reg add "HKLM\\SYSTEM\\CurrentControlSet\\Services\\$key\\Parameters\\Interfaces" /f >/dev/null 2>&1
done
wineserver -w
cp -r "$STAGE/sing-box" "$ROOT/bin/"
cat > "$ROOT/config.json" <<JSON
{"proxy":{"enable":true,"listener":{"port":$PROXY},"singBox":{"clashApiPort":$CLASH,"testPort":$TEST},"routing":{"directRu":false}}}
JSON
start
ctl proxy outbounds add "ss://2022-blake3-aes-128-gcm:${KEY//=/%3D}@127.0.0.1:$SS#Wine%20Test"
for _ in $(seq 60); do ctl status 2>/dev/null | grep -q 'proxy: active' && break; sleep 0.5; done
ctl status
BODY=$(curl -s --max-time 10 --retry 10 --retry-delay 1 --retry-all-errors --socks5-hostname "127.0.0.1:$PROXY" "http://web.test:$WEB/" || true)
if [ "$BODY" = hello-through-windows ]; then
echo "OK: a page through proxy-suite-service.exe and sing-box.exe under Wine"
elif LOGS=$(ctl logs -n 400 2>/dev/null) && [[ $LOGS == *"InitializeIpInterfaceEntry procedure in iphlpapi.dll"* ]]; then
# Wine's iphlpapi lacks what sing-box's network monitor calls at start, TUN or not:
# everything up to sing-box itself (the service, its pipe, the component's hash, the
# rendered and checked config, the supervisor) worked; proxying needs real Windows.
echo "OK up to sing-box: Wine lacks iphlpapi!InitializeIpInterfaceEntry, which sing-box needs to run"
exit 0
else
echo "FAIL: got '$BODY'"; ctl logs -n 400 > "$WORK/units.log" 2>&1 || true; grep -m1 -B2 -A12 "panic\|FATAL" "$WORK/units.log" || tail -40 "$WORK/units.log"; exit 1
fi