burrow-cli 0.0.0-stage → 0.1.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/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 +118 -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,246 @@
|
|
|
1
|
+
// Panel: bulk actions and start-at-boot. Tick projects to start or stop them together;
|
|
2
|
+
// a switch per project sets `autostart: true` in its tunnel.yml; and the bar under the
|
|
3
|
+
// table installs the systemd unit from `burrow service` that brings them up at boot.
|
|
4
|
+
import { spawnSync } from 'node:child_process';
|
|
5
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
6
|
+
import YAML from 'yaml';
|
|
7
|
+
import { assertLabel } from '../../naming.js';
|
|
8
|
+
import { parseProject, DASHBOARD_PROJECT } from '../../project.js';
|
|
9
|
+
import { readMeta } from '../../process.js';
|
|
10
|
+
import { autostartProjects, unitPath, unitOutdated, UNIT_NAME } from '../../service.js';
|
|
11
|
+
|
|
12
|
+
export const order = 90;
|
|
13
|
+
const MAX_BULK = 50;
|
|
14
|
+
|
|
15
|
+
const configList = (cfg) => (cfg.autostart || '').split(',').map((s) => s.trim()).filter(Boolean);
|
|
16
|
+
const projectFile = (project) => {
|
|
17
|
+
if (project === DASHBOARD_PROJECT) return null;
|
|
18
|
+
const file = readMeta(project)?.file;
|
|
19
|
+
return file && existsSync(file) ? file : null;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// Set or remove `autostart: true`, keeping the rest of the file (comments included).
|
|
23
|
+
export function setAutostart(text, enabled, source = 'tunnel.yml') {
|
|
24
|
+
const doc = YAML.parseDocument(text);
|
|
25
|
+
if (enabled) doc.set('autostart', true);
|
|
26
|
+
else doc.delete('autostart');
|
|
27
|
+
const out = doc.toString({ lineWidth: 0 });
|
|
28
|
+
parseProject(out, source);
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Cached briefly: systemctl runs synchronously and the page polls.
|
|
33
|
+
let unitCache = { at: 0, data: null };
|
|
34
|
+
function unitState(fresh = false) {
|
|
35
|
+
if (!fresh && unitCache.data && Date.now() - unitCache.at < 15000) return unitCache.data;
|
|
36
|
+
const run = (...a) => {
|
|
37
|
+
const r = spawnSync(process.env.BURROW_SYSTEMCTL || 'systemctl', ['--user', ...a, UNIT_NAME], { encoding: 'utf8', timeout: 5000, killSignal: 'SIGKILL' });
|
|
38
|
+
return r.error ? 'unknown' : (r.stdout || '').trim() || 'unknown';
|
|
39
|
+
};
|
|
40
|
+
unitCache = { at: Date.now(), data: { installed: existsSync(unitPath()), outdated: unitOutdated(), enabled: run('is-enabled'), active: run('is-active') } };
|
|
41
|
+
return unitCache.data;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function enrich(projects, ctx) {
|
|
45
|
+
const cfg = ctx.loadConfig();
|
|
46
|
+
const fromConfig = new Set(configList(cfg));
|
|
47
|
+
for (const p of projects) {
|
|
48
|
+
let fromFile = false;
|
|
49
|
+
const file = projectFile(p.project);
|
|
50
|
+
if (file) { try { fromFile = parseProject(readFileSync(file, 'utf8'), file).autostart; } catch {} }
|
|
51
|
+
p.autostart = fromFile || fromConfig.has(p.project);
|
|
52
|
+
p.autostartSource = fromConfig.has(p.project) ? 'config' : fromFile ? 'file' : null;
|
|
53
|
+
p.autostartEditable = !!file && !fromConfig.has(p.project);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function route(ctx) {
|
|
58
|
+
const { method, path } = ctx;
|
|
59
|
+
if (method === 'GET' && path === '/api/boot') {
|
|
60
|
+
return ctx.json(200, { ...unitState(), unit: UNIT_NAME, projects: autostartProjects(ctx.loadConfig()) }) ?? true;
|
|
61
|
+
}
|
|
62
|
+
if (method !== 'POST') return false;
|
|
63
|
+
|
|
64
|
+
if (path === '/api/boot') {
|
|
65
|
+
const body = await ctx.readJson();
|
|
66
|
+
if (!body) return ctx.json(400, { error: 'invalid JSON' }) ?? true;
|
|
67
|
+
let args;
|
|
68
|
+
if (body.action === 'install') args = ['service', 'install', '--now'];
|
|
69
|
+
else if (body.action === 'uninstall') {
|
|
70
|
+
if (body.confirm !== true) return ctx.json(400, { error: 'confirm the uninstall' }) ?? true;
|
|
71
|
+
args = ['service', 'uninstall'];
|
|
72
|
+
} else return ctx.json(400, { error: 'action must be install or uninstall' }) ?? true;
|
|
73
|
+
const r = await ctx.withBusy('(boot)', () => ctx.runCli(args, 300000));
|
|
74
|
+
if (!r) return ctx.json(409, { error: 'busy' }) ?? true;
|
|
75
|
+
ctx.invalidate();
|
|
76
|
+
return ctx.json(r.ok ? 200 : 500, { ...r, ...unitState(true) }) ?? true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let m = path.match(/^\/api\/autostart\/([^/]+)$/);
|
|
80
|
+
if (m) {
|
|
81
|
+
const project = m[1];
|
|
82
|
+
try { assertLabel(project, 'project'); } catch { return ctx.json(400, { error: 'bad project' }) ?? true; }
|
|
83
|
+
const body = await ctx.readJson();
|
|
84
|
+
if (!body || typeof body.enabled !== 'boolean') return ctx.json(400, { error: 'enabled must be true or false' }) ?? true;
|
|
85
|
+
if (configList(ctx.loadConfig()).includes(project)) {
|
|
86
|
+
return ctx.json(409, { error: `${project} is in BURROW_AUTOSTART in the config; change it there` }) ?? true;
|
|
87
|
+
}
|
|
88
|
+
const file = projectFile(project);
|
|
89
|
+
if (!file) return ctx.json(404, { error: `${project} has no remembered tunnel.yml` }) ?? true;
|
|
90
|
+
try {
|
|
91
|
+
writeFileSync(file, setAutostart(readFileSync(file, 'utf8'), body.enabled, file));
|
|
92
|
+
} catch (e) { return ctx.json(400, { error: e.message }) ?? true; }
|
|
93
|
+
ctx.log(`autostart ${body.enabled ? 'on' : 'off'} for ${project} (requested from ${ctx.clientIp})`);
|
|
94
|
+
ctx.events.emit('event', { action: `autostart ${body.enabled ? 'on' : 'off'}`, project, ok: true, detail: file });
|
|
95
|
+
return ctx.json(200, { ok: true, project, autostart: body.enabled, file }) ?? true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (path === '/api/bulk') {
|
|
99
|
+
const body = await ctx.readJson();
|
|
100
|
+
if (!body) return ctx.json(400, { error: 'invalid JSON' }) ?? true;
|
|
101
|
+
if (!['up', 'down', 'restart'].includes(body.action)) return ctx.json(400, { error: 'action must be up, down or restart' }) ?? true;
|
|
102
|
+
const list = Array.isArray(body.projects) ? [...new Set(body.projects)] : [];
|
|
103
|
+
if (!list.length || list.length > MAX_BULK) return ctx.json(400, { error: `pick 1 to ${MAX_BULK} projects` }) ?? true;
|
|
104
|
+
for (const p of list) {
|
|
105
|
+
try { assertLabel(p, 'project'); } catch { return ctx.json(400, { error: `bad project ${String(p).slice(0, 40)}` }) ?? true; }
|
|
106
|
+
if (p === DASHBOARD_PROJECT) return ctx.json(400, { error: 'the dashboard project is not part of bulk actions' }) ?? true;
|
|
107
|
+
}
|
|
108
|
+
ctx.log(`bulk ${body.action} ${list.join(', ')} (requested from ${ctx.clientIp})`);
|
|
109
|
+
const results = [];
|
|
110
|
+
for (const project of list) { // one at a time: each `up` talks to the Cloudflare API
|
|
111
|
+
const r = await ctx.withBusy(project, () => ctx.runCli([body.action, project]));
|
|
112
|
+
results.push(r ? { project, ok: r.ok, output: r.output.slice(-2000) } : { project, ok: false, output: `${project} is busy` });
|
|
113
|
+
}
|
|
114
|
+
ctx.invalidate();
|
|
115
|
+
const ok = results.every((r) => r.ok);
|
|
116
|
+
return ctx.json(ok ? 200 : 207, { ok, results }) ?? true;
|
|
117
|
+
}
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export const html = {
|
|
122
|
+
headLead: '<th class="pick"><input type="checkbox" :checked="allPicked" @change="togglePickAll" aria-label="Select all projects"></th>',
|
|
123
|
+
rowLead: '<td class="pick"><input type="checkbox" :disabled="p.builtIn" :checked="isPicked(p)" @change="togglePick(p)" :aria-label="pickLabel(p)"></td>',
|
|
124
|
+
headCols: '<th title="Start at boot (autostart)">Boot</th>',
|
|
125
|
+
rowCols: '<td class="col-boot" data-label="Boot"><input type="checkbox" role="switch" :checked="p.autostart" :disabled="autostartDisabled(p)" :title="autostartTitle(p)" @change="toggleAutostart(p)" :aria-label="autostartTitle(p)"></td>',
|
|
126
|
+
toolbarMain: `<li x-show="hasPicks"><small x-text="pickedText"></small></li>
|
|
127
|
+
<li x-show="hasPicks"><button :aria-busy="bulkBusy" :disabled="bulkBusy" @click="bulkUp">Start</button></li>
|
|
128
|
+
<li x-show="hasPicks"><button class="secondary" :disabled="bulkBusy" @click="bulkDown">Stop</button></li>`,
|
|
129
|
+
sections: `
|
|
130
|
+
<div class="boot-bar" x-show="hasProjects">
|
|
131
|
+
<small>Start at boot: <code x-text="bootUnit"></code> <span class="badge" :class="bootClass" x-text="bootText"></span>
|
|
132
|
+
<span x-text="bootProjectsText"></span></small>
|
|
133
|
+
<button class="secondary outline small" x-show="bootCanInstall" :aria-busy="bootBusy" :disabled="bootBusy" @click="installBoot" x-text="bootInstallLabel"></button>
|
|
134
|
+
<button class="danger outline small" x-show="bootInstalled" :disabled="bootBusy" @click="askUninstallBoot">Uninstall</button>
|
|
135
|
+
</div>`,
|
|
136
|
+
dialogs: `
|
|
137
|
+
<dialog :open="bootUninstallOpen">
|
|
138
|
+
<article>
|
|
139
|
+
<header><strong>Uninstall the boot service?</strong></header>
|
|
140
|
+
<p>This disables and removes <code>burrow.service</code>. systemd runs its stop command, which is
|
|
141
|
+
<code>burrow down --keep-shares</code>: <strong>every running connector stops</strong>, the dashboard's own tunnel included;
|
|
142
|
+
temporary shares keep running. Tunnels and DNS records are kept.</p>
|
|
143
|
+
<footer>
|
|
144
|
+
<button class="secondary" @click="closeUninstallBoot">Cancel</button>
|
|
145
|
+
<button class="danger" @click="uninstallBoot">Uninstall</button>
|
|
146
|
+
</footer>
|
|
147
|
+
</article>
|
|
148
|
+
</dialog>`,
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export const css = `
|
|
152
|
+
th.pick,td.pick{width:2rem}.pick input{margin:0}
|
|
153
|
+
.boot-bar{display:flex;align-items:center;gap:.6rem;flex-wrap:wrap;margin:.5rem 0 1rem;color:var(--pico-muted-color)}
|
|
154
|
+
.boot-bar button.small{width:auto;margin:0;padding:.2rem .6rem;font-size:.8rem}
|
|
155
|
+
`;
|
|
156
|
+
|
|
157
|
+
export const js = `
|
|
158
|
+
burrowPanel(function () {
|
|
159
|
+
return {
|
|
160
|
+
picked: [], bulkBusy: false, boot: { installed: false, enabled: 'unknown', active: 'unknown', projects: [], unit: 'burrow.service' },
|
|
161
|
+
bootBusy: false, bootUninstallOpen: false, bootAt: 0,
|
|
162
|
+
|
|
163
|
+
onLoad: function (data) {
|
|
164
|
+
var names = data.projects.map(function (p) { return p.project; });
|
|
165
|
+
this.picked = this.picked.filter(function (n) { return names.indexOf(n) >= 0; });
|
|
166
|
+
if (Date.now() - this.bootAt > 30000) this.loadBoot();
|
|
167
|
+
},
|
|
168
|
+
get pickable() { return this.projects.filter(function (p) { return !p.builtIn; }); },
|
|
169
|
+
get hasPicks() { return this.picked.length > 0; },
|
|
170
|
+
get pickedText() { return this.picked.length + ' selected'; },
|
|
171
|
+
get allPicked() { return this.pickable.length > 0 && this.picked.length === this.pickable.length; },
|
|
172
|
+
isPicked: function (p) { return this.picked.indexOf(p.project) >= 0; },
|
|
173
|
+
pickLabel: function (p) { return 'Select ' + p.project; },
|
|
174
|
+
togglePick: function (p) {
|
|
175
|
+
var i = this.picked.indexOf(p.project);
|
|
176
|
+
if (i >= 0) this.picked.splice(i, 1); else this.picked.push(p.project);
|
|
177
|
+
},
|
|
178
|
+
togglePickAll: function () {
|
|
179
|
+
this.picked = this.allPicked ? [] : this.pickable.map(function (p) { return p.project; });
|
|
180
|
+
},
|
|
181
|
+
runBulk: async function (action) {
|
|
182
|
+
var list = this.picked.slice();
|
|
183
|
+
this.bulkBusy = true;
|
|
184
|
+
var self = this;
|
|
185
|
+
list.forEach(function (n) { self.busy[n] = true; });
|
|
186
|
+
this.showToast(action + ' ' + list.join(', ') + '…', true, 300000);
|
|
187
|
+
try {
|
|
188
|
+
var r = await post('/api/bulk', { action: action, projects: list });
|
|
189
|
+
if (r.status === 401) { location.assign('/login'); return; }
|
|
190
|
+
var j = await r.json().catch(function () { return {}; });
|
|
191
|
+
var text = (j.results || []).map(function (x) { return (x.ok ? '✓ ' : '✗ ') + x.project; }).join('\\n');
|
|
192
|
+
this.showToast((j.ok ? '✓ ' : '✗ ') + action + ' ' + list.length + ' project(s)\\n' + (text || j.error || ''), !!j.ok, 12000);
|
|
193
|
+
if (j.ok) this.picked = [];
|
|
194
|
+
} catch (e) { this.showToast('✗ ' + action + ': network error', false); } finally {
|
|
195
|
+
list.forEach(function (n) { self.busy[n] = false; });
|
|
196
|
+
this.bulkBusy = false; this.load();
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
bulkUp: function () { this.runBulk('up'); },
|
|
200
|
+
bulkDown: function () { this.runBulk('down'); },
|
|
201
|
+
|
|
202
|
+
autostartDisabled: function (p) { return !p.autostartEditable; },
|
|
203
|
+
autostartTitle: function (p) {
|
|
204
|
+
if (p.autostartSource === 'config') return 'Set by BURROW_AUTOSTART in the config';
|
|
205
|
+
if (p.builtIn) return 'Add burrow to BURROW_AUTOSTART in the config to start the dashboard at boot';
|
|
206
|
+
if (!p.autostartEditable) return 'No remembered tunnel.yml';
|
|
207
|
+
return p.autostart ? 'Starts at boot (autostart: true)' : 'Start at boot';
|
|
208
|
+
},
|
|
209
|
+
toggleAutostart: async function (p) {
|
|
210
|
+
var want = !p.autostart;
|
|
211
|
+
try {
|
|
212
|
+
var r = await post('/api/autostart/' + encodeURIComponent(p.project), { enabled: want });
|
|
213
|
+
var j = await r.json().catch(function () { return {}; });
|
|
214
|
+
if (r.ok) { p.autostart = want; this.showToast('✓ ' + p.project + (want ? ' starts at boot' : ' no longer starts at boot'), true, 4000); }
|
|
215
|
+
else this.showToast('✗ ' + (j.error || ('HTTP ' + r.status)), false);
|
|
216
|
+
} catch (e) { this.showToast('✗ network error', false); }
|
|
217
|
+
this.load();
|
|
218
|
+
this.loadBoot();
|
|
219
|
+
},
|
|
220
|
+
|
|
221
|
+
loadBoot: async function () { this.bootAt = Date.now(); try { this.boot = await getJson('/api/boot'); } catch (e) {} },
|
|
222
|
+
get bootUnit() { return this.boot.unit; },
|
|
223
|
+
get bootInstalled() { return this.boot.installed; },
|
|
224
|
+
get bootCanInstall() { return !this.boot.installed || this.boot.outdated || this.boot.active !== 'active'; },
|
|
225
|
+
get bootInstallLabel() { return !this.boot.installed ? 'Install' : this.boot.outdated ? 'Update' : 'Start it'; },
|
|
226
|
+
get bootText() { return this.boot.installed ? this.boot.enabled + ', ' + this.boot.active + (this.boot.outdated ? ', outdated' : '') : 'not installed'; },
|
|
227
|
+
get bootClass() { return !this.boot.installed ? '' : this.boot.enabled === 'enabled' && !this.boot.outdated ? 'ok' : 'warn'; },
|
|
228
|
+
get bootProjectsText() {
|
|
229
|
+
var n = this.boot.projects.length;
|
|
230
|
+
return n ? '· brings up ' + this.boot.projects.join(', ') : '· no projects marked yet';
|
|
231
|
+
},
|
|
232
|
+
bootPost: async function (body, okText) {
|
|
233
|
+
this.bootBusy = true;
|
|
234
|
+
try {
|
|
235
|
+
var r = await post('/api/boot', body);
|
|
236
|
+
var j = await r.json().catch(function () { return {}; });
|
|
237
|
+
this.showToast((r.ok ? '✓ ' + okText : '✗ boot service') + '\\n' + (j.output || j.error || ''), r.ok, 12000);
|
|
238
|
+
} catch (e) { this.showToast('✗ network error', false); } finally { this.bootBusy = false; this.loadBoot(); }
|
|
239
|
+
},
|
|
240
|
+
installBoot: function () { this.bootPost({ action: 'install' }, 'boot service installed'); },
|
|
241
|
+
askUninstallBoot: function () { this.bootUninstallOpen = true; },
|
|
242
|
+
closeUninstallBoot: function () { this.bootUninstallOpen = false; },
|
|
243
|
+
uninstallBoot: function () { this.bootUninstallOpen = false; this.bootPost({ action: 'uninstall', confirm: true }, 'boot service removed'); },
|
|
244
|
+
};
|
|
245
|
+
});
|
|
246
|
+
`;
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// Panel: cleanup. Lists orphaned burrow-* tunnels and burrow:* DNS records with the
|
|
2
|
+
// same rules as `burrow prune`, and deletes the ones you pick after a typed confirmation.
|
|
3
|
+
// The orphan list is recomputed before deleting, so only things that are still orphans go.
|
|
4
|
+
// DNS records are looked up in every zone burrow knows (BURROW_DOMAIN's, zones of
|
|
5
|
+
// per-project `domain:` settings, cached and remembered), like `burrow prune`.
|
|
6
|
+
// DNS deletions queued by a destroy that couldn't read a zone are listed with a Retry button.
|
|
7
|
+
import { Cloudflare } from '../../cloudflare.js';
|
|
8
|
+
import { listPending, retryPending } from '../../pending.js';
|
|
9
|
+
import { findOrphans, localProjectNames } from '../../prune.js';
|
|
10
|
+
import { inZone, knownZones } from '../../zones.js';
|
|
11
|
+
|
|
12
|
+
export const order = 80;
|
|
13
|
+
export const CONFIRM_WORD = 'delete';
|
|
14
|
+
|
|
15
|
+
async function scan(cfg, includeActive) {
|
|
16
|
+
if (!(cfg.token && cfg.accountId && cfg.domain)) throw Object.assign(new Error('Cloudflare not configured'), { status: 400 });
|
|
17
|
+
const cf = new Cloudflare({ token: cfg.token, accountId: cfg.accountId, zoneId: cfg.zoneId });
|
|
18
|
+
const tunnels = (await cf.listTunnels()).filter((t) => t.name.startsWith('burrow-'));
|
|
19
|
+
// One unreadable zone (e.g. a stale cache entry for a domain removed from the account)
|
|
20
|
+
// doesn't block the rest; it's reported so the page can say so.
|
|
21
|
+
// Also when BURROW_DOMAIN's own zone can't be found: it's listed as not scanned.
|
|
22
|
+
const zoneErrors = [];
|
|
23
|
+
const zones = await knownZones(cf, cfg, { onError: (z, e) => zoneErrors.push({ zone: z.domain, error: e.message }) });
|
|
24
|
+
const records = [];
|
|
25
|
+
for (const z of zones) {
|
|
26
|
+
try {
|
|
27
|
+
for (const r of await inZone(cf, z.id).recordsByCommentPrefix('burrow:')) records.push({ ...r, zoneId: z.id, zoneName: z.domain });
|
|
28
|
+
} catch (e) { zoneErrors.push({ zone: z.domain, error: e.message }); }
|
|
29
|
+
}
|
|
30
|
+
return { cf, zones, zoneErrors, ...findOrphans({ tunnels, records, local: localProjectNames(), includeActive }) };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const tunnelView = (t) => ({
|
|
34
|
+
id: t.id, name: t.name, status: t.status || 'unknown', createdAt: t.created_at || null,
|
|
35
|
+
connections: (t.connections || []).length,
|
|
36
|
+
});
|
|
37
|
+
const recordView = (r) => ({ id: r.id, type: r.type, name: r.name, content: r.content, comment: r.comment, zone: r.zoneName });
|
|
38
|
+
|
|
39
|
+
const pendingView = (e) => ({ project: e.project, domain: e.domain, comment: e.comment, at: e.at, error: e.error });
|
|
40
|
+
|
|
41
|
+
export async function route(ctx) {
|
|
42
|
+
if (ctx.path === '/api/cleanup/pending/retry' && ctx.method === 'POST') {
|
|
43
|
+
const cfg = ctx.loadConfig();
|
|
44
|
+
if (!(cfg.token && cfg.accountId && cfg.domain)) return ctx.json(400, { error: 'Cloudflare not configured' }) ?? true;
|
|
45
|
+
const r = await ctx.withBusy('(cleanup)', async () => {
|
|
46
|
+
const cf = new Cloudflare({ token: cfg.token, accountId: cfg.accountId, zoneId: cfg.zoneId });
|
|
47
|
+
const res = await retryPending(cf);
|
|
48
|
+
ctx.log(`cleanup: retried ${res.done.length + res.dropped.length + res.left.length} queued DNS cleanup(s), ${res.left.length} left (requested from ${ctx.clientIp})`);
|
|
49
|
+
ctx.events.emit('event', { action: 'cleanup', ok: !res.left.length, detail: `retried queued DNS cleanups: ${res.done.length} done, ${res.left.length} left` });
|
|
50
|
+
ctx.invalidate();
|
|
51
|
+
return { ok: !res.left.length, output: res.lines.join('\n') || 'Nothing queued', pending: res.left.map(pendingView) };
|
|
52
|
+
}).catch((e) => ({ ok: false, output: e.message, error: e.message }));
|
|
53
|
+
if (!r) ctx.json(409, { error: 'a cleanup is already running' });
|
|
54
|
+
else ctx.json(r.ok ? 200 : 502, r);
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
if (ctx.path !== '/api/cleanup') return false;
|
|
58
|
+
const cfg = ctx.loadConfig();
|
|
59
|
+
if (ctx.method === 'GET') {
|
|
60
|
+
try {
|
|
61
|
+
const s = await scan(cfg, ctx.url.searchParams.get('includeActive') === '1');
|
|
62
|
+
ctx.json(200, {
|
|
63
|
+
tunnels: s.orphanTunnels.map(tunnelView), records: s.orphanRecords.map(recordView), skipped: s.skipped.map(tunnelView),
|
|
64
|
+
zones: s.zones.map((z) => z.domain), zoneErrors: s.zoneErrors, pending: listPending().map(pendingView),
|
|
65
|
+
});
|
|
66
|
+
} catch (e) { ctx.json(e.status || 502, { error: e.message, pending: listPending().map(pendingView) }); }
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
if (ctx.method !== 'POST') return false;
|
|
70
|
+
const body = await ctx.readJson(64 * 1024);
|
|
71
|
+
if (!body) return ctx.json(400, { error: 'invalid JSON' }) ?? true;
|
|
72
|
+
if (body.confirm !== CONFIRM_WORD) return ctx.json(400, { error: `type "${CONFIRM_WORD}" to confirm` }) ?? true;
|
|
73
|
+
const ids = (v) => new Set(Array.isArray(v) ? v.filter((x) => typeof x === 'string').slice(0, 500) : []);
|
|
74
|
+
const wantTunnels = ids(body.tunnelIds);
|
|
75
|
+
const wantRecords = ids(body.recordIds);
|
|
76
|
+
if (!wantTunnels.size && !wantRecords.size) return ctx.json(400, { error: 'nothing selected' }) ?? true;
|
|
77
|
+
|
|
78
|
+
const r = await ctx.withBusy('(cleanup)', async () => {
|
|
79
|
+
const s = await scan(cfg, body.includeActive === true);
|
|
80
|
+
const tunnels = s.orphanTunnels.filter((t) => wantTunnels.has(t.id));
|
|
81
|
+
// A record of a tunnel being deleted goes with it, as in `burrow prune`.
|
|
82
|
+
const doomed = new Set(tunnels.map((t) => t.id));
|
|
83
|
+
const records = s.orphanRecords.filter((x) => wantRecords.has(x.id) || [...doomed].some((id) => x.content === `${id}.cfargotunnel.com`));
|
|
84
|
+
const lines = [];
|
|
85
|
+
let failed = 0;
|
|
86
|
+
for (const rec of records) {
|
|
87
|
+
try { await inZone(s.cf, rec.zoneId).deleteRecord(rec.id); lines.push(`Deleted DNS ${rec.name}`); } catch (e) { failed++; lines.push(`Failed DNS ${rec.name}: ${e.message}`); }
|
|
88
|
+
}
|
|
89
|
+
for (const t of tunnels) {
|
|
90
|
+
try {
|
|
91
|
+
await s.cf.deleteTunnelConnections(t.id);
|
|
92
|
+
await s.cf.deleteTunnel(t.id);
|
|
93
|
+
lines.push(`Deleted tunnel ${t.name}`);
|
|
94
|
+
} catch (e) { failed++; lines.push(`Failed tunnel ${t.name}: ${e.message}`); }
|
|
95
|
+
}
|
|
96
|
+
const notOrphans = [...wantTunnels].filter((id) => !s.orphanTunnels.some((t) => t.id === id)).length +
|
|
97
|
+
[...wantRecords].filter((id) => !s.orphanRecords.some((x) => x.id === id)).length;
|
|
98
|
+
if (notOrphans) lines.push(`Skipped ${notOrphans} item(s) that are no longer orphans`);
|
|
99
|
+
for (const z of s.zoneErrors) lines.push(`Couldn't read DNS in ${z.zone}: ${z.error}`);
|
|
100
|
+
ctx.log(`cleanup: deleted ${tunnels.length} tunnel(s), ${records.length} record(s) (requested from ${ctx.clientIp})`);
|
|
101
|
+
ctx.events.emit('event', { action: 'cleanup', ok: !failed, detail: `${tunnels.length} tunnel(s), ${records.length} DNS record(s)` });
|
|
102
|
+
ctx.invalidate();
|
|
103
|
+
return { ok: !failed, output: lines.join('\n') || 'Nothing deleted', deleted: { tunnels: tunnels.length, records: records.length }, skipped: notOrphans };
|
|
104
|
+
}).catch((e) => ({ ok: false, output: e.message, error: e.message }));
|
|
105
|
+
if (!r) ctx.json(409, { error: 'a cleanup is already running' });
|
|
106
|
+
else ctx.json(r.ok ? 200 : 500, r);
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export const html = {
|
|
111
|
+
toolbar: '<li><button class="secondary outline" @click="openCleanup">Cleanup</button></li>',
|
|
112
|
+
sections: `
|
|
113
|
+
<article x-show="cleanupOpen" class="cleanup">
|
|
114
|
+
<header class="logs-head">
|
|
115
|
+
<strong>Cleanup</strong><small class="muted">orphaned tunnels and DNS records</small>
|
|
116
|
+
<span class="spacer"></span>
|
|
117
|
+
<label><input type="checkbox" role="switch" x-model="cleanupActive" @change="scanCleanup"> Include tunnels with live connections</label>
|
|
118
|
+
<button class="secondary outline" :aria-busy="cleanupLoading" @click="scanCleanup">Scan</button>
|
|
119
|
+
<button class="secondary outline" @click="closeCleanup">Close</button>
|
|
120
|
+
</header>
|
|
121
|
+
<p class="error" x-show="hasCleanupError" x-text="cleanupError"></p>
|
|
122
|
+
<p class="zones" x-show="hasZones"><small class="muted" x-text="zonesText"></small></p>
|
|
123
|
+
<template x-for="z in cleanup.zoneErrors" :key="z.zone">
|
|
124
|
+
<p class="zone-error"><small><span class="badge warn">not scanned</span> <code x-text="z.zone"></code> <span x-text="z.error"></span></small></p>
|
|
125
|
+
</template>
|
|
126
|
+
<div class="pending" x-show="hasPending">
|
|
127
|
+
<h6>Queued DNS deletions</h6>
|
|
128
|
+
<small class="muted">These projects were destroyed while a zone couldn't be read, so their DNS records there may still exist.</small>
|
|
129
|
+
<template x-for="e in cleanup.pending" :key="pendingKey(e)">
|
|
130
|
+
<p><small><span class="badge warn">pending</span> <code x-text="e.project"></code> in <code x-text="e.domain"></code> <span class="muted" x-text="e.error"></span></small></p>
|
|
131
|
+
</template>
|
|
132
|
+
<div class="row-buttons"><button class="secondary" :aria-busy="pendingBusy" :disabled="pendingBusy" @click="retryPending">Retry now</button></div>
|
|
133
|
+
</div>
|
|
134
|
+
<p x-show="cleanupClean"><span class="badge ok">clean</span> No orphaned burrow tunnels or DNS records.</p>
|
|
135
|
+
<div x-show="hasOrphans">
|
|
136
|
+
<h6>Tunnels</h6>
|
|
137
|
+
<p x-show="noOrphanTunnels"><small class="muted">none</small></p>
|
|
138
|
+
<template x-for="t in cleanup.tunnels" :key="t.id">
|
|
139
|
+
<label class="orphan"><input type="checkbox" :value="t.id" x-model="pickTunnels">
|
|
140
|
+
<code x-text="t.name"></code> <small class="muted" x-text="tunnelInfo(t)"></small></label>
|
|
141
|
+
</template>
|
|
142
|
+
<h6>DNS records</h6>
|
|
143
|
+
<p x-show="noOrphanRecords"><small class="muted">none</small></p>
|
|
144
|
+
<template x-for="r in cleanup.records" :key="r.id">
|
|
145
|
+
<label class="orphan"><input type="checkbox" :value="r.id" x-model="pickRecords">
|
|
146
|
+
<code x-text="r.name"></code> <small class="muted" x-text="recordInfo(r)"></small></label>
|
|
147
|
+
</template>
|
|
148
|
+
<div class="row-buttons"><button class="danger" :disabled="nothingPicked" @click="askCleanup" x-text="cleanupLabel"></button></div>
|
|
149
|
+
</div>
|
|
150
|
+
<div x-show="hasSkipped">
|
|
151
|
+
<small class="muted">Not listed: tunnels with no local state but live connections (probably another machine):
|
|
152
|
+
<span x-text="skippedNames"></span>. Switch on "Include tunnels with live connections" to clean them up too.</small>
|
|
153
|
+
</div>
|
|
154
|
+
</article>
|
|
155
|
+
<dialog :open="cleanupConfirmOpen">
|
|
156
|
+
<article>
|
|
157
|
+
<header><strong>Delete from Cloudflare?</strong></header>
|
|
158
|
+
<p>This deletes <strong x-text="pickSummary"></strong> from Cloudflare. It can't be undone.
|
|
159
|
+
Type <strong>delete</strong> to confirm.</p>
|
|
160
|
+
<input type="text" x-model="cleanupConfirmText" placeholder="delete" aria-label="Type delete to confirm" autocomplete="off">
|
|
161
|
+
<footer>
|
|
162
|
+
<button class="secondary" @click="closeCleanupConfirm">Cancel</button>
|
|
163
|
+
<button class="danger" :disabled="cannotCleanup" :aria-busy="cleanupBusy" @click="runCleanup">Delete</button>
|
|
164
|
+
</footer>
|
|
165
|
+
</article>
|
|
166
|
+
</dialog>`,
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export const css = `
|
|
170
|
+
.cleanup .zones,.cleanup .zone-error,.cleanup .pending p{margin:.25rem 0}
|
|
171
|
+
.cleanup h6{margin:.75rem 0 .35rem}.cleanup label.orphan{display:block;margin:.2rem 0}
|
|
172
|
+
.cleanup .row-buttons{display:flex;gap:.5rem;margin:.75rem 0}.cleanup .row-buttons button{width:auto;margin:0}
|
|
173
|
+
`;
|
|
174
|
+
|
|
175
|
+
export const js = `
|
|
176
|
+
burrowPanel(function () {
|
|
177
|
+
function none() { return { tunnels: [], records: [], skipped: [], zones: [], zoneErrors: [], pending: [] }; }
|
|
178
|
+
return {
|
|
179
|
+
cleanupOpen: false, cleanup: none(), cleanupLoaded: false, cleanupLoading: false, cleanupError: '', cleanupActive: false,
|
|
180
|
+
pickTunnels: [], pickRecords: [], cleanupConfirmOpen: false, cleanupConfirmText: '', cleanupBusy: false, pendingBusy: false,
|
|
181
|
+
get hasPending() { return (this.cleanup.pending || []).length > 0; },
|
|
182
|
+
pendingKey: function (e) { return e.comment + ' ' + e.domain; },
|
|
183
|
+
|
|
184
|
+
get hasCleanupError() { return this.cleanupError.length > 0; },
|
|
185
|
+
get hasZones() { return (this.cleanup.zones || []).length > 0; },
|
|
186
|
+
get zonesText() { var z = this.cleanup.zones || []; return 'DNS checked in ' + z.length + (z.length === 1 ? ' zone: ' : ' zones: ') + z.join(', '); },
|
|
187
|
+
get hasOrphans() { return this.cleanup.tunnels.length + this.cleanup.records.length > 0; },
|
|
188
|
+
get cleanupClean() { return this.cleanupLoaded && !this.cleanupError && !this.hasOrphans && !this.hasPending; },
|
|
189
|
+
get noOrphanTunnels() { return this.cleanup.tunnels.length === 0; },
|
|
190
|
+
get noOrphanRecords() { return this.cleanup.records.length === 0; },
|
|
191
|
+
get hasSkipped() { return this.cleanup.skipped.length > 0 && !this.cleanupActive; },
|
|
192
|
+
get skippedNames() { return this.cleanup.skipped.map(function (t) { return t.name; }).join(', '); },
|
|
193
|
+
get nothingPicked() { return this.pickTunnels.length + this.pickRecords.length === 0; },
|
|
194
|
+
get pickSummary() { return this.pickTunnels.length + ' tunnel(s) and ' + this.pickRecords.length + ' DNS record(s)'; },
|
|
195
|
+
get cleanupLabel() { return 'Delete ' + this.pickSummary; },
|
|
196
|
+
get cannotCleanup() { return this.cleanupConfirmText !== 'delete' || this.cleanupBusy; },
|
|
197
|
+
tunnelInfo: function (t) { return t.status + (t.connections ? ' · ' + t.connections + ' live conn' : '') + (t.createdAt ? ' · created ' + t.createdAt.slice(0, 10) : ''); },
|
|
198
|
+
recordInfo: function (r) { return r.type + ' → ' + r.content + ' (' + r.comment + (r.zone ? ', zone ' + r.zone : '') + ')'; },
|
|
199
|
+
|
|
200
|
+
openCleanup: function () { this.cleanupOpen = true; this.scanCleanup(); },
|
|
201
|
+
closeCleanup: function () { this.cleanupOpen = false; },
|
|
202
|
+
scanCleanup: async function () {
|
|
203
|
+
this.cleanupLoading = true; this.cleanupError = '';
|
|
204
|
+
try {
|
|
205
|
+
this.cleanup = Object.assign(none(), await getJson('/api/cleanup' + (this.cleanupActive ? '?includeActive=1' : '')));
|
|
206
|
+
this.pickTunnels = this.cleanup.tunnels.map(function (t) { return t.id; });
|
|
207
|
+
this.pickRecords = this.cleanup.records.map(function (r) { return r.id; });
|
|
208
|
+
} catch (e) { this.cleanup = none(); this.cleanupError = e.message; } finally { this.cleanupLoading = false; this.cleanupLoaded = true; }
|
|
209
|
+
},
|
|
210
|
+
retryPending: async function () {
|
|
211
|
+
this.pendingBusy = true;
|
|
212
|
+
try {
|
|
213
|
+
var r = await post('/api/cleanup/pending/retry', {});
|
|
214
|
+
if (r.status === 401) { location.assign('/login'); return; }
|
|
215
|
+
var j = await r.json().catch(function () { return {}; });
|
|
216
|
+
this.showToast((j.ok ? '✓ queued DNS cleanup\\n' : '✗ queued DNS cleanup\\n') + (j.output || j.error || ''), !!j.ok, 12000);
|
|
217
|
+
} catch (e) { this.showToast('✗ queued DNS cleanup: network error', false); } finally {
|
|
218
|
+
this.pendingBusy = false; this.scanCleanup(); this.load();
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
askCleanup: function () { this.cleanupConfirmText = ''; this.cleanupConfirmOpen = true; },
|
|
222
|
+
closeCleanupConfirm: function () { this.cleanupConfirmOpen = false; },
|
|
223
|
+
runCleanup: async function () {
|
|
224
|
+
if (this.cannotCleanup) return;
|
|
225
|
+
this.cleanupBusy = true;
|
|
226
|
+
try {
|
|
227
|
+
var r = await post('/api/cleanup', { confirm: 'delete', tunnelIds: this.pickTunnels, recordIds: this.pickRecords, includeActive: this.cleanupActive });
|
|
228
|
+
if (r.status === 401) { location.assign('/login'); return; }
|
|
229
|
+
var j = await r.json().catch(function () { return {}; });
|
|
230
|
+
this.showToast((j.ok ? '✓ cleanup\\n' : '✗ cleanup\\n') + (j.output || j.error || ''), !!j.ok, 12000);
|
|
231
|
+
} catch (e) { this.showToast('✗ cleanup: network error', false); } finally {
|
|
232
|
+
this.cleanupBusy = false; this.cleanupConfirmOpen = false; this.scanCleanup(); this.load();
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
});
|
|
237
|
+
`;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// Panel: doctor. Runs `burrow doctor --json --all` (config, token, zone, permissions,
|
|
2
|
+
// cloudflared and every remembered project's local services) and shows each check with a
|
|
3
|
+
// pass / warning / fail badge and, for problems, what to do about it.
|
|
4
|
+
export const order = 85;
|
|
5
|
+
|
|
6
|
+
// The last line of the CLI output that is the JSON report.
|
|
7
|
+
export function parseReport(output) {
|
|
8
|
+
const lines = String(output || '').split('\n').reverse();
|
|
9
|
+
for (const l of lines) {
|
|
10
|
+
if (!l.startsWith('{"counts"')) continue;
|
|
11
|
+
try { return JSON.parse(l); } catch {}
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// [{ title, checks: [...] }] in report order.
|
|
17
|
+
export function groupChecks(checks) {
|
|
18
|
+
const out = [];
|
|
19
|
+
for (const c of checks || []) {
|
|
20
|
+
let g = out[out.length - 1];
|
|
21
|
+
if (!g || g.title !== c.section) { g = { title: c.section, checks: [] }; out.push(g); }
|
|
22
|
+
g.checks.push(c);
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let last = null;
|
|
28
|
+
|
|
29
|
+
export async function route(ctx) {
|
|
30
|
+
if (ctx.path !== '/api/doctor') return false;
|
|
31
|
+
if (ctx.method === 'GET') return ctx.json(200, last || { ran: false }) ?? true;
|
|
32
|
+
if (ctx.method !== 'POST') return false;
|
|
33
|
+
const body = await ctx.readJson();
|
|
34
|
+
if (!body) return ctx.json(400, { error: 'invalid JSON' }) ?? true;
|
|
35
|
+
const args = ['doctor', '--json', '--all', ...(body.offline === true ? ['--offline'] : [])];
|
|
36
|
+
const r = await ctx.withBusy('(doctor)', async () => {
|
|
37
|
+
const run = await ctx.runCli(args, 60000);
|
|
38
|
+
const report = parseReport(run.output);
|
|
39
|
+
if (!report) return { ok: false, error: 'doctor did not finish', output: run.output.slice(-2000) };
|
|
40
|
+
return { ok: true, ran: true, at: new Date().toISOString(), offline: body.offline === true, counts: report.counts, groups: groupChecks(report.checks) };
|
|
41
|
+
});
|
|
42
|
+
if (!r) return ctx.json(409, { error: 'doctor is already running' }) ?? true;
|
|
43
|
+
if (r.ok) last = r;
|
|
44
|
+
ctx.json(r.ok ? 200 : 500, r);
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const html = {
|
|
49
|
+
toolbar: '<li><button class="secondary outline" @click="openDoctor">Doctor</button></li>',
|
|
50
|
+
sections: `
|
|
51
|
+
<article x-show="doctorOpen" class="doctor">
|
|
52
|
+
<header class="logs-head">
|
|
53
|
+
<strong>Doctor</strong><small class="muted" x-text="doctorSummary"></small>
|
|
54
|
+
<span class="spacer"></span>
|
|
55
|
+
<label><input type="checkbox" role="switch" x-model="doctorOffline"> Skip Cloudflare API checks</label>
|
|
56
|
+
<button class="secondary outline" :aria-busy="doctorBusy" :disabled="doctorBusy" @click="runDoctor">Run checks</button>
|
|
57
|
+
<button class="secondary outline" @click="closeDoctor">Close</button>
|
|
58
|
+
</header>
|
|
59
|
+
<p class="error" x-show="hasDoctorError" x-text="doctorError"></p>
|
|
60
|
+
<p x-show="doctorNeverRan"><small class="muted">Checks the config, the API token and its permissions, the zone, cloudflared and the local services of every remembered project.</small></p>
|
|
61
|
+
<template x-for="g in doctorGroups" :key="g.title">
|
|
62
|
+
<section class="doctor-group">
|
|
63
|
+
<h6 x-text="g.title"></h6>
|
|
64
|
+
<ul>
|
|
65
|
+
<template x-for="(c, i) in g.checks" :key="i">
|
|
66
|
+
<li :class="c.status">
|
|
67
|
+
<span class="badge" :class="doctorBadgeClass(c)" x-text="doctorBadge(c)"></span>
|
|
68
|
+
<strong x-text="c.label"></strong> <small class="muted" x-text="c.detail"></small>
|
|
69
|
+
<div class="fix" x-show="hasFix(c)"><small><strong>Fix:</strong> <span x-text="c.fix"></span></small></div>
|
|
70
|
+
</li>
|
|
71
|
+
</template>
|
|
72
|
+
</ul>
|
|
73
|
+
</section>
|
|
74
|
+
</template>
|
|
75
|
+
</article>`,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const css = `
|
|
79
|
+
.doctor-group h6{margin:.8rem 0 .3rem}.doctor-group ul{list-style:none;padding:0;margin:0}
|
|
80
|
+
.doctor-group li{list-style:none;padding:.25rem 0;border-bottom:1px solid var(--pico-muted-border-color)}
|
|
81
|
+
.doctor-group .badge{min-width:3.6rem;text-align:center;margin-right:.4rem}
|
|
82
|
+
.doctor-group .fix{margin:.15rem 0 0 4.1rem;color:var(--pico-color)}
|
|
83
|
+
.doctor-group li.fail .fix{color:#dc2626}.doctor-group li.warn .fix{color:#b45309}
|
|
84
|
+
`;
|
|
85
|
+
|
|
86
|
+
export const js = `
|
|
87
|
+
burrowPanel(function () {
|
|
88
|
+
var BADGE = { pass: 'pass', warn: 'warning', fail: 'fail', skip: 'skipped' };
|
|
89
|
+
var CLASS = { pass: 'ok', warn: 'warn', fail: 'bad', skip: '' };
|
|
90
|
+
return {
|
|
91
|
+
doctorOpen: false, doctorBusy: false, doctorOffline: false, doctorError: '', doctor: { ran: false, groups: [] },
|
|
92
|
+
get doctorGroups() { return this.doctor.groups || []; },
|
|
93
|
+
get hasDoctorError() { return this.doctorError.length > 0; },
|
|
94
|
+
get doctorNeverRan() { return !this.doctor.ran && !this.doctorBusy; },
|
|
95
|
+
get doctorSummary() {
|
|
96
|
+
var c = this.doctor.counts;
|
|
97
|
+
if (!c) return '';
|
|
98
|
+
return c.pass + ' passed · ' + c.warn + ' warning(s) · ' + c.fail + ' failed' + (c.skip ? ' · ' + c.skip + ' skipped' : '') + ' · ' + ago(Date.now() - Date.parse(this.doctor.at)) + ' ago';
|
|
99
|
+
},
|
|
100
|
+
doctorBadge: function (c) { return BADGE[c.status] || c.status; },
|
|
101
|
+
doctorBadgeClass: function (c) { return CLASS[c.status] || ''; },
|
|
102
|
+
hasFix: function (c) { return !!c.fix; },
|
|
103
|
+
openDoctor: async function () {
|
|
104
|
+
this.doctorOpen = true;
|
|
105
|
+
try { var j = await getJson('/api/doctor'); if (j.ran) this.doctor = j; else this.runDoctor(); } catch (e) { this.doctorError = e.message; }
|
|
106
|
+
},
|
|
107
|
+
closeDoctor: function () { this.doctorOpen = false; },
|
|
108
|
+
runDoctor: async function () {
|
|
109
|
+
this.doctorBusy = true; this.doctorError = '';
|
|
110
|
+
try {
|
|
111
|
+
var r = await post('/api/doctor', { offline: this.doctorOffline });
|
|
112
|
+
if (r.status === 401) { location.assign('/login'); return; }
|
|
113
|
+
var j = await r.json().catch(function () { return {}; });
|
|
114
|
+
if (r.ok) this.doctor = j; else this.doctorError = j.error || ('HTTP ' + r.status);
|
|
115
|
+
} catch (e) { this.doctorError = 'Network error'; } finally { this.doctorBusy = false; }
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
});
|
|
119
|
+
`;
|