hopwire 0.1.8 → 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 +2 -0
- package/dist/agent/redact.js +38 -0
- package/dist/bin.js +5 -1
- package/dist/cli.js +237 -10
- package/dist/commands/connect.js +23 -7
- package/dist/commands/doctor.js +63 -3
- package/dist/commands/http.js +25 -4
- package/dist/commands/init.js +355 -0
- package/dist/commands/up.js +22 -3
- package/dist/commands/watch.js +54 -9
- package/dist/config-template.js +80 -0
- package/dist/config.js +65 -5
- package/dist/control-plane-client.js +70 -0
- package/dist/inspector/assets/index-CIOfyIbK.js +5 -0
- package/dist/inspector/assets/index-Du4-DCNN.css +2 -0
- package/dist/inspector/index.html +2 -2
- package/dist/inspector-api.js +145 -0
- package/dist/interceptor.js +19 -4
- package/dist/log-follow.js +473 -0
- package/dist/log.js +178 -11
- package/dist/main.js +1 -0
- package/dist/mcp/logs.js +46 -0
- package/dist/mcp/tools.js +136 -3
- package/dist/output.js +67 -1
- package/dist/p2p.js +249 -24
- package/dist/prompt.js +63 -0
- package/dist/relay.js +545 -0
- package/dist/serve.js +62 -2
- package/node_modules/@hopwire/protocol/dist/constants.d.ts +10 -0
- package/node_modules/@hopwire/protocol/dist/constants.js +10 -0
- package/node_modules/@hopwire/protocol/dist/index.d.ts +2 -0
- package/node_modules/@hopwire/protocol/dist/index.js +2 -0
- package/node_modules/@hopwire/protocol/dist/inspector.d.ts +18 -0
- package/node_modules/@hopwire/protocol/dist/log-entries.d.ts +72 -0
- package/node_modules/@hopwire/protocol/dist/log-entries.js +147 -0
- package/node_modules/@hopwire/protocol/dist/relay.d.ts +81 -0
- package/node_modules/@hopwire/protocol/dist/relay.js +102 -0
- package/node_modules/@hopwire/protocol/dist/ws-messages.d.ts +2 -1
- package/package.json +2 -1
- package/dist/inspector/assets/index-D3yNjmSA.js +0 -2
- package/dist/inspector/assets/index-XotF7D1X.css +0 -2
package/dist/commands/doctor.js
CHANGED
|
@@ -29,9 +29,16 @@ export async function doctor(options) {
|
|
|
29
29
|
print(`Config ${await config(options.cwd)}`);
|
|
30
30
|
print(`Proxy ${proxy(options.env ?? process.env)}`);
|
|
31
31
|
print(`Network ${options.network ? await network(options) : NOT_CHECKED}`);
|
|
32
|
+
const edge = options.network ? await relayEdge(dataDir) : null;
|
|
33
|
+
print(`Relay ${options.network ? await relay(edge) : RELAY_NOT_CHECKED}`);
|
|
34
|
+
// Only with --network: the list comes from the control plane, and without it nothing leaves the machine
|
|
35
|
+
const domains = options.network ? await customDomains(dataDir, options.signal) : null;
|
|
36
|
+
for (const line of domains ?? []) {
|
|
37
|
+
print(line);
|
|
38
|
+
}
|
|
32
39
|
print('');
|
|
33
40
|
print(options.network
|
|
34
|
-
? 'Nothing was sent to us. The network check joined the DHT for a moment; its nodes saw the IP address of this machine.'
|
|
41
|
+
? `${edge === null && domains === null ? 'Nothing was sent to us. ' : ''}The network check joined the DHT for a moment; its nodes saw the IP address of this machine.${edge === null ? '' : ' It also asked the edge of your account whether its relay answers, without a token: the edge saw the IP address, nothing else.'}${domains === null ? '' : ' It asked the control plane of your account for its custom domains, with your API token.'}`
|
|
35
42
|
: 'Nothing was sent anywhere: this report was put together on this machine without a network connection.');
|
|
36
43
|
print('It holds no keys, no tokens and no traffic. If you paste it into a bug report, read it first: the paths can name your user.');
|
|
37
44
|
}
|
|
@@ -53,6 +60,7 @@ function proxy(env) {
|
|
|
53
60
|
return error instanceof Error ? error.message : String(error);
|
|
54
61
|
}
|
|
55
62
|
}
|
|
63
|
+
const RELAY_NOT_CHECKED = 'not checked. hopwire doctor --network also tests whether the relay of your edge (--team --relay, Team plan) can be reached from here, when this machine is logged in';
|
|
56
64
|
const NOT_CHECKED = 'not checked. hopwire doctor --network tests whether this network allows direct P2P connections; for that it joins the public DHT, whose nodes see your IP address';
|
|
57
65
|
/** Whether a file or directory is there, and whether others than its owner can get at it. */
|
|
58
66
|
async function entry(file, expected, missing = 'missing') {
|
|
@@ -112,7 +120,7 @@ async function network(options) {
|
|
|
112
120
|
const { probeNetwork } = await import('../p2p.js');
|
|
113
121
|
const found = await probeNetwork({ bootstrap: options.bootstrap, signal: options.signal });
|
|
114
122
|
if (found === 'unreachable') {
|
|
115
|
-
return 'the DHT could not be reached within 15 seconds;
|
|
123
|
+
return 'the DHT could not be reached within 15 seconds; direct P2P with teammates (--share, connect, watch) will not work from this network. On the Team plan --team --relay goes through the relay of the edge instead. A tunnel on this machine alone and public mode do not need the DHT';
|
|
116
124
|
}
|
|
117
125
|
if (found === 'open') {
|
|
118
126
|
return 'direct P2P works: this machine can be reached from outside without hole punching';
|
|
@@ -120,7 +128,59 @@ async function network(options) {
|
|
|
120
128
|
if (found === 'consistent') {
|
|
121
129
|
return 'direct P2P should work: this network keeps one outside port per connection, which is what hole punching needs';
|
|
122
130
|
}
|
|
123
|
-
return 'direct P2P will often fail from here: this network picks a new outside port for every destination (a symmetric NAT). It still works when the teammate is on a network that keeps its port; two networks of this kind cannot connect.
|
|
131
|
+
return 'direct P2P will often fail from here: this network picks a new outside port for every destination (a symmetric NAT). It still works when the teammate is on a network that keeps its port; two networks of this kind cannot connect directly. On the Team plan --team --relay goes through the relay of the edge then; public mode works from here';
|
|
132
|
+
}
|
|
133
|
+
/** The edge whose relay is checked: the one the logged-in account is bound to; null when there is none. */
|
|
134
|
+
async function relayEdge(dataDir) {
|
|
135
|
+
try {
|
|
136
|
+
return (await readAccount(dataDir)) === null ? null : await boundEdge(dataDir);
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/** Whether the relay of the edge answers from this network. */
|
|
143
|
+
async function relay(edge) {
|
|
144
|
+
if (edge === null) {
|
|
145
|
+
return 'not checked: this machine is not logged in, or its account has no edge yet (the relay is for --team --relay on the Team plan)';
|
|
146
|
+
}
|
|
147
|
+
// Loaded only here: the one module that opens a relay socket
|
|
148
|
+
const { probeRelay } = await import('../relay.js');
|
|
149
|
+
const found = await probeRelay(edge);
|
|
150
|
+
return found.reachable
|
|
151
|
+
? `the relay of ${edge.origin} can be reached from here: --team --relay works on this network when direct P2P does not`
|
|
152
|
+
: `the relay of ${edge.origin} cannot be reached from here (${found.problem})`;
|
|
153
|
+
}
|
|
154
|
+
/** What the control plane says about the status of a custom domain, in plain words. */
|
|
155
|
+
const DOMAIN_STATUS_WORDS = {
|
|
156
|
+
active: 'active',
|
|
157
|
+
pending_dns: 'waiting for DNS',
|
|
158
|
+
pending_certificate: 'waiting for certificate',
|
|
159
|
+
failed: 'failed',
|
|
160
|
+
offline_plan: 'off (beyond plan)',
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* The custom domains of the account, one line each, from GET /api/v1/me; null when this machine is not logged in (nothing was asked), nothing when
|
|
164
|
+
* the account has none or the control plane cannot be asked right now (the report is not about that).
|
|
165
|
+
*/
|
|
166
|
+
async function customDomains(dataDir, signal) {
|
|
167
|
+
try {
|
|
168
|
+
const stored = await readAccount(dataDir);
|
|
169
|
+
if (stored === null) {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
// Loaded only here: doctor without --network never contacts the control plane
|
|
173
|
+
const { createControlPlaneClient } = await import('../control-plane-client.js');
|
|
174
|
+
const { customDomains: domains } = await createControlPlaneClient({
|
|
175
|
+
url: stored.controlPlaneUrl,
|
|
176
|
+
apiToken: stored.apiToken,
|
|
177
|
+
}).me(signal);
|
|
178
|
+
return domains.map((domain, index) => `${index === 0 ? 'Domains' : ''}`.padEnd(13) +
|
|
179
|
+
printable(`${domain.host} (${DOMAIN_STATUS_WORDS[domain.status]}; ${domain.subdomain})`));
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return [];
|
|
183
|
+
}
|
|
124
184
|
}
|
|
125
185
|
function message(error) {
|
|
126
186
|
return printable(error instanceof Error ? error.message : String(error));
|
package/dist/commands/http.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MODE_LOCAL, MODE_P2P, MODE_P2P_TEAM, MODE_PUBLIC, MODE_PUBLIC_P2P, MODE_PUBLIC_P2P_TEAM, AGENTS_ALLOWED_LINE, agentLine, agentOffLine, formatExchange, formatLimitReached, formatMeteredUsage, formatTeamEvent, formatUsage, shareNotice, tunnelGuide, } from '../output.js';
|
|
1
|
+
import { MODE_LOCAL, MODE_P2P, MODE_P2P_TEAM, relayLine, MODE_PUBLIC, MODE_PUBLIC_P2P, MODE_PUBLIC_P2P_TEAM, AGENTS_ALLOWED_LINE, agentLine, agentOffLine, formatExchange, formatLimitReached, formatLogNotice, logLines, formatMeteredUsage, formatRelayEvent, formatTeamEvent, formatUsage, shareNotice, tunnelGuide, mainPublicUrl, publicUrlText, } from '../output.js';
|
|
2
2
|
import { startServing } from '../serve.js';
|
|
3
3
|
export { INSPECTOR_FILES } from '../serve.js';
|
|
4
4
|
/**
|
|
@@ -12,7 +12,18 @@ export async function startHttpTunnel(options) {
|
|
|
12
12
|
// The line about a tunnel --force stopped follows the startup lines: the mode stays the first line
|
|
13
13
|
const stopped = [];
|
|
14
14
|
const serving = await startServing({
|
|
15
|
-
services: [
|
|
15
|
+
services: [
|
|
16
|
+
{
|
|
17
|
+
name: options.serviceName,
|
|
18
|
+
target,
|
|
19
|
+
listenPort: options.listenPort,
|
|
20
|
+
public: options.public,
|
|
21
|
+
logs: options.logs,
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
shareLogs: options.shareLogs ?? false,
|
|
25
|
+
logRequestId: options.logRequestId ?? false,
|
|
26
|
+
onLogNotice: (serviceName, line) => options.print(formatLogNotice(serviceName, line)),
|
|
16
27
|
inspectPort: options.inspectPort,
|
|
17
28
|
dataDir: options.dataDir,
|
|
18
29
|
force: options.force ?? false,
|
|
@@ -22,6 +33,8 @@ export async function startHttpTunnel(options) {
|
|
|
22
33
|
allow,
|
|
23
34
|
team: options.team,
|
|
24
35
|
onTeamEvent: (event, organisation) => options.print(formatTeamEvent(event, organisation)),
|
|
36
|
+
relay: options.relay,
|
|
37
|
+
onRelayEvent: (event) => options.print(formatRelayEvent(event)),
|
|
25
38
|
bootstrap: options.bootstrap,
|
|
26
39
|
inspectorFiles: options.inspectorFiles,
|
|
27
40
|
agent: options.agent,
|
|
@@ -36,6 +49,7 @@ export async function startHttpTunnel(options) {
|
|
|
36
49
|
const { inspectorUrl, agentSocket, logKey, peerKey, ended, close } = serving;
|
|
37
50
|
const url = serving.services[0]?.url ?? '';
|
|
38
51
|
const publicUrl = serving.services[0]?.publicUrl ?? null;
|
|
52
|
+
const customDomains = serving.services[0]?.customDomains ?? [];
|
|
39
53
|
const subdomain = serving.services[0]?.subdomain ?? '';
|
|
40
54
|
const usage = serving.services[0]?.usage ?? null;
|
|
41
55
|
const access = serving.team === null
|
|
@@ -51,7 +65,7 @@ export async function startHttpTunnel(options) {
|
|
|
51
65
|
}
|
|
52
66
|
options.print(`Forwarding ${url} -> ${target.origin}`);
|
|
53
67
|
if (options.public !== undefined) {
|
|
54
|
-
options.print(`Public URL ${publicUrl ?? `subdomain ${subdomain} (the edge did not report its URL)`}`);
|
|
68
|
+
options.print(`Public URL ${publicUrlText(publicUrl, customDomains) ?? `subdomain ${subdomain} (the edge did not report its URL)`}`);
|
|
55
69
|
if (usage !== null) {
|
|
56
70
|
options.print(formatUsage(usage));
|
|
57
71
|
}
|
|
@@ -60,10 +74,16 @@ export async function startHttpTunnel(options) {
|
|
|
60
74
|
if (peerKey !== null) {
|
|
61
75
|
options.print(`Peer key ${peerKey}`);
|
|
62
76
|
options.print(shareNotice(access));
|
|
77
|
+
if (serving.relay !== null) {
|
|
78
|
+
options.print(relayLine(serving.relay.edgeUrl, serving.relay.problem));
|
|
79
|
+
}
|
|
63
80
|
if (options.allowAgents === true) {
|
|
64
81
|
options.print(AGENTS_ALLOWED_LINE);
|
|
65
82
|
}
|
|
66
83
|
}
|
|
84
|
+
for (const line of logLines(serving.logs, peerKey !== null && options.shareLogs === true, options.cwd ?? process.cwd())) {
|
|
85
|
+
options.print(line);
|
|
86
|
+
}
|
|
67
87
|
if (inspectorUrl !== null) {
|
|
68
88
|
options.print(`Inspector ${inspectorUrl}`);
|
|
69
89
|
}
|
|
@@ -79,7 +99,7 @@ export async function startHttpTunnel(options) {
|
|
|
79
99
|
name: options.serviceName,
|
|
80
100
|
url,
|
|
81
101
|
target: target.origin,
|
|
82
|
-
publicUrl,
|
|
102
|
+
publicUrl: mainPublicUrl(publicUrl, customDomains),
|
|
83
103
|
stableUrl: options.public?.connectTokens !== undefined,
|
|
84
104
|
},
|
|
85
105
|
],
|
|
@@ -90,6 +110,7 @@ export async function startHttpTunnel(options) {
|
|
|
90
110
|
logKey,
|
|
91
111
|
allowed: access,
|
|
92
112
|
restrictWith: '--allow <their key>',
|
|
113
|
+
relay: serving.relay !== null,
|
|
93
114
|
},
|
|
94
115
|
inspector: inspectorUrl !== null,
|
|
95
116
|
widenWith: { share: '--share', public: '--public' },
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { access, readFile, writeFile } from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { isReservableSubdomain } from '@hopwire/protocol';
|
|
5
|
+
import { boundEdge, readAccount } from '../account.js';
|
|
6
|
+
import { renderConfig } from '../config-template.js';
|
|
7
|
+
import { isServiceName, parseConfig } from '../config.js';
|
|
8
|
+
import { HopwireError, UsageError, errorCode } from '../errors.js';
|
|
9
|
+
import { printable } from '../output.js';
|
|
10
|
+
import { parseLocalTarget } from '../urls.js';
|
|
11
|
+
/** Refusal of a target that exists; the file stays as it is. */
|
|
12
|
+
function exists(shown) {
|
|
13
|
+
return new HopwireError(`${shown} already exists; edit it, or remove it and run hopwire init again.`);
|
|
14
|
+
}
|
|
15
|
+
export const NO_TERMINAL = 'There is no terminal to ask on; pass the services with --service name=port and --yes, for example hopwire init --service web=3000 --yes';
|
|
16
|
+
const PUBLIC_NEEDS_LOGIN = 'Public URLs need an account: run hopwire login, then hopwire init again or set public: true in the file.';
|
|
17
|
+
export async function init(options) {
|
|
18
|
+
if (await present(options.file)) {
|
|
19
|
+
throw exists(options.shown);
|
|
20
|
+
}
|
|
21
|
+
const answers = options.flags === null ? await interview(options) : await fromFlags(options.flags, options.dataDir);
|
|
22
|
+
const text = renderConfig(answers);
|
|
23
|
+
// The parser of hopwire up decides; a refusal here is a fault of init and ends with its sentence
|
|
24
|
+
parseConfig(text, options.shown);
|
|
25
|
+
try {
|
|
26
|
+
// wx: a file that appeared while the questions were asked is not replaced
|
|
27
|
+
await writeFile(options.file, text, { flag: 'wx' });
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
const code = errorCode(error);
|
|
31
|
+
if (code === 'EEXIST') {
|
|
32
|
+
throw exists(options.shown);
|
|
33
|
+
}
|
|
34
|
+
if (code === 'ENOENT') {
|
|
35
|
+
throw new HopwireError(`The directory of ${options.shown} does not exist; create it first.`);
|
|
36
|
+
}
|
|
37
|
+
throw new HopwireError(`${options.shown} cannot be written (${code}); check the permissions of its directory.`);
|
|
38
|
+
}
|
|
39
|
+
const names = answers.services.map((service) => printable(service.name));
|
|
40
|
+
options.print(`Wrote ${options.shown} (${names.length === 1 ? '1 service' : `${names.length} services`}: ${names.join(', ')})`);
|
|
41
|
+
if ((await (options.gitIgnores ?? gitIgnores)(options.file)) === false) {
|
|
42
|
+
options.print(`Git ${options.shown} is not ignored by git; add it to .gitignore before you put a password in auth.basic.`);
|
|
43
|
+
}
|
|
44
|
+
options.print(`Next hopwire up${options.named ? ` --config ${options.shown}` : ''}`);
|
|
45
|
+
}
|
|
46
|
+
async function present(file) {
|
|
47
|
+
try {
|
|
48
|
+
await access(file);
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (errorCode(error) === 'ENOENT') {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
throw new HopwireError(`${file} cannot be checked (${errorCode(error)}); check the permissions of its directory.`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** What the account gives a public service: its edge and its control plane. Null when the machine cannot have one. */
|
|
59
|
+
async function accountUrls(dataDir) {
|
|
60
|
+
const account = await readAccount(dataDir);
|
|
61
|
+
if (account === null) {
|
|
62
|
+
return 'logged-out';
|
|
63
|
+
}
|
|
64
|
+
const edgeUrl = await boundEdge(dataDir);
|
|
65
|
+
return edgeUrl === null ? 'no-edge' : { edgeUrl, controlPlaneUrl: account.controlPlaneUrl };
|
|
66
|
+
}
|
|
67
|
+
const NO_EDGE = 'The account on this machine names no edge yet; run hopwire login again (or start hopwire http <port> --public once), then hopwire init again.';
|
|
68
|
+
// --- The flags ---
|
|
69
|
+
async function fromFlags(flags, dataDir) {
|
|
70
|
+
if (flags.services.length === 0) {
|
|
71
|
+
throw new UsageError('hopwire init --yes needs at least one --service name=port, for example --service web=3000');
|
|
72
|
+
}
|
|
73
|
+
const services = flags.services.map((value) => {
|
|
74
|
+
const separator = value.indexOf('=');
|
|
75
|
+
const name = separator < 0 ? '' : value.slice(0, separator);
|
|
76
|
+
if (separator < 0 || !isServiceName(name)) {
|
|
77
|
+
throw new UsageError('--service must be name=port or name=url, for example web=3000, with a name of 1 to 255 bytes without control characters');
|
|
78
|
+
}
|
|
79
|
+
const local = localTarget(value.slice(separator + 1));
|
|
80
|
+
if (local === null) {
|
|
81
|
+
throw new UsageError(`--service ${printable(name)} needs a port or an http URL on this machine without a path, for example ${printable(name)}=3000 or ${printable(name)}=http://127.0.0.1:3000`);
|
|
82
|
+
}
|
|
83
|
+
return { name, local, public: false, subdomain: null, logs: [] };
|
|
84
|
+
});
|
|
85
|
+
const byName = new Map();
|
|
86
|
+
for (const service of services) {
|
|
87
|
+
if (byName.has(service.name)) {
|
|
88
|
+
throw new UsageError(`--service ${printable(service.name)} is given twice; give each service its own name`);
|
|
89
|
+
}
|
|
90
|
+
byName.set(service.name, service);
|
|
91
|
+
}
|
|
92
|
+
for (const name of flags.public) {
|
|
93
|
+
const service = byName.get(name);
|
|
94
|
+
if (service === undefined) {
|
|
95
|
+
throw new UsageError(`--public ${printable(name)} names no service; name one given with --service`);
|
|
96
|
+
}
|
|
97
|
+
service.public = true;
|
|
98
|
+
}
|
|
99
|
+
const subdomains = new Set();
|
|
100
|
+
for (const value of flags.subdomains) {
|
|
101
|
+
const separator = value.indexOf('=');
|
|
102
|
+
const name = value.slice(0, Math.max(separator, 0));
|
|
103
|
+
const subdomain = value.slice(separator + 1);
|
|
104
|
+
const service = byName.get(name);
|
|
105
|
+
if (separator < 0 || service === undefined) {
|
|
106
|
+
throw new UsageError('--subdomain must be name=subdomain for a service given with --service and --public');
|
|
107
|
+
}
|
|
108
|
+
if (!service.public) {
|
|
109
|
+
throw new UsageError(`--subdomain ${printable(name)}=... only works together with --public ${printable(name)}`);
|
|
110
|
+
}
|
|
111
|
+
if (!isReservableSubdomain(subdomain)) {
|
|
112
|
+
throw new UsageError('--subdomain must name 3 to 63 lowercase letters, digits or hyphens, must not start or end with a hyphen, must not start with t- and must not be a reserved name such as www or api');
|
|
113
|
+
}
|
|
114
|
+
if (service.subdomain !== null || subdomains.has(subdomain)) {
|
|
115
|
+
throw new UsageError(`--subdomain is given twice for ${printable(name)} or for ${subdomain}; one subdomain serves one service`);
|
|
116
|
+
}
|
|
117
|
+
subdomains.add(subdomain);
|
|
118
|
+
service.subdomain = subdomain;
|
|
119
|
+
}
|
|
120
|
+
if (flags.team && !flags.share) {
|
|
121
|
+
throw new UsageError('--team only works together with --share');
|
|
122
|
+
}
|
|
123
|
+
if (flags.allowAgents && !flags.share) {
|
|
124
|
+
throw new UsageError('--allow-agents only works together with --share');
|
|
125
|
+
}
|
|
126
|
+
let edgeUrl = null;
|
|
127
|
+
let controlPlaneUrl = null;
|
|
128
|
+
if (flags.public.length > 0 || flags.team) {
|
|
129
|
+
const urls = await accountUrls(dataDir);
|
|
130
|
+
if (urls === 'logged-out') {
|
|
131
|
+
throw new HopwireError(flags.public.length > 0
|
|
132
|
+
? '--public needs an account: run hopwire login first; nothing was written.'
|
|
133
|
+
: '--team needs an account in an organisation on the Team plan: run hopwire login first; nothing was written.');
|
|
134
|
+
}
|
|
135
|
+
if (flags.public.length > 0) {
|
|
136
|
+
if (urls === 'no-edge') {
|
|
137
|
+
throw new HopwireError(NO_EDGE);
|
|
138
|
+
}
|
|
139
|
+
edgeUrl = urls.edgeUrl;
|
|
140
|
+
controlPlaneUrl = urls.controlPlaneUrl;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
services,
|
|
145
|
+
edgeUrl,
|
|
146
|
+
controlPlaneUrl,
|
|
147
|
+
share: flags.share,
|
|
148
|
+
team: flags.team,
|
|
149
|
+
allowAgents: flags.allowAgents,
|
|
150
|
+
shareLogs: false,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/** A port (`3000` becomes http://127.0.0.1:3000) or a loopback http URL without a path; null for anything else. */
|
|
154
|
+
export function localTarget(value) {
|
|
155
|
+
const trimmed = value.trim();
|
|
156
|
+
if (/^\d{1,5}$/.test(trimmed)) {
|
|
157
|
+
const port = Number(trimmed);
|
|
158
|
+
return port >= 1 && port <= 65535 ? new URL(`http://127.0.0.1:${port}`) : null;
|
|
159
|
+
}
|
|
160
|
+
return parseLocalTarget(trimmed);
|
|
161
|
+
}
|
|
162
|
+
// --- The questions ---
|
|
163
|
+
async function interview(options) {
|
|
164
|
+
const { ask, say } = options;
|
|
165
|
+
if (ask === undefined) {
|
|
166
|
+
throw new UsageError(NO_TERMINAL);
|
|
167
|
+
}
|
|
168
|
+
const question = async (text, fallback) => {
|
|
169
|
+
const answer = (await ask(fallback === null ? `${text} ` : `${text} [${fallback}] `)).trim();
|
|
170
|
+
return answer === '' && fallback !== null ? fallback : answer;
|
|
171
|
+
};
|
|
172
|
+
const yesNo = async (text) => {
|
|
173
|
+
for (;;) {
|
|
174
|
+
const answer = (await ask(`${text} [y/N] `)).trim().toLowerCase();
|
|
175
|
+
if (answer === '' || answer === 'n' || answer === 'no') {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
if (answer === 'y' || answer === 'yes') {
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
say('Answer y or n.');
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
// Read once, before the first question: whether a service can be public at all
|
|
185
|
+
const urls = await accountUrls(options.dataDir);
|
|
186
|
+
const detected = await detectPort(options.cwd);
|
|
187
|
+
const services = [];
|
|
188
|
+
let toldAboutPublic = false;
|
|
189
|
+
do {
|
|
190
|
+
let name;
|
|
191
|
+
for (;;) {
|
|
192
|
+
name = await question('Name of the service', services.some((s) => s.name === 'web') ? null : 'web');
|
|
193
|
+
if (!isServiceName(name)) {
|
|
194
|
+
say('A service name is 1 to 255 bytes without control characters.');
|
|
195
|
+
}
|
|
196
|
+
else if (services.some((service) => service.name === name)) {
|
|
197
|
+
say(`There is a service ${printable(name)} already; give each service its own name.`);
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
let local;
|
|
204
|
+
for (;;) {
|
|
205
|
+
const answer = await question('Port or URL of the service on this machine', services.length === 0 && detected !== null ? String(detected) : null);
|
|
206
|
+
local = localTarget(answer);
|
|
207
|
+
if (local !== null) {
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
say('Give a port, for example 3000, or an http URL on this machine without a path, for example http://127.0.0.1:3000.');
|
|
211
|
+
}
|
|
212
|
+
const service = { name, local, public: false, subdomain: null, logs: [] };
|
|
213
|
+
if (typeof urls === 'object') {
|
|
214
|
+
service.public = await yesNo('Serve it under a public URL?');
|
|
215
|
+
if (service.public) {
|
|
216
|
+
for (;;) {
|
|
217
|
+
const subdomain = await question('Subdomain (empty: the fixed URL of your organisation)', '');
|
|
218
|
+
if (subdomain === '') {
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
if (!isReservableSubdomain(subdomain)) {
|
|
222
|
+
say('A subdomain is 3 to 63 lowercase letters, digits or hyphens, does not start or end with a hyphen, does not start with t- and is not a reserved name such as www or api.');
|
|
223
|
+
}
|
|
224
|
+
else if (services.some((other) => other.subdomain === subdomain)) {
|
|
225
|
+
say(`${subdomain} serves another service already; give each its own.`);
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
service.subdomain = subdomain;
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
else if (!toldAboutPublic) {
|
|
235
|
+
say(urls === 'logged-out' ? PUBLIC_NEEDS_LOGIN : NO_EDGE);
|
|
236
|
+
toldAboutPublic = true;
|
|
237
|
+
}
|
|
238
|
+
// Phase 15: nothing is followed that was not named here
|
|
239
|
+
for (;;) {
|
|
240
|
+
const suggested = services.length === 0 && (await present(path.join(options.cwd, LARAVEL_LOG)));
|
|
241
|
+
const answer = await question('Log file to show next to the traffic (empty for none)', suggested ? LARAVEL_LOG : null);
|
|
242
|
+
if (answer === '') {
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
if (printable(answer) !== answer || path.dirname(answer).includes('*')) {
|
|
246
|
+
say('Give the path of a log file, with a * at most in the file name, or nothing.');
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
// In the file the path is relative to its folder
|
|
250
|
+
const absolute = path.resolve(options.cwd, answer);
|
|
251
|
+
const relative = path.relative(path.dirname(options.file), absolute);
|
|
252
|
+
service.logs.push(path.isAbsolute(answer) || relative.startsWith('..') ? absolute : relative);
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
services.push(service);
|
|
256
|
+
} while (await yesNo('Another service?'));
|
|
257
|
+
const share = await yesNo('Share with teammates over P2P (hopwire watch, hopwire connect)?');
|
|
258
|
+
let team = false;
|
|
259
|
+
let allowAgents = false;
|
|
260
|
+
let shareLogs = false;
|
|
261
|
+
if (share) {
|
|
262
|
+
if (urls !== 'logged-out') {
|
|
263
|
+
team = await yesNo("Only machines of your organisation's members (Team plan)?");
|
|
264
|
+
}
|
|
265
|
+
if (services.some((service) => service.logs.length > 0)) {
|
|
266
|
+
say("Logs often hold secrets and other people's data.");
|
|
267
|
+
shareLogs = await yesNo('Show the log entries to teammates who watch too?');
|
|
268
|
+
}
|
|
269
|
+
say("What a teammate's coding agent reads goes to its model provider.");
|
|
270
|
+
allowAgents = await yesNo("Let teammates' coding agents read what is captured?");
|
|
271
|
+
}
|
|
272
|
+
const anyPublic = services.some((service) => service.public);
|
|
273
|
+
return {
|
|
274
|
+
services,
|
|
275
|
+
edgeUrl: anyPublic && typeof urls === 'object' ? urls.edgeUrl : null,
|
|
276
|
+
controlPlaneUrl: anyPublic && typeof urls === 'object' ? urls.controlPlaneUrl : null,
|
|
277
|
+
share,
|
|
278
|
+
team,
|
|
279
|
+
allowAgents,
|
|
280
|
+
shareLogs,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
/** The log file `hopwire init` suggests when the working directory has it. */
|
|
284
|
+
const LARAVEL_LOG = 'storage/logs/laravel.log';
|
|
285
|
+
// --- The port suggestion ---
|
|
286
|
+
/**
|
|
287
|
+
* The port suggested for the first service, never used with --yes: PORT=<n> in .env of the working directory (only
|
|
288
|
+
* that key is looked at), else an explicit port in the dev or start script of package.json. No port is probed.
|
|
289
|
+
*/
|
|
290
|
+
export async function detectPort(cwd) {
|
|
291
|
+
const env = await readText(path.join(cwd, '.env'));
|
|
292
|
+
if (env !== null) {
|
|
293
|
+
const match = /^[ \t]*(?:export[ \t]+)?PORT[ \t]*=[ \t]*["']?(\d{1,5})["']?[ \t]*(?:#.*)?$/m.exec(env);
|
|
294
|
+
const port = validPort(match?.[1]);
|
|
295
|
+
if (port !== null) {
|
|
296
|
+
return port;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const manifest = await readText(path.join(cwd, 'package.json'));
|
|
300
|
+
if (manifest !== null) {
|
|
301
|
+
let scripts;
|
|
302
|
+
try {
|
|
303
|
+
scripts = JSON.parse(manifest).scripts;
|
|
304
|
+
}
|
|
305
|
+
catch {
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
if (typeof scripts === 'object' && scripts !== null) {
|
|
309
|
+
for (const key of ['dev', 'start']) {
|
|
310
|
+
const script = scripts[key];
|
|
311
|
+
if (typeof script !== 'string') {
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
const match = /(?:--port[= ]|(?:^|\s)-p\s+|(?:^|\s)PORT=)(\d{1,5})\b/.exec(script);
|
|
315
|
+
const port = validPort(match?.[1]);
|
|
316
|
+
if (port !== null) {
|
|
317
|
+
return port;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
function validPort(value) {
|
|
325
|
+
const port = Number(value);
|
|
326
|
+
return value !== undefined && Number.isInteger(port) && port >= 1 && port <= 65535 ? port : null;
|
|
327
|
+
}
|
|
328
|
+
async function readText(file) {
|
|
329
|
+
try {
|
|
330
|
+
return await readFile(file, 'utf8');
|
|
331
|
+
}
|
|
332
|
+
catch {
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
// --- git ---
|
|
337
|
+
/**
|
|
338
|
+
* `git check-ignore -q`: exit 0 when the file is ignored, 1 when it is not; anything else (no work tree, no git) is
|
|
339
|
+
* null and prints no line. Runs locally; git contacts nothing for it.
|
|
340
|
+
*/
|
|
341
|
+
export function gitIgnores(file) {
|
|
342
|
+
return new Promise((resolve) => {
|
|
343
|
+
execFile('git', ['check-ignore', '-q', '--', path.basename(file)], { cwd: path.dirname(file), timeout: 5000, windowsHide: true }, (error) => {
|
|
344
|
+
if (error === null) {
|
|
345
|
+
resolve(true);
|
|
346
|
+
}
|
|
347
|
+
else if (typeof error.code === 'number' && error.code === 1) {
|
|
348
|
+
resolve(false);
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
resolve(null);
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
}
|
package/dist/commands/up.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import path from 'node:path';
|
|
2
3
|
import { LOCAL_ENTRY_DEFAULT_PORT, RANDOM_SUBDOMAIN_BYTES, randomSubdomain } from '@hopwire/protocol';
|
|
3
4
|
import { HopwireError } from '../errors.js';
|
|
4
|
-
import { MODE_LOCAL, MODE_P2P, MODE_P2P_TEAM, MODE_PUBLIC, MODE_PUBLIC_P2P, MODE_PUBLIC_P2P_TEAM, AGENTS_ALLOWED_LINE, agentLine, agentOffLine, formatExchange, formatTeamEvent, formatUsage, shareNotice, tunnelGuide, } from '../output.js';
|
|
5
|
+
import { MODE_LOCAL, MODE_P2P, MODE_P2P_TEAM, relayLine, MODE_PUBLIC, MODE_PUBLIC_P2P, MODE_PUBLIC_P2P_TEAM, AGENTS_ALLOWED_LINE, agentLine, agentOffLine, formatExchange, formatLogNotice, logLines, formatRelayEvent, formatTeamEvent, formatUsage, shareNotice, tunnelGuide, mainPublicUrl, publicUrlText, } from '../output.js';
|
|
5
6
|
import { startServing } from '../serve.js';
|
|
6
7
|
import { formatBridgeEvent } from './http.js';
|
|
7
8
|
/**
|
|
@@ -30,6 +31,11 @@ export async function startUp(options) {
|
|
|
30
31
|
listenPort: port > 65535 ? 0 : port,
|
|
31
32
|
listenFallback: true,
|
|
32
33
|
public: publicOptions(service, config, options),
|
|
34
|
+
// Relative to the folder of hopwire.yaml
|
|
35
|
+
logs: service.logs.map((log) => ({
|
|
36
|
+
name: log.name,
|
|
37
|
+
path: path.resolve(path.dirname(path.resolve(options.configFile)), log.path),
|
|
38
|
+
})),
|
|
33
39
|
};
|
|
34
40
|
});
|
|
35
41
|
// The line about a tunnel --force stopped follows the startup lines: the mode stays the first line
|
|
@@ -45,10 +51,15 @@ export async function startUp(options) {
|
|
|
45
51
|
allow: config.allow ?? [],
|
|
46
52
|
team: options.team,
|
|
47
53
|
onTeamEvent: (event, organisation) => options.print(formatTeamEvent(event, organisation)),
|
|
54
|
+
relay: options.relay,
|
|
55
|
+
onRelayEvent: (event) => options.print(formatRelayEvent(event)),
|
|
48
56
|
bootstrap: options.bootstrap,
|
|
49
57
|
inspectorFiles: options.inspectorFiles,
|
|
50
58
|
agent: options.agent,
|
|
51
59
|
captureWebSocketMessages: config.captureWebSocket,
|
|
60
|
+
shareLogs: config.shareLogs,
|
|
61
|
+
logRequestId: config.logRequestId,
|
|
62
|
+
onLogNotice: (name, line) => options.print(formatLogNotice(name, line, config.services.length)),
|
|
52
63
|
signal: options.signal,
|
|
53
64
|
listenOption: 'a free port',
|
|
54
65
|
inspectOption: 'inspector.port in hopwire.yaml',
|
|
@@ -74,7 +85,8 @@ export async function startUp(options) {
|
|
|
74
85
|
}
|
|
75
86
|
options.print(`Forwarding ${service.url} -> ${service.target}`);
|
|
76
87
|
if (service.subdomain !== null) {
|
|
77
|
-
const url = service.publicUrl
|
|
88
|
+
const url = publicUrlText(service.publicUrl, service.customDomains) ??
|
|
89
|
+
`subdomain ${service.subdomain} (the edge did not report its URL)`;
|
|
78
90
|
options.print(`Public URL ${url}${settings?.basicAuth == null ? '' : ' (asks for the user and password of auth.basic)'}`);
|
|
79
91
|
if (service.usage !== null) {
|
|
80
92
|
options.print(formatUsage(service.usage));
|
|
@@ -85,10 +97,16 @@ export async function startUp(options) {
|
|
|
85
97
|
if (serving.peerKey !== null) {
|
|
86
98
|
options.print(`Peer key ${serving.peerKey}`);
|
|
87
99
|
options.print(shareNotice(access, 'p2p.allow in hopwire.yaml'));
|
|
100
|
+
if (serving.relay !== null) {
|
|
101
|
+
options.print(relayLine(serving.relay.edgeUrl, serving.relay.problem));
|
|
102
|
+
}
|
|
88
103
|
if (config.allowAgents) {
|
|
89
104
|
options.print(AGENTS_ALLOWED_LINE);
|
|
90
105
|
}
|
|
91
106
|
}
|
|
107
|
+
for (const line of logLines(serving.logs, serving.peerKey !== null && config.shareLogs, path.dirname(path.resolve(options.configFile)), config.services.length)) {
|
|
108
|
+
options.print(line);
|
|
109
|
+
}
|
|
92
110
|
options.print(`Inspector ${serving.inspectorUrl ?? ''}`);
|
|
93
111
|
if (serving.agentSocket !== null) {
|
|
94
112
|
const [first, ...rest] = serving.services;
|
|
@@ -102,7 +120,7 @@ export async function startUp(options) {
|
|
|
102
120
|
name: service.name,
|
|
103
121
|
url: service.url,
|
|
104
122
|
target: service.target,
|
|
105
|
-
publicUrl: service.publicUrl,
|
|
123
|
+
publicUrl: mainPublicUrl(service.publicUrl, service.customDomains),
|
|
106
124
|
basicAuth: config.services[index]?.basicAuth != null,
|
|
107
125
|
stableUrl: options.connectTokens !== undefined,
|
|
108
126
|
})),
|
|
@@ -113,6 +131,7 @@ export async function startUp(options) {
|
|
|
113
131
|
logKey: serving.logKey,
|
|
114
132
|
allowed: access,
|
|
115
133
|
restrictWith: 'p2p.allow in hopwire.yaml',
|
|
134
|
+
relay: serving.relay !== null,
|
|
116
135
|
},
|
|
117
136
|
inspector: serving.inspectorUrl !== null,
|
|
118
137
|
widenWith: { share: 'p2p.share: true in hopwire.yaml', public: 'public: true on a service' },
|