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/bin.js
CHANGED
|
@@ -8,14 +8,19 @@ import { wantsColor } from './style.js';
|
|
|
8
8
|
import { LiveFeed } from './live.js';
|
|
9
9
|
import { STARTING, Spinner, TerminalScreen, hopFrames, terminalLook } from './terminal.js';
|
|
10
10
|
import { readVersion } from './version.js';
|
|
11
|
+
import { App } from './tui/app.js';
|
|
12
|
+
import { Facts, tunnelInfo } from './tui/model.js';
|
|
13
|
+
import { Screen } from './tui/screen.js';
|
|
11
14
|
const argv = process.argv.slice(2);
|
|
12
15
|
const look = terminalLook(process.env);
|
|
13
|
-
|
|
14
|
-
const
|
|
16
|
+
// --plain: the lines a pipe gets, on a terminal too. No colour, no keys, nothing redrawn
|
|
17
|
+
const plain = argv.includes('--plain');
|
|
18
|
+
const color = !plain && wantsColor(process.stdout, process.env);
|
|
19
|
+
const colorErrors = !plain && wantsColor(process.stderr, process.env);
|
|
15
20
|
// A command that runs a tunnel, not its help: only these get the spinner and the keys
|
|
16
21
|
const command = argv[0] ?? '';
|
|
17
22
|
const tunnel = command in STARTING && !argv.some((arg) => arg === '--help' || arg === '-h' || arg === '--version');
|
|
18
|
-
const keys = tunnel && process.stdin.isTTY === true && command !== 'connect';
|
|
23
|
+
const keys = tunnel && !plain && process.stdin.isTTY === true && command !== 'connect';
|
|
19
24
|
// hopwire --version on a terminal shows the mark beside the version; a script reading it gets the bare version
|
|
20
25
|
const versionOnly = argv.length === 1 && argv[0] === '--version';
|
|
21
26
|
const screen = color || colorErrors
|
|
@@ -30,8 +35,28 @@ const screen = color || colorErrors
|
|
|
30
35
|
const spinner = tunnel && colorErrors && screen !== null ? new Spinner(process.stderr, look, `${STARTING[command] ?? ''}...`) : null;
|
|
31
36
|
spinner?.start();
|
|
32
37
|
const quiet = () => spinner?.stop();
|
|
33
|
-
//
|
|
34
|
-
|
|
38
|
+
// On a terminal with a keyboard http, up and watch take the whole window once they run: the full-screen view.
|
|
39
|
+
// Until then their lines are kept, not shown; a start that fails prints them as before
|
|
40
|
+
const fullScreen = keys && color && process.stdout.isTTY === true;
|
|
41
|
+
const facts = fullScreen ? new Facts() : null;
|
|
42
|
+
let app = null;
|
|
43
|
+
let held = fullScreen ? [] : null;
|
|
44
|
+
/** What was written to standard error while the view ran, with its time: shown again after it is gone */
|
|
45
|
+
const problems = [];
|
|
46
|
+
const release = () => {
|
|
47
|
+
if (held !== null) {
|
|
48
|
+
const lines = held;
|
|
49
|
+
held = null;
|
|
50
|
+
for (const { error, line } of lines) {
|
|
51
|
+
;
|
|
52
|
+
(error ? process.stderr : process.stdout).write(`${line}\n`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
// A start that was stopped or failed never opened the view: its lines are shown as they would have been
|
|
57
|
+
process.once('exit', release);
|
|
58
|
+
// A tunnel's feed on a colour terminal without a keyboard: rows scroll above a status line that stays put
|
|
59
|
+
const live = tunnel && color && !fullScreen && screen !== null
|
|
35
60
|
? new LiveFeed(screen, process.stdout, () => process.stdout.columns ?? 100)
|
|
36
61
|
: null;
|
|
37
62
|
process.once('exit', () => live?.stop());
|
|
@@ -61,6 +86,87 @@ const hop = () => {
|
|
|
61
86
|
process.stdout.write(`\u001b[${height}A\r\u001b[J`);
|
|
62
87
|
}
|
|
63
88
|
};
|
|
89
|
+
/**
|
|
90
|
+
* Opens the full-screen view for a tunnel that runs: the terminal goes into raw mode and the alternate screen, and
|
|
91
|
+
* both are given back when the process exits, however it does.
|
|
92
|
+
*/
|
|
93
|
+
function openView(info, known, running) {
|
|
94
|
+
let rowWidth = 100;
|
|
95
|
+
const rows = new TerminalScreen({ look, width: () => rowWidth, version: readVersion(), shortcuts: false });
|
|
96
|
+
if ((running.kind === 'http' || running.kind === 'up') && running.webhookSecrets !== undefined) {
|
|
97
|
+
rows.checkSignatures(running.webhookSecrets);
|
|
98
|
+
}
|
|
99
|
+
const dataDir = argv.findIndex((arg) => arg === '--data-dir' || arg.startsWith('--data-dir='));
|
|
100
|
+
const dataDirValue = dataDir < 0
|
|
101
|
+
? null
|
|
102
|
+
: (argv[dataDir] ?? '').includes('=')
|
|
103
|
+
? (argv[dataDir] ?? '').split('=')[1]
|
|
104
|
+
: argv[dataDir + 1];
|
|
105
|
+
const view = new Screen(process.stdout, () => opened.draw());
|
|
106
|
+
const opened = new App(view, {
|
|
107
|
+
version: readVersion(),
|
|
108
|
+
info,
|
|
109
|
+
facts: known,
|
|
110
|
+
extraArgs: dataDirValue === null || dataDirValue === undefined ? '' : ` --data-dir ${dataDirValue}`,
|
|
111
|
+
ui: {
|
|
112
|
+
look,
|
|
113
|
+
row: (entry, width) => {
|
|
114
|
+
rowWidth = width;
|
|
115
|
+
return rows.row(entry.exchange, entry.label);
|
|
116
|
+
},
|
|
117
|
+
signature: (entry) => rows.signatureOf(entry.exchange),
|
|
118
|
+
now: Date.now,
|
|
119
|
+
},
|
|
120
|
+
actions: {
|
|
121
|
+
openBrowser: (url) => openBrowser(url),
|
|
122
|
+
copy: (text) => copyToClipboard(text, (escape) => view.control(escape)),
|
|
123
|
+
replay: replayThroughInspector,
|
|
124
|
+
fetchJson: async (url) => {
|
|
125
|
+
const response = await fetch(url);
|
|
126
|
+
if (!response.ok) {
|
|
127
|
+
throw new Error(`the inspector answered ${response.status}`);
|
|
128
|
+
}
|
|
129
|
+
return response.json();
|
|
130
|
+
},
|
|
131
|
+
interrupt: () => process.kill(process.pid, 'SIGINT'),
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
known.start();
|
|
135
|
+
held = null;
|
|
136
|
+
const input = process.stdin;
|
|
137
|
+
const onData = (chunk) => opened.input(chunk);
|
|
138
|
+
input.setRawMode(true);
|
|
139
|
+
input.setEncoding('utf8');
|
|
140
|
+
input.resume();
|
|
141
|
+
input.on('data', onData);
|
|
142
|
+
let closed = false;
|
|
143
|
+
const close = () => {
|
|
144
|
+
if (closed) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
closed = true;
|
|
148
|
+
input.off('data', onData);
|
|
149
|
+
input.setRawMode(false);
|
|
150
|
+
input.pause();
|
|
151
|
+
view.stop();
|
|
152
|
+
// The window of the shell is back: what ran, and what went wrong at the end
|
|
153
|
+
process.stdout.write(`${opened.summary().join('\n')}\n`);
|
|
154
|
+
for (const problem of problems) {
|
|
155
|
+
if (Date.now() - problem.at < 3000) {
|
|
156
|
+
process.stderr.write(`${problem.line}\n`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
process.once('exit', close);
|
|
161
|
+
// A crash prints its trace after this: on the window of the shell, not on the view that is about to go
|
|
162
|
+
process.once('uncaughtExceptionMonitor', close);
|
|
163
|
+
for (const signal of ['SIGINT', 'SIGTERM']) {
|
|
164
|
+
process.on(signal, () => opened.stopping());
|
|
165
|
+
}
|
|
166
|
+
view.start();
|
|
167
|
+
opened.draw();
|
|
168
|
+
return opened;
|
|
169
|
+
}
|
|
64
170
|
// Without the live feed (no colour), the newest exchange is still noted for the r key
|
|
65
171
|
let last = null;
|
|
66
172
|
let markShown = false;
|
|
@@ -68,6 +174,16 @@ await main({
|
|
|
68
174
|
argv,
|
|
69
175
|
env: process.env,
|
|
70
176
|
print: (line) => {
|
|
177
|
+
if (app !== null) {
|
|
178
|
+
app.line(line);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (held !== null && facts !== null && screen !== null) {
|
|
182
|
+
quiet();
|
|
183
|
+
facts.line(line);
|
|
184
|
+
held.push({ error: false, line: screen.render(line) });
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
71
187
|
if (color && screen !== null && (versionOnly || line.startsWith('Logged in '))) {
|
|
72
188
|
hop();
|
|
73
189
|
}
|
|
@@ -75,6 +191,17 @@ await main({
|
|
|
75
191
|
},
|
|
76
192
|
error: (line) => {
|
|
77
193
|
quiet();
|
|
194
|
+
if (app !== null) {
|
|
195
|
+
problems.push({ at: Date.now(), line });
|
|
196
|
+
app.problem(line);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
// A notice while the tunnel starts: kept with the lines of the start, and on record in the Status tab
|
|
200
|
+
if (held !== null && facts !== null) {
|
|
201
|
+
facts.note(line);
|
|
202
|
+
held.push({ error: true, line });
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
78
205
|
const write = () => process.stderr.write(`${line}\n`);
|
|
79
206
|
if (live === null) {
|
|
80
207
|
write();
|
|
@@ -112,6 +239,10 @@ await main({
|
|
|
112
239
|
exchange: color || keys
|
|
113
240
|
? (exchange, label = '') => {
|
|
114
241
|
quiet();
|
|
242
|
+
if (app !== null) {
|
|
243
|
+
app.exchange(exchange, label);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
115
246
|
if (exchange.source !== 'replay') {
|
|
116
247
|
last = exchange;
|
|
117
248
|
}
|
|
@@ -125,7 +256,19 @@ await main({
|
|
|
125
256
|
: undefined,
|
|
126
257
|
started: (running) => {
|
|
127
258
|
quiet();
|
|
259
|
+
const info = fullScreen && facts !== null ? tunnelInfo(running) : null;
|
|
260
|
+
if (info !== null && facts !== null) {
|
|
261
|
+
app = openView(info, facts, running);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
release();
|
|
128
265
|
const target = shortcutTarget(running);
|
|
266
|
+
// The signing secrets named on the command line or in hopwire.yaml: webhook rows show their signature
|
|
267
|
+
if (screen !== null &&
|
|
268
|
+
(running.kind === 'http' || running.kind === 'up') &&
|
|
269
|
+
running.webhookSecrets !== undefined) {
|
|
270
|
+
screen.checkSignatures(running.webhookSecrets);
|
|
271
|
+
}
|
|
129
272
|
if (target !== null) {
|
|
130
273
|
live?.start();
|
|
131
274
|
}
|
|
@@ -150,6 +293,7 @@ await main({
|
|
|
150
293
|
failure: colorErrors && screen !== null
|
|
151
294
|
? (message, hint) => {
|
|
152
295
|
quiet();
|
|
296
|
+
release();
|
|
153
297
|
process.stderr.write(`${screen.failure(message, hint)}\n`);
|
|
154
298
|
}
|
|
155
299
|
: undefined,
|
package/dist/cli.js
CHANGED
|
@@ -13,11 +13,18 @@ import { startMcp } from './commands/mcp.js';
|
|
|
13
13
|
import { rotate } from './commands/rotate.js';
|
|
14
14
|
import { startUp } from './commands/up.js';
|
|
15
15
|
import { startWatch } from './commands/watch.js';
|
|
16
|
+
import { chaos } from './commands/chaos.js';
|
|
17
|
+
import { startFix } from './commands/fix.js';
|
|
18
|
+
import { snapshot } from './commands/snapshot.js';
|
|
19
|
+
import { verify } from './commands/verify.js';
|
|
20
|
+
import { parseTarget } from './traffic.js';
|
|
16
21
|
import { ConfigError, isServiceName, loadConfig } from './config.js';
|
|
17
22
|
import { HopwireError, TemporaryError, UsageError, errorCode } from './errors.js';
|
|
23
|
+
import { commandHelp, overview } from './help.js';
|
|
18
24
|
import { loadOrCreateIdentity } from './identity.js';
|
|
19
25
|
import { defaultLogName, isFollowablePath } from './log-follow.js';
|
|
20
26
|
import { DOCS_URL, printable } from './output.js';
|
|
27
|
+
import { SIGNED_PROVIDERS, isSignedProvider, isVariableName, } from './signature.js';
|
|
21
28
|
import { readVersion } from './version.js';
|
|
22
29
|
import { createUpdateNotice } from './update-notice.js';
|
|
23
30
|
import { parseControlPlaneUrl, parseEdgeUrl, parseLocalTarget } from './urls.js';
|
|
@@ -60,6 +67,13 @@ Commands:
|
|
|
60
67
|
hopwire.yaml) to read or to paste into a bug report. It is sent nowhere
|
|
61
68
|
hopwire mcp An MCP server for a coding agent on this machine (stdio): it reads and replays
|
|
62
69
|
the traffic of a tunnel running on the same data directory
|
|
70
|
+
hopwire snapshot <file> Write the requests a running tunnel recorded to a file, for verify and chaos
|
|
71
|
+
hopwire verify <file> Send the requests of a snapshot to your service and compare every answer with
|
|
72
|
+
the recorded one; exits 1 when one differs (for CI)
|
|
73
|
+
hopwire chaos <file> Deliver the webhooks of a snapshot the way providers do: twice, out of order,
|
|
74
|
+
with a wrong and with an old signature; says whether your handler held
|
|
75
|
+
hopwire fix [<exchange-id>] Replay a failed request through the running tunnel after every file change until
|
|
76
|
+
it passes (default: the newest that failed)
|
|
63
77
|
|
|
64
78
|
Options for http:
|
|
65
79
|
--listen <port> Loopback port of the entry point (default ${LOCAL_ENTRY_DEFAULT_PORT}, 0 picks a free one)
|
|
@@ -107,6 +121,11 @@ Options for http:
|
|
|
107
121
|
other people's data. Neither the edge nor the control plane ever sees them
|
|
108
122
|
--log-request-id With --log: every request forwarded to the service carries X-Hopwire-Exchange-Id; a
|
|
109
123
|
service that writes it into its log gets exact links when requests overlap
|
|
124
|
+
--webhook-secret <provider>=<VARIABLE>
|
|
125
|
+
Checks the signatures of a provider's webhooks with the signing secret in that
|
|
126
|
+
environment variable and shows the result next to each request; repeatable. Providers:
|
|
127
|
+
${SIGNED_PROVIDERS.join(', ')}
|
|
128
|
+
--plain Print lines, as a pipe gets them, instead of the full-screen view of a terminal
|
|
110
129
|
|
|
111
130
|
Options for init:
|
|
112
131
|
--config <path> The file to write (default ${HOPWIRE_YAML_FILE} in the working directory). An existing file
|
|
@@ -130,6 +149,7 @@ Options for up:
|
|
|
130
149
|
p2p.team in the file is --team of http, p2p.relay is --relay (and turns on p2p.team)
|
|
131
150
|
--no-agent, --agent As for http, for every service; agent: false in the file turns it off too
|
|
132
151
|
--force As for http: stop the tunnel that holds the data directory first
|
|
152
|
+
--plain As for http
|
|
133
153
|
|
|
134
154
|
Options for login:
|
|
135
155
|
--api <url> The URL of the control plane; default: the one of the last login on this data
|
|
@@ -176,13 +196,35 @@ Options for watch:
|
|
|
176
196
|
--log <path> As for http: follow a log file of your own service, so the entries of your replays
|
|
177
197
|
show next to them. They stay on this machine
|
|
178
198
|
--force As for http: stop the tunnel that holds the data directory first
|
|
199
|
+
--plain As for http
|
|
200
|
+
|
|
201
|
+
Options for snapshot:
|
|
202
|
+
--since <time> Only requests of the last 30s, 30m, 2h or 1d (default 30m)
|
|
203
|
+
--last <n> At most the newest n requests (default 200)
|
|
204
|
+
--service <name> Only requests of this service
|
|
205
|
+
--path <text> Only requests whose path contains this text
|
|
206
|
+
|
|
207
|
+
Options for verify and chaos:
|
|
208
|
+
--target <url> Where the requests go: an http origin such as http://127.0.0.1:3000 or a port
|
|
209
|
+
--webhook-secret <provider>=<VARIABLE>
|
|
210
|
+
Sign the webhooks of that provider again with the secret in that environment variable,
|
|
211
|
+
since recorded signatures are too old for every provider; repeatable
|
|
212
|
+
--strict verify only: compare values too, not only the shape of JSON answers
|
|
213
|
+
--ignore <field> verify only: leave a field out, as a dotted path such as data.created; repeatable
|
|
214
|
+
|
|
215
|
+
Options for fix:
|
|
216
|
+
--watch <dir> A folder whose changes start a new attempt (default the working directory);
|
|
217
|
+
repeatable
|
|
218
|
+
--webhook-secret <provider>=<VARIABLE>
|
|
219
|
+
Sign the webhook again for every attempt, as for verify
|
|
179
220
|
|
|
180
221
|
Global options:
|
|
181
222
|
--data-dir <path> Where identity, traffic log and the API token are stored (default $HOPWIRE_HOME or
|
|
182
223
|
~/.hopwire)
|
|
183
224
|
--profile <name> Use the login of this profile for this command only, not the current one (default
|
|
184
225
|
$HOPWIRE_PROFILE); identity, traffic log and inspector are the same for every profile
|
|
185
|
-
-h, --help Show
|
|
226
|
+
-h, --help Show the help: of a command after its name (hopwire http --help), else the overview;
|
|
227
|
+
hopwire help --all prints this page
|
|
186
228
|
--version Show the version
|
|
187
229
|
`;
|
|
188
230
|
/** What `hopwire mcp` says on standard error when a person starts it on a terminal. */
|
|
@@ -213,22 +255,28 @@ const COMMAND_OPTIONS = {
|
|
|
213
255
|
'log',
|
|
214
256
|
'share-logs',
|
|
215
257
|
'log-request-id',
|
|
258
|
+
'webhook-secret',
|
|
216
259
|
'public',
|
|
217
260
|
'subdomain',
|
|
218
261
|
'agent',
|
|
219
262
|
'no-agent',
|
|
220
263
|
'force',
|
|
264
|
+
'plain',
|
|
221
265
|
],
|
|
222
|
-
up: ['config', 'agent', 'no-agent', 'force'],
|
|
266
|
+
up: ['config', 'agent', 'no-agent', 'force', 'plain'],
|
|
223
267
|
id: [],
|
|
224
268
|
rotate: [],
|
|
225
269
|
connect: ['port', 'service', 'allow-host', 'team', 'relay'],
|
|
226
|
-
watch: ['replay-target', 'inspect', 'team', 'relay', 'agent', 'log', 'force'],
|
|
270
|
+
watch: ['replay-target', 'inspect', 'team', 'relay', 'agent', 'log', 'force', 'plain'],
|
|
227
271
|
login: ['api', 'token', 'no-browser'],
|
|
228
272
|
logout: [],
|
|
229
273
|
profiles: [],
|
|
230
274
|
use: [],
|
|
231
275
|
mcp: ['read-only'],
|
|
276
|
+
snapshot: ['since', 'last', 'service', 'path'],
|
|
277
|
+
verify: ['target', 'webhook-secret', 'strict', 'ignore'],
|
|
278
|
+
chaos: ['target', 'webhook-secret'],
|
|
279
|
+
fix: ['watch', 'webhook-secret'],
|
|
232
280
|
doctor: ['network'],
|
|
233
281
|
};
|
|
234
282
|
/** Parses the arguments and starts the command. Resolves with what keeps running, or null when the command is done. */
|
|
@@ -240,16 +288,22 @@ export async function run(argv, io) {
|
|
|
240
288
|
}
|
|
241
289
|
const { values, positionals } = parse(argv);
|
|
242
290
|
const [command, ...rest] = positionals;
|
|
243
|
-
|
|
291
|
+
// hopwire help --all: the full reference; hopwire <command> --help and hopwire help <command>: one command
|
|
292
|
+
if (values.all && (values.help || command === 'help')) {
|
|
244
293
|
io.print(USAGE);
|
|
245
294
|
return null;
|
|
246
295
|
}
|
|
296
|
+
if (values.help || command === 'help') {
|
|
297
|
+
const about = command === 'help' ? rest[0] : command;
|
|
298
|
+
io.print((about === undefined ? null : commandHelp(about)) ?? overview());
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
247
301
|
if (values.version) {
|
|
248
302
|
io.print(readVersion());
|
|
249
303
|
return null;
|
|
250
304
|
}
|
|
251
305
|
if (command === undefined) {
|
|
252
|
-
io.print(
|
|
306
|
+
io.print(overview());
|
|
253
307
|
return null;
|
|
254
308
|
}
|
|
255
309
|
const allowed = COMMAND_OPTIONS[command];
|
|
@@ -268,6 +322,75 @@ export async function run(argv, io) {
|
|
|
268
322
|
const profile = resolveProfile(values.profile, io.env);
|
|
269
323
|
// The login a command uses: the profile it names, else the current one of the data directory
|
|
270
324
|
const store = { dataDir, profile };
|
|
325
|
+
if (command === 'snapshot') {
|
|
326
|
+
noMoreArguments(rest, 1, 'hopwire snapshot <file>');
|
|
327
|
+
const file = rest[0] ?? '';
|
|
328
|
+
if (file === '') {
|
|
329
|
+
throw new UsageError('hopwire snapshot needs the file to write, for example hooks.snapshot.json');
|
|
330
|
+
}
|
|
331
|
+
const last = values.last === undefined ? 200 : Number(values.last);
|
|
332
|
+
if (!Number.isInteger(last) || last < 1 || last > 5000) {
|
|
333
|
+
throw new UsageError('--last must be a number of requests between 1 and 5000');
|
|
334
|
+
}
|
|
335
|
+
if (values.service !== undefined && !isServiceName(values.service)) {
|
|
336
|
+
throw new UsageError('--service must be the name of a service');
|
|
337
|
+
}
|
|
338
|
+
if (values.path === '') {
|
|
339
|
+
throw new UsageError('--path needs the text to look for in the path');
|
|
340
|
+
}
|
|
341
|
+
await snapshot({
|
|
342
|
+
dataDir,
|
|
343
|
+
file: path.resolve(io.cwd ?? process.cwd(), file),
|
|
344
|
+
shown: file,
|
|
345
|
+
sinceMs: parseDuration(values.since ?? '30m'),
|
|
346
|
+
last,
|
|
347
|
+
service: values.service,
|
|
348
|
+
pathContains: values.path,
|
|
349
|
+
print: io.print,
|
|
350
|
+
signal: io.signal,
|
|
351
|
+
});
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
if (command === 'verify' || command === 'chaos') {
|
|
355
|
+
noMoreArguments(rest, 1, `hopwire ${command} <file>`);
|
|
356
|
+
const file = rest[0] ?? '';
|
|
357
|
+
if (file === '') {
|
|
358
|
+
throw new UsageError(`hopwire ${command} needs a snapshot file, written with hopwire snapshot <file>`);
|
|
359
|
+
}
|
|
360
|
+
const target = values.target === undefined ? null : parseTarget(values.target);
|
|
361
|
+
if (target === null) {
|
|
362
|
+
throw new UsageError(`hopwire ${command} needs --target <url>: an http origin without a path, such as http://127.0.0.1:3000, or a port`);
|
|
363
|
+
}
|
|
364
|
+
const secrets = parseWebhookSecrets(values['webhook-secret'], io.env);
|
|
365
|
+
const where = path.resolve(io.cwd ?? process.cwd(), file);
|
|
366
|
+
if (command === 'verify') {
|
|
367
|
+
await verify({
|
|
368
|
+
file: where,
|
|
369
|
+
shown: file,
|
|
370
|
+
target,
|
|
371
|
+
secrets,
|
|
372
|
+
strict: values.strict ?? false,
|
|
373
|
+
ignore: values.ignore ?? [],
|
|
374
|
+
print: io.print,
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
await chaos({ file: where, shown: file, target, secrets, print: io.print });
|
|
379
|
+
}
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
if (command === 'fix') {
|
|
383
|
+
noMoreArguments(rest, 1, 'hopwire fix [<exchange-id>]');
|
|
384
|
+
const cwd = io.cwd ?? process.cwd();
|
|
385
|
+
return startFix({
|
|
386
|
+
dataDir,
|
|
387
|
+
id: rest[0] ?? null,
|
|
388
|
+
watch: (values.watch ?? ['.']).map((folder) => path.resolve(cwd, folder)),
|
|
389
|
+
secrets: parseWebhookSecrets(values['webhook-secret'], io.env),
|
|
390
|
+
print: io.print,
|
|
391
|
+
signal: io.signal,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
271
394
|
if (command === 'id') {
|
|
272
395
|
noMoreArguments(rest, 0, 'hopwire id');
|
|
273
396
|
io.print((await loadOrCreateIdentity(dataDir)).publicKey.toString('hex'));
|
|
@@ -407,6 +530,11 @@ export async function run(argv, io) {
|
|
|
407
530
|
const cwd = io.cwd ?? process.cwd();
|
|
408
531
|
const { file, shown } = configFile(cwd, values.config);
|
|
409
532
|
const config = await loadConfig(file, values.config !== undefined, shown);
|
|
533
|
+
// Read before anything starts: a variable that is not set stops the start, like any other mistake in the file
|
|
534
|
+
const webhookSecrets = new Map(config.services.map((service) => [
|
|
535
|
+
service.name,
|
|
536
|
+
service.webhooks.map(({ provider, variable }) => webhookSecret(provider, variable, io.env, `services.${service.name}.webhooks.${provider}`)),
|
|
537
|
+
]));
|
|
410
538
|
const update = createUpdateNotice(readVersion());
|
|
411
539
|
const running = await startUp({
|
|
412
540
|
force: values.force ?? false,
|
|
@@ -429,7 +557,7 @@ export async function run(argv, io) {
|
|
|
429
557
|
});
|
|
430
558
|
// After the lines of the start, so the mode stays the first line of every tunnel
|
|
431
559
|
update.print(io.print);
|
|
432
|
-
return running;
|
|
560
|
+
return Object.assign(running, { webhookSecrets });
|
|
433
561
|
}
|
|
434
562
|
noMoreArguments(rest, 1, 'hopwire http <port>');
|
|
435
563
|
if (values.inspect !== undefined && values['no-inspect']) {
|
|
@@ -473,6 +601,7 @@ export async function run(argv, io) {
|
|
|
473
601
|
// The rule of a service name in hopwire.yaml: a teammate asks for a service by this name
|
|
474
602
|
throw new UsageError('--name must be a service name of 1 to 255 bytes without control characters');
|
|
475
603
|
}
|
|
604
|
+
const webhookSecrets = new Map([[values.name ?? 'default', parseWebhookSecrets(values['webhook-secret'], io.env)]]);
|
|
476
605
|
const update = createUpdateNotice(readVersion());
|
|
477
606
|
const running = await startHttpTunnel({
|
|
478
607
|
force: values.force ?? false,
|
|
@@ -504,7 +633,40 @@ export async function run(argv, io) {
|
|
|
504
633
|
});
|
|
505
634
|
// After the lines of the start, so the mode stays the first line of every tunnel
|
|
506
635
|
update.print(io.print);
|
|
507
|
-
return running;
|
|
636
|
+
return Object.assign(running, { webhookSecrets });
|
|
637
|
+
}
|
|
638
|
+
/** 30s, 30m, 2h, 1d as milliseconds. */
|
|
639
|
+
function parseDuration(value) {
|
|
640
|
+
const match = /^(\d{1,6})(s|m|h|d)$/.exec(value);
|
|
641
|
+
if (match === null) {
|
|
642
|
+
throw new UsageError('--since must be a time such as 30s, 30m, 2h or 1d');
|
|
643
|
+
}
|
|
644
|
+
const unit = { s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 }[match[2]];
|
|
645
|
+
return Number(match[1]) * unit;
|
|
646
|
+
}
|
|
647
|
+
/** --webhook-secret <provider>=<VARIABLE>, repeatable: each provider once. */
|
|
648
|
+
function parseWebhookSecrets(values, env) {
|
|
649
|
+
const named = [];
|
|
650
|
+
for (const value of values ?? []) {
|
|
651
|
+
const [provider = '', variable = ''] = value.split('=', 2);
|
|
652
|
+
if (!isSignedProvider(provider) || !isVariableName(variable) || !value.includes('=')) {
|
|
653
|
+
throw new UsageError(`--webhook-secret must be <provider>=<VARIABLE>, the name of an environment variable that holds the signing secret (never the secret itself); providers: ${SIGNED_PROVIDERS.join(', ')}`);
|
|
654
|
+
}
|
|
655
|
+
if (named.some((entry) => entry.provider === provider)) {
|
|
656
|
+
throw new UsageError(`--webhook-secret names ${provider} twice`);
|
|
657
|
+
}
|
|
658
|
+
named.push({ provider, variable });
|
|
659
|
+
}
|
|
660
|
+
// Read once the command line is known to be right
|
|
661
|
+
return named.map(({ provider, variable }) => webhookSecret(provider, variable, env, '--webhook-secret'));
|
|
662
|
+
}
|
|
663
|
+
/** The secret in an environment variable; one that is not set stops the start. The secret is never in the sentence. */
|
|
664
|
+
function webhookSecret(provider, variable, env, named) {
|
|
665
|
+
const secret = env[variable] ?? '';
|
|
666
|
+
if (secret === '') {
|
|
667
|
+
throw new HopwireError(`${named} names the environment variable ${variable}, which is not set or empty; set it to the signing secret of your ${provider} webhook endpoint.`);
|
|
668
|
+
}
|
|
669
|
+
return { provider, secret, variable };
|
|
508
670
|
}
|
|
509
671
|
/**
|
|
510
672
|
* The files of --log, resolved against the working directory and named after themselves. Two files with the same name
|
|
@@ -602,7 +764,7 @@ async function runInit(argv, io) {
|
|
|
602
764
|
}
|
|
603
765
|
const { values, positionals } = parsed;
|
|
604
766
|
if (values.help) {
|
|
605
|
-
io.print(
|
|
767
|
+
io.print(commandHelp('init') ?? overview());
|
|
606
768
|
return;
|
|
607
769
|
}
|
|
608
770
|
if (values.version) {
|
|
@@ -665,6 +827,8 @@ function parse(argv) {
|
|
|
665
827
|
allowPositionals: true,
|
|
666
828
|
options: {
|
|
667
829
|
help: { type: 'boolean', short: 'h' },
|
|
830
|
+
all: { type: 'boolean' },
|
|
831
|
+
plain: { type: 'boolean' },
|
|
668
832
|
version: { type: 'boolean' },
|
|
669
833
|
listen: { type: 'string' },
|
|
670
834
|
inspect: { type: 'string' },
|
|
@@ -692,6 +856,14 @@ function parse(argv) {
|
|
|
692
856
|
log: { type: 'string', multiple: true },
|
|
693
857
|
'share-logs': { type: 'boolean' },
|
|
694
858
|
'log-request-id': { type: 'boolean' },
|
|
859
|
+
'webhook-secret': { type: 'string', multiple: true },
|
|
860
|
+
since: { type: 'string' },
|
|
861
|
+
last: { type: 'string' },
|
|
862
|
+
path: { type: 'string' },
|
|
863
|
+
target: { type: 'string' },
|
|
864
|
+
strict: { type: 'boolean' },
|
|
865
|
+
ignore: { type: 'string', multiple: true },
|
|
866
|
+
watch: { type: 'string', multiple: true },
|
|
695
867
|
'read-only': { type: 'boolean' },
|
|
696
868
|
api: { type: 'string' },
|
|
697
869
|
token: { type: 'boolean' },
|