burrow-cli 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +594 -2
- package/bin/burrow.js +200 -0
- package/examples/acme-shop.tunnel.yml +18 -0
- package/package.json +45 -4
- package/src/access.js +163 -0
- package/src/add.js +106 -0
- package/src/cloudflare.js +131 -0
- package/src/commands.js +386 -0
- package/src/completion.js +127 -0
- package/src/config-file.js +31 -0
- package/src/config.js +82 -0
- package/src/dashboard/auth.js +79 -0
- package/src/dashboard/data.js +70 -0
- package/src/dashboard/logs.js +95 -0
- package/src/dashboard/manage.js +102 -0
- package/src/dashboard/panels/README.md +50 -0
- package/src/dashboard/panels/access.js +218 -0
- package/src/dashboard/panels/alerts.js +244 -0
- package/src/dashboard/panels/backup.js +193 -0
- package/src/dashboard/panels/bulk.js +246 -0
- package/src/dashboard/panels/cleanup.js +237 -0
- package/src/dashboard/panels/doctor.js +119 -0
- package/src/dashboard/panels/health.js +146 -0
- package/src/dashboard/panels/history.js +104 -0
- package/src/dashboard/panels/hooks.js +86 -0
- package/src/dashboard/panels/live.js +153 -0
- package/src/dashboard/panels/project-form.js +463 -0
- package/src/dashboard/panels/quick-actions.js +133 -0
- package/src/dashboard/panels/sessions.js +267 -0
- package/src/dashboard/panels/shares.js +280 -0
- package/src/dashboard/panels/stats.js +165 -0
- package/src/dashboard/panels/updates.js +156 -0
- package/src/dashboard/panels/uptime.js +115 -0
- package/src/dashboard/panels.js +53 -0
- package/src/dashboard/server.js +390 -0
- package/src/dashboard/session-store.js +47 -0
- package/src/dashboard/totp.js +103 -0
- package/src/dashboard/ui.js +454 -0
- package/src/doctor.js +342 -0
- package/src/edit.js +135 -0
- package/src/health-observer.js +81 -0
- package/src/history.js +145 -0
- package/src/hooks.js +256 -0
- package/src/naming.js +38 -0
- package/src/open.js +67 -0
- package/src/pending.js +72 -0
- package/src/process.js +95 -0
- package/src/project-form.js +134 -0
- package/src/project.js +114 -0
- package/src/prune.js +99 -0
- package/src/rename-state.js +39 -0
- package/src/restart.js +35 -0
- package/src/service.js +173 -0
- package/src/share-expiry.js +55 -0
- package/src/share.js +275 -0
- package/src/shell-words.js +284 -0
- package/src/stats.js +180 -0
- package/src/transfer.js +124 -0
- package/src/update.js +254 -0
- package/src/uptime.js +91 -0
- package/src/watch.js +263 -0
- package/src/zones.js +102 -0
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
// Panel: add or edit a project from the dashboard. Writes a validated tunnel.yml and
|
|
2
|
+
// can run `burrow up` right after. File paths never come from the browser: new files
|
|
3
|
+
// go where `burrow import` puts them, edits go to the file `burrow up` remembered.
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import { stateDir } from '../../config.js';
|
|
6
|
+
import { join } from 'node:path';
|
|
7
|
+
import { assertLabel, tunnelName } from '../../naming.js';
|
|
8
|
+
import { parseProject, hostnames, DASHBOARD_PROJECT } from '../../project.js';
|
|
9
|
+
import { readMeta, runningPid, saveMeta } from '../../process.js';
|
|
10
|
+
import { isShare } from '../../share.js';
|
|
11
|
+
import { defaultImportDir } from '../../transfer.js';
|
|
12
|
+
import { Cloudflare } from '../../cloudflare.js';
|
|
13
|
+
import { moveProjectState } from '../../rename-state.js';
|
|
14
|
+
import { formFromText, renderNew, renderEdit, renameText } from '../../project-form.js';
|
|
15
|
+
import { projectDomain, readZoneCache } from '../../zones.js';
|
|
16
|
+
|
|
17
|
+
export const order = 10;
|
|
18
|
+
|
|
19
|
+
// The remembered tunnel.yml of a project the form may edit, or null.
|
|
20
|
+
export function editableFile(project) {
|
|
21
|
+
if (project === DASHBOARD_PROJECT || isShare(project)) return null;
|
|
22
|
+
const file = readMeta(project)?.file;
|
|
23
|
+
return file && existsSync(file) ? file : null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const newProjectFile = (project) => join(defaultImportDir(), `${project}.tunnel.yml`);
|
|
27
|
+
|
|
28
|
+
// Hostnames on the project's own domain (`domain:`), else BURROW_DOMAIN.
|
|
29
|
+
const hostsOn = (proj, cfg) => {
|
|
30
|
+
const domain = projectDomain(proj, cfg);
|
|
31
|
+
return domain ? hostnames(proj, domain).map((h) => h.hostname) : [];
|
|
32
|
+
};
|
|
33
|
+
function preview(text, cfg) {
|
|
34
|
+
const proj = parseProject(text);
|
|
35
|
+
return { yaml: text, domain: projectDomain(proj, cfg) || null, hostnames: hostsOn(proj, cfg) };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Domains the form offers: Cloudflare zones on the account (cached for 5 minutes), the
|
|
39
|
+
// zone cache, and domains remembered projects use. BURROW_DOMAIN is the default choice.
|
|
40
|
+
let zoneList = { at: 0, key: '', names: [], error: null };
|
|
41
|
+
const ZONE_LIST_TTL = 5 * 60 * 1000;
|
|
42
|
+
async function accountZones(cfg) {
|
|
43
|
+
const key = `${cfg.accountId}`;
|
|
44
|
+
if (zoneList.key === key && Date.now() - zoneList.at < ZONE_LIST_TTL) return zoneList;
|
|
45
|
+
try {
|
|
46
|
+
const cf = new Cloudflare({ token: cfg.token, accountId: cfg.accountId, zoneId: cfg.zoneId });
|
|
47
|
+
const zones = await cf.all('/zones', { 'account.id': cfg.accountId });
|
|
48
|
+
const names = zones.filter((z) => !z.account?.id || z.account.id === cfg.accountId)
|
|
49
|
+
.map((z) => ({ name: z.name, status: z.status || 'active' }));
|
|
50
|
+
zoneList = { at: Date.now(), key, names, error: null };
|
|
51
|
+
} catch (e) { zoneList = { at: Date.now(), key, names: [], error: e.message }; }
|
|
52
|
+
return zoneList;
|
|
53
|
+
}
|
|
54
|
+
export async function domainChoices(cfg) {
|
|
55
|
+
const out = new Map();
|
|
56
|
+
const add = (name, source, status) => {
|
|
57
|
+
if (!name || name === cfg.domain) return;
|
|
58
|
+
const d = out.get(name) || { name, sources: [], status: null };
|
|
59
|
+
if (!d.sources.includes(source)) d.sources.push(source);
|
|
60
|
+
if (status) d.status = status;
|
|
61
|
+
out.set(name, d);
|
|
62
|
+
};
|
|
63
|
+
let error = null;
|
|
64
|
+
if (cfg.token && cfg.accountId) {
|
|
65
|
+
const z = await accountZones(cfg);
|
|
66
|
+
error = z.error;
|
|
67
|
+
for (const x of z.names) add(x.name, 'cloudflare', x.status);
|
|
68
|
+
} else error = 'Cloudflare not configured';
|
|
69
|
+
for (const [name, z] of Object.entries(readZoneCache())) if (z.accountId === cfg.accountId) add(name, 'cached');
|
|
70
|
+
for (const p of rememberedProjects()) {
|
|
71
|
+
const file = editableFile(p);
|
|
72
|
+
if (!file) continue;
|
|
73
|
+
try { const proj = parseProject(readFileSync(file, 'utf8'), file); if (proj.domain) add(proj.domain, 'in use'); } catch {}
|
|
74
|
+
}
|
|
75
|
+
return { default: cfg.domain || null, domains: [...out.values()].sort((a, b) => a.name.localeCompare(b.name)), error };
|
|
76
|
+
}
|
|
77
|
+
function rememberedProjects() {
|
|
78
|
+
try { return readdirSync(stateDir()).filter((f) => f.endsWith('.json')).map((f) => f.slice(0, -5)); } catch { return []; }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function enrich(projects) {
|
|
82
|
+
for (const p of projects) p.editable = !p.builtIn && !!editableFile(p.project);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function route(ctx) {
|
|
86
|
+
const { method, path } = ctx;
|
|
87
|
+
if (method === 'GET' && path === '/api/domains') return ctx.json(200, await domainChoices(ctx.loadConfig())) ?? true;
|
|
88
|
+
const rm = path.match(/^\/api\/projects\/([^/]+)\/rename$/);
|
|
89
|
+
if (rm && method === 'POST') return rename(ctx, rm[1]);
|
|
90
|
+
let m = path.match(/^\/api\/projects\/([^/]+)\/config$/);
|
|
91
|
+
if (m) {
|
|
92
|
+
const project = m[1];
|
|
93
|
+
try { assertLabel(project, 'project'); } catch { return ctx.json(400, { error: 'bad project' }) ?? true; }
|
|
94
|
+
const file = editableFile(project);
|
|
95
|
+
if (!file) return ctx.json(404, { error: `${project} has no remembered tunnel.yml to edit` }) ?? true;
|
|
96
|
+
if (method === 'GET') {
|
|
97
|
+
try { ctx.json(200, { file, form: formFromText(readFileSync(file, 'utf8'), file) }); } catch (e) {
|
|
98
|
+
ctx.json(422, { error: `${file} is not valid: ${e.message}. Fix it with "burrow edit ${project}".` });
|
|
99
|
+
}
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
if (method !== 'POST') return false;
|
|
103
|
+
const body = await ctx.readJson();
|
|
104
|
+
if (!body) return ctx.json(400, { error: 'invalid JSON' }) ?? true;
|
|
105
|
+
let text;
|
|
106
|
+
try {
|
|
107
|
+
text = renderEdit(readFileSync(file, 'utf8'), { ...body.form, project }, file);
|
|
108
|
+
} catch (e) { return ctx.json(400, { error: e.message }) ?? true; }
|
|
109
|
+
return save(ctx, { project, file, text, up: body.up === true, action: 'edit' });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (method === 'POST' && path === '/api/projects/check') {
|
|
113
|
+
const body = await ctx.readJson();
|
|
114
|
+
if (!body) return ctx.json(400, { error: 'invalid JSON' }) ?? true;
|
|
115
|
+
try {
|
|
116
|
+
let text;
|
|
117
|
+
if (body.editing) {
|
|
118
|
+
const file = editableFile(String(body.form?.project || ''));
|
|
119
|
+
if (!file) throw new Error('this project has no remembered tunnel.yml');
|
|
120
|
+
text = renderEdit(readFileSync(file, 'utf8'), body.form, file);
|
|
121
|
+
} else {
|
|
122
|
+
text = renderNew(body.form);
|
|
123
|
+
assertNew(parseProject(text).project);
|
|
124
|
+
}
|
|
125
|
+
ctx.json(200, { ok: true, ...preview(text, ctx.loadConfig()) });
|
|
126
|
+
} catch (e) { ctx.json(400, { error: e.message }); }
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (method === 'POST' && path === '/api/projects') {
|
|
131
|
+
const body = await ctx.readJson();
|
|
132
|
+
if (!body) return ctx.json(400, { error: 'invalid JSON' }) ?? true;
|
|
133
|
+
let text;
|
|
134
|
+
let project;
|
|
135
|
+
try {
|
|
136
|
+
text = renderNew(body.form);
|
|
137
|
+
project = parseProject(text).project;
|
|
138
|
+
assertNew(project);
|
|
139
|
+
} catch (e) { return ctx.json(400, { error: e.message }) ?? true; }
|
|
140
|
+
const file = newProjectFile(project);
|
|
141
|
+
return save(ctx, { project, file, text, up: body.up === true, action: 'create', create: true });
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function assertNew(project) {
|
|
147
|
+
if (project === DASHBOARD_PROJECT) throw new Error(`"${DASHBOARD_PROJECT}" is reserved for the dashboard`);
|
|
148
|
+
if (readMeta(project)) throw new Error(`${project} already exists; edit it instead`);
|
|
149
|
+
if (existsSync(newProjectFile(project))) throw new Error(`${newProjectFile(project)} already exists`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Renaming changes every hostname (they're built from the project name) and the tunnel
|
|
153
|
+
// name, so it is done the safe way: the project must be stopped; `burrow destroy <old>`
|
|
154
|
+
// deletes the old tunnel and DNS records first (if that fails nothing else changes); then
|
|
155
|
+
// tunnel.yml gets the new `project:` (a file in the import directory is also renamed),
|
|
156
|
+
// the new name is remembered, its log files and watch state are moved, and `burrow up <new>` creates a fresh tunnel and records.
|
|
157
|
+
async function rename(ctx, from) {
|
|
158
|
+
try { assertLabel(from, 'project'); } catch { return ctx.json(400, { error: 'bad project' }) ?? true; }
|
|
159
|
+
const file = editableFile(from);
|
|
160
|
+
if (!file) return ctx.json(404, { error: `${from} has no remembered tunnel.yml to rename` }) ?? true;
|
|
161
|
+
const body = await ctx.readJson();
|
|
162
|
+
if (!body) return ctx.json(400, { error: 'invalid JSON' }) ?? true;
|
|
163
|
+
const to = String(body.to ?? '').trim();
|
|
164
|
+
const cfg = ctx.loadConfig();
|
|
165
|
+
let text;
|
|
166
|
+
let plan;
|
|
167
|
+
try {
|
|
168
|
+
assertLabel(to, 'new project name');
|
|
169
|
+
if (to === from) throw new Error('that is the current name');
|
|
170
|
+
assertNew(to);
|
|
171
|
+
const before = readFileSync(file, 'utf8');
|
|
172
|
+
text = renameText(before, to, file);
|
|
173
|
+
const target = file === newProjectFile(from) ? newProjectFile(to) : file;
|
|
174
|
+
if (target !== file && existsSync(target)) throw new Error(`${target} already exists`);
|
|
175
|
+
const hostsOf = (t) => hostsOn(parseProject(t), cfg);
|
|
176
|
+
const a = hostsOf(before);
|
|
177
|
+
const b = hostsOf(text);
|
|
178
|
+
plan = { from, to, file: target, hosts: a.map((h, i) => ({ from: h, to: b[i] })), running: !!runningPid(from) };
|
|
179
|
+
} catch (e) { return ctx.json(400, { error: e.message }) ?? true; }
|
|
180
|
+
if (body.dryRun === true) return ctx.json(200, { ok: true, ...plan }) ?? true;
|
|
181
|
+
if (body.confirm !== true) return ctx.json(400, { error: 'confirm the rename (confirm: true)' }) ?? true;
|
|
182
|
+
if (plan.running) return ctx.json(409, { error: `${from} is running; stop it first (Down), then rename` }) ?? true;
|
|
183
|
+
try {
|
|
184
|
+
const cf = new Cloudflare({ token: cfg.token, accountId: cfg.accountId, zoneId: cfg.zoneId });
|
|
185
|
+
if (await cf.findTunnel(tunnelName(to))) {
|
|
186
|
+
return ctx.json(409, { error: `Cloudflare already has a tunnel named ${tunnelName(to)}; pick another name or remove it first` }) ?? true;
|
|
187
|
+
}
|
|
188
|
+
} catch (e) { return ctx.json(502, { error: `Cloudflare check failed: ${e.message}` }) ?? true; }
|
|
189
|
+
|
|
190
|
+
const r = await ctx.withBusy(from, () => ctx.withBusy(to, async () => {
|
|
191
|
+
if (runningPid(from)) return { ok: false, error: `${from} was started meanwhile; stop it first` };
|
|
192
|
+
ctx.log(`rename ${from} -> ${to} (requested from ${ctx.clientIp})`);
|
|
193
|
+
const d = await ctx.runCli(['destroy', from]);
|
|
194
|
+
ctx.invalidate();
|
|
195
|
+
if (!d.ok) return { ok: false, output: d.output, error: `burrow destroy ${from} failed, so nothing was renamed` };
|
|
196
|
+
if (plan.file !== file) {
|
|
197
|
+
mkdirSync(join(plan.file, '..'), { recursive: true, mode: 0o700 });
|
|
198
|
+
writeFileSync(plan.file, text, { flag: 'wx' });
|
|
199
|
+
rmSync(file, { force: true });
|
|
200
|
+
} else writeFileSync(file, text);
|
|
201
|
+
saveMeta(to, { project: to, file: plan.file, hostnames: [] });
|
|
202
|
+
// Logs and watch state follow the project (before `up`, which appends to the new log).
|
|
203
|
+
const moved = moveProjectState(from, to);
|
|
204
|
+
if (moved.length) d.output += `\nMoved ${moved.length} state file(s) to ${to}`;
|
|
205
|
+
ctx.events.emit('event', { action: 'rename', project: to, ok: true, source: 'dashboard', detail: `from ${from}` });
|
|
206
|
+
const out = { ok: true, renamed: true, ...plan, running: false, output: d.output };
|
|
207
|
+
if (body.up === true) {
|
|
208
|
+
const run = await ctx.runCli(['up', to]);
|
|
209
|
+
out.ok = run.ok;
|
|
210
|
+
out.output += '\n' + run.output;
|
|
211
|
+
}
|
|
212
|
+
ctx.invalidate();
|
|
213
|
+
return out;
|
|
214
|
+
}));
|
|
215
|
+
if (r === null) ctx.json(409, { error: `${from} or ${to} is busy` });
|
|
216
|
+
else ctx.json(r.renamed ? (r.ok ? 200 : 500) : 500, r);
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async function save(ctx, { project, file, text, up, action, create }) {
|
|
221
|
+
const r = await ctx.withBusy(project, async () => {
|
|
222
|
+
if (create) {
|
|
223
|
+
mkdirSync(join(file, '..'), { recursive: true, mode: 0o700 });
|
|
224
|
+
writeFileSync(file, text, { flag: 'wx' });
|
|
225
|
+
saveMeta(project, { project, file, hostnames: [] });
|
|
226
|
+
} else writeFileSync(file, text);
|
|
227
|
+
ctx.log(`${action} ${project} tunnel.yml (requested from ${ctx.clientIp})`);
|
|
228
|
+
ctx.events.emit('event', { action, project, ok: true, source: 'dashboard', detail: file });
|
|
229
|
+
const out = { ok: true, saved: true, file, ...preview(text, ctx.loadConfig()) };
|
|
230
|
+
if (!up) return out;
|
|
231
|
+
const run = await ctx.runCli(['up', project]);
|
|
232
|
+
ctx.invalidate();
|
|
233
|
+
return { ...out, ok: run.ok, output: run.output };
|
|
234
|
+
});
|
|
235
|
+
if (!r) ctx.json(409, { error: `${project} is busy` });
|
|
236
|
+
else ctx.json(r.ok ? 200 : 500, r);
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export const html = {
|
|
241
|
+
toolbarMain: '<li><button class="outline" @click="newProject">New project</button></li>',
|
|
242
|
+
rowActions: '<button class="secondary outline" x-show="canEdit(p)" @click="editProject(p)">Edit</button>',
|
|
243
|
+
dialogs: `
|
|
244
|
+
<dialog :open="formOpen" class="project-form">
|
|
245
|
+
<article>
|
|
246
|
+
<header><strong x-text="formTitle"></strong></header>
|
|
247
|
+
<form @submit.prevent="saveProject">
|
|
248
|
+
<label>Project name
|
|
249
|
+
<input type="text" name="project" x-model="form.project" :disabled="formEditing" placeholder="acme-shop" autocomplete="off" required>
|
|
250
|
+
</label>
|
|
251
|
+
<label>Domain
|
|
252
|
+
<select x-model="domainPick" @change="onDomainPick" aria-label="Domain">
|
|
253
|
+
<option value="" x-text="defaultDomainLabel"></option>
|
|
254
|
+
<template x-for="d in domainList" :key="d.name"><option :value="d.name" x-text="domainLabel(d)"></option></template>
|
|
255
|
+
<option value="__other__">Other domain…</option>
|
|
256
|
+
</select>
|
|
257
|
+
</label>
|
|
258
|
+
<input type="text" x-show="domainIsOther" x-model="domainOther" placeholder="example.org" aria-label="Other domain" autocomplete="off">
|
|
259
|
+
<small class="muted" x-show="hasDomainHint" x-text="domainHint"></small>
|
|
260
|
+
<label>App: port or URL for <code x-text="rootHost"></code>
|
|
261
|
+
<input type="text" name="app" x-model="form.app" placeholder="3000" autocomplete="off">
|
|
262
|
+
</label>
|
|
263
|
+
<fieldset>
|
|
264
|
+
<legend>Extra paths on the app</legend>
|
|
265
|
+
<template x-for="(row, i) in form.paths" :key="i">
|
|
266
|
+
<div class="form-row">
|
|
267
|
+
<input type="text" x-model="row.path" placeholder="/api" aria-label="Path" autocomplete="off">
|
|
268
|
+
<input type="text" x-model="row.target" placeholder="3001" aria-label="Port or URL" autocomplete="off">
|
|
269
|
+
<button type="button" class="secondary outline" @click="removePath(i)" aria-label="Remove path">×</button>
|
|
270
|
+
</div>
|
|
271
|
+
</template>
|
|
272
|
+
<button type="button" class="secondary outline small" @click="addPath">Add path</button>
|
|
273
|
+
</fieldset>
|
|
274
|
+
<fieldset>
|
|
275
|
+
<legend>Sub-services (<code><service>-<project></code>)</legend>
|
|
276
|
+
<template x-for="(row, i) in form.services" :key="i">
|
|
277
|
+
<div class="form-row">
|
|
278
|
+
<input type="text" x-model="row.name" placeholder="mail" aria-label="Service name" autocomplete="off">
|
|
279
|
+
<input type="text" x-model="row.target" placeholder="8025" aria-label="Port or URL" autocomplete="off">
|
|
280
|
+
<button type="button" class="secondary outline" @click="removeService(i)" aria-label="Remove service">×</button>
|
|
281
|
+
</div>
|
|
282
|
+
</template>
|
|
283
|
+
<button type="button" class="secondary outline small" @click="addService">Add sub-service</button>
|
|
284
|
+
</fieldset>
|
|
285
|
+
<label><input type="checkbox" role="switch" x-model="form.autostart"> Start at boot (<code>autostart: true</code>)</label>
|
|
286
|
+
<label><input type="checkbox" x-model="formUp"> Run <code>burrow up</code> after saving</label>
|
|
287
|
+
<p class="error" x-show="hasFormError" x-text="formError"></p>
|
|
288
|
+
<div x-show="hasPreview" class="form-preview">
|
|
289
|
+
<small>Hostnames</small>
|
|
290
|
+
<ul><template x-for="h in preview.hostnames" :key="h"><li><code x-text="h"></code></li></template></ul>
|
|
291
|
+
<details><summary>tunnel.yml</summary><pre x-text="preview.yaml"></pre></details>
|
|
292
|
+
</div>
|
|
293
|
+
<footer>
|
|
294
|
+
<button type="button" class="secondary" @click="closeForm">Cancel</button>
|
|
295
|
+
<button type="button" class="secondary outline" x-show="formEditing" :disabled="formBusy" @click="openRename">Rename…</button>
|
|
296
|
+
<button type="button" class="secondary outline" :disabled="formBusy" @click="checkProject">Check</button>
|
|
297
|
+
<button type="submit" :aria-busy="formBusy" :disabled="formBusy">Save</button>
|
|
298
|
+
</footer>
|
|
299
|
+
</form>
|
|
300
|
+
</article>
|
|
301
|
+
</dialog>
|
|
302
|
+
<dialog :open="renameOpen" class="rename-dialog">
|
|
303
|
+
<article>
|
|
304
|
+
<header><strong>Rename <span x-text="form.project"></span></strong></header>
|
|
305
|
+
<p class="error" x-show="formRunning">It's running. Stop it first (Down): renaming deletes and recreates its tunnel.</p>
|
|
306
|
+
<label>New name
|
|
307
|
+
<input type="text" x-model="renameTo" @input="onRenameInput" placeholder="acme-store" aria-label="New project name" autocomplete="off">
|
|
308
|
+
</label>
|
|
309
|
+
<p class="error" x-show="hasRenameError" x-text="renameError"></p>
|
|
310
|
+
<div x-show="hasRenamePlan" class="rename-plan">
|
|
311
|
+
<p><small>The tunnel and DNS records of <strong x-text="form.project"></strong> are deleted, the new name is written to
|
|
312
|
+
<code x-text="renameFile"></code>, and new ones are created on the next start. Old URLs stop working:</small></p>
|
|
313
|
+
<ul><template x-for="row in renameHosts" :key="row.from"><li><code x-text="row.from"></code> → <code x-text="row.to"></code></li></template></ul>
|
|
314
|
+
</div>
|
|
315
|
+
<label><input type="checkbox" x-model="renameUp"> Run <code>burrow up</code> after renaming</label>
|
|
316
|
+
<footer>
|
|
317
|
+
<button class="secondary" @click="closeRename">Cancel</button>
|
|
318
|
+
<button class="danger" :disabled="cannotRename" :aria-busy="renameBusy" @click="doRename">Rename</button>
|
|
319
|
+
</footer>
|
|
320
|
+
</article>
|
|
321
|
+
</dialog>`,
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
export const css = `
|
|
325
|
+
.project-form article{width:min(680px,100%)}
|
|
326
|
+
.project-form fieldset{margin-bottom:.75rem}
|
|
327
|
+
.form-row{display:grid;grid-template-columns:1fr 1fr auto;gap:.4rem;align-items:start}
|
|
328
|
+
.form-row input{margin-bottom:.4rem}.form-row button{width:auto;padding:.45rem .8rem;margin:0}
|
|
329
|
+
button.small{width:auto;padding:.25rem .7rem;font-size:.8rem}
|
|
330
|
+
.form-preview pre{max-height:220px;overflow:auto;font-size:.78rem}
|
|
331
|
+
.form-preview ul{margin-bottom:.4rem}
|
|
332
|
+
.rename-dialog article{width:min(560px,100%)}.rename-plan ul{margin-bottom:.5rem}
|
|
333
|
+
`;
|
|
334
|
+
|
|
335
|
+
export const js = `
|
|
336
|
+
burrowPanel(function () {
|
|
337
|
+
function blank() { return { project: '', domain: '', app: '', paths: [], services: [], autostart: false }; }
|
|
338
|
+
function noPreview() { return { hostnames: [], yaml: '' }; }
|
|
339
|
+
return {
|
|
340
|
+
formOpen: false, formEditing: false, form: blank(), formUp: true, formError: '', formBusy: false, preview: noPreview(),
|
|
341
|
+
domainList: [], domainDefault: '', domainError: '', domainPick: '', domainOther: '', domainStart: '',
|
|
342
|
+
formRunning: false, renameOpen: false, renameTo: '', renamePlan: null, renameError: '', renameBusy: false, renameUp: true, renameTimer: null,
|
|
343
|
+
get hasRenameError() { return this.renameError.length > 0; },
|
|
344
|
+
get hasRenamePlan() { return !!this.renamePlan; },
|
|
345
|
+
get renameHosts() { return this.renamePlan ? this.renamePlan.hosts : []; },
|
|
346
|
+
get renameFile() { return this.renamePlan ? this.renamePlan.file : ''; },
|
|
347
|
+
get cannotRename() { return !this.renamePlan || this.renameBusy || this.formRunning || this.renamePlan.to !== this.renameTo.trim(); },
|
|
348
|
+
renameUrl: function () { return '/api/projects/' + encodeURIComponent(this.form.project) + '/rename'; },
|
|
349
|
+
openRename: function () { this.renameTo = ''; this.renamePlan = null; this.renameError = ''; this.renameUp = true; this.renameOpen = true; },
|
|
350
|
+
closeRename: function () { this.renameOpen = false; },
|
|
351
|
+
onRenameInput: function () {
|
|
352
|
+
var self = this;
|
|
353
|
+
clearTimeout(this.renameTimer);
|
|
354
|
+
this.renamePlan = null;
|
|
355
|
+
this.renameTimer = setTimeout(function () { self.checkRename(); }, 300);
|
|
356
|
+
},
|
|
357
|
+
checkRename: async function () {
|
|
358
|
+
var to = this.renameTo.trim();
|
|
359
|
+
this.renameError = '';
|
|
360
|
+
if (!to) return;
|
|
361
|
+
try {
|
|
362
|
+
var r = await post(this.renameUrl(), { to: to, dryRun: true });
|
|
363
|
+
var j = await r.json().catch(function () { return {}; });
|
|
364
|
+
if (to !== this.renameTo.trim()) return;
|
|
365
|
+
if (r.ok) this.renamePlan = j; else this.renameError = j.error || ('HTTP ' + r.status);
|
|
366
|
+
} catch (e) { this.renameError = 'Network error'; }
|
|
367
|
+
},
|
|
368
|
+
doRename: async function () {
|
|
369
|
+
if (this.cannotRename) return;
|
|
370
|
+
this.renameBusy = true; this.renameError = '';
|
|
371
|
+
try {
|
|
372
|
+
var r = await post(this.renameUrl(), { to: this.renameTo.trim(), confirm: true, up: this.renameUp });
|
|
373
|
+
if (r.status === 401) { location.assign('/login'); return; }
|
|
374
|
+
var j = await r.json().catch(function () { return {}; });
|
|
375
|
+
if (j.renamed) {
|
|
376
|
+
this.renameOpen = false; this.formOpen = false;
|
|
377
|
+
this.showToast((j.ok ? '✓ ' : '✗ ') + 'renamed ' + j.from + ' → ' + j.to + (j.output ? '\\n' + j.output : ''), !!j.ok, 12000);
|
|
378
|
+
this.load();
|
|
379
|
+
} else this.renameError = j.error || ('HTTP ' + r.status);
|
|
380
|
+
} catch (e) { this.renameError = 'Network error'; } finally { this.renameBusy = false; }
|
|
381
|
+
},
|
|
382
|
+
|
|
383
|
+
get formTitle() { return this.formEditing ? 'Edit ' + this.form.project : 'New project'; },
|
|
384
|
+
get hasFormError() { return this.formError.length > 0; },
|
|
385
|
+
get hasPreview() { return this.preview.yaml.length > 0; },
|
|
386
|
+
get rootHost() { return (this.form.project || '<project>') + '.' + (this.formDomain() || this.domainDefault || '<domain>'); },
|
|
387
|
+
get defaultDomainLabel() { return this.domainDefault ? this.domainDefault + ' (default)' : 'Default (BURROW_DOMAIN)'; },
|
|
388
|
+
get domainIsOther() { return this.domainPick === '__other__'; },
|
|
389
|
+
get domainHint() {
|
|
390
|
+
if (this.formEditing && this.formDomain() !== this.domainStart) {
|
|
391
|
+
return 'Changing the domain moves every hostname: the next burrow up deletes the old DNS records and creates new ones on the same tunnel.';
|
|
392
|
+
}
|
|
393
|
+
return this.domainError ? 'Could not list Cloudflare zones (' + this.domainError + '); pick Other to type one.' : '';
|
|
394
|
+
},
|
|
395
|
+
get hasDomainHint() { return this.domainHint.length > 0; },
|
|
396
|
+
domainLabel: function (d) {
|
|
397
|
+
var extra = d.status && d.status !== 'active' ? ' (' + d.status + ')' : (d.sources.indexOf('cloudflare') < 0 ? ' (' + d.sources.join(', ') + ')' : '');
|
|
398
|
+
return d.name + extra;
|
|
399
|
+
},
|
|
400
|
+
formDomain: function () { return this.domainPick === '__other__' ? this.domainOther.trim().toLowerCase() : this.domainPick; },
|
|
401
|
+
formPayload: function () { return Object.assign({}, this.form, { domain: this.formDomain() }); },
|
|
402
|
+
onDomainPick: function () { if (this.domainIsOther) this.domainOther = ''; this.preview = noPreview(); },
|
|
403
|
+
loadDomains: async function () {
|
|
404
|
+
var want = this.form.domain || '';
|
|
405
|
+
this.domainStart = want; this.domainOther = '';
|
|
406
|
+
try {
|
|
407
|
+
var j = await getJson('/api/domains');
|
|
408
|
+
this.domainList = j.domains; this.domainDefault = j.default || ''; this.domainError = j.error || '';
|
|
409
|
+
} catch (e) { this.domainError = e.message; }
|
|
410
|
+
var known = !want || this.domainList.some(function (d) { return d.name === want; });
|
|
411
|
+
var self = this;
|
|
412
|
+
this.$nextTick(function () {
|
|
413
|
+
if (known) self.domainPick = want; else { self.domainPick = '__other__'; self.domainOther = want; }
|
|
414
|
+
});
|
|
415
|
+
},
|
|
416
|
+
canEdit: function (p) { return !!p.editable; },
|
|
417
|
+
|
|
418
|
+
openForm: function (form, editing, up) {
|
|
419
|
+
this.form = form; this.formEditing = editing; this.formUp = up;
|
|
420
|
+
this.formError = ''; this.preview = noPreview(); this.formOpen = true;
|
|
421
|
+
this.loadDomains();
|
|
422
|
+
},
|
|
423
|
+
newProject: function () { this.openForm(blank(), false, true); },
|
|
424
|
+
editProject: async function (p) {
|
|
425
|
+
try {
|
|
426
|
+
var j = await getJson('/api/projects/' + encodeURIComponent(p.project) + '/config');
|
|
427
|
+
this.formRunning = !!p.running;
|
|
428
|
+
this.openForm(j.form, true, !!p.running);
|
|
429
|
+
} catch (e) { this.showToast('✗ ' + e.message, false); }
|
|
430
|
+
},
|
|
431
|
+
closeForm: function () { this.formOpen = false; },
|
|
432
|
+
addPath: function () { this.form.paths.push({ path: '', target: '' }); },
|
|
433
|
+
removePath: function (i) { this.form.paths.splice(i, 1); },
|
|
434
|
+
addService: function () { this.form.services.push({ name: '', target: '' }); },
|
|
435
|
+
removeService: function (i) { this.form.services.splice(i, 1); },
|
|
436
|
+
formUrl: function () {
|
|
437
|
+
return this.formEditing ? '/api/projects/' + encodeURIComponent(this.form.project) + '/config' : '/api/projects';
|
|
438
|
+
},
|
|
439
|
+
checkProject: async function () {
|
|
440
|
+
this.formError = '';
|
|
441
|
+
try {
|
|
442
|
+
var r = await post('/api/projects/check', { form: this.formPayload(), editing: this.formEditing });
|
|
443
|
+
var j = await r.json().catch(function () { return {}; });
|
|
444
|
+
if (r.ok) this.preview = { hostnames: j.hostnames || [], yaml: j.yaml || '' };
|
|
445
|
+
else { this.preview = noPreview(); this.formError = j.error || ('HTTP ' + r.status); }
|
|
446
|
+
} catch (e) { this.formError = 'Network error'; }
|
|
447
|
+
},
|
|
448
|
+
saveProject: async function () {
|
|
449
|
+
this.formBusy = true; this.formError = '';
|
|
450
|
+
try {
|
|
451
|
+
var r = await post(this.formUrl(), { form: this.formPayload(), up: this.formUp });
|
|
452
|
+
if (r.status === 401) { location.assign('/login'); return; }
|
|
453
|
+
var j = await r.json().catch(function () { return {}; });
|
|
454
|
+
if (j.saved) {
|
|
455
|
+
this.formOpen = false;
|
|
456
|
+
this.showToast((j.ok ? '✓ ' : '✗ ') + 'saved ' + j.file + (j.output ? '\\n' + j.output : ''), !!j.ok, 12000);
|
|
457
|
+
this.load();
|
|
458
|
+
} else this.formError = j.error || ('HTTP ' + r.status);
|
|
459
|
+
} catch (e) { this.formError = 'Network error'; } finally { this.formBusy = false; }
|
|
460
|
+
},
|
|
461
|
+
};
|
|
462
|
+
});
|
|
463
|
+
`;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Panel: quick actions. Per hostname: copy the URL, open it, or show a QR code for
|
|
2
|
+
// testing on a phone (qrcode-generator, vendored from node_modules and loaded on first
|
|
3
|
+
// use). Per project: Restart (re-sync and restart the connector, like `burrow restart`).
|
|
4
|
+
import { assertLabel } from '../../naming.js';
|
|
5
|
+
import { DASHBOARD_PROJECT } from '../../project.js';
|
|
6
|
+
|
|
7
|
+
export const order = 40;
|
|
8
|
+
|
|
9
|
+
// Inline SVG icons: no icon font or glyph that may be missing from the system fonts, and
|
|
10
|
+
// no inline style, so the strict CSP stays as it is. Drawn with currentColor.
|
|
11
|
+
const icon = (body) => `<svg class="qicon" viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">${body}</svg>`;
|
|
12
|
+
export const ICONS = {
|
|
13
|
+
copy: icon('<rect x="9" y="9" width="12" height="12" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/>'),
|
|
14
|
+
open: icon('<path d="M14 3h7v7"/><path d="M10 14 21 3"/><path d="M21 14v5a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5"/>'),
|
|
15
|
+
qr: icon('<rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/><path d="M14 14h3v3h-3z"/><path d="M21 14v.01"/><path d="M14 21h.01"/><path d="M17 21h4v-4"/>'),
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const vendor = {
|
|
19
|
+
'/vendor/qrcode.js': { pkg: 'qrcode-generator', type: 'text/javascript; charset=utf-8' },
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export async function route(ctx) {
|
|
23
|
+
const m = ctx.path.match(/^\/api\/tunnels\/([^/]+)\/restart$/);
|
|
24
|
+
if (ctx.method !== 'POST' || !m) return false;
|
|
25
|
+
const project = m[1];
|
|
26
|
+
try { assertLabel(project, 'project'); } catch { return ctx.json(400, { error: 'bad project' }) ?? true; }
|
|
27
|
+
if (project === DASHBOARD_PROJECT) {
|
|
28
|
+
return ctx.json(400, { error: 'Restarting the dashboard tunnel would cut off this page; use "burrow restart burrow" on the server' }) ?? true;
|
|
29
|
+
}
|
|
30
|
+
const r = await ctx.withBusy(project, async () => {
|
|
31
|
+
ctx.log(`restart ${project} (requested from ${ctx.clientIp})`);
|
|
32
|
+
const run = await ctx.runCli(['restart', project]);
|
|
33
|
+
ctx.invalidate();
|
|
34
|
+
return run;
|
|
35
|
+
});
|
|
36
|
+
if (!r) ctx.json(409, { error: `${project} is busy` });
|
|
37
|
+
else ctx.json(r.ok ? 200 : 500, r);
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const html = {
|
|
42
|
+
hostExtras: `<span class="quick">
|
|
43
|
+
<button class="icon" type="button" title="Copy URL" aria-label="Copy URL" @click="copyUrl(h)">${ICONS.copy}</button>
|
|
44
|
+
<button class="icon" type="button" title="Open in a new tab" aria-label="Open URL" @click="openUrl(h)">${ICONS.open}</button>
|
|
45
|
+
<button class="icon" type="button" title="QR code for your phone" aria-label="Show QR code" @click="showQr(h)">${ICONS.qr}</button>
|
|
46
|
+
</span>`,
|
|
47
|
+
rowActions: '<button class="secondary outline" :disabled="restartDisabled(p)" :title="restartTitle(p)" :aria-busy="isBusy(p)" @click="restart(p)">Restart</button>',
|
|
48
|
+
dialogs: `
|
|
49
|
+
<dialog :open="qrOpen" class="qr-dialog" @click.self="closeQr">
|
|
50
|
+
<article>
|
|
51
|
+
<header><strong>Open on your phone</strong></header>
|
|
52
|
+
<p class="qr-img"><img :src="qrSrc" :alt="qrAlt" width="264" height="264" x-show="hasQr"></p>
|
|
53
|
+
<p class="error" x-show="hasQrError" x-text="qrError"></p>
|
|
54
|
+
<p class="qr-url"><code x-text="qrUrl"></code></p>
|
|
55
|
+
<footer>
|
|
56
|
+
<button class="secondary" @click="closeQr">Close</button>
|
|
57
|
+
<button @click="copyQrUrl">Copy URL</button>
|
|
58
|
+
</footer>
|
|
59
|
+
</article>
|
|
60
|
+
</dialog>`,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const css = `
|
|
64
|
+
.quick{display:inline-flex;gap:.15rem;opacity:.45}.host:hover .quick,.quick:focus-within{opacity:1}
|
|
65
|
+
button.icon{all:unset;cursor:pointer;display:inline-flex;align-items:center;line-height:1;padding:.2rem;border-radius:4px;color:var(--pico-muted-color)}
|
|
66
|
+
button.icon .qicon{display:block;width:14px;height:14px}
|
|
67
|
+
button.icon:hover,button.icon:focus-visible{color:var(--pico-primary);background:var(--pico-secondary-focus)}
|
|
68
|
+
.qr-dialog article{width:min(360px,100%);text-align:center}
|
|
69
|
+
.qr-img img{image-rendering:pixelated;background:#fff;padding:8px;border-radius:8px}
|
|
70
|
+
.qr-url code{word-break:break-all}
|
|
71
|
+
`;
|
|
72
|
+
|
|
73
|
+
export const js = `
|
|
74
|
+
burrowPanel(function () {
|
|
75
|
+
var qrLoading = null;
|
|
76
|
+
function loadQrLib() {
|
|
77
|
+
if (window.qrcode) return Promise.resolve();
|
|
78
|
+
if (!qrLoading) {
|
|
79
|
+
qrLoading = new Promise(function (resolve, reject) {
|
|
80
|
+
var s = document.createElement('script');
|
|
81
|
+
s.src = '/vendor/qrcode.js';
|
|
82
|
+
s.onload = function () { resolve(); };
|
|
83
|
+
s.onerror = function () { qrLoading = null; reject(new Error('could not load the QR library')); };
|
|
84
|
+
document.head.appendChild(s);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return qrLoading;
|
|
88
|
+
}
|
|
89
|
+
function fallbackCopy(text) {
|
|
90
|
+
var ta = document.createElement('textarea');
|
|
91
|
+
ta.value = text; ta.setAttribute('readonly', ''); ta.style.position = 'fixed'; ta.style.opacity = '0';
|
|
92
|
+
document.body.appendChild(ta); ta.select();
|
|
93
|
+
var ok = false;
|
|
94
|
+
try { ok = document.execCommand('copy'); } catch (e) { ok = false; }
|
|
95
|
+
document.body.removeChild(ta);
|
|
96
|
+
return ok;
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
qrOpen: false, qrUrl: '', qrSrc: '', qrError: '',
|
|
100
|
+
get hasQr() { return this.qrSrc.length > 0; },
|
|
101
|
+
get hasQrError() { return this.qrError.length > 0; },
|
|
102
|
+
get qrAlt() { return 'QR code for ' + this.qrUrl; },
|
|
103
|
+
|
|
104
|
+
copyText: async function (text) {
|
|
105
|
+
var ok = false;
|
|
106
|
+
try { if (navigator.clipboard && window.isSecureContext) { await navigator.clipboard.writeText(text); ok = true; } } catch (e) { ok = false; }
|
|
107
|
+
if (!ok) ok = fallbackCopy(text);
|
|
108
|
+
this.showToast(ok ? '✓ Copied ' + text : 'Copy this URL: ' + text, ok, 4000);
|
|
109
|
+
},
|
|
110
|
+
copyUrl: function (h) { this.copyText(this.hostUrl(h)); },
|
|
111
|
+
copyQrUrl: function () { this.copyText(this.qrUrl); },
|
|
112
|
+
openUrl: function (h) { window.open(this.hostUrl(h), '_blank', 'noopener,noreferrer'); },
|
|
113
|
+
showQr: async function (h) {
|
|
114
|
+
this.qrUrl = this.hostUrl(h); this.qrSrc = ''; this.qrError = ''; this.qrOpen = true;
|
|
115
|
+
try {
|
|
116
|
+
await loadQrLib();
|
|
117
|
+
var q = window.qrcode(0, 'M');
|
|
118
|
+
q.addData(this.qrUrl);
|
|
119
|
+
q.make();
|
|
120
|
+
this.qrSrc = q.createDataURL(6, 2);
|
|
121
|
+
} catch (e) { this.qrError = e.message; }
|
|
122
|
+
},
|
|
123
|
+
closeQr: function () { this.qrOpen = false; },
|
|
124
|
+
|
|
125
|
+
restartDisabled: function (p) { return p.builtIn || !p.canUp || this.isBusy(p); },
|
|
126
|
+
restartTitle: function (p) {
|
|
127
|
+
if (p.builtIn) return 'Use "burrow restart burrow" on the server';
|
|
128
|
+
return p.canUp ? 'Re-sync tunnel.yml and restart cloudflared' : 'No remembered tunnel.yml';
|
|
129
|
+
},
|
|
130
|
+
restart: function (p) { this.act(p, 'restart'); },
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
`;
|