tailcat-webui 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NOTICE +29 -0
- package/README.en.md +112 -0
- package/README.md +101 -2
- package/bin/tailcat-webui.js +12 -0
- package/docs/CORE-API.md +343 -0
- package/docs/DESIGN.md +398 -0
- package/docs/assets/character.jpg +0 -0
- package/docs/assets/portrait.jpg +0 -0
- package/docs/screenshots/01-overview-light.png +0 -0
- package/docs/screenshots/02-overview-dark.png +0 -0
- package/docs/screenshots/03-ticket-light.png +0 -0
- package/docs/screenshots/04-ticket-qr-light.png +0 -0
- package/docs/screenshots/05-settings-light.png +0 -0
- package/docs/screenshots/06-diagnostics-light.png +0 -0
- package/docs/screenshots/07-mobile-overview.png +0 -0
- package/docs/screenshots/08-install-missing.png +0 -0
- package/docs/screenshots/09-keeper.png +0 -0
- package/package.json +56 -4
- package/src/cli.js +238 -0
- package/src/config.js +167 -0
- package/src/devices.js +163 -0
- package/src/errors.js +24 -0
- package/src/index.js +19 -0
- package/src/installer.js +907 -0
- package/src/manager.js +647 -0
- package/src/parse.js +207 -0
- package/src/paths.js +97 -0
- package/src/preferences.js +190 -0
- package/src/qr.js +37 -0
- package/src/relay.js +76 -0
- package/src/runtime/node.js +281 -0
- package/src/serve-targets.js +127 -0
- package/src/server.js +674 -0
- package/src/settings.js +262 -0
- package/src/supervisor.js +833 -0
- package/vendor/qrcode.mjs +2289 -0
- package/web/api.js +114 -0
- package/web/assets/portrait.jpg +0 -0
- package/web/boot.js +29 -0
- package/web/components.js +558 -0
- package/web/dict.js +663 -0
- package/web/drawer-access.js +318 -0
- package/web/drawer-address.js +271 -0
- package/web/drawer-automation.js +223 -0
- package/web/drawer-relay.js +128 -0
- package/web/drawer-tunnel.js +309 -0
- package/web/drawer.js +302 -0
- package/web/format.js +252 -0
- package/web/icon.png +0 -0
- package/web/index.html +29 -0
- package/web/main.js +471 -0
- package/web/room-diagnostics.js +426 -0
- package/web/room-overview.js +438 -0
- package/web/room-settings.js +71 -0
- package/web/store.js +168 -0
- package/web/styles.css +2873 -0
- package/web/ticket.js +212 -0
package/src/settings.js
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { RemoteAccessError } from './errors.js';
|
|
2
|
+
import { normalizeServeTargets } from './serve-targets.js';
|
|
3
|
+
|
|
4
|
+
/** Fields the settings panel may override. Everything else is deployment config. */
|
|
5
|
+
export const SETTING_KEYS = Object.freeze([
|
|
6
|
+
'addressMode',
|
|
7
|
+
'keyName',
|
|
8
|
+
'regionMode',
|
|
9
|
+
'regionValue',
|
|
10
|
+
'psk',
|
|
11
|
+
'hybrid',
|
|
12
|
+
'fullAddress',
|
|
13
|
+
'verbose',
|
|
14
|
+
'logAccess',
|
|
15
|
+
'autoStart',
|
|
16
|
+
'restartEnabled',
|
|
17
|
+
'maxRestartAttempts',
|
|
18
|
+
'restartBaseDelayMs',
|
|
19
|
+
'restartMaxDelayMs',
|
|
20
|
+
'extraTargets',
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
/** Fields whose change only takes effect on a running tunnel restart. */
|
|
24
|
+
const RESTART_FIELDS = Object.freeze([
|
|
25
|
+
'addressMode',
|
|
26
|
+
'keyName',
|
|
27
|
+
'psk',
|
|
28
|
+
'hybrid',
|
|
29
|
+
'fullAddress',
|
|
30
|
+
'verbose',
|
|
31
|
+
'logAccess',
|
|
32
|
+
'extraTargets',
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
const CONTROL = /[\u0000-\u001f\u007f]/;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The concrete settings the runtime uses, after saved overrides are folded
|
|
39
|
+
* over the deployment config. `overrides` only carries keys that were actually
|
|
40
|
+
* saved; everything else keeps following the config.
|
|
41
|
+
* @param {ReturnType<import('./config.js').resolveConfig>} config - resolved config.
|
|
42
|
+
* @param {Record<string, unknown>} [overrides] - saved settings.
|
|
43
|
+
* @returns {object} effective settings.
|
|
44
|
+
*/
|
|
45
|
+
export function effectiveSettings(config, overrides = {}) {
|
|
46
|
+
const o = overrides && typeof overrides === 'object' && !Array.isArray(overrides) ? overrides : {};
|
|
47
|
+
const pick = (key) => (o[key] !== undefined ? o[key] : config[key]);
|
|
48
|
+
const maxRestartAttempts = integer(pick('maxRestartAttempts'), 0, 100, config.maxRestartAttempts);
|
|
49
|
+
const restartBaseDelayMs = integer(pick('restartBaseDelayMs'), 100, 60_000, config.restartBaseDelayMs);
|
|
50
|
+
const restartMaxDelayMs = integer(pick('restartMaxDelayMs'), 1_000, 600_000, config.restartMaxDelayMs);
|
|
51
|
+
return {
|
|
52
|
+
addressMode: pick('addressMode') === 'ephemeral' ? 'ephemeral' : 'stable',
|
|
53
|
+
keyName: text(pick('keyName'), config.keyName, 256),
|
|
54
|
+
regionMode: ['auto', 'explicit', 'hosts'].includes(pick('regionMode')) ? pick('regionMode') : 'auto',
|
|
55
|
+
regionValue: text(pick('regionValue'), '', 512),
|
|
56
|
+
psk: pick('psk') !== false,
|
|
57
|
+
hybrid: pick('hybrid') === true,
|
|
58
|
+
fullAddress: pick('fullAddress') === true,
|
|
59
|
+
verbose: pick('verbose') === true,
|
|
60
|
+
logAccess: pick('logAccess') !== false,
|
|
61
|
+
autoStart: pick('autoStart') !== false,
|
|
62
|
+
restartEnabled: pick('restartEnabled') !== false,
|
|
63
|
+
maxRestartAttempts,
|
|
64
|
+
restartBaseDelayMs,
|
|
65
|
+
restartMaxDelayMs,
|
|
66
|
+
extraTargets: normalizeServeTargets(pick('extraTargets') ?? []),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Which layer supplied each effective value. The panel marks only `panel`
|
|
72
|
+
* fields, so an unmarked field keeps following the deployment config.
|
|
73
|
+
* @param {ReturnType<import('./config.js').resolveConfig>} config - resolved config.
|
|
74
|
+
* @param {Record<string, unknown>} [overrides] - saved settings.
|
|
75
|
+
* @returns {Record<string, 'panel' | 'config'>} origins.
|
|
76
|
+
*/
|
|
77
|
+
export function settingsSources(config, overrides = {}) {
|
|
78
|
+
const o = overrides && typeof overrides === 'object' ? overrides : {};
|
|
79
|
+
const sources = {};
|
|
80
|
+
for (const key of SETTING_KEYS) sources[key] = o[key] !== undefined ? 'panel' : 'config';
|
|
81
|
+
return sources;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Validate one patch from the control API. `null` removes an override and
|
|
86
|
+
* makes that field follow the deployment config again. Unknown keys are
|
|
87
|
+
* rejected so a typo cannot quietly do nothing.
|
|
88
|
+
* @param {unknown} patch - candidate settings object.
|
|
89
|
+
* @returns {Record<string, unknown>} normalized patch, with nulls for resets.
|
|
90
|
+
* @throws {RemoteAccessError} on an invalid value.
|
|
91
|
+
*/
|
|
92
|
+
export function normalizeSettingsPatch(patch) {
|
|
93
|
+
if (patch === null || typeof patch !== 'object' || Array.isArray(patch)) {
|
|
94
|
+
throw new RemoteAccessError('invalid-settings', 'The settings must be an object.', 400);
|
|
95
|
+
}
|
|
96
|
+
const normalized = {};
|
|
97
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
98
|
+
if (!SETTING_KEYS.includes(key)) {
|
|
99
|
+
throw new RemoteAccessError('invalid-settings', `Unknown setting ${JSON.stringify(key)}.`, 400);
|
|
100
|
+
}
|
|
101
|
+
if (value === null) {
|
|
102
|
+
normalized[key] = null;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
normalized[key] = normalizeValue(key, value);
|
|
106
|
+
}
|
|
107
|
+
return normalized;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Merge a patch into the stored override object.
|
|
112
|
+
* @param {Record<string, unknown>} current - stored overrides.
|
|
113
|
+
* @param {Record<string, unknown>} patch - normalized patch.
|
|
114
|
+
* @returns {Record<string, unknown>} next overrides.
|
|
115
|
+
*/
|
|
116
|
+
export function mergeSettings(current, patch) {
|
|
117
|
+
const next = { ...(current ?? {}) };
|
|
118
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
119
|
+
if (value === null) delete next[key];
|
|
120
|
+
else next[key] = value;
|
|
121
|
+
}
|
|
122
|
+
return next;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Check the relationships that cannot be expressed per field.
|
|
127
|
+
* @param {object} settings - effective settings.
|
|
128
|
+
* @param {number[]} [reservedPorts] - virtual ports the caller keeps for itself.
|
|
129
|
+
* @throws {RemoteAccessError} when a combination is impossible.
|
|
130
|
+
*/
|
|
131
|
+
export function validateEffectiveSettings(settings, reservedPorts = []) {
|
|
132
|
+
if (settings.hybrid && !settings.psk) {
|
|
133
|
+
throw new RemoteAccessError('invalid-settings', 'The hybrid handshake requires the WireGuard pre-shared key.', 400);
|
|
134
|
+
}
|
|
135
|
+
if (settings.regionMode !== 'auto' && String(settings.regionValue ?? '').trim() === '') {
|
|
136
|
+
throw new RemoteAccessError('invalid-settings', 'Choose a region or a relay host for the selected region mode.', 400);
|
|
137
|
+
}
|
|
138
|
+
if (settings.restartBaseDelayMs > settings.restartMaxDelayMs) {
|
|
139
|
+
throw new RemoteAccessError('invalid-settings', 'The restart base delay cannot be larger than the maximum delay.', 400);
|
|
140
|
+
}
|
|
141
|
+
try {
|
|
142
|
+
normalizeServeTargets(settings.extraTargets, reservedPorts);
|
|
143
|
+
} catch (error) {
|
|
144
|
+
throw new RemoteAccessError(
|
|
145
|
+
'invalid-settings',
|
|
146
|
+
error instanceof Error ? error.message : String(error),
|
|
147
|
+
400,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @param {object} before - effective settings before a save.
|
|
154
|
+
* @param {object} after - effective settings after a save.
|
|
155
|
+
* @returns {boolean} whether a running tunnel must restart to apply the change.
|
|
156
|
+
*/
|
|
157
|
+
export function settingsNeedRestart(before, after) {
|
|
158
|
+
return RESTART_FIELDS.some((key) => !same(before[key], after[key]));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* @param {unknown} value - candidate.
|
|
163
|
+
* @param {number} min - lower bound.
|
|
164
|
+
* @param {number} max - upper bound.
|
|
165
|
+
* @param {number} fallback - value used when absent.
|
|
166
|
+
* @returns {number} normalized integer.
|
|
167
|
+
*/
|
|
168
|
+
function integer(value, min, max, fallback) {
|
|
169
|
+
if (value === undefined) return fallback;
|
|
170
|
+
if (!Number.isInteger(value) || value < min || value > max) return fallback;
|
|
171
|
+
return value;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* @param {unknown} value - candidate.
|
|
176
|
+
* @param {string} fallback - value used when absent.
|
|
177
|
+
* @param {number} maxLength - maximum accepted length.
|
|
178
|
+
* @returns {string} trimmed text.
|
|
179
|
+
*/
|
|
180
|
+
function text(value, fallback, maxLength) {
|
|
181
|
+
if (value === undefined) return fallback;
|
|
182
|
+
if (typeof value !== 'string') return fallback;
|
|
183
|
+
const trimmed = value.trim();
|
|
184
|
+
return trimmed.length <= maxLength ? trimmed : trimmed.slice(0, maxLength);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* @param {string} key - setting name.
|
|
189
|
+
* @param {unknown} value - candidate.
|
|
190
|
+
* @returns {unknown} normalized value.
|
|
191
|
+
* @throws {RemoteAccessError} when invalid.
|
|
192
|
+
*/
|
|
193
|
+
function normalizeValue(key, value) {
|
|
194
|
+
const fail = (message) => {
|
|
195
|
+
throw new RemoteAccessError('invalid-settings', message, 400);
|
|
196
|
+
};
|
|
197
|
+
switch (key) {
|
|
198
|
+
case 'addressMode':
|
|
199
|
+
if (value !== 'stable' && value !== 'ephemeral') fail('addressMode must be stable or ephemeral.');
|
|
200
|
+
return value;
|
|
201
|
+
case 'keyName': {
|
|
202
|
+
if (typeof value !== 'string') fail('The key name must be a string.');
|
|
203
|
+
const trimmed = value.trim();
|
|
204
|
+
if (trimmed === '') fail('The key name cannot be empty.');
|
|
205
|
+
if (trimmed.length > 256) fail('The key name is too long.');
|
|
206
|
+
if (CONTROL.test(trimmed)) fail('The key name contains a control character.');
|
|
207
|
+
return trimmed;
|
|
208
|
+
}
|
|
209
|
+
case 'regionMode':
|
|
210
|
+
if (!['auto', 'explicit', 'hosts'].includes(value)) fail('regionMode must be auto, explicit, or hosts.');
|
|
211
|
+
return value;
|
|
212
|
+
case 'regionValue': {
|
|
213
|
+
if (typeof value !== 'string') fail('The region value must be a string.');
|
|
214
|
+
const trimmed = value.trim();
|
|
215
|
+
if (trimmed.length > 512) fail('The region value is too long.');
|
|
216
|
+
if (CONTROL.test(trimmed)) fail('The region value contains a control character.');
|
|
217
|
+
return trimmed;
|
|
218
|
+
}
|
|
219
|
+
case 'psk':
|
|
220
|
+
case 'hybrid':
|
|
221
|
+
case 'fullAddress':
|
|
222
|
+
case 'verbose':
|
|
223
|
+
case 'logAccess':
|
|
224
|
+
case 'autoStart':
|
|
225
|
+
case 'restartEnabled':
|
|
226
|
+
if (typeof value !== 'boolean') fail(`${key} must be a boolean.`);
|
|
227
|
+
return value;
|
|
228
|
+
case 'maxRestartAttempts':
|
|
229
|
+
if (!Number.isInteger(value) || value < 0 || value > 100) {
|
|
230
|
+
fail('maxRestartAttempts must be an integer from 0 to 100.');
|
|
231
|
+
}
|
|
232
|
+
return value;
|
|
233
|
+
case 'restartBaseDelayMs':
|
|
234
|
+
if (!Number.isInteger(value) || value < 100 || value > 60_000) {
|
|
235
|
+
fail('restartBaseDelayMs must be from 100 to 60000.');
|
|
236
|
+
}
|
|
237
|
+
return value;
|
|
238
|
+
case 'restartMaxDelayMs':
|
|
239
|
+
if (!Number.isInteger(value) || value < 1_000 || value > 600_000) {
|
|
240
|
+
fail('restartMaxDelayMs must be from 1000 to 600000.');
|
|
241
|
+
}
|
|
242
|
+
return value;
|
|
243
|
+
case 'extraTargets':
|
|
244
|
+
try {
|
|
245
|
+
return normalizeServeTargets(value);
|
|
246
|
+
} catch (error) {
|
|
247
|
+
fail(error instanceof Error ? error.message : String(error));
|
|
248
|
+
}
|
|
249
|
+
return value;
|
|
250
|
+
default:
|
|
251
|
+
return value;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* @param {unknown} a - left value.
|
|
257
|
+
* @param {unknown} b - right value.
|
|
258
|
+
* @returns {boolean} structural equality for settings values.
|
|
259
|
+
*/
|
|
260
|
+
function same(a, b) {
|
|
261
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
262
|
+
}
|