@reclaimprotocol/agent 0.1.29 → 0.1.31
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 +28 -9
- package/lib/consts.d.ts +5 -5
- package/lib/consts.js +7 -7
- package/lib/live/tunnel-watchdog.d.ts +1 -0
- package/lib/live/tunnel-watchdog.js +66 -0
- package/lib/live/tunnel.d.ts +30 -7
- package/lib/live/tunnel.js +217 -40
- package/lib/mcp/api/operations.gen.d.ts +12 -0
- package/lib/mcp/api/operations.gen.js +537 -59
- package/lib/mcp/api/tools.js +27 -5
- package/lib/mcp/instructions.js +23 -32
- package/lib/mcp/remap_errors.js +6 -0
- package/lib/mcp/server.js +2 -8
- package/lib/mcp/start.js +3 -5
- package/lib/mcp/success-response.d.ts +5 -0
- package/lib/mcp/success-response.js +21 -0
- package/lib/mcp/tools/agent/attach.js +9 -66
- package/lib/mcp/tools/agent/build.d.ts +0 -3
- package/lib/mcp/tools/agent/build.js +3 -4
- package/lib/mcp/tools/agent/how-it-works.d.ts +5 -15
- package/lib/mcp/tools/agent/how-it-works.js +9 -22
- package/lib/mcp/tools/agent/live-view.d.ts +1 -15
- package/lib/mcp/tools/agent/live-view.js +71 -96
- package/lib/mcp/tools/agent/propose.js +7 -23
- package/lib/mcp/tools/agent/publish.js +80 -18
- package/lib/mcp/tools/agent/session.d.ts +4 -7
- package/lib/mcp/tools/agent/session.js +8 -23
- package/lib/mcp/tools/verification/result.d.ts +2 -1
- package/lib/mcp/tools/verification/result.js +29 -28
- package/lib/old/client.d.ts +2 -2
- package/lib/old/flag.d.ts +2 -5
- package/lib/old/flag.js +2 -5
- package/lib/old/instructions.d.ts +1 -1
- package/lib/old/instructions.js +1 -1
- package/lib/old/to-register.d.ts +4 -3
- package/lib/old/tools/publish.js +11 -14
- package/lib/proof/attestor-auth.d.ts +9 -1
- package/lib/proof/attestor-auth.js +15 -2
- package/lib/proof/attestor.d.ts +2 -2
- package/lib/proof/attestor.js +20 -2
- package/lib/proof/pod-prover.js +1 -1
- package/lib/proof/proof-core.d.ts +4 -0
- package/lib/proof/proof-core.js +20 -0
- package/lib/provider/injection-doc.d.ts +3 -2
- package/lib/provider/injection-doc.js +12 -10
- package/lib/provider/matcher.js +19 -15
- package/lib/provider/to-version.d.ts +3 -0
- package/lib/provider/to-version.js +2 -1
- package/package.json +3 -3
- package/skill/SKILL.builder.md +30 -83
- package/skill/SKILL.common.md +38 -22
- package/skill/SKILL.old.md +17 -3
- package/skill/builder-mode.md +37 -4
- package/skill/community-contributions.md +12 -3
- package/skill/hash-validation.md +9 -9
- package/skill/interception.md +39 -46
- package/skill/old-mode.md +13 -0
- package/skill/user-script.md +24 -8
- package/skill/verification-client-integration.md +130 -0
- package/src/consts.ts +9 -7
- package/src/mcp/api/operations.gen.ts +539 -59
- package/src/mcp/api/tools.ts +38 -7
- package/src/mcp/instructions.ts +33 -32
- package/src/mcp/remap_errors.ts +6 -0
- package/src/mcp/server.ts +2 -8
- package/src/mcp/start.ts +3 -5
- package/src/mcp/success-response.ts +26 -0
- package/src/mcp/tools/agent/attach.ts +9 -71
- package/src/mcp/tools/agent/build.ts +4 -7
- package/src/mcp/tools/agent/how-it-works.ts +9 -22
- package/src/mcp/tools/agent/live-view.ts +76 -116
- package/src/mcp/tools/agent/propose.ts +7 -23
- package/src/mcp/tools/agent/publish.ts +89 -18
- package/src/mcp/tools/agent/session.ts +6 -23
- package/src/mcp/tools/verification/result.ts +30 -29
- package/src/old/client.ts +2 -2
- package/src/old/flag.ts +2 -5
- package/src/old/instructions.ts +1 -1
- package/src/old/to-register.ts +4 -3
- package/src/old/tools/publish.ts +16 -14
- package/src/proof/attestor-auth.ts +18 -1
- package/src/proof/attestor.ts +24 -3
- package/src/proof/pod-prover.ts +1 -1
- package/src/proof/proof-core.ts +28 -0
- package/src/provider/injection-doc.ts +12 -10
- package/src/provider/matcher.ts +25 -17
- package/src/provider/to-version.ts +7 -1
- package/src/live/cloudflared.ts +0 -348
- package/src/live/tunnel.ts +0 -254
package/src/live/cloudflared.ts
DELETED
|
@@ -1,348 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Finding — and if necessary fetching — the `cloudflared` binary the live view
|
|
3
|
-
* tunnels through.
|
|
4
|
-
*
|
|
5
|
-
* Resolution order, first hit wins:
|
|
6
|
-
* 1. `RECLAIM_CLOUDFLARED_PATH`, for an air-gapped box or a corporate mirror.
|
|
7
|
-
* 2. `cloudflared` on PATH — someone who installed it with a package manager
|
|
8
|
-
* keeps their own copy, updated their way.
|
|
9
|
-
* 3. `~/.reclaim/bin/cloudflared`, ours from a previous run.
|
|
10
|
-
* 4. Download it from Cloudflare's GitHub releases into `~/.reclaim/bin`.
|
|
11
|
-
*
|
|
12
|
-
* Step 4 happens on FIRST USE, never at install time. The npm `cloudflared`
|
|
13
|
-
* package would have done this for us, but its postinstall pulls a ~38 MB
|
|
14
|
-
* binary on every `npm install` of this package — a cost paid by everyone who
|
|
15
|
-
* never shares a live view. `run_proof`'s ZK circuits are deferred for exactly
|
|
16
|
-
* the same reason.
|
|
17
|
-
*
|
|
18
|
-
* Every download is SHA-256 verified. Cloudflare publishes a checksum table in
|
|
19
|
-
* the release notes (not as a release asset), so we read the release metadata
|
|
20
|
-
* first, take the digest for our asset, and refuse to install anything that
|
|
21
|
-
* doesn't match. The release TAG from that same response is used to build the
|
|
22
|
-
* download URL, so the checksum and the bytes always come from one release —
|
|
23
|
-
* `/latest/download/` could otherwise hand us a newer build than the table we
|
|
24
|
-
* just read.
|
|
25
|
-
*
|
|
26
|
-
* Verification is not optional: this writes an executable to the developer's
|
|
27
|
-
* machine. If the checksum can't be established, we fail rather than install.
|
|
28
|
-
* The escape hatch is to install cloudflared yourself — step 2 then wins and
|
|
29
|
-
* nothing is downloaded.
|
|
30
|
-
*/
|
|
31
|
-
import { execFile } from 'node:child_process'
|
|
32
|
-
import { createHash } from 'node:crypto'
|
|
33
|
-
import {
|
|
34
|
-
chmod,
|
|
35
|
-
mkdir,
|
|
36
|
-
rename,
|
|
37
|
-
rm,
|
|
38
|
-
stat,
|
|
39
|
-
writeFile
|
|
40
|
-
} from 'node:fs/promises'
|
|
41
|
-
import { arch, platform } from 'node:os'
|
|
42
|
-
import { join } from 'node:path'
|
|
43
|
-
import { promisify } from 'node:util'
|
|
44
|
-
import { gunzipSync } from 'node:zlib'
|
|
45
|
-
import { LOGGER } from '../logger.ts'
|
|
46
|
-
import { reclaimHomeDirPath } from '../paths.ts'
|
|
47
|
-
|
|
48
|
-
const execFileAsync = promisify(execFile)
|
|
49
|
-
|
|
50
|
-
/** Release metadata: the tag to pin to, and the checksum table in its notes. */
|
|
51
|
-
const LATEST_RELEASE_API =
|
|
52
|
-
'https://api.github.com/repos/cloudflare/cloudflared/releases/latest'
|
|
53
|
-
/** Asset downloads, keyed by the tag we read the checksums from. */
|
|
54
|
-
function releaseDownloadUrl(tag: string, asset: string): string {
|
|
55
|
-
return 'https://github.com/cloudflare/cloudflared/releases/download'
|
|
56
|
-
+ `/${tag}/${asset}`
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** Give up on a slow or stalled download rather than hanging the tool. */
|
|
60
|
-
const DOWNLOAD_TIMEOUT_MS = 180_000
|
|
61
|
-
|
|
62
|
-
export interface CloudflaredBinary {
|
|
63
|
-
/** Path to run. */
|
|
64
|
-
path: string
|
|
65
|
-
/** How it was found — surfaced so the caller can tell the developer when a
|
|
66
|
-
* download just happened on their machine. */
|
|
67
|
-
source: 'env' | 'path' | 'cached' | 'downloaded'
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* The release asset for this machine, or `undefined` when Cloudflare ships no
|
|
72
|
-
* build for it. macOS assets are gzipped tarballs; everything else is the bare
|
|
73
|
-
* executable.
|
|
74
|
-
*/
|
|
75
|
-
function assetFor(
|
|
76
|
-
os: string,
|
|
77
|
-
cpu: string,
|
|
78
|
-
): { name: string, tarball: boolean } | undefined {
|
|
79
|
-
if(os === 'darwin') {
|
|
80
|
-
const suffix = cpu === 'arm64' ? 'arm64' : 'amd64'
|
|
81
|
-
return { name: `cloudflared-darwin-${suffix}.tgz`, tarball: true }
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if(os === 'linux') {
|
|
85
|
-
const map: Record<string, string> = {
|
|
86
|
-
x64: 'amd64',
|
|
87
|
-
arm64: 'arm64',
|
|
88
|
-
arm: 'arm',
|
|
89
|
-
ia32: '386',
|
|
90
|
-
}
|
|
91
|
-
const suffix = map[cpu]
|
|
92
|
-
return suffix
|
|
93
|
-
? { name: `cloudflared-linux-${suffix}`, tarball: false }
|
|
94
|
-
: undefined
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if(os === 'win32') {
|
|
98
|
-
const suffix = cpu === 'ia32' ? '386' : 'amd64'
|
|
99
|
-
return { name: `cloudflared-windows-${suffix}.exe`, tarball: false }
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return undefined
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** Our install location. Kept beside the other `~/.reclaim` caches. */
|
|
106
|
-
function cachedBinaryPath(): string {
|
|
107
|
-
const name = platform() === 'win32' ? 'cloudflared.exe' : 'cloudflared'
|
|
108
|
-
return join(reclaimHomeDirPath(), 'bin', name)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** Whether `command` runs. Used both for a PATH probe and to smoke-test a
|
|
112
|
-
* freshly downloaded binary before we hand it back. */
|
|
113
|
-
async function runsOk(command: string): Promise<boolean> {
|
|
114
|
-
try {
|
|
115
|
-
await execFileAsync(command, ['--version'], { timeout: 15_000 })
|
|
116
|
-
return true
|
|
117
|
-
} catch{
|
|
118
|
-
return false
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async function exists(path: string): Promise<boolean> {
|
|
123
|
-
try {
|
|
124
|
-
await stat(path)
|
|
125
|
-
return true
|
|
126
|
-
} catch{
|
|
127
|
-
return false
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Resolve a runnable cloudflared, downloading one only if `autoInstall` allows
|
|
133
|
-
* it and nothing else turned up. Throws with an actionable message when this
|
|
134
|
-
* platform has no build, or the download fails.
|
|
135
|
-
*/
|
|
136
|
-
export async function resolveCloudflared(
|
|
137
|
-
{ autoInstall = true }: { autoInstall?: boolean } = {},
|
|
138
|
-
): Promise<CloudflaredBinary> {
|
|
139
|
-
const override = process.env.RECLAIM_CLOUDFLARED_PATH?.trim()
|
|
140
|
-
if(override) {
|
|
141
|
-
if(!await runsOk(override)) {
|
|
142
|
-
throw new Error(
|
|
143
|
-
`RECLAIM_CLOUDFLARED_PATH is set to "${override}", but that did not `
|
|
144
|
-
+ 'run. Point it at a working cloudflared binary, or unset it to let '
|
|
145
|
-
+ 'the agent find one.',
|
|
146
|
-
)
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
return { path: override, source: 'env' }
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if(await runsOk('cloudflared')) {
|
|
153
|
-
return { path: 'cloudflared', source: 'path' }
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const cached = cachedBinaryPath()
|
|
157
|
-
if(await exists(cached) && await runsOk(cached)) {
|
|
158
|
-
return { path: cached, source: 'cached' }
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if(!autoInstall) {
|
|
162
|
-
throw new Error(
|
|
163
|
-
'cloudflared is not installed. Install it (macOS: `brew install '
|
|
164
|
-
+ 'cloudflared`; other platforms: https://developers.cloudflare.com/'
|
|
165
|
-
+ 'cloudflare-one/connections/connect-networks/downloads/), or allow '
|
|
166
|
-
+ 'the agent to fetch it.',
|
|
167
|
-
)
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
await install(cached)
|
|
171
|
-
return { path: cached, source: 'downloaded' }
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/** `cloudflared-linux-amd64: <64 hex>` lines inside the notes' fenced block. */
|
|
175
|
-
const CHECKSUM_LINE = /^\s*(\S+):\s*([a-f0-9]{64})\s*$/gim
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* The current release's tag and its published SHA-256 table.
|
|
179
|
-
*
|
|
180
|
-
* Both come from one response on purpose: pinning the download to this tag is
|
|
181
|
-
* what makes the digest meaningful.
|
|
182
|
-
*/
|
|
183
|
-
async function fetchRelease(): Promise<{
|
|
184
|
-
tag: string
|
|
185
|
-
checksums: Map<string, string>
|
|
186
|
-
}> {
|
|
187
|
-
const res = await fetch(LATEST_RELEASE_API, {
|
|
188
|
-
headers: {
|
|
189
|
-
accept: 'application/vnd.github+json',
|
|
190
|
-
'user-agent': '@reclaimprotocol/agent',
|
|
191
|
-
},
|
|
192
|
-
signal: AbortSignal.timeout(30_000),
|
|
193
|
-
})
|
|
194
|
-
if(!res.ok) {
|
|
195
|
-
throw new Error(
|
|
196
|
-
`Could not read cloudflared release metadata: ${res.status} `
|
|
197
|
-
+ `${res.statusText}. Needed to verify the download's checksum. `
|
|
198
|
-
+ 'Install cloudflared yourself, or set RECLAIM_CLOUDFLARED_PATH.',
|
|
199
|
-
)
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
const release = await res.json() as { tag_name?: string, body?: string }
|
|
203
|
-
const tag = release.tag_name
|
|
204
|
-
if(!tag) {
|
|
205
|
-
throw new Error('cloudflared release metadata carried no tag.')
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
const checksums = new Map<string, string>()
|
|
209
|
-
for(const match of (release.body ?? '').matchAll(CHECKSUM_LINE)) {
|
|
210
|
-
checksums.set(match[1], match[2].toLowerCase())
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
return { tag, checksums }
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/** Download the release asset for this machine to `target`, verified. */
|
|
217
|
-
async function install(target: string): Promise<void> {
|
|
218
|
-
const asset = assetFor(platform(), arch())
|
|
219
|
-
if(!asset) {
|
|
220
|
-
throw new Error(
|
|
221
|
-
`Cloudflare publishes no cloudflared build for ${platform()}/${arch()}. `
|
|
222
|
-
+ 'Install it another way and set RECLAIM_CLOUDFLARED_PATH, or use '
|
|
223
|
-
+ '`attach_browser` in builder mode, whose Popcorn browser returns a '
|
|
224
|
-
+ 'hosted liveViewUrl and needs no tunnel.',
|
|
225
|
-
)
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// Read the checksum table BEFORE downloading, and pin the download to the
|
|
229
|
-
// tag it came from.
|
|
230
|
-
const { tag, checksums } = await fetchRelease()
|
|
231
|
-
const expected = checksums.get(asset.name)
|
|
232
|
-
if(!expected) {
|
|
233
|
-
throw new Error(
|
|
234
|
-
`cloudflared release ${tag} publishes no SHA-256 for ${asset.name}, so `
|
|
235
|
-
+ 'the download cannot be verified. Refusing to install an unverified '
|
|
236
|
-
+ 'binary. Install cloudflared yourself, or set '
|
|
237
|
-
+ 'RECLAIM_CLOUDFLARED_PATH.',
|
|
238
|
-
)
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
const url = releaseDownloadUrl(tag, asset.name)
|
|
242
|
-
LOGGER.info({ url, target, tag }, 'cloudflared: downloading (first use)')
|
|
243
|
-
const res = await fetch(url, {
|
|
244
|
-
redirect: 'follow',
|
|
245
|
-
signal: AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS),
|
|
246
|
-
})
|
|
247
|
-
if(!res.ok) {
|
|
248
|
-
throw new Error(
|
|
249
|
-
`Downloading cloudflared failed: ${res.status} ${res.statusText} `
|
|
250
|
-
+ `from ${url}. Install it manually and retry, or set `
|
|
251
|
-
+ 'RECLAIM_CLOUDFLARED_PATH.',
|
|
252
|
-
)
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
const downloaded = Buffer.from(await res.arrayBuffer())
|
|
256
|
-
|
|
257
|
-
// The macOS assets are tarballs, so unpack before hashing. Unpacked
|
|
258
|
-
// in-process rather than by shelling out to `tar`: that would add a
|
|
259
|
-
// dependency on something being on PATH, which is exactly the assumption
|
|
260
|
-
// this module exists to stop making.
|
|
261
|
-
//
|
|
262
|
-
// This ordering is not incidental. Cloudflare's table lists a digest for
|
|
263
|
-
// `cloudflared-darwin-arm64.tgz`, but that digest is of the BINARY INSIDE,
|
|
264
|
-
// not of the tarball (verified against a real release: the tarball hashes
|
|
265
|
-
// to something else entirely, and the binary self-reports the published
|
|
266
|
-
// value as its own checksum). The linux and windows entries, which ship a
|
|
267
|
-
// bare executable, do match the downloaded file. Hashing the payload we are
|
|
268
|
-
// about to write covers both, and is the stronger invariant anyway: what
|
|
269
|
-
// gets verified is what gets executed.
|
|
270
|
-
const payload = asset.tarball
|
|
271
|
-
? readTarEntry(gunzipSync(downloaded), 'cloudflared')
|
|
272
|
-
: downloaded
|
|
273
|
-
|
|
274
|
-
const actual = createHash('sha256').update(payload).digest('hex')
|
|
275
|
-
if(actual !== expected) {
|
|
276
|
-
// Nothing is written to disk on a mismatch — not even a partial file.
|
|
277
|
-
throw new Error(
|
|
278
|
-
`cloudflared checksum mismatch for ${asset.name} (release ${tag}). `
|
|
279
|
-
+ `Expected ${expected}, got ${actual}. The download was discarded. `
|
|
280
|
-
+ 'Retry; if it persists, install cloudflared yourself and set '
|
|
281
|
-
+ 'RECLAIM_CLOUDFLARED_PATH.',
|
|
282
|
-
)
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
LOGGER.info({ asset: asset.name, tag }, 'cloudflared: checksum verified')
|
|
286
|
-
|
|
287
|
-
// Write beside the target, then rename: a half-written or unverified
|
|
288
|
-
// binary is never left at a path a later run would trust.
|
|
289
|
-
await mkdir(join(target, '..'), { recursive: true })
|
|
290
|
-
const partial = `${target}.download`
|
|
291
|
-
await writeFile(partial, payload)
|
|
292
|
-
await rename(partial, target)
|
|
293
|
-
|
|
294
|
-
if(platform() !== 'win32') {
|
|
295
|
-
await chmod(target, 0o755)
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
if(!await runsOk(target)) {
|
|
299
|
-
await rm(target, { force: true })
|
|
300
|
-
throw new Error(
|
|
301
|
-
'Downloaded cloudflared, but it did not run. Removed it. Install '
|
|
302
|
-
+ 'cloudflared manually and retry.',
|
|
303
|
-
)
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
LOGGER.info({ target }, 'cloudflared: installed')
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/** A tar header block is 512 bytes, and so is every data block. */
|
|
310
|
-
const TAR_BLOCK = 512
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Pull one file out of an uncompressed tar archive.
|
|
314
|
-
*
|
|
315
|
-
* Only as much of the format as this needs: walk the 512-byte headers, read
|
|
316
|
-
* each entry's name and octal size, and return the payload of the first
|
|
317
|
-
* regular file whose basename matches. Cloudflare's macOS tarball holds a
|
|
318
|
-
* single binary, so there is nothing more to handle.
|
|
319
|
-
*/
|
|
320
|
-
function readTarEntry(tar: Buffer, wanted: string): Buffer {
|
|
321
|
-
let offset = 0
|
|
322
|
-
while(offset + TAR_BLOCK <= tar.length) {
|
|
323
|
-
const header = tar.subarray(offset, offset + TAR_BLOCK)
|
|
324
|
-
const name = header.subarray(0, 100).toString('utf8').replace(/\0.*$/, '')
|
|
325
|
-
if(!name) {
|
|
326
|
-
break // two zero blocks mark the end of the archive
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
const size = parseInt(
|
|
330
|
-
header.subarray(124, 136).toString('utf8').replace(/\0.*$/, '').trim(),
|
|
331
|
-
8,
|
|
332
|
-
) || 0
|
|
333
|
-
const type = header.subarray(156, 157).toString('utf8')
|
|
334
|
-
const dataStart = offset + TAR_BLOCK
|
|
335
|
-
if((type === '0' || type === '\0') && name.split('/').pop() === wanted) {
|
|
336
|
-
return tar.subarray(dataStart, dataStart + size)
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
// Data is padded up to the next block boundary.
|
|
340
|
-
offset = dataStart + Math.ceil(size / TAR_BLOCK) * TAR_BLOCK
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
throw new Error(
|
|
344
|
-
`The cloudflared archive did not contain "${wanted}". Cloudflare may `
|
|
345
|
-
+ 'have changed its release layout — install cloudflared manually and '
|
|
346
|
-
+ 'set RECLAIM_CLOUDFLARED_PATH.',
|
|
347
|
-
)
|
|
348
|
-
}
|
package/src/live/tunnel.ts
DELETED
|
@@ -1,254 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Public HTTPS front door for a local port, via a `cloudflared` quick tunnel.
|
|
3
|
-
*
|
|
4
|
-
* A quick tunnel needs no Cloudflare account and no DNS: `cloudflared` dials
|
|
5
|
-
* out, Cloudflare hands back a throwaway `*.trycloudflare.com` hostname, and
|
|
6
|
-
* traffic arrives on the loopback port we pass in. That is the whole reason it
|
|
7
|
-
* is used here — the developer's machine keeps no inbound port open.
|
|
8
|
-
*
|
|
9
|
-
* **The hostname IS the secret, and it is the only one.** The tunnel carries no
|
|
10
|
-
* authorization of its own, and the browser runtime's viewer behind it has no
|
|
11
|
-
* login either — so whoever learns the URL has the browser. Cloudflare's
|
|
12
|
-
* hostnames are randomly generated, which is what makes that tolerable for a
|
|
13
|
-
* short, deliberate hand-off, and `stop_browser_view` is how it ends.
|
|
14
|
-
*
|
|
15
|
-
* That is weaker than what this file used to front. The agent's own screencast
|
|
16
|
-
* viewer added a 24-byte token to every path, so the hostname alone was not
|
|
17
|
-
* enough; the container's viewer has no equivalent. Callers must say plainly
|
|
18
|
-
* what the URL grants, and it must never front the container's CDP port, which
|
|
19
|
-
* is unauthenticated total control of the browser.
|
|
20
|
-
*/
|
|
21
|
-
import { spawn } from 'node:child_process'
|
|
22
|
-
import { LOGGER } from '../logger.ts'
|
|
23
|
-
import { type CloudflaredBinary, resolveCloudflared } from './cloudflared.ts'
|
|
24
|
-
|
|
25
|
-
/** How long to wait for cloudflared to report its hostname before giving up. */
|
|
26
|
-
const URL_TIMEOUT_MS = 30_000
|
|
27
|
-
/** After the hostname is printed, Cloudflare needs time before the edge routes
|
|
28
|
-
* to it. Probe briefly, then return the still-running tunnel as propagating so
|
|
29
|
-
* slow DNS or edge setup does not destroy an otherwise healthy share. */
|
|
30
|
-
const READY_TIMEOUT_MS = 20_000
|
|
31
|
-
const READY_POLL_MS = 1_000
|
|
32
|
-
const STOP_TIMEOUT_MS = 5_000
|
|
33
|
-
|
|
34
|
-
/** `https://<something>.trycloudflare.com`, as printed on cloudflared's log
|
|
35
|
-
* stream. Anchored to the scheme so a hostname inside prose can't match. */
|
|
36
|
-
const QUICK_TUNNEL_URL = /https:\/\/[a-z0-9-]+\.trycloudflare\.com/i
|
|
37
|
-
|
|
38
|
-
/** Tail of cloudflared's log kept for error messages. */
|
|
39
|
-
const LOG_TAIL_BYTES = 4096
|
|
40
|
-
|
|
41
|
-
export interface Tunnel {
|
|
42
|
-
/** The public HTTPS origin Cloudflare assigned. */
|
|
43
|
-
url: string
|
|
44
|
-
/** Whether the public viewer answered during the initial readiness probe. */
|
|
45
|
-
ready: boolean
|
|
46
|
-
/** Bounded cloudflared output when the edge is still propagating. */
|
|
47
|
-
cloudflaredLog?: string
|
|
48
|
-
/** Terminate the tunnel. Safe to call more than once. */
|
|
49
|
-
stop: () => Promise<void>
|
|
50
|
-
/** Which cloudflared ran, and whether we had to fetch it. */
|
|
51
|
-
binary: CloudflaredBinary
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Wait until the tunnel is actually carrying traffic.
|
|
56
|
-
*
|
|
57
|
-
* A cloudflared "Registered tunnel connection" log is not enough: Cloudflare
|
|
58
|
-
* can report one while the assigned hostname still returns DNS NXDOMAIN. The
|
|
59
|
-
* public viewer itself must return a successful response.
|
|
60
|
-
*/
|
|
61
|
-
async function waitUntilRouted(
|
|
62
|
-
url: string,
|
|
63
|
-
{
|
|
64
|
-
isRunning,
|
|
65
|
-
pollMs,
|
|
66
|
-
timeoutMs,
|
|
67
|
-
}: {
|
|
68
|
-
isRunning: () => boolean
|
|
69
|
-
pollMs: number
|
|
70
|
-
timeoutMs: number
|
|
71
|
-
},
|
|
72
|
-
): Promise<boolean> {
|
|
73
|
-
const deadline = Date.now() + timeoutMs
|
|
74
|
-
while(Date.now() < deadline) {
|
|
75
|
-
if(!isRunning()) {
|
|
76
|
-
return false
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
try {
|
|
80
|
-
const response = await fetch(url, {
|
|
81
|
-
method: 'GET',
|
|
82
|
-
redirect: 'manual',
|
|
83
|
-
signal: AbortSignal.timeout(pollMs * 2),
|
|
84
|
-
})
|
|
85
|
-
if(response.ok) {
|
|
86
|
-
return true
|
|
87
|
-
}
|
|
88
|
-
} catch{}
|
|
89
|
-
|
|
90
|
-
await new Promise((r) => setTimeout(r, pollMs))
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return false
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Open a quick tunnel to `http://127.0.0.1:<port>` and resolve once
|
|
98
|
-
* Cloudflare has assigned a hostname.
|
|
99
|
-
*
|
|
100
|
-
* Rejects with an actionable message when `cloudflared` is not installed,
|
|
101
|
-
* exits early, or never reports a hostname.
|
|
102
|
-
*/
|
|
103
|
-
export async function startTunnel(
|
|
104
|
-
port: number,
|
|
105
|
-
{
|
|
106
|
-
autoInstall = true,
|
|
107
|
-
probePath = '/',
|
|
108
|
-
readyPollMs = READY_POLL_MS,
|
|
109
|
-
readyTimeoutMs = READY_TIMEOUT_MS,
|
|
110
|
-
}: {
|
|
111
|
-
autoInstall?: boolean
|
|
112
|
-
/** Public path that must answer before the tunnel is returned. */
|
|
113
|
-
probePath?: string
|
|
114
|
-
/** Test-only timing override. */
|
|
115
|
-
readyPollMs?: number
|
|
116
|
-
/** Test-only timing override. */
|
|
117
|
-
readyTimeoutMs?: number
|
|
118
|
-
} = {},
|
|
119
|
-
): Promise<Tunnel> {
|
|
120
|
-
// Resolves an existing cloudflared, or fetches one on first use. Throws
|
|
121
|
-
// with an actionable message when neither is possible.
|
|
122
|
-
const binary = await resolveCloudflared({ autoInstall })
|
|
123
|
-
const child = spawn(binary.path, [
|
|
124
|
-
'tunnel',
|
|
125
|
-
'--no-autoupdate',
|
|
126
|
-
'--url', `http://127.0.0.1:${port}`,
|
|
127
|
-
], { stdio: ['ignore', 'pipe', 'pipe'] })
|
|
128
|
-
const exited = new Promise<void>((resolve) => {
|
|
129
|
-
child.once('exit', () => resolve())
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
let stopPromise: Promise<void> | undefined
|
|
133
|
-
const stop = () => stopPromise ??= stopChild()
|
|
134
|
-
|
|
135
|
-
return new Promise<Tunnel>((resolve, reject) => {
|
|
136
|
-
let tail = ''
|
|
137
|
-
let settled = false
|
|
138
|
-
const finish = (fn: () => void) => {
|
|
139
|
-
if(settled) {
|
|
140
|
-
return
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
settled = true
|
|
144
|
-
clearTimeout(timer)
|
|
145
|
-
fn()
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const timer = setTimeout(() => {
|
|
149
|
-
finish(() => {
|
|
150
|
-
void stop().then(() => reject(new Error(
|
|
151
|
-
'cloudflared did not report a tunnel hostname within '
|
|
152
|
-
+ `${URL_TIMEOUT_MS / 1000}s. Check network egress to `
|
|
153
|
-
+ 'Cloudflare, or run `cloudflared tunnel --url '
|
|
154
|
-
+ `http://127.0.0.1:${port}` + '` by hand to see its output.',
|
|
155
|
-
)))
|
|
156
|
-
})
|
|
157
|
-
}, URL_TIMEOUT_MS)
|
|
158
|
-
|
|
159
|
-
// cloudflared logs the assigned hostname to stderr, not stdout. Watch
|
|
160
|
-
// both so a future version that switches streams still works.
|
|
161
|
-
const onChunk = (chunk: Buffer) => {
|
|
162
|
-
const text = chunk.toString()
|
|
163
|
-
// Keep watching after the hostname so a readiness failure can include
|
|
164
|
-
// cloudflared's account of what happened.
|
|
165
|
-
tail = (tail + text).slice(-LOG_TAIL_BYTES)
|
|
166
|
-
|
|
167
|
-
// Search the accumulated tail so a URL split across stream chunks is not
|
|
168
|
-
// missed. The tail is bounded and the URL appears near startup.
|
|
169
|
-
const match = QUICK_TUNNEL_URL.exec(tail)
|
|
170
|
-
if(match) {
|
|
171
|
-
const url = match[0]
|
|
172
|
-
finish(() => {
|
|
173
|
-
const probeUrl = new URL(probePath, `${url}/`).toString()
|
|
174
|
-
void waitUntilRouted(probeUrl, {
|
|
175
|
-
isRunning: () => child.exitCode === null
|
|
176
|
-
&& child.signalCode === null,
|
|
177
|
-
pollMs: readyPollMs,
|
|
178
|
-
timeoutMs: readyTimeoutMs,
|
|
179
|
-
}).then((ready) => {
|
|
180
|
-
if(ready) {
|
|
181
|
-
resolve({ url, ready, stop, binary })
|
|
182
|
-
return
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if(child.exitCode !== null || child.signalCode !== null) {
|
|
186
|
-
reject(new Error(
|
|
187
|
-
'cloudflared exited before the public viewer became reachable. '
|
|
188
|
-
+ 'cloudflared output:\n' + tail,
|
|
189
|
-
))
|
|
190
|
-
return
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
LOGGER.warn({ url, tail },
|
|
194
|
-
'tunnel still propagating; returning the live hostname')
|
|
195
|
-
resolve({
|
|
196
|
-
url,
|
|
197
|
-
ready,
|
|
198
|
-
cloudflaredLog: tail,
|
|
199
|
-
stop,
|
|
200
|
-
binary,
|
|
201
|
-
})
|
|
202
|
-
})
|
|
203
|
-
})
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
child.stdout?.on('data', onChunk)
|
|
208
|
-
child.stderr?.on('data', onChunk)
|
|
209
|
-
|
|
210
|
-
child.on('error', (err: NodeJS.ErrnoException) => {
|
|
211
|
-
// `resolveCloudflared` already proved the binary runs, so an ENOENT
|
|
212
|
-
// here means it vanished between resolving and spawning.
|
|
213
|
-
finish(() => reject(
|
|
214
|
-
err.code === 'ENOENT'
|
|
215
|
-
? new Error(
|
|
216
|
-
`cloudflared disappeared from ${binary.path} before it could `
|
|
217
|
-
+ 'start. Retry — the agent will fetch it again if needed.',
|
|
218
|
-
)
|
|
219
|
-
: err,
|
|
220
|
-
))
|
|
221
|
-
})
|
|
222
|
-
|
|
223
|
-
child.on('exit', (code) => {
|
|
224
|
-
LOGGER.debug({ code }, 'cloudflared exited')
|
|
225
|
-
finish(() => reject(new Error(
|
|
226
|
-
`cloudflared exited with code ${code} before reporting a `
|
|
227
|
-
+ 'hostname.',
|
|
228
|
-
)))
|
|
229
|
-
})
|
|
230
|
-
})
|
|
231
|
-
|
|
232
|
-
async function stopChild() {
|
|
233
|
-
if(child.exitCode !== null || child.signalCode !== null) {
|
|
234
|
-
return
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
let timeout: NodeJS.Timeout | undefined
|
|
238
|
-
const timedOut = new Promise<void>((resolve) => {
|
|
239
|
-
timeout = setTimeout(resolve, STOP_TIMEOUT_MS)
|
|
240
|
-
})
|
|
241
|
-
child.kill('SIGTERM')
|
|
242
|
-
await Promise.race([
|
|
243
|
-
exited,
|
|
244
|
-
timedOut,
|
|
245
|
-
])
|
|
246
|
-
clearTimeout(timeout)
|
|
247
|
-
if(child.exitCode !== null || child.signalCode !== null) {
|
|
248
|
-
return
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
child.kill('SIGKILL')
|
|
252
|
-
await exited
|
|
253
|
-
}
|
|
254
|
-
}
|