#!/usr/bin/env python3 """cdp_shot.py — R-1125: screenshots of dashboard pages of SCRATCH 9202 (felhom.enkisfelhom.hu mapped to 9202 by --host-resolver-rules). Based on the v0.324.0 night (R-983, R-976): the household's normal flow in headless Chrome through the PUBLIC addresses — sign in, sign out (now a POST form), sign in again, a family-gated app (Grimmory), a setup-gated app (Rallly). Minimal CDP client: log in to the controller through its PUBLIC address in headless Chrome, open each app-page tab, assert computed styles, collect console errors, save screenshots. The password is read from a 0600 file and only ever sent inside a CDP message; it is never printed.""" import base64, json, os, socket, struct, subprocess, sys, time, urllib.request CHROME = os.path.expanduser('~/.cache/ms-playwright/chromium_headless_shell-1243/chrome-headless-shell-linux64/chrome-headless-shell') BASE = sys.argv[1] # https://felhom.enkisfelhom.hu PWFILE = sys.argv[2] # the controller password (0600 file) PATHS = sys.argv[3].split(',') # dashboard paths to open and shoot OUT = sys.argv[4] PORT = 9333 os.makedirs(OUT, exist_ok=True) prof = os.path.join(OUT, '.profile') proc = subprocess.Popen([CHROME, f'--remote-debugging-port={PORT}', '--no-sandbox', f'--user-data-dir={prof}', '--window-size=1280,1400', '--host-resolver-rules=MAP felhom.enkisfelhom.hu 192.168.0.114', '--ignore-certificate-errors', 'about:blank'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) try: for _ in range(50): try: tabs = json.load(urllib.request.urlopen(f'http://127.0.0.1:{PORT}/json')) break except Exception: time.sleep(0.2) ws_url = [t for t in tabs if t['type'] == 'page'][0]['webSocketDebuggerUrl'] host, path = ws_url[len('ws://'):].split('/', 1) h, p = host.split(':') sock = socket.create_connection((h, int(p))) key = base64.b64encode(os.urandom(16)).decode() sock.send(f'GET /{path} HTTP/1.1\r\nHost: {host}\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\n\r\n'.encode()) buf = b'' while b'\r\n\r\n' not in buf: buf += sock.recv(4096) st = {'pending': buf.split(b'\r\n\r\n', 1)[1]} def send(obj): data = json.dumps(obj).encode() hdr = bytearray([0x81]) n = len(data) if n < 126: hdr.append(0x80 | n) elif n < 65536: hdr.append(0x80 | 126); hdr += struct.pack('>H', n) else: hdr.append(0x80 | 127); hdr += struct.pack('>Q', n) mask = os.urandom(4); hdr += mask sock.sendall(bytes(hdr) + bytes(b ^ mask[i % 4] for i, b in enumerate(data))) def recv_exact(n): while len(st['pending']) < n: st['pending'] += sock.recv(1 << 20) out, st['pending'] = st['pending'][:n], st['pending'][n:] return out def recv(): msg = b'' while True: b0, b1 = recv_exact(2) n = b1 & 0x7f if n == 126: n = struct.unpack('>H', recv_exact(2))[0] elif n == 127: n = struct.unpack('>Q', recv_exact(8))[0] msg += recv_exact(n) if b0 & 0x80: return json.loads(msg) console = [] mid = [0] def call(method, **params): mid[0] += 1; i = mid[0] send({'id': i, 'method': method, 'params': params}) while True: m = recv() if m.get('method') == 'Runtime.exceptionThrown': console.append(('exception', m['params']['exceptionDetails'].get('text', ''), m['params']['exceptionDetails'].get('exception', {}).get('description', '')[:200])) elif m.get('method') == 'Runtime.consoleAPICalled' and m['params']['type'] in ('error', 'warning'): console.append((m['params']['type'], ' '.join(str(a.get('value', a.get('description', ''))) for a in m['params']['args'])[:200])) elif m.get('method') == 'Log.entryAdded' and m['params']['entry']['level'] in ('error', 'warning'): e = m['params']['entry']; console.append(('log-' + e['level'], e.get('text', '')[:200], e.get('url', ''))) if m.get('id') == i: if 'error' in m: raise RuntimeError(f'{method}: {m["error"]}') return m.get('result', {}) def ev(expr): r = call('Runtime.evaluate', expression=expr, returnByValue=True, awaitPromise=True) return r.get('result', {}).get('value') def goto(url): call('Page.navigate', url=url) for _ in range(100): time.sleep(0.2) if ev('document.readyState') == 'complete': break time.sleep(1.5) # images' onerror, layout for d in ('Page', 'Runtime', 'Log', 'Network'): call(d + '.enable') def login(): pw = open(PWFILE).read().strip() goto(BASE + '/login') ev("(function(){var f=document.querySelector('input[type=password]'); f.value=%s; f.form.submit(); return 1})()" % json.dumps(pw)) time.sleep(3) return ev('location.pathname') def shot(name): s = call('Page.captureScreenshot', format='png', captureBeyondViewport=True) open(os.path.join(OUT, name + '.png'), 'wb').write(base64.b64decode(s['data'])) print("signed in, at", login()) for i, p in enumerate(PATHS): goto(BASE + p) time.sleep(2) info = ev("({path: location.pathname + location.search, box: (document.querySelector('.memory-blocked .alert')||{}).innerText || null, button_disabled: !!document.querySelector('#deploy-form button[type=submit][disabled]'), button_title: (document.querySelector('#deploy-form button[type=submit]')||{}).title || null})") print(json.dumps(info, ensure_ascii=False)) shot("page%d" % i) finally: proc.terminate() subprocess.run(['rm', '-rf', prof])