tripwire-e2e 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +201 -169
- package/operators/_util.js +14 -1
- package/operators/dead-control.js +23 -23
- package/operators/hide-control.js +3 -3
- package/operators/index.js +81 -77
- package/operators/label-swap.js +1 -1
- package/operators/missing-row-action.js +24 -24
- package/operators/rendered-item-drop.js +31 -0
- package/operators/rendered-item-dup.js +20 -0
- package/operators/rendered-number-drift.js +24 -0
- package/package.json +1 -1
- package/src/agent.js +1 -1
- package/src/browser/mutate.js +156 -113
- package/src/browser/recorder.js +102 -80
- package/src/cli.js +41 -37
- package/src/commands/check.js +24 -7
- package/src/commands/coverage.js +110 -0
- package/src/commands/doctor.js +28 -0
- package/src/commands/fix.js +13 -4
- package/src/commands/run.js +370 -310
- package/src/commands/sabotage.js +85 -74
- package/src/kane.js +158 -150
- package/src/mutant.js +26 -26
- package/src/orphans.js +40 -0
- package/src/profile.js +13 -1
- package/src/proxy.js +292 -289
- package/src/report.js +183 -182
package/src/proxy.js
CHANGED
|
@@ -1,289 +1,292 @@
|
|
|
1
|
-
// The Tripwire proxy: a reverse proxy in front of the app under test.
|
|
2
|
-
// Disarmed it passes everything through (and, in record mode, notes traffic and what the page showed).
|
|
3
|
-
// Armed with one mutant it changes exactly one realistic thing about the app's behaviour.
|
|
4
|
-
import http from 'node:http';
|
|
5
|
-
import net from 'node:net';
|
|
6
|
-
import fs from 'node:fs';
|
|
7
|
-
import path from 'node:path';
|
|
8
|
-
import { fileURLToPath } from 'node:url';
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (match
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
state.
|
|
50
|
-
state.
|
|
51
|
-
state.
|
|
52
|
-
state.
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
delete headers[
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return send(res,
|
|
119
|
-
|
|
120
|
-
log(`
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
case '
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
case 'POST /_tw/
|
|
134
|
-
|
|
135
|
-
return send(res, 204, '');
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const
|
|
148
|
-
const
|
|
149
|
-
const
|
|
150
|
-
const
|
|
151
|
-
const
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
let
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
return
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
1
|
+
// The Tripwire proxy: a reverse proxy in front of the app under test.
|
|
2
|
+
// Disarmed it passes everything through (and, in record mode, notes traffic and what the page showed).
|
|
3
|
+
// Armed with one mutant it changes exactly one realistic thing about the app's behaviour.
|
|
4
|
+
import http from 'node:http';
|
|
5
|
+
import net from 'node:net';
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
import crypto from 'node:crypto';
|
|
10
|
+
import { applyJsonOps, injectHtml, replaceHtml } from './actions.js';
|
|
11
|
+
|
|
12
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
const RECORDER_JS = fs.readFileSync(path.join(here, 'browser/recorder.js'), 'utf8');
|
|
14
|
+
const MUTATE_JS = fs.readFileSync(path.join(here, 'browser/mutate.js'), 'utf8');
|
|
15
|
+
// Fingerprint of the code a proxy serves; a runner refuses to reuse a proxy built from older code.
|
|
16
|
+
export const PROXY_CODE = crypto.createHash('sha1').update(RECORDER_JS + MUTATE_JS + fs.readFileSync(fileURLToPath(import.meta.url), 'utf8')).digest('hex').slice(0, 12);
|
|
17
|
+
|
|
18
|
+
const HOP = ['connection', 'keep-alive', 'proxy-connection', 'transfer-encoding', 'upgrade', 'te', 'trailer'];
|
|
19
|
+
const MAX_BODY_SAMPLE = 256 * 1024;
|
|
20
|
+
const STATIC_EXT = /\.(js|mjs|css|png|jpe?g|gif|svg|ico|webp|woff2?|ttf|map)$/i;
|
|
21
|
+
|
|
22
|
+
export function matches(match, req) {
|
|
23
|
+
if (!match) return false;
|
|
24
|
+
if (match.document) return req.isDocument;
|
|
25
|
+
if (match.method && match.method !== '*' && match.method.toUpperCase() !== req.method) return false;
|
|
26
|
+
if (match.path) {
|
|
27
|
+
if (match.path.startsWith('re:')) { if (!new RegExp(match.path.slice(3)).test(req.path)) return false; }
|
|
28
|
+
else if (match.path !== req.path) return false;
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function createProxy({ target, port = 8080, log = () => {} }) {
|
|
34
|
+
const upstream = new URL(target);
|
|
35
|
+
const state = {
|
|
36
|
+
armed: null, // current mutant definition
|
|
37
|
+
hits: 0, // times the armed mutant actually changed something
|
|
38
|
+
uses: 0, // times it matched (for `times` limits)
|
|
39
|
+
record: false, // inject the DOM recorder + keep full bodies
|
|
40
|
+
test: null, // label of the test currently running (set by the runner)
|
|
41
|
+
seq: 0,
|
|
42
|
+
traffic: [],
|
|
43
|
+
dom: [],
|
|
44
|
+
lastGet: new Map(), // path -> last GET response (for stale-read)
|
|
45
|
+
staleFor: new Map() // path -> response as it was before the most recent write
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const resetRun = (test) => {
|
|
49
|
+
state.test = test ?? null;
|
|
50
|
+
state.traffic = [];
|
|
51
|
+
state.dom = [];
|
|
52
|
+
state.hits = 0;
|
|
53
|
+
state.uses = 0;
|
|
54
|
+
state.lastGet.clear();
|
|
55
|
+
state.staleFor.clear();
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
function send(res, status, body, headers = {}) {
|
|
59
|
+
const data = typeof body === 'string' ? body : JSON.stringify(body);
|
|
60
|
+
res.writeHead(status, { 'content-type': typeof body === 'string' ? 'text/plain' : 'application/json', 'cache-control': 'no-store', ...headers });
|
|
61
|
+
res.end(data);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function readBody(req) {
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
const chunks = [];
|
|
67
|
+
req.on('data', (c) => chunks.push(c));
|
|
68
|
+
req.on('end', () => resolve(Buffer.concat(chunks)));
|
|
69
|
+
req.on('error', reject);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function forward(req, body) {
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
const headers = { ...req.headers, host: upstream.host };
|
|
76
|
+
for (const h of HOP) delete headers[h];
|
|
77
|
+
// Ask for plain bodies and no conditional requests: a 304 would skip the mutation.
|
|
78
|
+
delete headers['accept-encoding'];
|
|
79
|
+
delete headers['if-none-match'];
|
|
80
|
+
delete headers['if-modified-since'];
|
|
81
|
+
if (body.length) headers['content-length'] = String(body.length);
|
|
82
|
+
const out = http.request({ protocol: upstream.protocol, hostname: upstream.hostname, port: upstream.port || 80, method: req.method, path: req.url, headers }, (up) => {
|
|
83
|
+
const chunks = [];
|
|
84
|
+
up.on('data', (c) => chunks.push(c));
|
|
85
|
+
up.on('end', () => resolve({ status: up.statusCode, headers: up.headers, body: Buffer.concat(chunks) }));
|
|
86
|
+
up.on('error', reject);
|
|
87
|
+
});
|
|
88
|
+
out.on('error', reject);
|
|
89
|
+
out.end(body);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function cleanHeaders(h) {
|
|
94
|
+
const out = { ...h };
|
|
95
|
+
for (const k of [...HOP, 'content-length', 'content-encoding', 'etag', 'last-modified', 'content-security-policy', 'content-security-policy-report-only']) delete out[k];
|
|
96
|
+
out['cache-control'] = 'no-store';
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const isJson = (h) => /json/i.test(h['content-type'] || '');
|
|
101
|
+
const isHtml = (h) => /text\/html/i.test(h['content-type'] || '');
|
|
102
|
+
const tryJson = (buf) => { try { return JSON.parse(buf.toString('utf8')); } catch { return undefined; } };
|
|
103
|
+
|
|
104
|
+
function record(entry) {
|
|
105
|
+
state.traffic.push({ seq: ++state.seq, test: state.test, ...entry });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function fire(note) {
|
|
109
|
+
state.hits++;
|
|
110
|
+
log(`fired ${state.armed?.id}${note ? ' (' + note + ')' : ''}`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function handleControl(req, res, url) {
|
|
114
|
+
const body = req.method === 'POST' ? tryJson(await readBody(req)) ?? {} : {};
|
|
115
|
+
switch (`${req.method} ${url.pathname}`) {
|
|
116
|
+
case 'GET /_tw/health': return send(res, 200, { ok: true, target, code: PROXY_CODE, armed: state.armed?.id ?? null, record: state.record });
|
|
117
|
+
case 'POST /_tw/arm':
|
|
118
|
+
if (!body || !body.id || !body.action) return send(res, 400, { error: 'mutant needs id and action' });
|
|
119
|
+
state.armed = body; state.hits = 0; state.uses = 0; state.staleFor.clear(); state.lastGet.clear();
|
|
120
|
+
log(`armed ${body.id}`);
|
|
121
|
+
return send(res, 200, { armed: body.id });
|
|
122
|
+
case 'POST /_tw/disarm':
|
|
123
|
+
log(`disarmed ${state.armed?.id ?? '(none)'}`);
|
|
124
|
+
state.armed = null; state.hits = 0; state.uses = 0;
|
|
125
|
+
return send(res, 200, { armed: null });
|
|
126
|
+
case 'POST /_tw/reset':
|
|
127
|
+
resetRun(body.test);
|
|
128
|
+
if (typeof body.record === 'boolean') state.record = body.record;
|
|
129
|
+
return send(res, 200, { test: state.test, record: state.record });
|
|
130
|
+
case 'GET /_tw/traffic': return send(res, 200, { traffic: state.traffic });
|
|
131
|
+
case 'GET /_tw/dom': return send(res, 200, { dom: state.dom });
|
|
132
|
+
case 'GET /_tw/hit': return send(res, 200, { armed: state.armed?.id ?? null, hits: state.hits, fired: state.hits > 0 });
|
|
133
|
+
case 'POST /_tw/dom':
|
|
134
|
+
for (const ev of body.events || []) state.dom.push({ test: state.test, ...ev });
|
|
135
|
+
return send(res, 204, '');
|
|
136
|
+
case 'POST /_tw/fired':
|
|
137
|
+
if (state.armed && body.id === state.armed.id) fire('in page');
|
|
138
|
+
return send(res, 204, '');
|
|
139
|
+
default: return send(res, 404, { error: 'unknown control endpoint' });
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function handle(req, res) {
|
|
144
|
+
const url = new URL(req.url, 'http://proxy');
|
|
145
|
+
if (url.pathname.startsWith('/_tw/')) return handleControl(req, res, url);
|
|
146
|
+
|
|
147
|
+
const reqBody = await readBody(req);
|
|
148
|
+
const accept = req.headers.accept || '';
|
|
149
|
+
const info = { method: req.method, path: url.pathname, isDocument: req.method === 'GET' && /text\/html/.test(accept) };
|
|
150
|
+
const m = state.armed;
|
|
151
|
+
const action = m?.action;
|
|
152
|
+
const limited = m && m.times && state.uses >= m.times;
|
|
153
|
+
const armedHere = m && !limited && matches(m.match, info);
|
|
154
|
+
const isWrite = !['GET', 'HEAD', 'OPTIONS'].includes(req.method);
|
|
155
|
+
const reqJson = isJson(req.headers) ? tryJson(reqBody) : undefined;
|
|
156
|
+
|
|
157
|
+
// ---- request-phase mutants: the app never sees (or sees twice) the request ----
|
|
158
|
+
if (armedHere && action.type === 'respond') {
|
|
159
|
+
state.uses++; fire(`${action.status} instead of forwarding`);
|
|
160
|
+
if (isWrite) snapshotStale(url.pathname);
|
|
161
|
+
const body = action.body === undefined ? { error: action.status >= 500 ? 'Internal Server Error' : 'Unauthorized' } : action.body;
|
|
162
|
+
record({ method: req.method, path: url.pathname, status: action.status, type: 'json', req: reqJson, json: body, mutated: true });
|
|
163
|
+
return send(res, action.status, body);
|
|
164
|
+
}
|
|
165
|
+
if (armedHere && action.type === 'stale-read' && req.method === 'GET' && state.staleFor.has(url.pathname)) {
|
|
166
|
+
state.uses++; fire('served the previous response');
|
|
167
|
+
const prev = state.staleFor.get(url.pathname);
|
|
168
|
+
record({ method: req.method, path: url.pathname, status: prev.status, type: 'json', json: tryJson(prev.body), mutated: true });
|
|
169
|
+
res.writeHead(prev.status, cleanHeaders(prev.headers));
|
|
170
|
+
return res.end(prev.body);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
let up;
|
|
174
|
+
try {
|
|
175
|
+
if (isWrite) snapshotStale(url.pathname);
|
|
176
|
+
up = await forward(req, reqBody);
|
|
177
|
+
if (armedHere && action.type === 'forward-twice' && isWrite) {
|
|
178
|
+
state.uses++; fire('request applied twice');
|
|
179
|
+
up = await forward(req, reqBody);
|
|
180
|
+
}
|
|
181
|
+
} catch (err) {
|
|
182
|
+
return send(res, 502, { error: `tripwire proxy could not reach ${target}: ${err.message}` });
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
let status = up.status;
|
|
186
|
+
let body = up.body;
|
|
187
|
+
const headers = cleanHeaders(up.headers);
|
|
188
|
+
let mutated = false;
|
|
189
|
+
|
|
190
|
+
// ---- response-phase mutants ----
|
|
191
|
+
if (armedHere && action.type === 'status-map' && action.from.includes(status)) {
|
|
192
|
+
state.uses++; fire(`${status} -> ${action.to}`);
|
|
193
|
+
status = action.to; mutated = true;
|
|
194
|
+
}
|
|
195
|
+
if (armedHere && action.type === 'json' && isJson(up.headers)) {
|
|
196
|
+
const parsed = tryJson(body);
|
|
197
|
+
if (parsed !== undefined) {
|
|
198
|
+
const r = applyJsonOps(parsed, action.ops);
|
|
199
|
+
if (r.changed) { state.uses++; fire(); body = Buffer.from(JSON.stringify(r.body)); mutated = true; }
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (isHtml(up.headers)) {
|
|
203
|
+
let html = body.toString('utf8');
|
|
204
|
+
if (state.record) html = injectHtml(html, `<script id="__tw-recorder">${RECORDER_JS}</script>`);
|
|
205
|
+
if (armedHere && action.type === 'dom') {
|
|
206
|
+
state.uses++;
|
|
207
|
+
// fired is reported by the page once the element was really changed
|
|
208
|
+
html = injectHtml(html, `<script id="__tw-mutant">window.__TW_MUTANT=${JSON.stringify({ id: m.id, ...action.dom }).replace(/</g, '\\u003c')};${MUTATE_JS}</script>`);
|
|
209
|
+
mutated = true;
|
|
210
|
+
}
|
|
211
|
+
if (armedHere && action.type === 'inject-css') {
|
|
212
|
+
state.uses++; fire('css injected');
|
|
213
|
+
html = injectHtml(html, `<style id="__tw-mutant">${action.css}</style>`);
|
|
214
|
+
mutated = true;
|
|
215
|
+
}
|
|
216
|
+
if (armedHere && action.type === 'html-replace') {
|
|
217
|
+
const r = replaceHtml(html, action.find, action.replace);
|
|
218
|
+
if (r.changed) { state.uses++; fire(); html = r.html; mutated = true; }
|
|
219
|
+
}
|
|
220
|
+
body = Buffer.from(html);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (req.method === 'GET' && !mutated && status < 400) state.lastGet.set(url.pathname, { status, headers: up.headers, body: up.body });
|
|
224
|
+
|
|
225
|
+
if (!STATIC_EXT.test(url.pathname)) {
|
|
226
|
+
const entry = { method: req.method, path: url.pathname, query: url.search || undefined, status, type: isJson(up.headers) ? 'json' : isHtml(up.headers) ? 'html' : 'other', mutated };
|
|
227
|
+
if (reqJson !== undefined) entry.req = reqJson;
|
|
228
|
+
if (entry.type === 'json' && body.length <= MAX_BODY_SAMPLE) entry.json = tryJson(body);
|
|
229
|
+
if (entry.type === 'html') entry.document = info.isDocument;
|
|
230
|
+
record(entry);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
res.writeHead(status, headers);
|
|
234
|
+
res.end(body);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function snapshotStale(writePath) {
|
|
238
|
+
// Everything read so far is now potentially out of date; remember it as the "stale" version.
|
|
239
|
+
for (const [p, v] of state.lastGet) if (p !== writePath) state.staleFor.set(p, v);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const server = http.createServer((req, res) => {
|
|
243
|
+
handle(req, res).catch((err) => {
|
|
244
|
+
log(`error: ${err.stack || err}`);
|
|
245
|
+
if (!res.headersSent) send(res, 500, { error: 'tripwire proxy error', detail: String(err.message || err) });
|
|
246
|
+
else res.end();
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// Websockets and other upgrades are passed through untouched.
|
|
251
|
+
server.on('upgrade', (req, socket, head) => {
|
|
252
|
+
const conn = net.connect(upstream.port || 80, upstream.hostname, () => {
|
|
253
|
+
const lines = [`${req.method} ${req.url} HTTP/${req.httpVersion}`];
|
|
254
|
+
for (let i = 0; i < req.rawHeaders.length; i += 2) {
|
|
255
|
+
const k = req.rawHeaders[i];
|
|
256
|
+
lines.push(`${k}: ${k.toLowerCase() === 'host' ? upstream.host : req.rawHeaders[i + 1]}`);
|
|
257
|
+
}
|
|
258
|
+
conn.write(lines.join('\r\n') + '\r\n\r\n');
|
|
259
|
+
if (head && head.length) conn.write(head);
|
|
260
|
+
socket.pipe(conn).pipe(socket);
|
|
261
|
+
});
|
|
262
|
+
conn.on('error', () => socket.destroy());
|
|
263
|
+
socket.on('error', () => conn.destroy());
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
return {
|
|
267
|
+
state,
|
|
268
|
+
server,
|
|
269
|
+
listen: () => new Promise((resolve) => server.listen(port, () => resolve(server.address().port))),
|
|
270
|
+
close: () => new Promise((resolve) => { server.closeAllConnections?.(); server.close(() => resolve()); })
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Small client used by the runner and tests.
|
|
275
|
+
export function proxyClient(base) {
|
|
276
|
+
const call = async (method, p, body) => {
|
|
277
|
+
const r = await fetch(base + p, { method, headers: { 'content-type': 'application/json' }, body: body ? JSON.stringify(body) : undefined });
|
|
278
|
+
if (r.status === 204) return null;
|
|
279
|
+
const data = await r.json();
|
|
280
|
+
if (!r.ok) throw new Error(`${method} ${p}: ${r.status} ${data.error || ''}`);
|
|
281
|
+
return data;
|
|
282
|
+
};
|
|
283
|
+
return {
|
|
284
|
+
health: () => call('GET', '/_tw/health'),
|
|
285
|
+
arm: (mutant) => call('POST', '/_tw/arm', mutant),
|
|
286
|
+
disarm: () => call('POST', '/_tw/disarm'),
|
|
287
|
+
reset: (test, record) => call('POST', '/_tw/reset', { test, record }),
|
|
288
|
+
traffic: async () => (await call('GET', '/_tw/traffic')).traffic,
|
|
289
|
+
dom: async () => (await call('GET', '/_tw/dom')).dom,
|
|
290
|
+
hit: () => call('GET', '/_tw/hit')
|
|
291
|
+
};
|
|
292
|
+
}
|