hopwire 0.3.0 → 0.5.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/dist/bin.js +149 -5
- package/dist/cli.js +180 -8
- package/dist/commands/chaos.js +186 -0
- package/dist/commands/fix.js +177 -0
- package/dist/commands/http.js +2 -0
- package/dist/commands/snapshot.js +81 -0
- package/dist/commands/up.js +2 -0
- package/dist/commands/verify.js +64 -0
- package/dist/commands/watch.js +9 -1
- package/dist/config-template.js +1 -0
- package/dist/config.js +22 -1
- package/dist/curl.js +59 -0
- package/dist/errors.js +3 -0
- package/dist/help.js +279 -0
- package/dist/main.js +4 -2
- package/dist/serve.js +26 -1
- package/dist/shapes.js +216 -0
- package/dist/shortcuts.js +29 -4
- package/dist/signature.js +207 -0
- package/dist/style.js +6 -0
- package/dist/terminal.js +96 -16
- package/dist/traffic.js +247 -0
- package/dist/tui/app.js +748 -0
- package/dist/tui/chrome.js +222 -0
- package/dist/tui/input.js +82 -0
- package/dist/tui/model.js +174 -0
- package/dist/tui/requests.js +271 -0
- package/dist/tui/screen.js +91 -0
- package/dist/tui/state.js +88 -0
- package/dist/tui/tabs.js +239 -0
- package/dist/tui/text.js +182 -0
- package/package.json +1 -1
package/dist/shortcuts.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
-
import {
|
|
2
|
+
import { curlCommand } from './curl.js';
|
|
3
|
+
import { mainPublicUrl, printable } from './output.js';
|
|
3
4
|
/** The keys act on tunnels that show exchanges: http, up and watch. Connect and mcp keep the terminal as it is. */
|
|
4
5
|
export function shortcutTarget(running) {
|
|
5
6
|
switch (running.kind) {
|
|
6
7
|
case 'http':
|
|
7
|
-
return { inspectorUrl: running.inspectorUrl, publicUrl: running.publicUrl };
|
|
8
|
+
return { inspectorUrl: running.inspectorUrl, publicUrl: running.publicUrl, entry: () => running.url };
|
|
8
9
|
case 'up': {
|
|
9
10
|
const published = running.services.find((service) => service.publicUrl !== null);
|
|
10
11
|
return {
|
|
11
12
|
inspectorUrl: running.inspectorUrl,
|
|
12
13
|
publicUrl: published === undefined ? null : mainPublicUrl(published.publicUrl, published.customDomains),
|
|
14
|
+
entry: (name) => running.services.find((service) => service.name === name)?.url ?? null,
|
|
13
15
|
};
|
|
14
16
|
}
|
|
15
17
|
case 'watch':
|
|
16
|
-
return { inspectorUrl: running.inspectorUrl, publicUrl: null };
|
|
18
|
+
return { inspectorUrl: running.inspectorUrl, publicUrl: null, entry: () => running.replayTarget };
|
|
17
19
|
default:
|
|
18
20
|
return null;
|
|
19
21
|
}
|
|
@@ -37,7 +39,7 @@ export function keysOf(chunk) {
|
|
|
37
39
|
return keys;
|
|
38
40
|
}
|
|
39
41
|
/** What ? lists. */
|
|
40
|
-
export const KEYS_HELP = '\u2191\u2193 pick a request, enter shows it, i opens it in the inspector, r replays it, o inspector, c copy the public URL, q quit';
|
|
42
|
+
export const KEYS_HELP = '\u2191\u2193 pick a request, enter shows it, i opens it in the inspector, r replays it, y copies it as curl, o inspector, c copy the public URL, q quit';
|
|
41
43
|
/**
|
|
42
44
|
* Reads single keys while a tunnel runs: o opens the inspector, c copies the public URL, r replays the newest
|
|
43
45
|
* request, q and Ctrl+C stop. The terminal is put in raw mode for it, so Ctrl+C reaches here as a key and is turned
|
|
@@ -67,6 +69,29 @@ export function startShortcuts(input, actions) {
|
|
|
67
69
|
say('No request has come in yet.', true);
|
|
68
70
|
}
|
|
69
71
|
break;
|
|
72
|
+
case 'y': {
|
|
73
|
+
const picked = actions.lastExchange();
|
|
74
|
+
const base = picked === null ? null : target.entry(picked.serviceName);
|
|
75
|
+
if (picked === null) {
|
|
76
|
+
say('No request has come in yet.', true);
|
|
77
|
+
}
|
|
78
|
+
else if (base === null) {
|
|
79
|
+
say('There is nowhere on this machine to send it: start with --replay-target.', true);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
const copy = curlCommand(picked, base);
|
|
83
|
+
void actions.copy(copy.command).then((how) => {
|
|
84
|
+
const notes = [
|
|
85
|
+
...(copy.credentials.length > 0
|
|
86
|
+
? [`it carries ${copy.credentials.join(', ')}; mind where you paste it`]
|
|
87
|
+
: []),
|
|
88
|
+
...(copy.bodyLeftOut === null ? [] : [`no body: ${copy.bodyLeftOut}`]),
|
|
89
|
+
];
|
|
90
|
+
say(`${how === 'copied' ? 'Copied' : 'Sent to the clipboard of the terminal:'} curl for ${printable(picked.request.method)} ${printable(picked.request.path)} to ${base}${notes.length > 0 ? ` (${notes.join('; ')})` : ''}`);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
70
95
|
case 'i': {
|
|
71
96
|
const picked = actions.lastExchange();
|
|
72
97
|
if (target.inspectorUrl === null) {
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { createHmac, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* Checks the signature a webhook provider put on a request, with the signing secret the user named in an environment
|
|
4
|
+
* variable. It runs on this machine over what was captured; the secret never leaves the process, is never printed and
|
|
5
|
+
* never written to the traffic log. The result is shown next to the request; nothing is refused because of it.
|
|
6
|
+
*/
|
|
7
|
+
/** The providers whose signatures can be checked from the request alone. */
|
|
8
|
+
export const SIGNED_PROVIDERS = ['stripe', 'github', 'gitlab', 'shopify', 'slack', 'svix', 'paddle', 'linear'];
|
|
9
|
+
export function isSignedProvider(name) {
|
|
10
|
+
return SIGNED_PROVIDERS.includes(name);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Stripe, Slack and Svix refuse a signature whose timestamp is older than five minutes by default; a replay keeps the
|
|
14
|
+
* original timestamp, so its signature is valid and still refused by those libraries. Said next to the result.
|
|
15
|
+
*/
|
|
16
|
+
export const TOLERANCE_MS = 5 * 60_000;
|
|
17
|
+
/** The check of one request against the secrets of its service; null when none of them is for this request. */
|
|
18
|
+
export function checkSignature(exchange, secrets, now = Date.now()) {
|
|
19
|
+
const headers = new Map(exchange.request.headers.map(([name, value]) => [name.toLowerCase(), value]));
|
|
20
|
+
for (const entry of secrets) {
|
|
21
|
+
const verify = VERIFIERS[entry.provider];
|
|
22
|
+
const signed = verify.signs(headers);
|
|
23
|
+
if (!signed) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const { provider, variable } = entry;
|
|
27
|
+
if (exchange.request.bodyTruncated) {
|
|
28
|
+
return {
|
|
29
|
+
result: 'unchecked',
|
|
30
|
+
provider,
|
|
31
|
+
variable,
|
|
32
|
+
reason: 'the body was longer than the capture limit, so the signature cannot be checked',
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const body = Buffer.from(exchange.request.bodyBase64, 'base64');
|
|
36
|
+
const outcome = verify.check(headers, body, entry.secret);
|
|
37
|
+
if (typeof outcome === 'string') {
|
|
38
|
+
return { result: 'invalid', provider, variable, reason: outcome };
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
result: 'valid',
|
|
42
|
+
provider,
|
|
43
|
+
variable,
|
|
44
|
+
ageMs: outcome.timestamp === null ? null : now - outcome.timestamp,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const MISMATCH = 'does not match the secret; check that it is the signing secret of this endpoint and that the body reaches the handler unchanged';
|
|
50
|
+
const VERIFIERS = {
|
|
51
|
+
// Stripe-Signature: t=<seconds>,v1=<hex>[,v1=...]; HMAC-SHA256 over "<t>.<body>"
|
|
52
|
+
stripe: {
|
|
53
|
+
signs: (h) => h.has('stripe-signature'),
|
|
54
|
+
check: (h, body, secret) => {
|
|
55
|
+
const parts = (h.get('stripe-signature') ?? '').split(',').map((part) => part.split('=', 2));
|
|
56
|
+
const t = parts.find(([key]) => key === 't')?.[1];
|
|
57
|
+
const signatures = parts.filter(([key]) => key === 'v1').map(([, value]) => value ?? '');
|
|
58
|
+
if (t === undefined || !/^\d+$/.test(t) || signatures.length === 0) {
|
|
59
|
+
return 'the Stripe-Signature header has no t= and v1= parts';
|
|
60
|
+
}
|
|
61
|
+
const expected = hmac(secret, Buffer.concat([Buffer.from(`${t}.`), body]), 'hex');
|
|
62
|
+
return signatures.some((value) => same(value, expected)) ? { timestamp: Number(t) * 1000 } : MISMATCH;
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
// X-Hub-Signature-256: sha256=<hex>; HMAC-SHA256 over the body
|
|
66
|
+
github: {
|
|
67
|
+
signs: (h) => h.has('x-hub-signature-256'),
|
|
68
|
+
check: (h, body, secret) => {
|
|
69
|
+
const value = h.get('x-hub-signature-256') ?? '';
|
|
70
|
+
if (!value.startsWith('sha256=')) {
|
|
71
|
+
return 'the X-Hub-Signature-256 header does not start with sha256=';
|
|
72
|
+
}
|
|
73
|
+
return same(value.slice(7), hmac(secret, body, 'hex')) ? { timestamp: null } : MISMATCH;
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
// X-Gitlab-Token: the secret token itself
|
|
77
|
+
gitlab: {
|
|
78
|
+
signs: (h) => h.has('x-gitlab-token'),
|
|
79
|
+
check: (h, _body, secret) => same(h.get('x-gitlab-token') ?? '', secret)
|
|
80
|
+
? { timestamp: null }
|
|
81
|
+
: 'the X-Gitlab-Token header is not the secret token of this webhook',
|
|
82
|
+
},
|
|
83
|
+
// X-Shopify-Hmac-Sha256: base64 HMAC-SHA256 over the body
|
|
84
|
+
shopify: {
|
|
85
|
+
signs: (h) => h.has('x-shopify-hmac-sha256'),
|
|
86
|
+
check: (h, body, secret) => same(h.get('x-shopify-hmac-sha256') ?? '', hmac(secret, body, 'base64')) ? { timestamp: null } : MISMATCH,
|
|
87
|
+
},
|
|
88
|
+
// X-Slack-Signature: v0=<hex>; HMAC-SHA256 over "v0:<X-Slack-Request-Timestamp>:<body>"
|
|
89
|
+
slack: {
|
|
90
|
+
signs: (h) => h.has('x-slack-signature'),
|
|
91
|
+
check: (h, body, secret) => {
|
|
92
|
+
const ts = h.get('x-slack-request-timestamp') ?? '';
|
|
93
|
+
const value = h.get('x-slack-signature') ?? '';
|
|
94
|
+
if (!/^\d+$/.test(ts) || !value.startsWith('v0=')) {
|
|
95
|
+
return 'X-Slack-Request-Timestamp or the v0= of X-Slack-Signature is missing';
|
|
96
|
+
}
|
|
97
|
+
const expected = hmac(secret, Buffer.concat([Buffer.from(`v0:${ts}:`), body]), 'hex');
|
|
98
|
+
return same(value.slice(3), expected) ? { timestamp: Number(ts) * 1000 } : MISMATCH;
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
// svix-id, svix-timestamp, svix-signature: "v1,<base64> ..."; the secret is whsec_<base64 key>
|
|
102
|
+
svix: {
|
|
103
|
+
signs: (h) => h.has('svix-signature') || h.has('webhook-signature'),
|
|
104
|
+
check: (h, body, secret) => {
|
|
105
|
+
const id = h.get('svix-id') ?? h.get('webhook-id') ?? '';
|
|
106
|
+
const ts = h.get('svix-timestamp') ?? h.get('webhook-timestamp') ?? '';
|
|
107
|
+
const value = h.get('svix-signature') ?? h.get('webhook-signature') ?? '';
|
|
108
|
+
if (id === '' || !/^\d+$/.test(ts)) {
|
|
109
|
+
return 'the id or timestamp header of the signature is missing';
|
|
110
|
+
}
|
|
111
|
+
const key = Buffer.from(secret.startsWith('whsec_') ? secret.slice(6) : secret, 'base64');
|
|
112
|
+
const expected = createHmac('sha256', key)
|
|
113
|
+
.update(Buffer.concat([Buffer.from(`${id}.${ts}.`), body]))
|
|
114
|
+
.digest('base64');
|
|
115
|
+
const signatures = value.split(' ').map((entry) => entry.split(',', 2));
|
|
116
|
+
return signatures.some(([version, signature]) => version === 'v1' && same(signature ?? '', expected))
|
|
117
|
+
? { timestamp: Number(ts) * 1000 }
|
|
118
|
+
: MISMATCH;
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
// Paddle-Signature: ts=<seconds>;h1=<hex>; HMAC-SHA256 over "<ts>:<body>"
|
|
122
|
+
paddle: {
|
|
123
|
+
signs: (h) => h.has('paddle-signature'),
|
|
124
|
+
check: (h, body, secret) => {
|
|
125
|
+
const parts = new Map((h.get('paddle-signature') ?? '').split(';').map((part) => part.split('=', 2)));
|
|
126
|
+
const ts = parts.get('ts') ?? '';
|
|
127
|
+
const h1 = parts.get('h1') ?? '';
|
|
128
|
+
if (!/^\d+$/.test(ts) || h1 === '') {
|
|
129
|
+
return 'the Paddle-Signature header has no ts= and h1= parts';
|
|
130
|
+
}
|
|
131
|
+
const expected = hmac(secret, Buffer.concat([Buffer.from(`${ts}:`), body]), 'hex');
|
|
132
|
+
return same(h1, expected) ? { timestamp: Number(ts) * 1000 } : MISMATCH;
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
// Linear-Signature: hex HMAC-SHA256 over the body
|
|
136
|
+
linear: {
|
|
137
|
+
signs: (h) => h.has('linear-signature'),
|
|
138
|
+
check: (h, body, secret) => same(h.get('linear-signature') ?? '', hmac(secret, body, 'hex')) ? { timestamp: null } : MISMATCH,
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
function hmac(secret, data, encoding) {
|
|
142
|
+
return createHmac('sha256', secret).update(data).digest(encoding);
|
|
143
|
+
}
|
|
144
|
+
/** Compares in constant time; texts of different length are different. */
|
|
145
|
+
function same(a, b) {
|
|
146
|
+
const left = Buffer.from(a);
|
|
147
|
+
const right = Buffer.from(b);
|
|
148
|
+
return left.byteLength === right.byteLength && timingSafeEqual(left, right);
|
|
149
|
+
}
|
|
150
|
+
/** An environment variable name as hopwire.yaml and --webhook-secret take it. */
|
|
151
|
+
export function isVariableName(name) {
|
|
152
|
+
return /^[A-Za-z_][A-Za-z0-9_]{0,127}$/.test(name);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The headers of a request signed again for `provider` at `timestampSeconds` (hopwire verify and chaos): the recorded
|
|
156
|
+
* signature headers are replaced, the rest kept. With `wrongKey` the signature is made with another key, for a handler
|
|
157
|
+
* that must refuse it. Without a timestamp in the provider's scheme (GitHub, GitLab, Shopify, Linear) only the
|
|
158
|
+
* signature changes.
|
|
159
|
+
*/
|
|
160
|
+
export function signHeaders(headers, body, secret, timestampSeconds, wrongKey = false) {
|
|
161
|
+
const key = wrongKey ? `${secret.secret}-not-the-signing-secret` : secret.secret;
|
|
162
|
+
const t = String(timestampSeconds);
|
|
163
|
+
const set = new Map();
|
|
164
|
+
switch (secret.provider) {
|
|
165
|
+
case 'stripe':
|
|
166
|
+
set.set('stripe-signature', `t=${t},v1=${hmac(key, Buffer.concat([Buffer.from(`${t}.`), body]), 'hex')}`);
|
|
167
|
+
break;
|
|
168
|
+
case 'github':
|
|
169
|
+
set.set('x-hub-signature-256', `sha256=${hmac(key, body, 'hex')}`);
|
|
170
|
+
break;
|
|
171
|
+
case 'gitlab':
|
|
172
|
+
set.set('x-gitlab-token', key);
|
|
173
|
+
break;
|
|
174
|
+
case 'shopify':
|
|
175
|
+
set.set('x-shopify-hmac-sha256', hmac(key, body, 'base64'));
|
|
176
|
+
break;
|
|
177
|
+
case 'slack':
|
|
178
|
+
set.set('x-slack-request-timestamp', t);
|
|
179
|
+
set.set('x-slack-signature', `v0=${hmac(key, Buffer.concat([Buffer.from(`v0:${t}:`), body]), 'hex')}`);
|
|
180
|
+
break;
|
|
181
|
+
case 'svix': {
|
|
182
|
+
const lower = new Map(headers.map(([name, value]) => [name.toLowerCase(), value]));
|
|
183
|
+
const prefix = lower.has('webhook-id') && !lower.has('svix-id') ? 'webhook' : 'svix';
|
|
184
|
+
const id = lower.get(`${prefix}-id`) ?? 'msg_hopwire';
|
|
185
|
+
const raw = Buffer.from(key.startsWith('whsec_') ? key.slice(6) : key, 'base64');
|
|
186
|
+
const signature = createHmac('sha256', wrongKey ? Buffer.concat([raw, Buffer.from('x')]) : raw)
|
|
187
|
+
.update(Buffer.concat([Buffer.from(`${id}.${t}.`), body]))
|
|
188
|
+
.digest('base64');
|
|
189
|
+
set.set(`${prefix}-id`, id);
|
|
190
|
+
set.set(`${prefix}-timestamp`, t);
|
|
191
|
+
set.set(`${prefix}-signature`, `v1,${signature}`);
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
case 'paddle':
|
|
195
|
+
set.set('paddle-signature', `ts=${t};h1=${hmac(key, Buffer.concat([Buffer.from(`${t}:`), body]), 'hex')}`);
|
|
196
|
+
break;
|
|
197
|
+
case 'linear':
|
|
198
|
+
set.set('linear-signature', hmac(key, body, 'hex'));
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
const kept = headers.filter(([name]) => !set.has(name.toLowerCase()));
|
|
202
|
+
return [...kept, ...set.entries()];
|
|
203
|
+
}
|
|
204
|
+
/** Whether a request carries a signature of `provider`: only those are signed again. */
|
|
205
|
+
export function signedBy(headers, provider) {
|
|
206
|
+
return VERIFIERS[provider].signs(new Map(headers.map(([name, value]) => [name.toLowerCase(), value])));
|
|
207
|
+
}
|
package/dist/style.js
CHANGED
|
@@ -21,6 +21,12 @@ export function decorate(line) {
|
|
|
21
21
|
if (line === 'Next steps') {
|
|
22
22
|
return paint(line, BOLD);
|
|
23
23
|
}
|
|
24
|
+
// A check of verify or chaos: its verdict first
|
|
25
|
+
const verdict = /^(pass|FAIL|skip)( {2}.*)$/.exec(line);
|
|
26
|
+
if (verdict !== null) {
|
|
27
|
+
const [, word = '', rest = ''] = verdict;
|
|
28
|
+
return `${paint(word, word === 'pass' ? `${BOLD};${GREEN}` : word === 'FAIL' ? `${BOLD};${RED}` : DIM)}${rest}`;
|
|
29
|
+
}
|
|
24
30
|
if (line.startsWith('Requests appear below') || line === 'Press Ctrl+C to stop.') {
|
|
25
31
|
return paint(line, DIM);
|
|
26
32
|
}
|
package/dist/terminal.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { printable } from './output.js';
|
|
2
2
|
import { decorate } from './style.js';
|
|
3
|
+
import { TOLERANCE_MS, checkSignature } from './signature.js';
|
|
3
4
|
import { recogniseWebhook } from './webhook.js';
|
|
4
5
|
/** The dark theme of docs/design_system.md: brand tokens and status colours as text. */
|
|
5
6
|
const DARK = {
|
|
@@ -34,13 +35,37 @@ export function terminalLook(env) {
|
|
|
34
35
|
return {
|
|
35
36
|
truecolor: /^(truecolor|24bit)$/i.test(env.COLORTERM ?? ''),
|
|
36
37
|
palette: background === 7 || background === 15 ? LIGHT : DARK,
|
|
37
|
-
|
|
38
|
-
links: env.TERM !== 'linux',
|
|
38
|
+
links: supportsHyperlinks(env),
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Whether the terminal turns OSC 8 into a link. Only terminals known to do it get the escape; the others show the URL
|
|
43
|
+
* as text, which they find by themselves (Cmd+click in Apple Terminal). FORCE_HYPERLINK=1 or 0 overrides, as other
|
|
44
|
+
* command line tools read it.
|
|
45
|
+
*/
|
|
46
|
+
export function supportsHyperlinks(env) {
|
|
47
|
+
if (env.FORCE_HYPERLINK !== undefined && env.FORCE_HYPERLINK !== '') {
|
|
48
|
+
return env.FORCE_HYPERLINK !== '0' && env.FORCE_HYPERLINK.toLowerCase() !== 'false';
|
|
49
|
+
}
|
|
50
|
+
// Inside tmux or screen the escape only passes when they are set up for it
|
|
51
|
+
if (env.TMUX !== undefined || /^(screen|tmux)/.test(env.TERM ?? '')) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
if (['iTerm.app', 'WezTerm', 'vscode', 'ghostty', 'Hyper', 'WarpTerminal'].includes(env.TERM_PROGRAM ?? '')) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
if (env.KITTY_WINDOW_ID !== undefined || env.WT_SESSION !== undefined || env.DOMTERM !== undefined) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
if (/^(xterm-kitty|xterm-ghostty|alacritty|foot)/.test(env.TERM ?? '')) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
// GNOME Terminal and the other VTE terminals since 0.50
|
|
64
|
+
return Number(env.VTE_VERSION ?? '0') >= 5000;
|
|
65
|
+
}
|
|
41
66
|
const RESET = '\u001b[0m';
|
|
42
67
|
/** Text in a colour, optionally bold. */
|
|
43
|
-
function paint(look, text, tone, bold = false) {
|
|
68
|
+
export function paint(look, text, tone, bold = false) {
|
|
44
69
|
if (text === '') {
|
|
45
70
|
return '';
|
|
46
71
|
}
|
|
@@ -62,7 +87,7 @@ const URL_PATTERN = /https?:\/\/[^\s"'<>()]*[^\s"'<>().,;:]/g;
|
|
|
62
87
|
/** Width of a label and its padding: every line of facts starts with one. */
|
|
63
88
|
const LABEL_WIDTH = 13;
|
|
64
89
|
/** Labels that warn. */
|
|
65
|
-
const WARNING_LABELS = new Set(['Notice', 'Limit']);
|
|
90
|
+
const WARNING_LABELS = new Set(['Notice', 'Limit', 'Drift']);
|
|
66
91
|
/** The line every tunnel prints after its facts, in the words of output.ts. */
|
|
67
92
|
const WAITING_LINE = 'Requests appear below as they arrive: method, path, status, duration. Press Ctrl+C to stop.';
|
|
68
93
|
/**
|
|
@@ -125,10 +150,23 @@ export function wrap(text, width) {
|
|
|
125
150
|
export class TerminalScreen {
|
|
126
151
|
options;
|
|
127
152
|
headerShown = false;
|
|
153
|
+
/** Checks the signature of a webhook row; set once the tunnel runs and its secrets are known */
|
|
154
|
+
verify = () => null;
|
|
128
155
|
seen = new Set();
|
|
129
156
|
constructor(options) {
|
|
130
157
|
this.options = options;
|
|
131
158
|
}
|
|
159
|
+
/** The secrets of the running tunnel by service: the rows of their webhooks show the signature from now on. */
|
|
160
|
+
checkSignatures(secrets) {
|
|
161
|
+
this.verify = (exchange) => {
|
|
162
|
+
const ofService = secrets.get(exchange.serviceName);
|
|
163
|
+
return ofService === undefined || ofService.length === 0 ? null : checkSignature(exchange, ofService);
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/** The signature check of a webhook row; null without a secret for its service or for a request that is none. */
|
|
167
|
+
signatureOf(exchange) {
|
|
168
|
+
return recogniseWebhook(exchange) === null ? null : this.verify(exchange);
|
|
169
|
+
}
|
|
132
170
|
get look() {
|
|
133
171
|
return this.options.look;
|
|
134
172
|
}
|
|
@@ -239,15 +277,15 @@ export class TerminalScreen {
|
|
|
239
277
|
}
|
|
240
278
|
const keys = [];
|
|
241
279
|
if (this.seen.has('Inspector')) {
|
|
242
|
-
keys.push(['o', '
|
|
280
|
+
keys.push(['o', 'inspector']);
|
|
243
281
|
}
|
|
244
282
|
if (this.seen.has('Public URL')) {
|
|
245
|
-
keys.push(['c', 'copy
|
|
283
|
+
keys.push(['c', 'copy URL']);
|
|
246
284
|
}
|
|
247
285
|
if (this.seen.has('Inspector')) {
|
|
248
286
|
keys.push(['r', 'replay']);
|
|
249
287
|
}
|
|
250
|
-
keys.push(['\u2191\u2193', 'pick
|
|
288
|
+
keys.push(['\u2191\u2193', 'pick'], ['enter', 'details'], ['y', 'curl'], ['?', 'keys'], ['q', 'quit']);
|
|
251
289
|
return keys
|
|
252
290
|
.map(([key, what]) => `${paint(look, key, null, true)} ${paint(look, what, look.palette.muted)}`)
|
|
253
291
|
.join(paint(look, ' · ', look.palette.muted));
|
|
@@ -275,11 +313,17 @@ export class TerminalScreen {
|
|
|
275
313
|
// The webhook after the path, dimmed; the path gives way first, down to a third of the column
|
|
276
314
|
const webhook = recogniseWebhook(exchange);
|
|
277
315
|
const named = webhook === null ? '' : printable(`${webhook.provider}${webhook.event === null ? '' : ` ${webhook.event}`}`);
|
|
278
|
-
|
|
316
|
+
// The signature after the name: valid, valid but older than the providers' tolerance, wrong, not checkable
|
|
317
|
+
const check = webhook === null ? null : this.verify(exchange);
|
|
318
|
+
const mark = check === null ? null : signatureMark(check, palette);
|
|
319
|
+
const markWidth = mark === null ? 0 : 2;
|
|
320
|
+
const room = named === '' ? pathWidth : Math.max(Math.floor(pathWidth / 3), pathWidth - named.length - 2 - markWidth);
|
|
279
321
|
const cutPath = truncate(path, room);
|
|
280
|
-
const cutName = named === '' ? '' : truncate(named, Math.max(pathWidth - cutPath.length - 2, 0));
|
|
281
|
-
const
|
|
282
|
-
const
|
|
322
|
+
const cutName = named === '' ? '' : truncate(named, Math.max(pathWidth - cutPath.length - 2 - markWidth, 0));
|
|
323
|
+
const used = cutPath.length + (cutName === '' ? 0 : cutName.length + 2 + markWidth);
|
|
324
|
+
const pad = ' '.repeat(Math.max(pathWidth - used, 0));
|
|
325
|
+
const signed = mark === null || cutName === '' ? '' : ` ${paint(look, mark.symbol, mark.tone, true)}`;
|
|
326
|
+
const shown = `${cutPath}${cutName === '' ? '' : ` ${paint(look, cutName, palette.muted)}${signed}`}${pad}`;
|
|
283
327
|
const slow = response.durationMs >= 1000;
|
|
284
328
|
return (`${label}${time}${paint(look, method, null, true)} ${shown} ` +
|
|
285
329
|
`${paint(look, status, statusTone(palette, response.status))}` +
|
|
@@ -345,6 +389,13 @@ export class TerminalScreen {
|
|
|
345
389
|
SOURCE_WORDS[exchange.source] ?? exchange.source,
|
|
346
390
|
];
|
|
347
391
|
lines.push(`${paint(look, '┌', palette.muted)} ${title.join(paint(look, ' · ', palette.muted))}`);
|
|
392
|
+
const check = webhook === null ? null : this.verify(exchange);
|
|
393
|
+
if (check !== null) {
|
|
394
|
+
const mark = signatureMark(check, palette);
|
|
395
|
+
for (const [index, part] of wrap(signatureSentence(check), this.width - 6).entries()) {
|
|
396
|
+
lines.push(`${bar} ${index === 0 ? paint(look, mark.symbol, mark.tone, true) : ' '} ${part}`);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
348
399
|
lines.push(`${bar} ${paint(look, 'Request', null, true)}`);
|
|
349
400
|
lines.push(...this.part(request.headers, request.bodyBase64, request.bodyTruncated).map((line) => `${bar} ${line}`));
|
|
350
401
|
lines.push(`${bar} ${paint(look, 'Response', null, true)} ${paint(look, String(response.status), statusTone(palette, response.status))} ${paint(look, `· ${formatDuration(response.durationMs)}`, palette.muted)}`);
|
|
@@ -450,7 +501,7 @@ export function sparkline(counts) {
|
|
|
450
501
|
'')
|
|
451
502
|
.join('');
|
|
452
503
|
}
|
|
453
|
-
const SOURCE_WORDS = {
|
|
504
|
+
export const SOURCE_WORDS = {
|
|
454
505
|
local: 'local',
|
|
455
506
|
p2p_peer: 'from a teammate',
|
|
456
507
|
public_edge: 'through the edge',
|
|
@@ -460,7 +511,7 @@ const SOURCE_WORDS = {
|
|
|
460
511
|
const DETAIL_HEADERS = 14;
|
|
461
512
|
const DETAIL_BODY_LINES = 24;
|
|
462
513
|
/** Headers whose values are credentials: shown shortened in the details block. */
|
|
463
|
-
const CREDENTIAL_HEADERS = new Set([
|
|
514
|
+
export const CREDENTIAL_HEADERS = new Set([
|
|
464
515
|
'authorization',
|
|
465
516
|
'proxy-authorization',
|
|
466
517
|
'cookie',
|
|
@@ -469,7 +520,7 @@ const CREDENTIAL_HEADERS = new Set([
|
|
|
469
520
|
'api-key',
|
|
470
521
|
]);
|
|
471
522
|
/** The first characters of a secret, enough to tell two apart. */
|
|
472
|
-
function hidden(value) {
|
|
523
|
+
export function hidden(value) {
|
|
473
524
|
return value.length <= 8 ? '••••' : `${value.slice(0, 6)}… (${value.length} characters)`;
|
|
474
525
|
}
|
|
475
526
|
/**
|
|
@@ -492,8 +543,37 @@ export function bodyLines(body, truncated) {
|
|
|
492
543
|
}
|
|
493
544
|
return text.split(/\r?\n/);
|
|
494
545
|
}
|
|
546
|
+
/** The mark of a signature check in a row and in the details. */
|
|
547
|
+
export function signatureMark(check, palette) {
|
|
548
|
+
switch (check.result) {
|
|
549
|
+
case 'valid':
|
|
550
|
+
return check.ageMs !== null && check.ageMs > TOLERANCE_MS
|
|
551
|
+
? { symbol: '\u2713', tone: palette.warn }
|
|
552
|
+
: { symbol: '\u2713', tone: palette.ok };
|
|
553
|
+
case 'invalid':
|
|
554
|
+
return { symbol: '\u2717', tone: palette.err };
|
|
555
|
+
case 'unchecked':
|
|
556
|
+
return { symbol: '?', tone: palette.muted };
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
/** What a signature check found, in one sentence that names the variable, never the secret. */
|
|
560
|
+
export function signatureSentence(check) {
|
|
561
|
+
const where = `${check.provider} signature, checked with ${check.variable}`;
|
|
562
|
+
switch (check.result) {
|
|
563
|
+
case 'valid': {
|
|
564
|
+
const minutes = check.ageMs === null ? 0 : Math.floor(check.ageMs / 60_000);
|
|
565
|
+
return check.ageMs !== null && check.ageMs > TOLERANCE_MS
|
|
566
|
+
? `Valid ${where}, but signed ${minutes} minutes ago: libraries that allow 5 minutes refuse it, as those of Stripe, Slack and Svix do by default (a replay keeps the original signature)`
|
|
567
|
+
: `Valid ${where}`;
|
|
568
|
+
}
|
|
569
|
+
case 'invalid':
|
|
570
|
+
return `Wrong ${where}: ${check.reason}`;
|
|
571
|
+
case 'unchecked':
|
|
572
|
+
return `Not checked (${where}): ${check.reason}`;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
495
575
|
/** The time of day as HH:MM:SS on this machine's clock. */
|
|
496
|
-
function clock(timestamp) {
|
|
576
|
+
export function clock(timestamp) {
|
|
497
577
|
const date = new Date(timestamp);
|
|
498
578
|
return [date.getHours(), date.getMinutes(), date.getSeconds()]
|
|
499
579
|
.map((part) => String(part).padStart(2, '0'))
|
|
@@ -548,7 +628,7 @@ function sourceTag(exchange, look) {
|
|
|
548
628
|
return `${paint(look, 'local', palette.muted)}${ws}`;
|
|
549
629
|
}
|
|
550
630
|
}
|
|
551
|
-
function statusTone(palette, status) {
|
|
631
|
+
export function statusTone(palette, status) {
|
|
552
632
|
if (status >= 500) {
|
|
553
633
|
return palette.err;
|
|
554
634
|
}
|