AtlatestRepositorysigil-quic

sigil-quic / tree / scriptsinject-retry-spy.mjs

1// Remote-only ownership observations. Never execute on the development laptop.
2// Inputs and outputs must differ; shipping modules are restored by the gate.
3import {readFileSync,writeFileSync} from 'node:fs';
4const [kind,input,output]=process.argv.slice(2);
5if(!['token','endpoint','fixture'].includes(kind)||!input||!output||input===output){
6 throw Error('Usage: inject-retry-spy.mjs token|endpoint|fixture INPUT OUTPUT');
7}
8let source=readFileSync(input,'utf8');
9function once(from,to){
10 if(source.split(from).length!==2)throw Error('Retry observation site changed: '+from);
11 source=source.replace(from,()=>to);
13if(kind==='fixture'){
14 const boundary=source.indexOf('\n\n'),end=source.indexOf('\n(test-group ');
15 if(!source.startsWith('(import ')||boundary<0||end<boundary||!source.includes('(define (er-with-pair ')){
16 throw Error('Expected public endpoint Retry fixture');
17 }
18 const imports=source.slice(0,boundary),helpers=source.slice(boundary+2,end);
19 const names=[...helpers.matchAll(/^\(define (?:\(([^\s()]+)|([^\s()]+))/gm)].map(m=>m[1]||m[2]);
20 if(names.length!==16||new Set(names).size!==16)throw Error('Public fixture definitions changed');
21 writeFileSync(output,';;; TEST ONLY: shared public UDP fixture, no named tests.\n'+
22 '(define-library (sigil quic tests retry fixture)\n '+imports+'\n'+
23 ' (export '+names.join(' ')+')\n (begin\n'+
24 helpers.split('\n').map(line=>' '+line).join('\n')+'\n ))\n');
25}else{
26 if(source.includes('retry-spy'))throw Error('Already instrumented');
27 if(kind==='token'){
28 if(!source.includes('(define-library (sigil quic retry token)'))throw Error('Expected token manager');
29 once(' (set! owned (if key (bytevector-copy key) (random-bytes 32)))',
30 ' (set! owned (if key (bytevector-copy key) (random-bytes 32)))\n'+
31 ' (retry-spy-owned! owned)');
32 once(' (let ((manager (retry-token-manager key: owned lifetime: lifetime)))',
33 ' (let ((manager (retry-token-manager key: owned lifetime: lifetime)))\n'+
34 ' (retry-spy-manager! manager)');
35 once(' (wipe! key)))',
36 " (wipe! key)\n (retry-spy-checkpoint! 'close-after)))");
37 once('(hmac-sha256-bytes (retry-token-manager-key manager)',
38 '(retry-spy-mac hmac-sha256-bytes (retry-token-manager-key manager)');
39 if(source.split('(random-bytes ').length!==3)throw Error('Token random sites changed');
40 source=source.replaceAll('(random-bytes ','(retry-spy-random random-bytes ');
41 }else{
42 if(!source.includes('(define-library (sigil quic endpoint)'))throw Error('Expected endpoint');
43 once(' (set-quic-endpoint-retry-manager! endpoint manager)',
44 ' (set-quic-endpoint-retry-manager! endpoint manager)\n'+
45 " (retry-spy-checkpoint! 'accept-after-manager)");
46 once('(random-bytes 8)','(retry-spy-random random-bytes 8)');
47 once('(encode-retry-packet original client retry token)',
48 '(retry-spy-packet encode-retry-packet original client retry token)');
49 once('\n (unless (and (quic-connection? connection) (eq? (quic-role connection) \'server)',
50 '\n (retry-spy-candidate! connection)\n'+
51 ' (unless (and (quic-connection? connection) (eq? (quic-role connection) \'server)');
52 const start=source.indexOf(' (define (send-retry!'),end=source.indexOf(' (define (admit-retry-connection!',start);
53 if(start<0||end<start)throw Error('Retry send boundary changed');
54 let body=source.slice(start,end);
55 const send='(udp-send (quic-endpoint-socket endpoint) packet "127.0.0.1" peer-port)';
56 if(body.split(send).length!==2)throw Error('Retry send operation changed');
57 body=body.replace(send,()=>'(retry-spy-send udp-send (quic-endpoint-socket endpoint) packet "127.0.0.1" peer-port)');
58 source=source.slice(0,start)+body+source.slice(end);
59 }
60 once('(import (sigil crypto)',
61 '(import (sigil quic tests retry spy)\n (sigil crypto)');
62 writeFileSync(output,';;; TEST ONLY: actual Retry key ownership/fault observations; never ship.\n'+source);