// install.sh suite (Docker): runs the real install.sh inside a throwaway Debian // container — HOST_IP detection on simulated hosts (physical NIC only, never a // VPN) and the fresh-install / upgrade / re-run flows. `docker` is stubbed // inside the container, so nothing is started. // node install.mjs import { execFileSync } from 'node:child_process' import { copyFileSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs' import path from 'node:path' import { CS, OUT, reporter } from './lib/env.mjs' const dir = path.join(OUT, 'install') mkdirSync(dir, { recursive: true }) copyFileSync(path.join(CS, 'install.sh'), path.join(dir, 'install.sh')) copyFileSync(path.join(CS, '.env.example'), path.join(dir, '.env.example')) const fn = readFileSync(path.join(CS, 'install.sh'), 'utf8').replace(/\r/g, '').match(/^detect_host_ip\(\) \{[\s\S]*?^\}/m)[0] writeFileSync(path.join(dir, 'fn.sh'), fn + '\n') writeFileSync(path.join(dir, 'run.sh'), `set -e apt-get update -qq >/dev/null && apt-get install -y -qq openssl >/dev/null 2>&1 . /t/fn.sh mkdir -p /stub && export PATH=/stub:$PATH XENIA_SYS_NET=/tmp/sys cat > /stub/ip <<'IP' #!/bin/sh case "$*" in "route show default") echo "$DEFAULT_ROUTE" ;; "-4 -o addr show dev eth0") echo "2: eth0 inet 10.98.20.70/24 brd 10.98.20.255 scope global eth0" ;; "-4 -o addr show dev wlan0") echo "3: wlan0 inet 192.168.1.44/24 brd 192.168.1.255 scope global wlan0" ;; "-4 -o addr show dev wg0") echo "5: wg0 inet 10.9.9.2/24 scope global wg0" ;; "-4 -o addr show dev docker0") echo "4: docker0 inet 172.17.0.1/16 scope global docker0" ;; esac IP printf '#!/bin/sh\\necho "10.9.9.2 10.98.20.70 172.17.0.1"\\n' > /stub/hostname printf '#!/bin/sh\\necho "[stub docker] $*"\\n' > /stub/docker chmod +x /stub/* host() { rm -rf /tmp/sys; mkdir -p /tmp/sys; for i in docker0 wg0 lo; do mkdir -p /tmp/sys/$i; done; for i in "$@"; do mkdir -p /tmp/sys/$i/device; done; } host eth0; DEFAULT_ROUTE="default via 10.98.20.1 dev eth0"; echo "DETECT wg-full-tunnel=$(detect_host_ip)" host eth0; DEFAULT_ROUTE="default dev wg0 scope link"; echo "DETECT tunnel-owns-default=$(detect_host_ip)" host eth0; DEFAULT_ROUTE=""; echo "DETECT no-default-route=$(detect_host_ip)" host wlan0; DEFAULT_ROUTE="default via 192.168.1.1 dev wlan0"; echo "DETECT wifi-only=$(detect_host_ip)" # full install.sh flows (real ip/hostname again, docker stubbed) rm /stub/ip /stub/hostname; unset XENIA_SYS_NET cp -r /t /a && cd /a && printf 'site-123\\nkey-abc\\n\\n\\n' | bash install.sh >/dev/null echo "FRESH env=$(grep -cE '^(SITE_ID=site-123|SITE_KEY=key-abc|HOST_IP=.+)$' .env) cert=$(test -s certs/cert.pem && echo yes) keymode=$(stat -c %a certs/key.pem) conf=$(test -s nginx-proxy/nginx.conf && echo yes)" cp -r /t /b && cd /b && mkdir -p certs && cp /a/certs/* certs/ && grep -v '^HOST_IP' .env.example > .env bash install.sh >/dev/null; echo "UPGRADE hostip=$(grep -c '^HOST_IP=.\\+' .env)" bash install.sh >/dev/null; echo "RERUN hostip_lines=$(grep -c '^HOST_IP=' .env)" `) const r = reporter('install.sh (Debian container)') const out = execFileSync('docker', ['run', '--rm', '-v', `${dir}:/t:ro`, 'debian:bookworm-slim', 'bash', '/t/run.sh'], { encoding: 'utf8', env: { ...process.env, MSYS_NO_PATHCONV: '1' } }) const val = key => out.match(new RegExp(`${key}=(\\S*)`))?.[1] r.check('HOST_IP: WireGuard full tunnel → physical NIC', val('DETECT wg-full-tunnel') === '10.98.20.70') r.check('HOST_IP: tunnel owns the default route → physical NIC', val('DETECT tunnel-owns-default') === '10.98.20.70') r.check('HOST_IP: no default route → physical NIC', val('DETECT no-default-route') === '10.98.20.70') r.check('HOST_IP: WiFi-only server', val('DETECT wifi-only') === '192.168.1.44') r.check('fresh install: .env filled (SITE_ID, SITE_KEY, HOST_IP)', val('env') === '3') r.check('fresh install: self-signed cert, key 600, proxy config written', val('cert') === 'yes' && val('keymode') === '600' && val('conf') === 'yes') r.check('upgrade: missing HOST_IP backfilled', val('UPGRADE hostip') === '1') r.check('re-run: HOST_IP not duplicated', val('RERUN hostip_lines') === '1') // the proxy config written by install.sh must be byte-identical to the repo copy (rule CS-5) const heredoc = readFileSync(path.join(CS, 'install.sh'), 'utf8').replace(/\r/g, '').match(/<< 'EOF'\n([\s\S]*?)\nEOF\n/)[1] r.check('install.sh proxy config == nginx-proxy/nginx.conf', heredoc === readFileSync(path.join(CS, 'nginx-proxy', 'nginx.conf'), 'utf8').replace(/\r/g, '').replace(/\n$/, '')) process.exit(r.done() ? 1 : 0)