A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
feat(installer): rank download sources and publish signed installers
RoversX80e336adec77
diff --git a/.github/workflows/installer.yml b/.github/workflows/installer.ymlnew file mode 100644index 00000000..8169be58--- /dev/null+++ b/.github/workflows/installer.yml@@ -0,0 +1,50 @@+name: publish installer++on:+ push:+ branches: [main]+ paths:+ - install.sh+ - assets/release-signing.pub+ - ci/sign-installer.sh+ - .github/workflows/installer.yml+ workflow_dispatch:++concurrency:+ group: signed-installer-v1+ cancel-in-progress: false++permissions:+ contents: read++jobs:+ publish:+ if: github.ref == 'refs/heads/main'+ runs-on: ubuntu-latest+ timeout-minutes: 10+ permissions:+ contents: write+ steps:+ - uses: actions/checkout@v7+ with:+ persist-credentials: false+ - name: Test installer+ run: python3 ci/test-remote-update.py -v+ - name: Sign installer+ env:+ THINKTERM_RELEASE_SIGNING_KEY: ${{ secrets.THINKTERM_RELEASE_SIGNING_KEY }}+ run: bash ci/sign-installer.sh "$RUNNER_TEMP/signed-installer"+ - name: Publish signed installer+ env:+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}+ run: |+ set -euo pipefail+ if ! gh release view installer-v1 >/dev/null 2>&1; then+ gh release create installer-v1 --prerelease --target "$GITHUB_SHA" \+ --title 'Signed installer v1' \+ --notes 'Signed installer channel for SSH installs; not an application release.'+ fi+ [ "$(gh release view installer-v1 --json isPrerelease --jq .isPrerelease)" = true ]+ gh release upload --clobber installer-v1 \+ "$RUNNER_TEMP/signed-installer/install.sh" \+ "$RUNNER_TEMP/signed-installer/install.sh.sig"diff --git a/ci/sign-installer.sh b/ci/sign-installer.shnew file mode 100644index 00000000..55a0299c--- /dev/null+++ b/ci/sign-installer.sh@@ -0,0 +1,22 @@+#!/bin/bash+# Use only the CI release-signing secret, without shell tracing.+set +x+set -euo pipefail+umask 077+dest=${1:?usage: sign-installer.sh OUTPUT_DIR}+: "${THINKTERM_RELEASE_SIGNING_KEY:?release signing secret is not configured}"+work=$(mktemp -d)+trap 'rm -rf "$work"' EXIT+trap 'exit 130' INT+trap 'exit 143' TERM+sh -n install.sh+[ "$(sed -n '2p' install.sh)" = '# thinkterm-installer-v1' ]+cp install.sh "$work/install.sh"+printf '%s\n' "$THINKTERM_RELEASE_SIGNING_KEY" > "$work/key"+unset THINKTERM_RELEASE_SIGNING_KEY+ssh-keygen -Y sign -f "$work/key" -n thinkterm-installer-v1 "$work/install.sh"+printf 'thinkterm-installer namespaces="thinkterm-installer-v1" %s\n' "$(cat assets/release-signing.pub)" > "$work/signers"+ssh-keygen -Y verify -f "$work/signers" -I thinkterm-installer -n thinkterm-installer-v1 \+ -s "$work/install.sh.sig" < "$work/install.sh"+mkdir -p "$dest"+mv "$work/install.sh" "$work/install.sh.sig" "$dest/"diff --git a/ci/test-remote-update.py b/ci/test-remote-update.pynew file mode 100644index 00000000..907e2ea7--- /dev/null+++ b/ci/test-remote-update.py@@ -0,0 +1,405 @@+#!/usr/bin/env python3+"""Offline installer/CI integration tests; all writes use isolated temp dirs."""++import hashlib+import io+import json+import os+from pathlib import Path+import shutil+import subprocess+import sys+import tarfile+import tempfile+import unittest+import zipfile++ROOT = Path(__file__).resolve().parent.parent+TAG = "9.9.9"+GZIP = f"thinkterm-server-{TAG}-linux-x86_64.tar.gz"+XZ = GZIP[:-2] + "xz"++FAKE_CURL = r'''+import json, os, sys, time+from pathlib import Path+from urllib.parse import urlparse+args = sys.argv[1:]+root = Path(os.environ["TEST_ROOT"])+config = json.loads((root / "network.json").read_text())+url = args[-1]+host = urlparse(url).hostname+name = url.rsplit("/", 1)[-1]+dest = Path(args[args.index("-o") + 1])+speed_limit = int(args[args.index("--speed-limit") + 1]) if "--speed-limit" in args else 0+with (root / "requests" / f"{time.time_ns()}-{os.getpid()}").open("w") as log:+ log.write(json.dumps({"host": host, "name": name, "probe": "--range" in args,+ "resume": "-C" in args, "speed_limit": speed_limit,+ "auth": any("Authorization:" in x for x in args)}) + "\n")+if host == "api.github.com":+ if config.get("api_failure", False):+ print("403", end="")+ sys.exit(22)+ dest.write_bytes((root / "release.json").read_bytes())+ print("200", end="")+ sys.exit(0)+if name in ("thinkterm-release.txt", "thinkterm-release.txt.sig"):+ if config.get("no_metadata", False) or (config.get("metadata_mirror_only") and host == "github.com"):+ sys.exit(22)+ dest.write_bytes((root / name).read_bytes())+ sys.exit(0)+if "--range" in args:+ rate = config.get("rates", {}).get(host, 10)+ dest.write_bytes(b"probe")+ print("206", rate, "1")+ sys.exit(0)+if host in config.get("fail_hosts", []):+ sys.exit(22)+if host in config.get("timeout_hosts", []):+ sys.exit(28)+data = (root / name).read_bytes()+if host in config.get("bad_hosts", []):+ data = b"tampered archive"+counter = root / ("count-" + host)+attempt = int(counter.read_text()) if counter.exists() else 0+counter.write_text(str(attempt + 1))+if speed_limit > config.get("transfer_rates", {}).get(host, speed_limit):+ dest.write_bytes(data[:20])+ sys.exit(28)+if config.get("resume") and attempt == 0:+ dest.write_bytes(data[:20])+ sys.exit(18)+if config.get("no_range") and attempt == 1 and dest.exists() and dest.stat().st_size:+ sys.exit(33)+if config.get("slow"):+ dest.write_bytes(data[:20])+ time.sleep(2.4)+if "-C" in args and dest.exists():+ offset = dest.stat().st_size+ assert dest.read_bytes() == data[:offset], "resume prefix corrupted"+ with dest.open("ab") as output:+ output.write(data[offset:])+else:+ dest.write_bytes(data)+'''+++class InstallerTests(unittest.TestCase):+ def setUp(self):+ self.temp = tempfile.TemporaryDirectory(prefix="thinkterm-update-test-")+ self.addCleanup(self.temp.cleanup)+ self.root = Path(self.temp.name)+ self.bin = self.root / "tools"+ self.bin.mkdir()+ self.home = self.root / "home"+ self.home.mkdir()+ (self.root / "tmp").mkdir()+ (self.root / "requests").mkdir()+ for name in ("awk", "basename", "cat", "chmod", "cp", "cut", "date", "dirname", "find",+ "grep", "head", "id", "ln", "mkdir", "mktemp", "mv", "rm", "sed", "sh",+ "sort", "stty", "tar", "tr", "wc", "xz", "sha256sum", "shasum", "ssh-keygen", "sleep", "ditto"):+ tool = shutil.which(name)+ if tool:+ (self.bin / name).symlink_to(tool)+ self.tool("uname", '#!/bin/sh\ncase "$1" in -s) echo "${TEST_OS:-Linux}";; *) echo x86_64;; esac\n')+ self.tool("getconf", '#!/bin/sh\necho "glibc ${TEST_GLIBC:-2.35}"\n')+ self.tool("pgrep", "#!/bin/sh\nexit 1\n")+ self.tool("curl", f"#!{sys.executable}\n" + FAKE_CURL)+ self.script = self.root / "install.sh"+ self.script.write_bytes((ROOT / "install.sh").read_bytes())+ self.env = dict(os.environ, HOME=str(self.home), TMPDIR=str(self.root / "tmp"),+ PATH=str(self.bin), TEST_ROOT=str(self.root), TERM="dumb",+ THINKTERM_INSTALL_PROGRESS="1")+ for name in ("GITHUB_TOKEN", "THINKTERM_UPDATE_TOKEN", "BASH_ENV", "ENV"):+ self.env.pop(name, None)+ self.network = {}+ self.packages = []+ self.package(GZIP)+ self.write_api()+ self.key = None++ def tool(self, name, text):+ path = self.bin / name+ if path.is_symlink():+ path.unlink()+ path.write_text(text)+ path.chmod(0o755)++ def package(self, name):+ path = self.root / name+ with tarfile.open(path, "w:xz" if name.endswith(".xz") else "w:gz") as archive:+ entry = tarfile.TarInfo("thinkterm-example/bin/thinkterm")+ entry.mode = 0o755+ data = b"#!/bin/sh\nexit 0\n"+ entry.size = len(data)+ archive.addfile(entry, io.BytesIO(data))+ self.packages.append(name)+ return path++ def write_api(self, missing_digest=False):+ assets = []+ for name in self.packages:+ data = (self.root / name).read_bytes()+ assets.append(dict(name=name, size=len(data),+ digest=None if missing_digest else "sha256:" + hashlib.sha256(data).hexdigest()))+ (self.root / "release.json").write_text(json.dumps(dict(tag_name=TAG, assets=assets), indent=2))++ def sign_metadata(self, tag=TAG):+ keygen = shutil.which("ssh-keygen")+ self.assertIsNotNone(keygen)+ self.key = self.root / "test-key"+ subprocess.run([keygen, "-q", "-t", "ed25519", "-N", "", "-C", "example", "-f", str(self.key)], check=True)+ public = self.key.with_suffix(".pub").read_text().strip()+ pinned = (ROOT / "assets/release-signing.pub").read_text().strip()+ self.script.write_text(self.script.read_text().replace(pinned, public))+ rows = ["thinkterm-release-v1", f"version {tag}"]+ for name in self.packages:+ data = (self.root / name).read_bytes()+ rows.append(f"{hashlib.sha256(data).hexdigest()} {len(data)} {name}")+ info = self.root / "thinkterm-release.txt"+ info.write_text("\n".join(rows) + "\n")+ subprocess.run([keygen, "-Y", "sign", "-f", str(self.key), "-n", "thinkterm-release", str(info)],+ check=True, capture_output=True)++ def run_install(self, *args, success=True):+ (self.root / "network.json").write_text(json.dumps(self.network))+ result = subprocess.run(["/bin/sh", str(self.script), "--server", "--version", TAG, *args],+ env=self.env, capture_output=True, text=True, timeout=35)+ if success:+ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)+ else:+ self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr)+ self.assertFalse((self.home / ".local/bin/thinkterm").exists())+ self.assertEqual(list((self.root / "tmp").iterdir()), [], "temporary files leaked")+ return result.stdout + result.stderr++ def requests(self):+ path = self.root / "requests"+ return [json.loads(entry.read_text()) for entry in sorted(path.iterdir())]++ def test_fast_github_never_contacts_mirrors(self):+ self.network["rates"] = {"github.com": 900000}+ output = self.run_install()+ self.assertIn("█", output)+ self.assertNotIn("\x1b", output)+ self.assertTrue(all(r["host"] in ("api.github.com", "github.com") for r in self.requests()))++ def test_slow_github_ranks_mirrors_and_rejects_fake_package(self):+ self.network.update(rates={"ghfast.top": 900000, "gh-proxy.org": 800000}, bad_hosts=["ghfast.top"])+ output = self.run_install()+ self.assertIn("mismatch", output)+ self.assertIn("from gh-proxy.org", output)+ self.assertEqual(len([r for r in self.requests() if r["probe"]]), 5)++ def slow_mirrors(self):+ mirrors = [f"mirror-{name}.example" for name in ("a", "b", "c", "d")]+ self.script.write_text("\n".join(+ f'mirrors="{" ".join(mirrors)}"' if line.startswith("mirrors=") else line+ for line in self.script.read_text().splitlines()) + "\n")+ self.network.update(rates={host: 900000 - n * 100000 for n, host in enumerate(mirrors)},+ transfer_rates={host: 131072 for host in mirrors},+ fail_hosts=["github.com"])+ return mirrors++ def test_fast_probes_slow_downloads_retry_fastest_mirror(self):+ mirrors = self.slow_mirrors()+ self.run_install()+ downloads = [r for r in self.requests() if r["name"] == GZIP and not r["probe"]]+ self.assertEqual([r["host"] for r in downloads], mirrors + ["github.com", mirrors[0]])+ self.assertEqual(downloads[-1]["speed_limit"], 1)+ self.assertTrue((self.home / ".local/bin/thinkterm").exists())++ def test_slow_fallback_still_rejects_corrupt_mirror(self):+ mirrors = self.slow_mirrors()+ self.network["bad_hosts"] = [mirrors[0]]+ output = self.run_install()+ self.assertIn("mismatch", output)+ downloads = [r for r in self.requests() if r["name"] == GZIP and not r["probe"]]+ self.assertEqual([r["host"] for r in downloads], mirrors + ["github.com", *mirrors[:2]])+ self.assertEqual([r["speed_limit"] for r in downloads[-2:]], [1, 1])++ def test_http_failures_do_not_get_slow_download_retry(self):+ mirrors = self.slow_mirrors()+ self.network["fail_hosts"].extend(mirrors)+ self.run_install(success=False)+ downloads = [r for r in self.requests() if r["name"] == GZIP and not r["probe"]]+ self.assertEqual([r["host"] for r in downloads], mirrors + ["github.com"])++ def test_connection_timeouts_do_not_get_slow_download_retry(self):+ mirrors = self.slow_mirrors()+ self.network["timeout_hosts"] = mirrors+ self.run_install(success=False)+ downloads = [r for r in self.requests() if r["name"] == GZIP and not r["probe"]]+ self.assertEqual([r["host"] for r in downloads], mirrors + ["github.com"])++ def test_explicit_mirror_does_not_add_slow_fallback(self):+ mirrors = self.slow_mirrors()+ self.run_install("--mirror", mirrors[0], success=False)+ downloads = [r for r in self.requests() if r["name"] == GZIP]+ self.assertEqual([r["host"] for r in downloads], [mirrors[0], "github.com"])+ self.assertFalse(any(r["probe"] for r in downloads))++ def test_signed_metadata_fallback_and_xz(self):+ self.package(XZ)+ self.sign_metadata()+ self.network.update(api_failure=True, metadata_mirror_only=True)+ self.run_install()+ self.assertTrue(any(r["name"] == XZ for r in self.requests()))++ def test_old_openssh_uses_github_only(self):+ self.sign_metadata()+ self.tool("ssh-keygen", "#!/bin/sh\necho 'unknown option -- Y' >&2\nexit 1\n")+ self.network["api_failure"] = True+ output = self.run_install()+ self.assertIn("does not support", output)+ self.assertTrue(all(r["host"] in ("api.github.com", "github.com") for r in self.requests()))++ def test_no_verifier_and_no_xz_keep_github_gzip(self):+ for tool in ("ssh-keygen", "xz"):+ (self.bin / tool).unlink(missing_ok=True)+ self.network["api_failure"] = True+ self.run_install()+ self.assertTrue(all(r["host"] in ("api.github.com", "github.com") for r in self.requests()))+ self.assertTrue(any(r["name"] == GZIP for r in self.requests()))++ def test_xz_absent_chooses_gzip_even_when_release_has_xz(self):+ self.package(XZ)+ self.write_api()+ (self.bin / "xz").unlink(missing_ok=True)+ self.network["rates"] = {"github.com": 900000}+ self.run_install()+ self.assertFalse(any(r["name"] == XZ for r in self.requests()))++ def test_tampered_metadata_is_rejected(self):+ self.sign_metadata()+ info = self.root / "thinkterm-release.txt"+ info.write_bytes(info.read_bytes() + b"tampered\n")+ self.network["api_failure"] = True+ self.assertIn("verification failed", self.run_install(success=False))++ def test_signed_wrong_version_is_rejected(self):+ self.sign_metadata(tag="9.9.8")+ self.network["api_failure"] = True+ self.assertIn("different version", self.run_install(success=False))++ def test_tampered_signature_is_rejected(self):+ self.sign_metadata()+ (self.root / "thinkterm-release.txt.sig").write_text("invalid signature\n")+ self.network["api_failure"] = True+ self.assertIn("verification failed", self.run_install(success=False))++ def test_missing_old_release_metadata_falls_back_without_mirrors(self):+ self.network.update(api_failure=True, no_metadata=True)+ output = self.run_install()+ self.assertIn("without mirrors", output)+ archives = [r for r in self.requests() if r["name"] == GZIP]+ self.assertTrue(all(r["host"] == "github.com" for r in archives))++ def test_glibc_floor_uses_compatible_package(self):+ name = GZIP.replace(".tar.gz", "-glibc2.31.tar.gz")+ self.package(name)+ self.write_api()+ self.env["TEST_GLIBC"] = "2.31"+ self.network["rates"] = {"github.com": 900000}+ self.run_install()+ self.assertTrue(any(r["name"] == name for r in self.requests()))++ @unittest.skipUnless(sys.platform == "darwin", "macOS ditto required")+ def test_macos_server_from_install_stays_inside_isolated_home(self):+ self.env["TEST_OS"] = "Darwin"+ archive = self.root / f"ThinkTerm-macos-x86_64-{TAG}.zip"+ with zipfile.ZipFile(archive, "w") as output:+ entry = zipfile.ZipInfo("ThinkTerm.app/Contents/MacOS/thinkterm")+ entry.create_system = 3+ entry.external_attr = 0o100755 << 16+ output.writestr(entry, b"#!/bin/sh\nexit 0\n")+ digest = hashlib.sha256(archive.read_bytes()).hexdigest()+ self.run_install("--from", str(archive), "--sha256", digest)+ self.assertTrue((self.home / ".local/libexec/ThinkTerm.app").is_dir())+ self.assertTrue((self.home / ".local/bin/thinkterm").is_symlink())++ def test_missing_digest_never_uses_mirror(self):+ self.write_api(missing_digest=True)+ self.run_install()+ self.assertTrue(all(r["host"] in ("api.github.com", "github.com") for r in self.requests()))++ def test_broken_transfer_resumes(self):+ self.network["resume"] = True+ self.run_install("--mirror", "direct")+ downloads = [r for r in self.requests() if r["name"] == GZIP]+ self.assertEqual(len(downloads), 2)+ self.assertTrue(all(r["resume"] for r in downloads))++ def test_range_refusal_restarts_cleanly(self):+ self.network.update(resume=True, no_range=True)+ self.run_install("--mirror", "direct")+ self.assertEqual(len([r for r in self.requests() if r["name"] == GZIP]), 3)++ def test_non_tty_progress_reports_speed(self):+ self.network["slow"] = True+ output = self.run_install("--mirror", "direct")+ self.assertIn("KB/s", output)+ self.assertIn("% of", output)++ def test_from_checksum_passes_and_rejects_corruption(self):+ data = (self.root / GZIP).read_bytes()+ digest = hashlib.sha256(data).hexdigest()+ self.run_install("--from", str(self.root / GZIP), "--sha256", "0" * 64, success=False)+ self.run_install("--from", str(self.root / GZIP), "--sha256", digest)+ self.assertEqual(self.requests(), [])++ def test_trusted_client_metadata_skips_api_and_checks_version(self):+ self.sign_metadata()+ info = self.root / "thinkterm-release.txt"+ self.network["rates"] = {"github.com": 900000}+ self.run_install("--release-info", str(info))+ self.assertFalse(any(r["host"] == "api.github.com" for r in self.requests()))++ def test_installer_signer_uses_separate_namespace_and_pinned_key(self):+ self.sign_metadata()+ repo = self.root / "installer-ci"+ (repo / "assets").mkdir(parents=True)+ shutil.copyfile(self.key.with_suffix(".pub"), repo / "assets/release-signing.pub")+ shutil.copyfile(ROOT / "install.sh", repo / "install.sh")+ env = dict(os.environ, THINKTERM_RELEASE_SIGNING_KEY=self.key.read_text())+ command = ["bash", str(ROOT / "ci/sign-installer.sh"), "out"]+ result = subprocess.run(command, cwd=repo, env=env, capture_output=True)+ self.assertEqual(result.returncode, 0, result.stderr)+ self.assertEqual((repo / "out/install.sh").read_bytes(), (ROOT / "install.sh").read_bytes())+ signers = repo / "signers"+ signers.write_text('example namespaces="thinkterm-installer-v1" ' + self.key.with_suffix(".pub").read_text())+ result = subprocess.run([shutil.which("ssh-keygen"), "-Y", "verify", "-f", str(signers),+ "-I", "example", "-n", "thinkterm-installer-v1", "-s", "out/install.sh.sig"],+ cwd=repo, input=(repo / "out/install.sh").read_bytes(), capture_output=True)+ self.assertEqual(result.returncode, 0, result.stderr)+ shutil.copyfile(ROOT / "assets/release-signing.pub", repo / "assets/release-signing.pub")+ self.assertNotEqual(subprocess.run(command, cwd=repo, env=env, capture_output=True).returncode, 0)++ def test_ci_signer_merges_existing_packages_and_checks_its_key(self):+ self.sign_metadata()+ repo = self.root / "ci-work"+ (repo / "ci").mkdir(parents=True)+ (repo / "assets").mkdir()+ for name in ("sign-release.sh", "release-manifest.py"):+ shutil.copyfile(ROOT / "ci" / name, repo / "ci" / name)+ shutil.copyfile(self.key.with_suffix(".pub"), repo / "assets/release-signing.pub")+ dist, previous = repo / "dist", repo / "previous"+ dist.mkdir(); previous.mkdir()+ (dist / "example-new.tar.gz").write_bytes(b"new package")+ (previous / "example-old.tar.gz").write_bytes(b"old package")+ (previous / "example-new.tar.gz").write_bytes(b"replaced package")+ env = dict(os.environ, THINKTERM_RELEASE_SIGNING_KEY=self.key.read_text())+ result = subprocess.run(["bash", "ci/sign-release.sh", TAG, "dist", "previous"],+ cwd=repo, env=env, capture_output=True, text=True)+ self.assertEqual(result.returncode, 0, result.stderr)+ info = (dist / "thinkterm-release.txt").read_text()+ self.assertIn("example-old.tar.gz", info)+ self.assertIn(hashlib.sha256(b"new package").hexdigest(), info)+ self.assertNotIn(hashlib.sha256(b"replaced package").hexdigest(), info)+ shutil.copyfile(ROOT / "assets/release-signing.pub", repo / "assets/release-signing.pub")+ result = subprocess.run(["bash", "ci/sign-release.sh", TAG, "dist", "previous"],+ cwd=repo, env=env, capture_output=True)+ self.assertNotEqual(result.returncode, 0)+++if __name__ == "__main__":+ unittest.main()diff --git a/install.sh b/install.shindex 1a2dd529..59b5debc 100755--- a/install.sh+++ b/install.sh@@ -1,10 +1,11 @@#!/bin/sh+# thinkterm-installer-v1# ThinkTerm installer for Linux and macOS.## curl -fsSL https://raw.githubusercontent.com/RoversX/thinkterm/main/install.sh | sh#-# Downloads the release for this machine, verifies it against the digest-# GitHub recorded at upload, and installs it for the current user -- no root,+# Downloads the release for this machine, verifies it against GitHub's digest+# or the signed release inventory, and installs it for the current user -- no root,# no package manager. Linux gets a tarball unpacked under ~/.local; macOS gets# the signed ThinkTerm.app plus command-line links under ~/.local/bin. The two# variants are mutually exclusive: installing one removes the other's files.@@ -20,10 +21,10 @@# (or ~/Applications when that is not writable).# --from FILE Use an already-downloaded tarball or zip; nothing is# fetched.-# --mirror M Where the release archive comes from: auto (GitHub,-# then each known mirror while it fails or crawls, then-# GitHub again; the default), direct (GitHub only), or a-# mirror's host such as ghfast.top (it, then GitHub).+# --sha256 HASH Verify a --from archive against this SHA-256.+# --mirror M auto tries GitHub, then ranks mirrors if GitHub is slow;+# direct uses only GitHub; a mirror host tries that host,+# then GitHub. Mirrors require a trusted checksum.# --dry-run Say what would be done and stop before touching the disk.## GITHUB_TOKEN in the environment is sent with every request. It is what lets@@ -55,8 +56,8 @@ min_glibc_desktop="2.35" # the GUI's alone; the rest need min_glibc_server_mai# beside the desktop's, which links the host's OpenSSL 3; an older host has# only OpenSSL 1.1 and gets the one made on Ubuntu 20.04 with OpenSSL built in.min_glibc_server_main="2.34"-# Hosts that proxy github.com downloads, for --mirror auto, in the order-# tried. thinkterm-update carries the same list for Settings.+# Hosts probed in parallel when GitHub is slow under --mirror auto.+# thinkterm-update carries the same list for Settings.mirrors="ghfast.top gh-proxy.org ghproxy.net gh.llkk.cc"# Bytes a second under which a source that is not the last one is given up.min_rate=307200@@ -67,6 +68,8 @@ version=""prefix="${HOME}/.local"app_dir_opt=""from=""+expected_sha256=""+release_info=""mirror="auto"dry_run=""old_glibc=""@@ -87,12 +90,11 @@ Install ThinkTerm on Linux or macOS.(or ~/Applications when that is not writable).--from FILE Use an already-downloaded tarball or zip; nothing isfetched.- --mirror M Where the release archive comes from: auto (GitHub, then- ghfast.top gh-proxy.org ghproxy.net gh.llkk.cc while it- fails or crawls, then GitHub again; the default), direct- (GitHub only), or one mirror's host (it, then GitHub).- Whatever serves it, the archive must match the SHA-256- GitHub recorded.+ --sha256 HASH Verify a --from archive against this SHA-256.+ --mirror M auto (default) tries GitHub, then probes mirrors in parallel+ if it is slow: ghfast.top gh-proxy.org ghproxy.net gh.llkk.cc.+ direct uses only GitHub; a mirror host tries it, then GitHub.+ Mirrors require a SHA-256 from GitHub or signed metadata.--dry-run Say what would be done and stop before touching the disk.EOF}@@ -230,7 +232,7 @@ with_spinner() {# half blocks, shaded lavender to blue from left to right. Draws the first# $1 letters with a cursor after them when $2 is 1.wordmark() {- awk -v shown="$1" -v cursor="$2" -v palette="$palette" -v color="$ui_color" -v esc="$esc" '+ awk -v shown="$1" -v cursor="$2" -v palette="$palette" -v color="$ui_color" -v esc="$esc" -v clear="$tty_out" 'function shade(x, f, r, g, b) {if (!color) return ""f = (x - 1) / (width - 1)@@ -270,7 +272,8 @@ wordmark() {else out = out " "}sub(/ +$/, "", out)- printf " %s%s[K\n", out, esc+ if (clear) printf " %s%s[K\n", out, esc+ else printf " %s\n", out}}'}@@ -278,6 +281,10 @@ wordmark() {# The wordmark types itself out on a terminal that can redraw; elsewhere it# is printed once, or, without UTF-8 or room, the name in plain text.banner() {+ if [ "${THINKTERM_INSTALL_PROGRESS:-}" = 1 ] && [ -z "$tty_out" ]; then+ say; wordmark 9 0; say ' installer'; say+ return+ fiif [ -z "$tty_out" ] || [ -z "$unicode" ] || [ "$cols" -lt 44 ]; thenprintf '\n %sThinkTerm%s %sinstaller%s\n\n' "$ui_bold" "$ui_reset" "$ui_dim" "$ui_reset"return@@ -311,6 +318,10 @@ while [ $# -gt 0 ]; do--app-dir=*) app_dir_opt="${1#--app-dir=}" ;;--from) [ $# -ge 2 ] || die "--from needs a value"; from="$2"; shift ;;--from=*) from="${1#--from=}" ;;+ --sha256) [ $# -ge 2 ] || die "--sha256 needs a value"; expected_sha256="$2"; shift ;;+ --sha256=*) expected_sha256="${1#--sha256=}" ;;+ # Trusted metadata delivered by the client over its authenticated SSH connection.+ --release-info) [ $# -ge 2 ] || die "--release-info needs a file"; release_info="$2"; shift ;;--mirror) [ $# -ge 2 ] || die "--mirror needs a value"; mirror="$2"; shift ;;--mirror=*) mirror="${1#--mirror=}" ;;--dry-run) dry_run=1 ;;@@ -321,13 +332,18 @@ while [ $# -gt 0 ]; dodone[ -n "$prefix" ] || die "--prefix needs a directory"+case "$tag" in *[!A-Za-z0-9._+-]*) die "invalid release version" ;; esac+if [ -n "$expected_sha256" ]; then+ [ -n "$from" ] || die "--sha256 requires --from"+ case "$expected_sha256" in *[!0-9a-f]*) die "invalid SHA-256" ;; esac+ [ "${#expected_sha256}" -eq 64 ] || die "invalid SHA-256"+fitag_requested=""[ -z "$tag" ] || tag_requested=1# Where the archive is fetched from, in order: "github" or a mirror's host.case "$mirror" indirect) sources="github" ;;- # GitHub again last, with no hurry: a link slow everywhere ends there.- auto) sources="github $mirrors github" ;;+ auto) sources="github $mirrors" ;;*)mirror="${mirror#https://}"; mirror="${mirror%/}"case "$mirror" in@@ -459,13 +475,14 @@ elsesha256() { return 1; }fi-fetch() {+fetch() (+ ulimit -f 2048 || exit 1if [ -n "${GITHUB_TOKEN:-}" ]; then- curl -fsSL --retry 3 -H "Authorization: Bearer $GITHUB_TOKEN" "$@"+ exec curl --disable -fsSL --proto =https --proto-redir =https --connect-timeout 3 --max-time 8 --retry 0 -H "Authorization: Bearer $GITHUB_TOKEN" "$@"else- curl -fsSL --retry 3 "$@"+ exec curl --disable -fsSL --proto =https --proto-redir =https --connect-timeout 3 --max-time 8 --retry 0 "$@"fi-}+)# Where the app bundle goes on macOS. /Applications is writable by any admin# user without sudo; a standard user gets ~/Applications, which Launchpad and@@ -486,6 +503,26 @@ if [ "$os" = macos ]; thenfiapp="$app_dir/ThinkTerm.app"+tmp=$(mktemp -d "${TMPDIR:-/tmp}/thinkterm-install.XXXXXX")+# Linux binaries are staged on the destination filesystem, in a directory+# owned by this invocation. Never glob temporary files in the shared bin/.+bin_stage=""+app_staging=""+probe_pids=""+cleanup() {+ [ -z "$bg_pid" ] || kill "$bg_pid" 2>/dev/null || true+ for pid in $probe_pids; do kill "$pid" 2>/dev/null || true; done+ for pid in $probe_pids; do wait "$pid" 2>/dev/null || true; done+ leave+ rm -rf "$tmp"+ [ -z "$bin_stage" ] || rm -rf "$bin_stage" 2>/dev/null || true+ [ -z "$app_staging" ] || rm -rf "$app_dir/.ThinkTerm.app.tmp" 2>/dev/null || true+ [ -z "$app_staging" ] || rm -rf "$app_dir/.ThinkTerm.app.old" 2>/dev/null || true+}+trap cleanup EXIT+trap 'cleanup; exit 130' INT+trap 'cleanup; exit 143' TERM+# ---- resolve the release ---------------------------------------------------api="https://api.github.com/repos/$repo/releases"@@ -504,60 +541,157 @@ asset_name() {esac}+# This public key is shared with assets/release-signing.pub, which the client+# embeds. Private signing material is only held by the release workflow.+release_signing_key='ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILio64rghh4lVquJuwWvNpMVnmV78OnXQmKYDH8Z83t0 thinkterm-release'++validate_release_info() {+ [ -f "$1" ] && [ "$(wc -c < "$1")" -le 1048576 ] || return 1+ LC_ALL=C awk -v version="$tag" -v missing="$2" '+ NR == 1 { if ($0 != "thinkterm-release-v1") exit 1; next }+ NR == 2 { if ($0 != "version " version) exit 1; next }+ {+ if (NF != 3 || $0 != $1 " " $2 " " $3) exit 1+ if (!(missing && $1 == "-") && (length($1) != 64 || $1 ~ /[^0-9a-f]/)) exit 1+ if ($2 !~ /^[0-9]+$/ || $2 + 0 <= 0 || $2 + 0 > 8589934592) exit 1+ if (length($3) > 200 || $3 !~ /^[A-Za-z0-9][A-Za-z0-9._+-]*$/ || seen[$3]++) exit 1+ }+ END { if (NR < 3) exit 1 }+ ' "$1"+}++# No token goes to a mirror. Time and file limits apply even when an older+# curl cannot enforce --max-filesize on a chunked response.+fetch_metadata() (+ ulimit -f 2048 || exit 1+ curl --disable -fsSL --proto =https --proto-redir =https --connect-timeout 3 \+ --max-time 8 --retry 0 --max-filesize 1048576 -o "$2" "$1"+)++signed_release_info() {+ have ssh-keygen || { warn "ssh-keygen is unavailable; signed metadata cannot be verified"; return 1; }+ printf 'thinkterm-release namespaces="thinkterm-release" %s\n' "$release_signing_key" > "$tmp/signers"+ invalid_metadata=""+ metadata_sources="$sources"+ [ -z "${GITHUB_TOKEN:-}" ] || metadata_sources=github+ for metadata_source in $metadata_sources; do+ metadata_url="https://github.com/$repo/releases/download/$tag/thinkterm-release.txt"+ [ "$metadata_source" = github ] || metadata_url="https://$metadata_source/$metadata_url"+ if ! fetch_metadata "$metadata_url" "$tmp/release-info" 2>/dev/null \+ || ! fetch_metadata "$metadata_url.sig" "$tmp/release-info.sig" 2>/dev/null; then+ continue+ fi+ if ! ssh-keygen -Y verify -f "$tmp/signers" -I thinkterm-release -n thinkterm-release \+ -s "$tmp/release-info.sig" < "$tmp/release-info" > "$tmp/verify.log" 2>&1; then+ # An old OpenSSH cannot verify SSHSIG. It must not authorize a mirror.+ if grep -Ei 'unknown option|illegal option|unsupported operation|invalid option' "$tmp/verify.log" >/dev/null; then+ warn "ssh-keygen does not support release signatures; using GitHub only"+ return 1+ fi+ invalid_metadata=1+ warn "release signature verification failed from $metadata_source"+ continue+ fi+ if ! validate_release_info "$tmp/release-info" 0; then+ invalid_metadata=1+ warn "signed release metadata has an invalid format or a different version"+ continue+ fi+ release_info="$tmp/release-info"+ return 0+ done+ [ -z "$invalid_metadata" ] || return 2+ return 1+}+if [ -z "$from" ]; then- if [ -n "$tag" ]; then- release_json=$(with_spinner "looking up release $tag" fetch "$api/tags/$tag") \- || die "release '$tag' not found at https://github.com/$repo/releases"+ release_json=""+ if [ -n "$release_info" ]; then+ [ -n "$tag" ] || die "client release metadata requires --version"+ validate_release_info "$release_info" 1 || die "invalid client release metadata or version mismatch"else- release_json=$(with_spinner "looking up the latest release" fetch "$api/latest") \- || die "could not look up the latest release (is the network up? does https://github.com/$repo have a release yet?)"- tag=$(printf '%s' "$release_json" | sed -n 's/^ *"tag_name": *"\([^"]*\)".*/\1/p' | head -n1)- [ -n "$tag" ] || die "could not read the release tag from the GitHub API response"+ api_path=latest+ [ -z "$tag" ] || api_path="tags/$tag"+ api_status=""+ if api_status=$(with_spinner "looking up release ${tag:-latest}" fetch --max-filesize 1048576 \+ -o "$tmp/release.json" -w '%{http_code}' "$api/$api_path"); then+ release_json=$(cat "$tmp/release.json")+ if [ -z "$tag" ]; then+ tag=$(printf '%s' "$release_json" | sed -n 's/^ *"tag_name": *"\([^"]*\)".*/\1/p' | head -n1)+ [ -n "$tag" ] || die "could not read the release tag from the GitHub API response"+ fi+ else+ case "$api_status" in+ 404) warn "GitHub API did not find release ${tag:-latest} (HTTP 404)" ;;+ 403|429) warn "GitHub API refused the lookup or rate-limited it (HTTP $api_status)" ;;+ *) warn "could not reach GitHub API or complete the lookup (HTTP ${api_status:-unknown})" ;;+ esac+ [ -n "$tag" ] || die "cannot resolve the latest release; retry or pass --version X"+ if signed_release_info; then+ ok "verified release metadata"+ else+ metadata_status=$?+ [ "$metadata_status" -ne 2 ] || die "release metadata verification failed; nothing was installed"+ warn "no verifiable release metadata is available; trying the GitHub archive directly, without mirrors"+ sources=github+ fi+ fifi-- # The tag as GitHub has it goes in the URL path; the filename is built from- # the version ci/deploy.sh stamped, which never carries a leading v.+ case "$tag" in ""|*[!A-Za-z0-9._+-]*) die "invalid release version" ;; esacversion="${tag#v}"asset=$(asset_name "$version")+ if [ "$os/$variant" = linux/server ] && have xz; then+ xz_asset="${asset%.gz}.xz"+ if { [ -n "$release_info" ] && awk -v name="$xz_asset" '$3 == name { found=1 } END { exit !found }' "$release_info"; } \+ || { [ -n "$release_json" ] && printf '%s' "$release_json" | grep -qF "\"name\": \"$xz_asset\""; }; then+ asset="$xz_asset"+ fi+ fiurl="https://github.com/$repo/releases/download/$tag/$asset"-- # GitHub stores a sha256 for every asset as it is uploaded and reports it- # on the API as "sha256:<hex>". That is the only checksum the release- # carries -- there is no checksums file -- so read it from the same JSON.- # The asset object is the block between the "name" line naming our file and- # the next "name" line, and "digest" sits inside it.- digest=$(printf '%s' "$release_json" \- | awk -v want="\"name\": \"$asset\"" '- index($0, want) { inside = 1; next }- inside && /"name":/ { exit }- inside && /"digest": *"sha256:/ {- sub(/.*"digest": *"sha256:/, ""); sub(/".*/, ""); print; exit- }')- # Anything but a hex string means the field was absent or null: install- # unverified with a warning rather than fail a good download against junk.- case "$digest" in *[!0-9a-f]* | "") digest="" ;; esac- # Its size, from the same object, for the download bar.- size=$(printf '%s' "$release_json" \- | awk -v want="\"name\": \"$asset\"" '- index($0, want) { inside = 1; next }- inside && /"name":/ { exit }- inside && /"size": *[0-9]/ { sub(/.*"size": */, ""); sub(/[^0-9].*/, ""); print; exit }')- # The asset's own API address, for a private repository: the download URL- # under github.com/.../releases/download answers only for public ones,- # while the API serves the bytes to a token with Accept: octet-stream. In- # the asset object "url" comes before "name", so it is the last "url"- # seen when the name line matches.- if [ -n "${GITHUB_TOKEN:-}" ]; then- asset_api_url=$(printf '%s' "$release_json" \+ if [ -n "$release_info" ]; then+ row=$(awk -v name="$asset" '$3 == name { print $1 " " $2 }' "$release_info")+ [ -n "$row" ] || die "release $version exists, but has no package $asset for this host"+ digest=${row%% *}; size=${row#* }+ [ "$digest" != - ] || digest=""+ elif [ -n "$release_json" ]; then+ # GitHub stores a sha256 for every asset as it is uploaded and reports it+ # on the API as "sha256:<hex>". Read it from the same authenticated API response.+ # The asset object is the block between the "name" line naming our file and+ # the next "name" line, and "digest" sits inside it.+ digest=$(printf '%s' "$release_json" \| awk -v want="\"name\": \"$asset\"" '- /^ *"url": *"https:\/\/api\.github\.com\/repos\/[^"]*\/releases\/assets\/[0-9]+"/ {- u = $0; sub(/.*"url": *"/, "", u); sub(/".*/, "", u)- }- index($0, want) { print u; exit }')- fi- if [ -z "$digest" ]; then- printf '%s' "$release_json" | grep -qF "\"name\": \"$asset\"" \- || die "release $version has no $asset; see https://github.com/$repo/releases/tag/$tag"+ index($0, want) { inside = 1; next }+ inside && /"name":/ { exit }+ inside && /"digest": *"sha256:/ {+ sub(/.*"digest": *"sha256:/, ""); sub(/".*/, ""); print; exit+ }')+ # Anything but a hex string means the field was absent or null: install+ # unverified with a warning rather than fail a good download against junk.+ case "$digest" in *[!0-9a-f]* | "") digest="" ;; esac+ [ "${#digest}" -eq 64 ] || digest=""+ # Its size, from the same object, for the download bar.+ size=$(printf '%s' "$release_json" \+ | awk -v want="\"name\": \"$asset\"" '+ index($0, want) { inside = 1; next }+ inside && /"name":/ { exit }+ inside && /"size": *[0-9]/ { sub(/.*"size": */, ""); sub(/[^0-9].*/, ""); print; exit }')+ # The asset's own API address, for a private repository: the download URL+ # under github.com/.../releases/download answers only for public ones,+ # while the API serves the bytes to a token with Accept: octet-stream. In+ # the asset object "url" comes before "name", so it is the last "url"+ # seen when the name line matches.+ if [ -n "${GITHUB_TOKEN:-}" ]; then+ asset_api_url=$(printf '%s' "$release_json" \+ | awk -v want="\"name\": \"$asset\"" '+ /^ *"url": *"https:\/\/api\.github\.com\/repos\/[^"]*\/releases\/assets\/[0-9]+"/ {+ u = $0; sub(/.*"url": *"/, "", u); sub(/".*/, "", u)+ }+ index($0, want) { print u; exit }')+ fi+ if [ -z "$digest" ]; then+ printf '%s' "$release_json" | grep -qF "\"name\": \"$asset\"" \+ || die "release $version has no $asset; see https://github.com/$repo/releases/tag/$tag"+ fifielse# No release lookup, so the version comes from --version, or else from the@@ -568,10 +702,15 @@ elseelseversion=$(basename "$from" | sed -n \-e 's/^ThinkTerm-macos-[^-]*-\([0-9A-Za-z][0-9A-Za-z.+-]*\)\.zip$/\1/p' \- -e 's/^thinkterm-\(server-\)\{0,1\}\([0-9A-Za-z][0-9A-Za-z.+-]*\)-linux-[^-]*\(-glibc[0-9.]*\)\{0,1\}\.tar\.gz$/\2/p')+ -e 's/^thinkterm-\(server-\)\{0,1\}\([0-9A-Za-z][0-9A-Za-z.+-]*\)-linux-[^-]*\(-glibc[0-9.]*\)\{0,1\}\.tar\.\(gz\|xz\)$/\2/p')fifi+if [ -n "$size" ]; then+ case "$size" in *[!0-9]*) die "invalid release archive size" ;; esac+ [ "$size" -gt 0 ] && [ "$size" -le 8589934592 ] || die "invalid release archive size"+fi+if [ -n "$from" ]; thenok "ThinkTerm${version:+ $ui_bold$version$ui_reset} $ui_dim(from $(pretty "$from"))$ui_reset"elif [ -n "$tag_requested" ]; then@@ -590,7 +729,7 @@ if [ -n "$prev_variant" ]; thenesacfiif [ -z "$from" ] && [ -z "$digest" ]; then- warn "GitHub recorded no checksum for $asset; the download will not be verified"+ warn "no trusted checksum is available for $asset; the GitHub download will not be verified"fiif [ -n "$dry_run" ]; then@@ -609,22 +748,6 @@ fi# ---- download and verify ----------------------------------------------------tmp=$(mktemp -d "${TMPDIR:-/tmp}/thinkterm-install.XXXXXX")-# Linux binaries are staged on the destination filesystem, in a directory-# owned by this invocation. Never glob temporary files in the shared bin/.-bin_stage=""-cleanup() {- [ -z "$bg_pid" ] || kill "$bg_pid" 2>/dev/null || true- leave- rm -rf "$tmp"- [ -z "$bin_stage" ] || rm -rf "$bin_stage" 2>/dev/null || true- [ -z "$app_dir" ] || rm -rf "$app_dir/.ThinkTerm.app.tmp" 2>/dev/null || true- [ -z "$app_dir" ] || rm -rf "$app_dir/.ThinkTerm.app.old" 2>/dev/null || true-}-trap cleanup EXIT-trap 'cleanup; exit 130' INT-trap 'cleanup; exit 143' TERM-mb() { t=$(($1 * 10 / 1048576)); printf '%d.%d' $((t / 10)) $((t % 10)); }# $1 of $2 bytes as a bar $3 cells wide. A line of light where 24-bit colour@@ -657,28 +780,47 @@ progress_bar() {# Runs curl with the remaining arguments in the background while drawing how# much of $1 has arrived: a line with the amount and rate, and the bar under# it. $2 names the mirror, if it is one; $3 is "token" to send GITHUB_TOKEN.-# With $4 set, a transfer that averages under $min_rate over the five seconds-# after its first byte is stopped there, rather than after curl's own twenty. Returns-# curl's status, or 28 (curl's for too slow) when stopped, with both lines-# gone. curl runs directly, not through a function, so $! is curl itself.+# Curl enforces the same transfer deadlines in interactive and SSH modes.+# A non-terminal gets periodic plain lines rather than redraw control codes.download() {- dest=$1; via=$2; auth=$3; early=$4; shift 4+ dest=$1; via=$2; auth=$3; shift 3+ initial_bytes=0+ [ ! -f "$dest" ] || initial_bytes=$(wc -c < "$dest")+ if [ -n "$size" ]; then+ set -- --max-filesize "$size" "$@"+ fiif [ "$auth" = token ] && [ -n "${GITHUB_TOKEN:-}" ]; thenset -- -H "Authorization: Bearer $GITHUB_TOKEN" "$@"fiif [ -z "$anim" ]; thenprintf ' %s>%s downloading%s...\n' "$ui_accent" "$ui_reset" "${via:+ from $via}" >&4- curl -fsSL "$@"- return+ ( [ -z "$size" ] || ulimit -f "$(((size + 511) / 512))" || exit 1+ exec curl --disable -fsSL --proto =https --proto-redir =https "$@" ) &+ bg_pid=$!+ plain_started=$(date +%s)+ plain_initial=$initial_bytes+ while kill -0 "$bg_pid" 2>/dev/null; do+ sleep 2+ kill -0 "$bg_pid" 2>/dev/null || break+ plain_got=0+ [ ! -f "$dest" ] || plain_got=$(wc -c < "$dest")+ plain_elapsed=$(($(date +%s) - plain_started))+ [ "$plain_elapsed" -gt 0 ] || plain_elapsed=1+ plain_rate=$(((plain_got - plain_initial) / plain_elapsed))+ [ "$plain_rate" -ge 0 ] || plain_rate=0+ plain_percent=""+ [ -z "$size" ] || plain_percent=" $((plain_got * 100 / size))% of $(mb "$size") MB"+ printf ' %s MB%s, %s KB/s%s\n' "$(mb "$plain_got")" "$plain_percent" "$((plain_rate / 1024))" "${via:+ from $via}" >&4+ done+ if wait "$bg_pid"; then status=0; else status=$?; fi+ bg_pid=""+ return "$status"fi- curl -fsSL "$@" 2>"$errlog" &+ ( [ -z "$size" ] || ulimit -f "$(((size + 511) / 512))" || exit 1+ exec curl --disable -fsSL --proto =https --proto-redir =https "$@" ) 2>"$errlog" &bg_pid=$!- cut_short=""hide_cursorstarted=$(date +%s)- # The early check times the transfer from its first byte, so the DNS- # lookup, TLS and redirects to a far-away server do not count against it.- flowing=""width=$((cols - 6)); [ "$width" -le 44 ] || width=44; [ "$width" -ge 10 ] || width=10i=0printf '\n' >&4@@ -687,16 +829,6 @@ download() {# shellcheck disable=SC2046if [ -f "$dest" ]; then set -- $(wc -c < "$dest"); got=${1:-0}; finow=$(date +%s)- if [ "$got" -gt 0 ] && [ -z "$flowing" ]; then flowing=$now; fi- # Whole seconds, so six on the clock is at least five gone.- if [ -n "$early" ] && [ -n "$flowing" ] && [ $((now - flowing)) -ge 6 ]; then- early=""- if [ "$got" -lt $((min_rate * (now - flowing - 1))) ]; then- cut_short=1- kill "$bg_pid" 2>/dev/null || true- break- fi- fi# The parts of the status line, dropped from the least useful while the# line would not fit: a wrapped line breaks the redraw a line above.amount=" / $(mb "${size:-0}") MB"; [ -n "$size" ] || amount=" MB"@@ -704,7 +836,8 @@ download() {rate_part=""elapsed=$((now - started))if [ "$elapsed" -gt 0 ]; then- rate=$((got / elapsed))+ rate=$(((got - initial_bytes) / elapsed))+ [ "$rate" -ge 0 ] || rate=0if [ "$rate" -ge 1048576 ]; then rate_part=" $dot $(mb "$rate") MB/s"else rate_part=" $dot $((rate / 1024)) KB/s"; fifi@@ -729,57 +862,169 @@ download() {show_cursorif wait "$bg_pid"; then status=0; else status=$?; fibg_pid=""- if [ -n "$cut_short" ]; then status=28; : > "$errlog"; fi[ "$status" -eq 0 ] || show_errorreturn "$status"}+# Probes never feed the archive: a fast error page is not a verified package.+# Each child owns its curl process so cancellation also stops parallel probes.+probe_source() (+ probe_source_name=$1+ probe_url="$url"+ [ "$probe_source_name" = github ] || probe_url="https://$probe_source_name/$url"+ probe_pid=""+ trap '[ -z "$probe_pid" ] || { kill "$probe_pid" 2>/dev/null || true; wait "$probe_pid" 2>/dev/null || true; }' EXIT+ trap 'exit 130' INT+ trap 'exit 143' TERM+ ulimit -f 2048 || exit 1+ curl --disable -fsSL --proto =https --proto-redir =https --connect-timeout 2 --max-time 3 \+ --range 0-1048575 --max-filesize 1048576 --retry 0 \+ -o "$tmp/probe-$probe_source_name" -w '%{http_code} %{size_download} %{time_total}\n' \+ "$probe_url" > "$tmp/probe-$probe_source_name.stats" 2>/dev/null &+ probe_pid=$!+ wait "$probe_pid" || true+ probe_pid=""+ awk -v source="$probe_source_name" '+ ($1 == 200 || $1 == 206) && $2 > 0 && $3 > 0 { rate=int($2/$3) }+ END { print rate+0, source }+ ' "$tmp/probe-$probe_source_name.stats" > "$tmp/rate-$probe_source_name"+ rm -f "$tmp/probe-$probe_source_name"+)++rank_sources() {+ github_fast=""+ if [ "$mirror" != auto ]; then+ printf '%s\n' $sources > "$tmp/ranked-sources"+ return+ fi+ if [ "${1:-}" != mirrors ]; then+ note "checking GitHub download speed"+ probe_source github+ github_rate=$(awk '{print $1}' "$tmp/rate-github")+ if [ "$github_rate" -ge "$min_rate" ]; then+ github_fast=1+ printf 'github\n' > "$tmp/ranked-sources"+ return+ fi+ fi+ note "GitHub is slow or unavailable; checking mirrors in parallel"+ probe_pids=""+ for probe_host in $mirrors; do+ probe_source "$probe_host" &+ probe_pids="$probe_pids $!"+ done+ for pid in $probe_pids; do wait "$pid" || true; done+ probe_pids=""+ # A source that failed the probe is still a last resort, after the measured+ # ones. A short probe can fail during DNS/TLS while a full transfer works.+ cat "$tmp"/rate-* | sort -nr | awk '{print $2}' > "$tmp/ranked-sources"+}++verified_download() {+ source=$1+ transfer_mode=${2:-}+ source_url="$url"; source_via=""; source_auth=token+ if [ "$source" != github ]; then+ source_url="https://$source/$url"; source_via=$source; source_auth=""+ fi+ rm -f "$archive" "$tmp/slow-$source"+ attempt=0+ while [ "$attempt" -lt 3 ]; do+ attempt=$((attempt + 1))+ # If every measured link is slow, keep the fastest rather than refuse all+ # of them. If it stalls or breaks, the next source still gets a chance.+ transfer_rate=1; transfer_window=15+ if [ "$transfer_mode" != slow ]; then+ if [ "$mirror" != auto ] && [ "$source" != github ]; then+ transfer_rate=$min_rate; transfer_window=5+ fi+ if [ -f "$tmp/rate-$source" ] && [ "$(awk '{print $1}' "$tmp/rate-$source")" -ge "$min_rate" ]; then+ transfer_rate=$min_rate; transfer_window=5+ fi+ fi+ if download "$archive" "$source_via" "$source_auth" --connect-timeout 4 \+ --speed-limit "$transfer_rate" --speed-time "$transfer_window" --retry 0 \+ -C - -o "$archive" "$source_url"; then+ if [ "$(sha256 "$archive")" = "$digest" ] \+ && { [ -z "$size" ] || [ "$(wc -c < "$archive")" -eq "$size" ]; }; then+ return 0+ fi+ warn "checksum or size mismatch from $source; discarded"+ rm -f "$archive"+ return 1+ else+ transfer_status=$?+ # Try other sources before allowing a slow transfer. Only one that+ # delivered bytes gets that fallback; a connection timeout does not.+ # Broken transfers resume; range refusal gets a fresh retry.+ case "$transfer_status" in+ 22|63) break ;;+ 28)+ if [ "$transfer_rate" -gt 1 ]; then+ if [ "$mirror" = auto ] && [ -s "$archive" ]; then+ : > "$tmp/slow-$source"+ fi+ break+ fi+ ;;+ 33|36) rm -f "$archive" ;;+ esac+ [ "$attempt" -ge 3 ] || note "retrying $source (resuming when supported)"+ fi+ done+ warn "$source download failed or was too slow"+ return 1+}+if [ -n "$from" ]; then[ -f "$from" ] || die "no such file: $from"archive="$from"+ if [ -n "$expected_sha256" ]; then+ got=$(sha256 "$archive") || die "SHA-256 verification requires sha256sum or shasum"+ [ "$got" = "$expected_sha256" ] || die "checksum mismatch for --from archive; nothing was installed"+ ok "verified SHA-256"+ fielsearchive="$tmp/$asset"verified=""came_from=""dl_started=$(date +%s)if [ -n "$asset_api_url" ]; then- download "$archive" "" token "" --retry 3 -H "Accept: application/octet-stream" -o "$archive" "$asset_api_url" \+ download "$archive" "" token --connect-timeout 4 --speed-limit 1 --speed-time 30 --retry 3 -H "Accept: application/octet-stream" -o "$archive" "$asset_api_url" \|| die "download failed: $asset_api_url"elif [ "$sources" != github ] && [ -n "$digest" ] && { have sha256sum || have shasum; }; then- # Each source in turn, until one sends the file GitHub recorded. A- # mirror is a third party serving the bytes, so mirrors are only used- # when the digest from the API can check what they send: anything that- # does not match is thrown away and the next source tried. The token- # never goes to a mirror. While another source is left, a transfer under- # 300 KB/s for 20 seconds counts as failed: a working connection to- # GitHub's CDN is far faster, and at that rate the server tarball still- # arrives in under three minutes. On a terminal, where the bar is- # watching the transfer anyway, a source that has averaged under that over- # the five seconds after its first byte is dropped then instead.- left=$(printf '%s\n' $sources | wc -l)- for source in $sources; do- left=$((left - 1))- slow="--retry 3"; early=""- [ "$left" -eq 0 ] || { slow="--retry 0 --connect-timeout 10 --speed-limit $min_rate --speed-time 20"; early=1; }- rm -f "$archive"- dl_started=$(date +%s)- if [ "$source" = github ]; then- # shellcheck disable=SC2086- download "$archive" "" token "$early" $slow -o "$archive" "$url" \- || { warn "GitHub failed or was too slow"; continue; }- else- # shellcheck disable=SC2086- download "$archive" "$source" "" "$early" $slow -o "$archive" "https://$source/$url" \- || { warn "mirror $source failed or was too slow"; continue; }- fi- if [ "$(sha256 "$archive")" = "$digest" ]; then+ rank_sources+ if [ -n "$github_fast" ]; then+ if verified_download github; thenverified=1- [ "$source" = github ] || came_from=$source- break+ else+ printf '0 github\n' > "$tmp/rate-github"+ rank_sources mirrorsfi- warn "checksum mismatch from $source; discarded"- done- [ -n "$verified" ] || die "could not download $asset from: $sources. Try another --mirror, or download $url yourself and pass it with --from"+ fi+ if [ -z "$verified" ]; then+ for source in $(cat "$tmp/ranked-sources"); do+ if verified_download "$source"; then+ verified=1+ [ "$source" = github ] || came_from=$source+ break+ fi+ done+ fi+ if [ -z "$verified" ] && [ "$mirror" = auto ]; then+ # A quick probe can overestimate sustained speed. Revisit progressing+ # sources in rank order with stall protection, not the fast cutoff.+ for source in $(cat "$tmp/ranked-sources"); do+ [ -f "$tmp/slow-$source" ] || continue+ note "retrying $source, allowing a slower download"+ if verified_download "$source" slow; then+ verified=1+ [ "$source" = github ] || came_from=$source+ break+ fi+ done+ fi+ [ -n "$verified" ] || die "all download sources failed, were too slow, or failed verification for $asset; retry or use --from with --sha256"elseif [ "$sources" != github ]; thenif [ -n "$digest" ]; then@@ -788,7 +1033,13 @@ elsewarn "GitHub recorded no checksum to check a mirror's download against; downloading from GitHub"fifi- download "$archive" "" token "" --retry 3 -o "$archive" "$url" || die "download failed: $url"+ if [ -n "$digest" ] && { have sha256sum || have shasum; }; then+ verified_download github || die "GitHub download failed or did not pass verification"+ verified=1+ else+ download "$archive" "" token --connect-timeout 4 --speed-limit 1 --speed-time 30 --retry 3 \+ -o "$archive" "$url" || die "GitHub archive download failed (network error or package unavailable): $url"+ fifi# shellcheck disable=SC2046set -- $(wc -c < "$archive")@@ -810,7 +1061,11 @@ fimkdir "$tmp/x"case "$os" inlinux)- with_spinner "unpacking" tar xzf "$archive" -C "$tmp/x" || die "could not unpack $archive"+ case "$archive" in+ *.tar.xz) have xz || die "xz is required to unpack this archive"; tar_flag=xJf ;;+ *) tar_flag=xzf ;;+ esac+ with_spinner "unpacking" tar "$tar_flag" "$archive" -C "$tmp/x" || die "could not unpack $archive"# The tarball holds exactly one top-level directory named after itself.root=$(find "$tmp/x" -mindepth 1 -maxdepth 1 -type d | head -n1)[ -n "$root" ] && [ -d "$root/bin" ] || die "unexpected tarball layout: no bin/ directory inside"@@ -919,6 +1174,7 @@ if [ "$os" = macos ]; then# The command-line tools are symlinks into the bundle: a copy would run# outside the seal, and the wezterm shim locates thinkterm next to itself.mkdir -p "$app_dir"+ app_staging=1rm -rf "$app_dir/.ThinkTerm.app.tmp" "$app_dir/.ThinkTerm.app.old"mv "$root" "$app_dir/.ThinkTerm.app.tmp"if [ -e "$app" ]; then