@popmelt.com/core 0.38.0 → 0.39.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 +4 -3
- package/claude-plugin/.claude-plugin/plugin.json +1 -1
- package/codex-plugin/.codex-plugin/plugin.json +1 -1
- package/dist/{ExploreAttachment-IIEORQP3.mjs → ExploreAttachment-OA2PUTNR.mjs} +1 -1
- package/dist/PagePreviewLayer-HKADULHD.mjs +1 -0
- package/dist/ParticipantRuntime-IU2STHIT.mjs +1 -0
- package/dist/PhoneHostGate-CS3KFCP5.mjs +1 -0
- package/dist/PopmeltTasteConnectedPane-TRQCY5SM.mjs +1 -0
- package/dist/{SamplePlacementOverlay-EOJW6XGE.mjs → SamplePlacementOverlay-SNA4WB6Z.mjs} +1 -1
- package/dist/ShareProjectPane-QLHMNZNU.mjs +1 -0
- package/dist/SuppliedContext-SFOM3R3K.mjs +1 -0
- package/dist/{ThreadErrorCard-CTMVMETS.mjs → ThreadErrorCard-NJUVLWMU.mjs} +1 -1
- package/dist/ThreadPanel-RVCNMX7B.mjs +1 -0
- package/dist/adapter-react-three-fiber.mjs +1 -1
- package/dist/adapter-three.d.mts +9 -8
- package/dist/adapter-three.mjs +3 -3
- package/dist/bridge-entry.mjs +1 -1
- package/dist/{bridgeSSEStream-HUFNXYPG.mjs → bridgeSSEStream-2KKFO64A.mjs} +1 -1
- package/dist/{bridgeWebSocketStream-AS22Y2FP.mjs → bridgeWebSocketStream-XTHQPBPI.mjs} +1 -1
- package/dist/browser-runtime-BUC4RUD7.mjs +1 -0
- package/dist/browser.mjs +1 -1
- package/dist/canvas-chat-note-XMVKWL2J.mjs +1 -0
- package/dist/{checkFixture-UNTXJKRN.mjs → checkFixture-I2ALOQTM.mjs} +1 -1
- package/dist/chunk-4B6HUJCH.mjs +1 -0
- package/dist/{chunk-BJUSVIDN.mjs → chunk-4LN72RDZ.mjs} +1 -1
- package/dist/{chunk-3VOEPWRL.mjs → chunk-5TCWBNV4.mjs} +1 -1
- package/dist/chunk-742KV4YS.mjs +1 -0
- package/dist/{chunk-WJVRGXOS.mjs → chunk-7D4BUDKG.mjs} +1 -1
- package/dist/chunk-ADOCYY5I.mjs +1 -0
- package/dist/chunk-B4RLDY5H.mjs +1 -0
- package/dist/chunk-B4ZKGIGU.mjs +1 -0
- package/dist/{chunk-ZHXTB5IN.mjs → chunk-BDUWFDBP.mjs} +1 -1
- package/dist/chunk-CHK24QU5.mjs +1 -0
- package/dist/{chunk-TCGXG7UA.mjs → chunk-CPFVBR6Q.mjs} +1 -1
- package/dist/{chunk-GTVTUWGH.mjs → chunk-D3BHAI37.mjs} +1 -1
- package/dist/chunk-DKBVNKIG.mjs +24 -0
- package/dist/chunk-EOAWCUFT.mjs +1 -0
- package/dist/{chunk-QXFBFKN6.mjs → chunk-EWTZVC7E.mjs} +1 -1
- package/dist/{chunk-ACBGA4NT.mjs → chunk-FD6IIFJC.mjs} +1 -1
- package/dist/{chunk-TEXVTTWA.mjs → chunk-FPSR4NAR.mjs} +1 -1
- package/dist/{chunk-KHDXOXYF.mjs → chunk-G6ITTTFD.mjs} +1 -1
- package/dist/chunk-H4GG2RS4.mjs +1 -0
- package/dist/chunk-HM6EBJVO.mjs +1 -0
- package/dist/chunk-HOWDQUPT.mjs +1 -0
- package/dist/{chunk-XJJDBMZI.mjs → chunk-HQVWOBYH.mjs} +1 -1
- package/dist/chunk-IWF42SDV.mjs +1 -0
- package/dist/{chunk-GLF7Z5IR.mjs → chunk-K22VZV3V.mjs} +1 -1
- package/dist/chunk-K2OUXYDM.mjs +1 -0
- package/dist/chunk-KOHR4LAQ.mjs +1 -0
- package/dist/chunk-KY4YVZ3O.mjs +1 -0
- package/dist/chunk-LLVF422C.mjs +1 -0
- package/dist/chunk-LSKP5UC6.mjs +1 -0
- package/dist/{chunk-F3VZQGPM.mjs → chunk-LUTP7QBD.mjs} +1 -1
- package/dist/chunk-ML7YV4RV.mjs +1 -0
- package/dist/{chunk-TXL2CR66.mjs → chunk-MLNC5TMP.mjs} +1 -1
- package/dist/chunk-MSWRCFPJ.mjs +1 -0
- package/dist/chunk-NC7YXD7Q.mjs +1 -0
- package/dist/{chunk-BEGAJYOY.mjs → chunk-OEJXPUAR.mjs} +1 -1
- package/dist/{chunk-43BZCDGL.mjs → chunk-OL7RDZ2R.mjs} +1 -1
- package/dist/{chunk-6V4Q2JFN.mjs → chunk-P3IOAIYB.mjs} +1 -1
- package/dist/chunk-PQKSXNIZ.mjs +1 -0
- package/dist/{chunk-7MYZEG74.mjs → chunk-QDFQPABM.mjs} +1 -1
- package/dist/chunk-RALP6ENI.mjs +30 -0
- package/dist/chunk-RRB5IHBT.mjs +1 -0
- package/dist/{chunk-O2OYVEL7.mjs → chunk-UFUH63SR.mjs} +1 -1
- package/dist/chunk-UIADZXVT.mjs +1 -0
- package/dist/{chunk-JAMYVQFC.mjs → chunk-V56MNQLF.mjs} +1 -1
- package/dist/{chunk-5F34UIKL.mjs → chunk-VWFST2IX.mjs} +1 -1
- package/dist/{chunk-Y3WTPLDG.mjs → chunk-W5JUAOLA.mjs} +1 -1
- package/dist/cli.mjs +1 -1
- package/dist/codex-note-context-4ORRY4CH.mjs +1 -0
- package/dist/codex-project-LMAR3FMS.mjs +1 -0
- package/dist/{codex-store-mount-RSRKEV2B.mjs → codex-store-mount-MNIL3VK6.mjs} +1 -1
- package/dist/dev-server-ZLT7TAUF.mjs +1 -0
- package/dist/guest-admission.mjs +1 -1
- package/dist/guest-live-canvas.mjs +1 -1
- package/dist/{index-CHp2bgs1.d.mts → index-D6bSXWNT.d.mts} +1 -1
- package/dist/index.mjs +1 -1
- package/dist/memory-legacy.d.mts +1 -1
- package/dist/memory-legacy.mjs +1 -1
- package/dist/memory-store.d.mts +84 -84
- package/dist/memory-store.mjs +1 -1
- package/dist/{page-interaction-TV3P6B3X.mjs → page-interaction-GC2UN47Y.mjs} +1 -1
- package/dist/{page-planes-2ZepTpen.d.mts → page-planes-9GVbnVZa.d.mts} +240 -1
- package/dist/peer-client-XNTMFSOV.mjs +1 -0
- package/dist/plugin-astro.d.mts +1 -1
- package/dist/plugin-astro.mjs +1 -1
- package/dist/plugin-nuxt.mjs +1 -1
- package/dist/plugin-vite.d.mts +1 -1
- package/dist/plugin-vite.mjs +1 -1
- package/dist/server.d.mts +4 -2
- package/dist/server.mjs +1 -1
- package/dist/shared-page-JN7T4DDQ.mjs +1 -0
- package/dist/webpEncoder-QJRHTWUU.mjs +1 -0
- package/infra/phone-pairing/README.md +25 -0
- package/infra/phone-pairing/binding.mjs +105 -0
- package/infra/phone-pairing/machine-config.mjs +134 -0
- package/infra/phone-pairing/machine-service.mjs +159 -0
- package/infra/phone-pairing/machine.mjs +98 -0
- package/infra/phone-pairing/managed-install.mjs +179 -0
- package/package.json +42 -25
- package/dist/PagePreviewLayer-AFSRCBAX.mjs +0 -1
- package/dist/ParticipantRuntime-CGJH7YZL.mjs +0 -1
- package/dist/PhoneHostGate-OCIMKPLM.mjs +0 -1
- package/dist/PopmeltTasteConnectedPane-N2A5XPRO.mjs +0 -1
- package/dist/ShareProjectPane-VYWTF3FW.mjs +0 -1
- package/dist/SuppliedContext-3S4HT6U6.mjs +0 -1
- package/dist/ThreadPanel-G7OKSWD4.mjs +0 -1
- package/dist/browser-runtime-WXDKC4TL.mjs +0 -1
- package/dist/canvas-chat-note-OLVVP55Y.mjs +0 -1
- package/dist/chunk-2GICKLUW.mjs +0 -1
- package/dist/chunk-3DUTQ6GY.mjs +0 -1
- package/dist/chunk-A4GARXVQ.mjs +0 -1
- package/dist/chunk-AGCS65LT.mjs +0 -1
- package/dist/chunk-AYZUOBFI.mjs +0 -1
- package/dist/chunk-BOZVNUHJ.mjs +0 -1
- package/dist/chunk-BRAEIDNX.mjs +0 -30
- package/dist/chunk-DBCBG5J6.mjs +0 -1
- package/dist/chunk-EFWBHWHP.mjs +0 -1
- package/dist/chunk-FYURSPM3.mjs +0 -1
- package/dist/chunk-G6BN5TAZ.mjs +0 -1
- package/dist/chunk-IW7V7WNL.mjs +0 -1
- package/dist/chunk-JJK4LAN6.mjs +0 -1
- package/dist/chunk-JJRKR4X3.mjs +0 -1
- package/dist/chunk-KF73S7MG.mjs +0 -1
- package/dist/chunk-KL5ODVJA.mjs +0 -1
- package/dist/chunk-L356S4EY.mjs +0 -1
- package/dist/chunk-OZBMHEOD.mjs +0 -1
- package/dist/chunk-P2WAXWAZ.mjs +0 -1
- package/dist/chunk-PRPXMDZ7.mjs +0 -1
- package/dist/chunk-TSQEBZYO.mjs +0 -1
- package/dist/chunk-UTP56NCY.mjs +0 -1
- package/dist/chunk-X56HNVHF.mjs +0 -1
- package/dist/chunk-X6I6KQDG.mjs +0 -1
- package/dist/chunk-ZSTKAPV5.mjs +0 -24
- package/dist/codex-note-context-WYH7D6SS.mjs +0 -1
- package/dist/codex-project-Q4CKTDOH.mjs +0 -1
- package/dist/dev-server-SDS67665.mjs +0 -1
- package/dist/peer-client-HYSUL7UZ.mjs +0 -1
- package/dist/shared-page-GO76UP7I.mjs +0 -1
- package/dist/webpEncoder-MTPCVDOG.mjs +0 -1
- /package/dist/{chunk-FIOGWEZA.mjs → chunk-CZJILD7T.mjs} +0 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Managed phone pairing
|
|
2
|
+
|
|
3
|
+
The desktop host opens **Pair and Share → Pair your phone**. The QR contains a one-minute enrollment ticket in the URL fragment. Safari removes the ticket from its address bar, creates a passkey and offers to open the project. Bookmark the pairing page. Returning to it calls for a fresh passkey assertion even when the phone already has a session.
|
|
4
|
+
|
|
5
|
+
## Where traffic goes
|
|
6
|
+
|
|
7
|
+
- The Worker at `pair.popmelt.com` serves the passkey UI. The signed assertion goes directly from the browser to the project's gateway through Cloudflare.
|
|
8
|
+
- The Mac stores the public key and verifies the exact origin, relying party, challenge and user verification. A one-use handoff opens the live app on a separate hostname. The gateway requires a private session cookie for assets and phone bridge requests. Active sessions last one hour.
|
|
9
|
+
- Remote collaborator invites continue using Quick Tunnels. Wi-Fi host pairing continues to use the existing LAN relay. A provisioned host can choose **Pair over Wi-Fi** in the same pane.
|
|
10
|
+
|
|
11
|
+
## Provision one named tunnel per Mac
|
|
12
|
+
|
|
13
|
+
On macOS with Node 22 or later and `cloudflared`, run the following from this directory. The operator needs control of the Cloudflare zone. The person pairing their phone needs no account. Deploy the shared Worker at `pair.popmelt.com` once.
|
|
14
|
+
|
|
15
|
+
1. Create one named tunnel for the Mac, or adopt its existing tunnel UUID with `node machine.mjs init <tunnel-uuid>`. The Mac needs the connector credentials and a Cloudflare login authorized to create its project DNS records. Run the service from a trusted account with a trusted `cloudflared` executable on its PATH.
|
|
16
|
+
2. Start the Mac's pairing service with `node machine.mjs run` in place of the direct `cloudflared tunnel ... run` launch command. For an existing connector, start the new service first and retire the old connector after the new one connects. Existing project hostnames, grants and bookmarks survive the cutover.
|
|
17
|
+
3. In any project running the updated Core, open **Pair and Share → Pair your phone** from its local desktop host. The service registers the hostname and updates its live route. The pane waits for the public hostname to reach that project’s gateway before starting the one-minute QR timer. Each project needs a distinct loopback app port. Reopening the pane or retrying a failed registration reuses the same project ID. **Pair over Wi-Fi** remains available independently.
|
|
18
|
+
|
|
19
|
+
The tunnel forwards traffic to a private socket on the Mac. The service accepts only registered project hostnames and forwards each to that project's private gateway. Unknown hosts receive 404. Registration uses a separate owner-only Unix socket. The browser and Core bridge receive no Cloudflare credential. Each gateway verifies its own enrolled passkeys, issues its own sessions and handles revocation independently.
|
|
20
|
+
|
|
21
|
+
Keep the project, its bridge and the machine service running. The service owns the connector process. It reloads its project list from `~/.popmelt-pair/machine/projects.json` on startup. Operator tooling can still use `node machine.mjs add <project-directory> <loopback-app-origin>` on a running service. To recover after a provisioner is killed while holding `machine/provision.lock`, confirm it has exited and remove that empty lock directory before retrying.
|
|
22
|
+
|
|
23
|
+
A directory rename on the same volume preserves the pairing ID. A copied checkout or replacement volume needs an explicit new binding. Keep the relying-party hostname reserved for the pairing UI. Retire an old project hostname together with its routing, and never reassign its pairing ID.
|
|
24
|
+
|
|
25
|
+
`popmelt phone revoke` invalidates the saved phone grants and closes active sessions **for the current project**. Access after revocation requires another scan from its desktop host.
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { createHash, randomBytes, randomUUID } from 'node:crypto';
|
|
2
|
+
import {
|
|
3
|
+
linkSync,
|
|
4
|
+
lstatSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
realpathSync,
|
|
8
|
+
renameSync,
|
|
9
|
+
unlinkSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from 'node:fs';
|
|
12
|
+
import { homedir } from 'node:os';
|
|
13
|
+
import { join } from 'node:path';
|
|
14
|
+
|
|
15
|
+
export const base = join(homedir(), '.popmelt-pair');
|
|
16
|
+
export function privateDir(path) {
|
|
17
|
+
mkdirSync(path, { recursive: true, mode: 0o700 });
|
|
18
|
+
const stat = lstatSync(path);
|
|
19
|
+
if (!stat.isDirectory() || stat.uid !== process.getuid() || stat.mode & 0o077)
|
|
20
|
+
throw Error('Expected a private directory owned by this user: ' + path);
|
|
21
|
+
}
|
|
22
|
+
export function readPrivate(path) {
|
|
23
|
+
const stat = lstatSync(path);
|
|
24
|
+
if (!stat.isFile() || stat.uid !== process.getuid() || stat.mode & 0o077)
|
|
25
|
+
throw Error('Expected a private file owned by this user: ' + path);
|
|
26
|
+
return readFileSync(path, 'utf8');
|
|
27
|
+
}
|
|
28
|
+
export function writePrivate(path, data, createOnly = false) {
|
|
29
|
+
const tmp = path + '.' + randomUUID() + '.tmp';
|
|
30
|
+
writeFileSync(tmp, data, { mode: 0o600, flag: 'wx' });
|
|
31
|
+
if (createOnly) {
|
|
32
|
+
try {
|
|
33
|
+
linkSync(tmp, path);
|
|
34
|
+
} finally {
|
|
35
|
+
unlinkSync(tmp);
|
|
36
|
+
}
|
|
37
|
+
} else {
|
|
38
|
+
try {
|
|
39
|
+
renameSync(tmp, path);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
unlinkSync(tmp);
|
|
42
|
+
throw e;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function checkOrigin(origin) {
|
|
47
|
+
if (
|
|
48
|
+
typeof origin !== 'string' ||
|
|
49
|
+
!/^https?:\/\/(127\.0\.0\.1|localhost|\[::1\]):[0-9]+$/.test(origin) ||
|
|
50
|
+
new URL(origin).origin !== origin
|
|
51
|
+
)
|
|
52
|
+
throw Error('Expected a loopback app origin with an explicit port.');
|
|
53
|
+
}
|
|
54
|
+
export function readBinding(projectKey) {
|
|
55
|
+
if (!/^[0-9a-f]{64}$/.test(projectKey)) throw Error('Invalid project key.');
|
|
56
|
+
const record = JSON.parse(readPrivate(join(base, projectKey + '.json')));
|
|
57
|
+
if (record.projectKey !== projectKey || !/^[0-9a-f]{32}$/.test(record.id))
|
|
58
|
+
throw Error('Invalid project binding.');
|
|
59
|
+
checkOrigin(record.appOrigin);
|
|
60
|
+
privateDir(join(base, record.id));
|
|
61
|
+
return record;
|
|
62
|
+
}
|
|
63
|
+
export function projectKeyFor(directory) {
|
|
64
|
+
const stat = lstatSync(realpathSync(directory));
|
|
65
|
+
if (!stat.isDirectory()) throw Error('Expected a project directory.');
|
|
66
|
+
return createHash('sha256')
|
|
67
|
+
.update([stat.dev, stat.ino, stat.birthtimeMs].join(':'))
|
|
68
|
+
.digest('hex');
|
|
69
|
+
}
|
|
70
|
+
export function bind(directory, appOrigin, assignedId) {
|
|
71
|
+
checkOrigin(appOrigin);
|
|
72
|
+
const projectKey = projectKeyFor(directory);
|
|
73
|
+
privateDir(base);
|
|
74
|
+
let record;
|
|
75
|
+
try {
|
|
76
|
+
record = readBinding(projectKey);
|
|
77
|
+
} catch (e) {
|
|
78
|
+
if (e.code !== 'ENOENT') throw e;
|
|
79
|
+
const id = assignedId ?? randomBytes(16).toString('hex');
|
|
80
|
+
try {
|
|
81
|
+
writePrivate(
|
|
82
|
+
join(base, projectKey + '.json'),
|
|
83
|
+
JSON.stringify({ projectKey, id, appOrigin }),
|
|
84
|
+
true,
|
|
85
|
+
);
|
|
86
|
+
} catch (e) {
|
|
87
|
+
if (e.code !== 'EEXIST') throw e;
|
|
88
|
+
}
|
|
89
|
+
record = readBinding(projectKey);
|
|
90
|
+
}
|
|
91
|
+
// localhost aliases for the same listener preserve the original binding.
|
|
92
|
+
const stored = new URL(record.appOrigin),
|
|
93
|
+
requested = new URL(appOrigin);
|
|
94
|
+
if (stored.protocol !== requested.protocol || stored.port !== requested.port)
|
|
95
|
+
throw Error('Review the existing binding before changing its app origin.');
|
|
96
|
+
return record;
|
|
97
|
+
}
|
|
98
|
+
export function route(id) {
|
|
99
|
+
return {
|
|
100
|
+
id,
|
|
101
|
+
bookmark: 'https://pair.popmelt.com/p/' + id,
|
|
102
|
+
hostname: 'p-' + id + '.popmelt.com',
|
|
103
|
+
socket: join(base, id, 'app.sock'),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// The CLI and the running provisioner share one registry and one writer lock.
|
|
2
|
+
import { existsSync, mkdirSync, rmdirSync } from 'node:fs';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import {
|
|
6
|
+
base,
|
|
7
|
+
bind,
|
|
8
|
+
privateDir,
|
|
9
|
+
projectKeyFor,
|
|
10
|
+
readBinding,
|
|
11
|
+
readPrivate,
|
|
12
|
+
route,
|
|
13
|
+
writePrivate,
|
|
14
|
+
} from './binding.mjs';
|
|
15
|
+
|
|
16
|
+
export const machine = join(base, 'machine');
|
|
17
|
+
export const registry = join(machine, 'projects.json');
|
|
18
|
+
export const config = join(machine, 'connector.yml');
|
|
19
|
+
export const routerSocket = join(machine, 'router.sock');
|
|
20
|
+
export const controlSocket = join(machine, 'control.sock');
|
|
21
|
+
const uuid = /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/;
|
|
22
|
+
|
|
23
|
+
export function readState() {
|
|
24
|
+
const state = JSON.parse(readPrivate(registry));
|
|
25
|
+
if (!uuid.test(state.tunnel) || !Array.isArray(state.projects))
|
|
26
|
+
throw Error('Invalid machine pairing state.');
|
|
27
|
+
return state;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function routingTable(state) {
|
|
31
|
+
const bindings = state.projects.map(readBinding),
|
|
32
|
+
table = new Map(),
|
|
33
|
+
ports = new Set();
|
|
34
|
+
for (const { id, appOrigin } of bindings) {
|
|
35
|
+
const { hostname, socket } = route(id),
|
|
36
|
+
port = new URL(appOrigin).port;
|
|
37
|
+
if (ports.has(port) || table.has(hostname))
|
|
38
|
+
throw Error('Each project needs its own hostname and loopback app port.');
|
|
39
|
+
ports.add(port);
|
|
40
|
+
table.set(hostname, socket);
|
|
41
|
+
}
|
|
42
|
+
return table;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function configure(command, args = [], provisionDns, idForKey) {
|
|
46
|
+
privateDir(base);
|
|
47
|
+
privateDir(machine);
|
|
48
|
+
const lock = join(machine, 'provision.lock');
|
|
49
|
+
try {
|
|
50
|
+
mkdirSync(lock, { mode: 0o700 });
|
|
51
|
+
} catch (e) {
|
|
52
|
+
if (e.code === 'EEXIST')
|
|
53
|
+
throw Error(
|
|
54
|
+
'Another project is being configured. Try again in a moment.',
|
|
55
|
+
);
|
|
56
|
+
throw e;
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
let state;
|
|
60
|
+
try {
|
|
61
|
+
state = readState();
|
|
62
|
+
} catch (e) {
|
|
63
|
+
if (e.code !== 'ENOENT' || command !== 'init' || !uuid.test(args[0]))
|
|
64
|
+
throw e;
|
|
65
|
+
state = { tunnel: args[0], projects: [] };
|
|
66
|
+
}
|
|
67
|
+
if (command === 'init' && state.tunnel !== args[0])
|
|
68
|
+
throw Error('This Mac already has a different named tunnel.');
|
|
69
|
+
const table = routingTable(state);
|
|
70
|
+
let project;
|
|
71
|
+
if (command === 'add') {
|
|
72
|
+
const key = projectKeyFor(args[0]);
|
|
73
|
+
const port = new URL(args[1]).port;
|
|
74
|
+
if (
|
|
75
|
+
state.projects.some(
|
|
76
|
+
(k) => k !== key && new URL(readBinding(k).appOrigin).port === port,
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
throw Error(
|
|
80
|
+
'That loopback app port is already assigned to another project.',
|
|
81
|
+
);
|
|
82
|
+
project = bind(...args, idForKey?.(key));
|
|
83
|
+
const { hostname, socket } = route(project.id);
|
|
84
|
+
if (!state.projects.includes(key)) {
|
|
85
|
+
if (table.has(hostname))
|
|
86
|
+
throw Error('This hostname already belongs to another project.');
|
|
87
|
+
if (provisionDns) await provisionDns(state.tunnel, hostname, key);
|
|
88
|
+
state.projects.push(key);
|
|
89
|
+
table.set(hostname, socket);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (command === 'run') state.liveRouting = true;
|
|
93
|
+
const ingress = state.liveRouting
|
|
94
|
+
? [{ hostname: '*.popmelt.com', service: 'unix:' + routerSocket }]
|
|
95
|
+
: Array.from(table, ([hostname, socket]) => ({
|
|
96
|
+
hostname,
|
|
97
|
+
service: 'unix:' + socket,
|
|
98
|
+
}));
|
|
99
|
+
ingress.push({ service: 'http_status:404' });
|
|
100
|
+
const data =
|
|
101
|
+
JSON.stringify(
|
|
102
|
+
{
|
|
103
|
+
tunnel: state.tunnel,
|
|
104
|
+
'credentials-file': existsSync(
|
|
105
|
+
join(machine, 'connector-credentials.json'),
|
|
106
|
+
)
|
|
107
|
+
? join(machine, 'connector-credentials.json')
|
|
108
|
+
: join(homedir(), '.cloudflared', state.tunnel + '.json'),
|
|
109
|
+
ingress,
|
|
110
|
+
},
|
|
111
|
+
null,
|
|
112
|
+
2,
|
|
113
|
+
) + '\n';
|
|
114
|
+
const stateData = JSON.stringify(state, null, 2) + '\n';
|
|
115
|
+
function update(file, value) {
|
|
116
|
+
try {
|
|
117
|
+
if (readPrivate(file) === value) return;
|
|
118
|
+
} catch (e) {
|
|
119
|
+
if (e.code !== 'ENOENT') throw e;
|
|
120
|
+
}
|
|
121
|
+
writePrivate(file, value);
|
|
122
|
+
}
|
|
123
|
+
update(registry, stateData);
|
|
124
|
+
update(config, data);
|
|
125
|
+
return {
|
|
126
|
+
tunnel: state.tunnel,
|
|
127
|
+
config,
|
|
128
|
+
projects: table.size,
|
|
129
|
+
...(project && route(project.id)),
|
|
130
|
+
};
|
|
131
|
+
} finally {
|
|
132
|
+
rmdirSync(lock);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { createServer, request } from 'node:http';
|
|
2
|
+
import { createConnection } from 'node:net';
|
|
3
|
+
import { chmod, lstat, unlink } from 'node:fs/promises';
|
|
4
|
+
import { execFile } from 'node:child_process';
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
|
+
import { isAbsolute } from 'node:path';
|
|
7
|
+
import {
|
|
8
|
+
configure,
|
|
9
|
+
controlSocket,
|
|
10
|
+
machine,
|
|
11
|
+
readState,
|
|
12
|
+
routerSocket,
|
|
13
|
+
routingTable,
|
|
14
|
+
} from './machine-config.mjs';
|
|
15
|
+
|
|
16
|
+
const exec = promisify(execFile);
|
|
17
|
+
export async function provisionDns(tunnel, hostname) {
|
|
18
|
+
try {
|
|
19
|
+
await exec('cloudflared', ['tunnel', 'route', 'dns', tunnel, hostname], {
|
|
20
|
+
cwd: machine,
|
|
21
|
+
env: { ...process.env, TUNNEL_FORCE_PROVISIONING_DNS: 'false' },
|
|
22
|
+
timeout: 15000,
|
|
23
|
+
maxBuffer: 65536,
|
|
24
|
+
});
|
|
25
|
+
} catch {
|
|
26
|
+
throw Error(
|
|
27
|
+
'Cloudflare could not register the project hostname. Retry, or pair over Wi-Fi.',
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Only reclaim a socket left by a dead process. A second live service cannot take it.
|
|
33
|
+
async function listen(server, path) {
|
|
34
|
+
try {
|
|
35
|
+
const stat = await lstat(path);
|
|
36
|
+
if (!stat.isSocket() || stat.uid !== process.getuid())
|
|
37
|
+
throw Error('Unexpected entry at ' + path);
|
|
38
|
+
await new Promise((resolve, reject) => {
|
|
39
|
+
const probe = createConnection(path);
|
|
40
|
+
probe.once('connect', () => {
|
|
41
|
+
probe.destroy();
|
|
42
|
+
reject(Error('The pairing service is already running.'));
|
|
43
|
+
});
|
|
44
|
+
probe.once('error', (e) =>
|
|
45
|
+
e.code === 'ECONNREFUSED' ? resolve() : reject(e),
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
await unlink(path);
|
|
49
|
+
} catch (e) {
|
|
50
|
+
if (e.code !== 'ENOENT') throw e;
|
|
51
|
+
}
|
|
52
|
+
await new Promise((resolve, reject) => {
|
|
53
|
+
server.once('error', reject);
|
|
54
|
+
server.listen(path, resolve);
|
|
55
|
+
});
|
|
56
|
+
await chmod(path, 0o600);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function serve(registerDns = provisionDns, idForKey) {
|
|
60
|
+
let routes = routingTable(readState());
|
|
61
|
+
const router = createServer((req, res) => {
|
|
62
|
+
const socket = routes.get(req.headers.host);
|
|
63
|
+
if (!socket) {
|
|
64
|
+
res.writeHead(404).end();
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
// Forward only to a registered project's private gateway, which checks Host
|
|
68
|
+
// again and authenticates every app request. The control socket is separate.
|
|
69
|
+
const upstream = request(
|
|
70
|
+
{
|
|
71
|
+
socketPath: socket,
|
|
72
|
+
path: req.url,
|
|
73
|
+
method: req.method,
|
|
74
|
+
headers: req.headers,
|
|
75
|
+
agent: false,
|
|
76
|
+
},
|
|
77
|
+
(reply) => {
|
|
78
|
+
res.writeHead(reply.statusCode, reply.headers);
|
|
79
|
+
reply.once('error', () => res.destroy());
|
|
80
|
+
reply.pipe(res);
|
|
81
|
+
},
|
|
82
|
+
);
|
|
83
|
+
upstream.once('error', () => {
|
|
84
|
+
if (!res.headersSent) res.writeHead(503, { 'Cache-Control': 'no-store' });
|
|
85
|
+
res.end();
|
|
86
|
+
});
|
|
87
|
+
res.once('close', () => upstream.destroy());
|
|
88
|
+
req.once('error', () => upstream.destroy());
|
|
89
|
+
req.pipe(upstream);
|
|
90
|
+
});
|
|
91
|
+
// Serialize adds, including the DNS operation. Duplicate clicks reuse the
|
|
92
|
+
// persisted binding and never start another connector.
|
|
93
|
+
let tail = Promise.resolve();
|
|
94
|
+
const control = createServer({ requestTimeout: 10000 }, async (req, res) => {
|
|
95
|
+
if (
|
|
96
|
+
req.method !== 'POST' ||
|
|
97
|
+
req.url !== '/projects' ||
|
|
98
|
+
req.headers['content-type'] !== 'application/json'
|
|
99
|
+
) {
|
|
100
|
+
res.writeHead(404).end();
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
const chunks = [];
|
|
105
|
+
let size = 0;
|
|
106
|
+
for await (const chunk of req) {
|
|
107
|
+
size += chunk.length;
|
|
108
|
+
if (size > 8192) throw Error('Invalid project registration.');
|
|
109
|
+
chunks.push(chunk);
|
|
110
|
+
}
|
|
111
|
+
const { root, origin } = JSON.parse(
|
|
112
|
+
Buffer.concat(chunks).toString('utf8'),
|
|
113
|
+
);
|
|
114
|
+
if (
|
|
115
|
+
typeof root !== 'string' ||
|
|
116
|
+
!isAbsolute(root) ||
|
|
117
|
+
typeof origin !== 'string'
|
|
118
|
+
)
|
|
119
|
+
throw Error('Invalid project registration.');
|
|
120
|
+
const added = tail.then(async () => {
|
|
121
|
+
const result = await configure(
|
|
122
|
+
'add',
|
|
123
|
+
[root, origin],
|
|
124
|
+
registerDns,
|
|
125
|
+
idForKey,
|
|
126
|
+
);
|
|
127
|
+
routes = routingTable(readState());
|
|
128
|
+
return result;
|
|
129
|
+
});
|
|
130
|
+
tail = added.catch(() => {});
|
|
131
|
+
const result = await added;
|
|
132
|
+
res
|
|
133
|
+
.writeHead(200, { 'Content-Type': 'application/json' })
|
|
134
|
+
.end(JSON.stringify(result));
|
|
135
|
+
} catch (e) {
|
|
136
|
+
const message = e.message?.includes('loopback app port')
|
|
137
|
+
? 'This app port is assigned to another paired project.'
|
|
138
|
+
: 'Could not register this project with the Mac’s tunnel. Retry, or pair over Wi-Fi.';
|
|
139
|
+
res
|
|
140
|
+
.writeHead(503, { 'Content-Type': 'application/json' })
|
|
141
|
+
.end(JSON.stringify({ error: message }));
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
// cloudflared only receives the router socket. Both are in the user's
|
|
145
|
+
// private directory and the control listener is never bound to TCP.
|
|
146
|
+
await listen(control, controlSocket);
|
|
147
|
+
try {
|
|
148
|
+
await listen(router, routerSocket);
|
|
149
|
+
} catch (e) {
|
|
150
|
+
control.close();
|
|
151
|
+
throw e;
|
|
152
|
+
}
|
|
153
|
+
return () => {
|
|
154
|
+
for (const server of [control, router]) {
|
|
155
|
+
server.close();
|
|
156
|
+
server.closeAllConnections();
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { enroll, wake, managedDns, credentials } from './managed-install.mjs';
|
|
3
|
+
import { spawn } from 'node:child_process';
|
|
4
|
+
import { request } from 'node:http';
|
|
5
|
+
import { resolve } from 'node:path';
|
|
6
|
+
import {
|
|
7
|
+
configure,
|
|
8
|
+
controlSocket,
|
|
9
|
+
machine,
|
|
10
|
+
readState,
|
|
11
|
+
} from './machine-config.mjs';
|
|
12
|
+
import { serve } from './machine-service.mjs';
|
|
13
|
+
|
|
14
|
+
const [command, ...args] = process.argv.slice(2);
|
|
15
|
+
if (
|
|
16
|
+
!(
|
|
17
|
+
(command === 'init' && args.length === 1) ||
|
|
18
|
+
(command === 'add' && args.length === 2) ||
|
|
19
|
+
(['sync', 'run', 'enroll', 'wake'].includes(command) && !args.length)
|
|
20
|
+
)
|
|
21
|
+
)
|
|
22
|
+
throw Error(
|
|
23
|
+
'Usage: machine.mjs init <tunnel-uuid> | add <project-directory> <loopback-app-origin> | sync | run',
|
|
24
|
+
);
|
|
25
|
+
if (command === 'enroll' || command === 'wake') {
|
|
26
|
+
try {
|
|
27
|
+
if (command === 'enroll') {
|
|
28
|
+
let invitation = '';
|
|
29
|
+
for await (const chunk of process.stdin) {
|
|
30
|
+
invitation += chunk;
|
|
31
|
+
if (invitation.length > 100) throw Error('Invalid invitation.');
|
|
32
|
+
}
|
|
33
|
+
await enroll(invitation.trim());
|
|
34
|
+
}
|
|
35
|
+
if (!existsSync(credentials)) throw Error('Mac enrollment was not found.');
|
|
36
|
+
await wake();
|
|
37
|
+
} catch (e) {
|
|
38
|
+
console.error(e.message);
|
|
39
|
+
process.exitCode = 1;
|
|
40
|
+
}
|
|
41
|
+
} else if (command === 'run') {
|
|
42
|
+
const remote = existsSync(credentials) ? await managedDns() : undefined;
|
|
43
|
+
const close = await serve(remote?.register, remote?.idForKey);
|
|
44
|
+
try {
|
|
45
|
+
const { config, tunnel } = await configure('run');
|
|
46
|
+
const child = spawn(
|
|
47
|
+
'cloudflared',
|
|
48
|
+
['tunnel', '--config', config, 'run', tunnel],
|
|
49
|
+
{ cwd: machine, stdio: 'inherit' },
|
|
50
|
+
);
|
|
51
|
+
const stop = () => child.kill('SIGTERM');
|
|
52
|
+
process.once('SIGINT', stop);
|
|
53
|
+
process.once('SIGTERM', stop);
|
|
54
|
+
try {
|
|
55
|
+
const code = await new Promise((resolve, reject) => {
|
|
56
|
+
child.once('error', reject);
|
|
57
|
+
child.once('exit', resolve);
|
|
58
|
+
});
|
|
59
|
+
process.exitCode = code ?? 0;
|
|
60
|
+
} finally {
|
|
61
|
+
process.off('SIGINT', stop);
|
|
62
|
+
process.off('SIGTERM', stop);
|
|
63
|
+
}
|
|
64
|
+
} finally {
|
|
65
|
+
close();
|
|
66
|
+
}
|
|
67
|
+
} else if (command === 'add' && readState().liveRouting) {
|
|
68
|
+
const result = await new Promise((done, fail) => {
|
|
69
|
+
const req = request(
|
|
70
|
+
{
|
|
71
|
+
socketPath: controlSocket,
|
|
72
|
+
path: '/projects',
|
|
73
|
+
method: 'POST',
|
|
74
|
+
headers: { 'Content-Type': 'application/json' },
|
|
75
|
+
signal: AbortSignal.timeout(25000),
|
|
76
|
+
},
|
|
77
|
+
(res) => {
|
|
78
|
+
let body = '';
|
|
79
|
+
res.on('error', fail);
|
|
80
|
+
res.on('data', (chunk) => (body += chunk));
|
|
81
|
+
res.on('end', () =>
|
|
82
|
+
res.statusCode === 200
|
|
83
|
+
? done(body)
|
|
84
|
+
: fail(
|
|
85
|
+
Error(
|
|
86
|
+
'Could not register this project with the Mac’s pairing service.',
|
|
87
|
+
),
|
|
88
|
+
),
|
|
89
|
+
);
|
|
90
|
+
},
|
|
91
|
+
);
|
|
92
|
+
req.on('error', fail);
|
|
93
|
+
req.end(JSON.stringify({ root: resolve(args[0]), origin: args[1] }));
|
|
94
|
+
});
|
|
95
|
+
console.log(result);
|
|
96
|
+
} else {
|
|
97
|
+
console.log(JSON.stringify(await configure(command, args), null, 2));
|
|
98
|
+
}
|