#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Fetch served /files/ artifacts into evidence/job1468/, verifying each digest. Read-only GETs against this project only. The credential is read from the protected store inside the process; it never appears in argv or in the saved files. Usage: python fetch_files.py [ ...] """ import hashlib import json import os import sys import urllib.error import urllib.request # The content store is at the SERVER ROOT, not under the project: a served artifact is # https://solveathome.org/files/. Under /projects/twin-primes/... every one of # /files, /files/, /file/, /return//files and /return//file/ answers 404 # "no such route", which is worth one probe and then a look at the shared tool's own URL builder. BASE = "https://solveathome.org" TOKEN_FILE = r"C:\Users\Max\AppData\Local\solveathome\credentials\twin-primes.token" def main(run, pairs): out = os.path.join(run, "evidence", "job1468") os.makedirs(out, exist_ok=True) tok = open(TOKEN_FILE, encoding="utf-8").read().strip() manifest = [] for spec in pairs: name, sha = spec.split("=", 1) url = "%s/files/%s" % (BASE, sha) req = urllib.request.Request(url, headers={"Authorization": "Bearer " + tok}) try: with urllib.request.urlopen(req, timeout=120) as r: body, status = r.read(), r.status except urllib.error.HTTPError as e: body, status = e.read(), e.code p = os.path.join(out, name) with open(p, "wb") as fh: fh.write(body) got = hashlib.sha256(body).hexdigest() rec = {"name": name, "sha256_declared": sha, "sha256_actual": got, "matches": got == sha, "bytes": len(body), "http_status": status, "path": p} manifest.append(rec) print(json.dumps(rec, sort_keys=True)) with open(os.path.join(out, "manifest.json"), "w", encoding="utf-8", newline="\n") as fh: json.dump(manifest, fh, indent=1, sort_keys=True) fh.write("\n") print("all match:", all(r["matches"] for r in manifest)) return 0 if all(r["matches"] for r in manifest) else 1 if __name__ == "__main__": sys.exit(main(sys.argv[1], sys.argv[2:]))