tailcat-webui 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/NOTICE +23 -0
- package/README.en.md +104 -0
- package/README.md +93 -2
- package/bin/tailcat-webui.js +12 -0
- package/docs/CORE-API.md +343 -0
- package/docs/DESIGN.md +378 -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/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 +669 -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/boot.js +29 -0
- package/web/components.js +504 -0
- package/web/dict.js +655 -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.svg +11 -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 +416 -0
- package/web/room-settings.js +71 -0
- package/web/store.js +168 -0
- package/web/styles.css +2741 -0
- package/web/ticket.js +212 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drawer: auto-start, crash restarts, and the retry backoff bounds.
|
|
3
|
+
*
|
|
4
|
+
* These four runtime numbers are read when the supervisor schedules a restart;
|
|
5
|
+
* saving them never restarts a running tunnel.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { createDrawer, draftApi } from './drawer.js';
|
|
9
|
+
import {
|
|
10
|
+
h,
|
|
11
|
+
field,
|
|
12
|
+
textInput,
|
|
13
|
+
switchControl,
|
|
14
|
+
setValue,
|
|
15
|
+
setSwitchState,
|
|
16
|
+
setOverride,
|
|
17
|
+
} from './components.js';
|
|
18
|
+
import { settingsOf, failureText } from './format.js';
|
|
19
|
+
|
|
20
|
+
const KEYS = ['autoStart', 'restartEnabled', 'maxRestartAttempts', 'restartBaseDelayMs', 'restartMaxDelayMs'];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {object} ctx - shell context.
|
|
24
|
+
* @returns {{ el: HTMLElement, update: (state: object | null) => void }} drawer.
|
|
25
|
+
*/
|
|
26
|
+
export function createAutomationDrawer(ctx) {
|
|
27
|
+
const t = ctx.t;
|
|
28
|
+
const drawer = createDrawer(ctx, { id: 'automation', testid: 'drawer-automation', title: t('drawerAutomation') });
|
|
29
|
+
const draft = draftApi(drawer.ui, KEYS);
|
|
30
|
+
|
|
31
|
+
const autoStartSwitch = switchControl({
|
|
32
|
+
checked: true,
|
|
33
|
+
label: t('automationAutoStart'),
|
|
34
|
+
testid: 'switch-autostart',
|
|
35
|
+
onChange: (next) => {
|
|
36
|
+
draft.set('autoStart', next);
|
|
37
|
+
drawer.clearFeedback();
|
|
38
|
+
update(ctx.state());
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
const autoStartField = field({
|
|
42
|
+
label: t('automationAutoStart'),
|
|
43
|
+
hint: t('automationAutoStartHint'),
|
|
44
|
+
override: t('overrideDot'),
|
|
45
|
+
inline: true,
|
|
46
|
+
control: autoStartSwitch,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const restartSwitch = switchControl({
|
|
50
|
+
checked: true,
|
|
51
|
+
label: t('automationRestart'),
|
|
52
|
+
testid: 'switch-restart',
|
|
53
|
+
onChange: (next) => {
|
|
54
|
+
draft.set('restartEnabled', next);
|
|
55
|
+
drawer.clearFeedback();
|
|
56
|
+
update(ctx.state());
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
const restartField = field({
|
|
60
|
+
label: t('automationRestart'),
|
|
61
|
+
override: t('overrideDot'),
|
|
62
|
+
inline: true,
|
|
63
|
+
control: restartSwitch,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @param {string} key - settings key.
|
|
68
|
+
* @param {string} labelKey - label copy.
|
|
69
|
+
* @returns {{ row: HTMLElement, input: HTMLInputElement }} number field.
|
|
70
|
+
*/
|
|
71
|
+
function numberField(key, labelKey, min, max, step) {
|
|
72
|
+
const input = textInput({
|
|
73
|
+
value: '',
|
|
74
|
+
ariaLabel: t(labelKey),
|
|
75
|
+
type: 'number',
|
|
76
|
+
min,
|
|
77
|
+
max,
|
|
78
|
+
step,
|
|
79
|
+
inputMode: 'numeric',
|
|
80
|
+
testid: `number-${key}`,
|
|
81
|
+
onInput: (value) => {
|
|
82
|
+
draft.set(key, value);
|
|
83
|
+
drawer.clearFeedback();
|
|
84
|
+
update(ctx.state());
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
const row = field({ label: t(labelKey), override: t('overrideDot'), control: input });
|
|
88
|
+
return { row, input };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const attempts = numberField('maxRestartAttempts', 'automationAttempts', 0, 100, 1);
|
|
92
|
+
const baseDelay = numberField('restartBaseDelayMs', 'automationBaseDelay', 100, 60_000, 100);
|
|
93
|
+
const maxDelay = numberField('restartMaxDelayMs', 'automationMaxDelay', 1_000, 600_000, 1_000);
|
|
94
|
+
|
|
95
|
+
drawer.body.append(
|
|
96
|
+
autoStartField,
|
|
97
|
+
restartField,
|
|
98
|
+
attempts.row,
|
|
99
|
+
baseDelay.row,
|
|
100
|
+
maxDelay.row,
|
|
101
|
+
h('p', { class: 'field-hint' }, t('automationNote')));
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @param {unknown} value - draft value.
|
|
105
|
+
* @returns {number} integer, or NaN.
|
|
106
|
+
*/
|
|
107
|
+
function toInteger(value) {
|
|
108
|
+
if (typeof value === 'number') return value;
|
|
109
|
+
if (typeof value === 'string' && value.trim() !== '') return Number(value.trim());
|
|
110
|
+
return Number.NaN;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @param {string} key - settings key.
|
|
115
|
+
* @param {number} min - lower bound.
|
|
116
|
+
* @param {number} max - upper bound.
|
|
117
|
+
* @param {string} message - validation message.
|
|
118
|
+
* @returns {{ value?: number, error?: string }} parsed value or message.
|
|
119
|
+
*/
|
|
120
|
+
function integerIn(key, min, max, message) {
|
|
121
|
+
const value = toInteger(draft.value(key));
|
|
122
|
+
if (!Number.isInteger(value) || value < min || value > max) return { error: message };
|
|
123
|
+
return { value };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function save() {
|
|
127
|
+
const patch = {};
|
|
128
|
+
if (draft.isDirty('autoStart')) patch.autoStart = draft.value('autoStart');
|
|
129
|
+
if (draft.isDirty('restartEnabled')) patch.restartEnabled = draft.value('restartEnabled');
|
|
130
|
+
if (draft.isDirty('maxRestartAttempts')) {
|
|
131
|
+
const parsed = integerIn('maxRestartAttempts', 0, 100, t('automationAttemptsRange'));
|
|
132
|
+
if (parsed.error) {
|
|
133
|
+
drawer.setFeedback('error', parsed.error);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
patch.maxRestartAttempts = parsed.value;
|
|
137
|
+
}
|
|
138
|
+
if (draft.isDirty('restartBaseDelayMs')) {
|
|
139
|
+
const parsed = integerIn('restartBaseDelayMs', 100, 60_000, t('automationBaseRange'));
|
|
140
|
+
if (parsed.error) {
|
|
141
|
+
drawer.setFeedback('error', parsed.error);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
patch.restartBaseDelayMs = parsed.value;
|
|
145
|
+
}
|
|
146
|
+
if (draft.isDirty('restartMaxDelayMs')) {
|
|
147
|
+
const parsed = integerIn('restartMaxDelayMs', 1_000, 600_000, t('automationMaxRange'));
|
|
148
|
+
if (parsed.error) {
|
|
149
|
+
drawer.setFeedback('error', parsed.error);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
patch.restartMaxDelayMs = parsed.value;
|
|
153
|
+
}
|
|
154
|
+
const nextBase = patch.restartBaseDelayMs ?? toInteger(draft.value('restartBaseDelayMs'));
|
|
155
|
+
const nextMax = patch.restartMaxDelayMs ?? toInteger(draft.value('restartMaxDelayMs'));
|
|
156
|
+
if (Number.isFinite(nextBase) && Number.isFinite(nextMax) && nextBase > nextMax) {
|
|
157
|
+
drawer.setFeedback('error', t('automationDelayOrder'));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (Object.keys(patch).length === 0) return;
|
|
161
|
+
const result = await ctx.act('settings', { settings: patch });
|
|
162
|
+
if (!result.ok) {
|
|
163
|
+
drawer.setFeedback('error', failureText(result.error, t, 'saveFailed'));
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
draft.clear();
|
|
167
|
+
drawer.setFeedback('ok', t('saved'));
|
|
168
|
+
update(ctx.state());
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function reset() {
|
|
172
|
+
const state = ctx.state();
|
|
173
|
+
draft.reset(settingsOf(state));
|
|
174
|
+
drawer.clearFeedback();
|
|
175
|
+
update(state);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
drawer.onSave(() => {
|
|
179
|
+
void save();
|
|
180
|
+
});
|
|
181
|
+
drawer.onReset(reset);
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* @param {object | null} state - latest snapshot.
|
|
185
|
+
*/
|
|
186
|
+
function update(state) {
|
|
187
|
+
if (!state) return;
|
|
188
|
+
const settings = settingsOf(state);
|
|
189
|
+
draft.sync(state);
|
|
190
|
+
|
|
191
|
+
const autoStart = draft.value('autoStart') !== false;
|
|
192
|
+
const restartEnabled = draft.value('restartEnabled') !== false;
|
|
193
|
+
setSwitchState(autoStartSwitch, autoStart, false);
|
|
194
|
+
setSwitchState(restartSwitch, restartEnabled, false);
|
|
195
|
+
setValue(attempts.input, draft.value('maxRestartAttempts') ?? settings.maxRestartAttempts ?? '');
|
|
196
|
+
setValue(baseDelay.input, draft.value('restartBaseDelayMs') ?? settings.restartBaseDelayMs ?? '');
|
|
197
|
+
setValue(maxDelay.input, draft.value('restartMaxDelayMs') ?? settings.restartMaxDelayMs ?? '');
|
|
198
|
+
|
|
199
|
+
const sources = settings.sources && typeof settings.sources === 'object' ? settings.sources : {};
|
|
200
|
+
setOverride(autoStartField, sources.autoStart === 'panel');
|
|
201
|
+
setOverride(restartField, sources.restartEnabled === 'panel');
|
|
202
|
+
setOverride(attempts.row, sources.maxRestartAttempts === 'panel');
|
|
203
|
+
setOverride(baseDelay.row, sources.restartBaseDelayMs === 'panel');
|
|
204
|
+
setOverride(maxDelay.row, sources.restartMaxDelayMs === 'panel');
|
|
205
|
+
|
|
206
|
+
const autoLabel = autoStart ? t('summaryAutoOn') : t('summaryAutoOff');
|
|
207
|
+
const restartLabel = restartEnabled ? t('summaryRestartOn') : t('summaryRestartOff');
|
|
208
|
+
const dirtyKeys = draft.dirtyKeys();
|
|
209
|
+
const overridden = KEYS.some((key) => sources[key] === 'panel');
|
|
210
|
+
drawer.renderChrome({
|
|
211
|
+
summary: `${autoLabel}${t('summaryJoin')}${restartLabel}`,
|
|
212
|
+
overridden,
|
|
213
|
+
dirty: dirtyKeys.length > 0,
|
|
214
|
+
restart: false,
|
|
215
|
+
address: false,
|
|
216
|
+
busy: ctx.busy() === 'settings',
|
|
217
|
+
busyText: t('saving'),
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
update(ctx.state());
|
|
222
|
+
return { el: drawer.el, update, shell: drawer };
|
|
223
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drawer: the optional custom DERP map.
|
|
3
|
+
*
|
|
4
|
+
* The map is its own preference (`POST /api/action` action `derpMap`), not a
|
|
5
|
+
* field of the settings patch: saving it restarts a running tunnel but never
|
|
6
|
+
* regenerates the saved key.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { createDrawer } from './drawer.js';
|
|
10
|
+
import {
|
|
11
|
+
h,
|
|
12
|
+
field,
|
|
13
|
+
textInput,
|
|
14
|
+
alertBox,
|
|
15
|
+
codeBlock,
|
|
16
|
+
setValue,
|
|
17
|
+
setHidden,
|
|
18
|
+
syncSignature,
|
|
19
|
+
} from './components.js';
|
|
20
|
+
import { relayOf, failureText } from './format.js';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {object} ctx - shell context.
|
|
24
|
+
* @returns {{ el: HTMLElement, update: (state: object | null) => void }} drawer.
|
|
25
|
+
*/
|
|
26
|
+
export function createRelayDrawer(ctx) {
|
|
27
|
+
const t = ctx.t;
|
|
28
|
+
const drawer = createDrawer(ctx, { id: 'relay', testid: 'drawer-relay', title: t('drawerRelay') });
|
|
29
|
+
const local = drawer.ui.local;
|
|
30
|
+
if (typeof local.value !== 'string') local.value = '';
|
|
31
|
+
if (typeof local.dirty !== 'boolean') local.dirty = false;
|
|
32
|
+
|
|
33
|
+
const sourceNote = h('p', { class: 'field-hint', hidden: true }, t('relaySourceConfig'));
|
|
34
|
+
const insecureBox = h('div', { class: 'field-note', hidden: true }, alertBox({ tone: 'warn', children: t('relayInsecure') }));
|
|
35
|
+
const clientBlock = h('div', { class: 'field-block', hidden: true },
|
|
36
|
+
h('h4', { class: 'subhead' }, t('relayClientLabel')),
|
|
37
|
+
h('p', { class: 'field-hint' }, t('relayClientHint')),
|
|
38
|
+
h('div', { class: 'client-env' }));
|
|
39
|
+
const clientEnvEl = clientBlock.querySelector('.client-env');
|
|
40
|
+
|
|
41
|
+
const urlInput = textInput({
|
|
42
|
+
value: local.value,
|
|
43
|
+
ariaLabel: t('relayLabel'),
|
|
44
|
+
placeholder: t('relayPlaceholder'),
|
|
45
|
+
mono: true,
|
|
46
|
+
testid: 'relay-map-url',
|
|
47
|
+
onInput: (value) => {
|
|
48
|
+
local.value = value;
|
|
49
|
+
local.dirty = true;
|
|
50
|
+
drawer.clearFeedback();
|
|
51
|
+
update(ctx.state());
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const urlField = field({
|
|
56
|
+
label: t('relayLabel'),
|
|
57
|
+
hint: t('relayHint'),
|
|
58
|
+
override: t('overrideDot'),
|
|
59
|
+
control: urlInput,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
drawer.body.append(urlField, sourceNote, insecureBox, clientBlock);
|
|
63
|
+
|
|
64
|
+
async function save() {
|
|
65
|
+
if (!local.dirty) return;
|
|
66
|
+
const result = await ctx.act('derpMap', { derpMapUrl: local.value.trim() });
|
|
67
|
+
if (!result.ok) {
|
|
68
|
+
drawer.setFeedback('error', failureText(result.error, t, 'saveFailed'));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
local.dirty = false;
|
|
72
|
+
drawer.setFeedback('ok', t('relaySaved'));
|
|
73
|
+
update(ctx.state());
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function reset() {
|
|
77
|
+
const localState = ctx.state();
|
|
78
|
+
local.value = String(relayOf(localState).mapUrl ?? '');
|
|
79
|
+
local.dirty = false;
|
|
80
|
+
drawer.clearFeedback();
|
|
81
|
+
update(localState);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
drawer.onSave(() => {
|
|
85
|
+
void save();
|
|
86
|
+
});
|
|
87
|
+
drawer.onReset(reset);
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @param {object | null} state - latest snapshot.
|
|
91
|
+
*/
|
|
92
|
+
function update(state) {
|
|
93
|
+
if (!state) return;
|
|
94
|
+
const relay = relayOf(state);
|
|
95
|
+
const mapUrl = typeof relay.mapUrl === 'string' ? relay.mapUrl : '';
|
|
96
|
+
if (!local.dirty) local.value = mapUrl;
|
|
97
|
+
setValue(urlInput, local.value);
|
|
98
|
+
setHidden(sourceNote, relay.source !== 'config');
|
|
99
|
+
setHidden(insecureBox, relay.insecure !== true);
|
|
100
|
+
|
|
101
|
+
const needsClient = relay.custom === true && relay.needsClientMap === true;
|
|
102
|
+
setHidden(clientBlock, !needsClient);
|
|
103
|
+
const env = needsClient && typeof relay.clientEnv === 'string' ? relay.clientEnv : '';
|
|
104
|
+
syncSignature(clientEnvEl, env, () => (env ? [codeBlock({ text: env, t, testid: 'relay-client-env' })] : []));
|
|
105
|
+
|
|
106
|
+
const custom = relay.custom === true || mapUrl !== '';
|
|
107
|
+
const summary = custom
|
|
108
|
+
? relay.source === 'config'
|
|
109
|
+
? t('summaryRelayConfig')
|
|
110
|
+
: t('summaryRelayCustom')
|
|
111
|
+
: t('summaryRelayDefault');
|
|
112
|
+
const busy = ctx.busy() === 'derpMap';
|
|
113
|
+
drawer.renderChrome({
|
|
114
|
+
summary,
|
|
115
|
+
overridden: relay.source === 'panel',
|
|
116
|
+
dirty: local.dirty,
|
|
117
|
+
restart: local.dirty,
|
|
118
|
+
address: false,
|
|
119
|
+
busy,
|
|
120
|
+
busyText: t('relayBusy'),
|
|
121
|
+
});
|
|
122
|
+
const dot = urlField.overrideDot;
|
|
123
|
+
if (dot) dot.hidden = relay.source !== 'panel';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
update(ctx.state());
|
|
127
|
+
return { el: drawer.el, update, shell: drawer };
|
|
128
|
+
}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drawer: the tunnel identity and the user-maintained port mappings.
|
|
3
|
+
*
|
|
4
|
+
* `settings.extraTargets` is the complete serve list for the standalone tool:
|
|
5
|
+
* every row is one `virtual:target` mapping the tunnel exposes. At least one
|
|
6
|
+
* row is required before the tunnel can start, so the empty state explains it.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { createDrawer, draftApi } from './drawer.js';
|
|
10
|
+
import {
|
|
11
|
+
h,
|
|
12
|
+
field,
|
|
13
|
+
textInput,
|
|
14
|
+
selectControl,
|
|
15
|
+
button,
|
|
16
|
+
iconButton,
|
|
17
|
+
alertBox,
|
|
18
|
+
setValue,
|
|
19
|
+
setSelect,
|
|
20
|
+
setDisabled,
|
|
21
|
+
setOverride,
|
|
22
|
+
} from './components.js';
|
|
23
|
+
import { settingsOf, targetsOf, parseMapping, buildMapping, failureText } from './format.js';
|
|
24
|
+
|
|
25
|
+
const KEYS = ['addressMode', 'keyName', 'extraTargets'];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {object} ctx - shell context.
|
|
29
|
+
* @returns {{ el: HTMLElement, update: (state: object | null) => void }} drawer.
|
|
30
|
+
*/
|
|
31
|
+
export function createTunnelDrawer(ctx) {
|
|
32
|
+
const t = ctx.t;
|
|
33
|
+
const drawer = createDrawer(ctx, { id: 'tunnel', testid: 'drawer-tunnel', title: t('drawerTunnel') });
|
|
34
|
+
const draft = draftApi(drawer.ui, KEYS);
|
|
35
|
+
const local = drawer.ui.local;
|
|
36
|
+
if (local.rows === undefined) local.rows = null;
|
|
37
|
+
|
|
38
|
+
/** @type {{ node: HTMLElement, virtual: HTMLInputElement, target: HTMLInputElement }[]} */
|
|
39
|
+
const rowControls = [];
|
|
40
|
+
const rowsEl = h('div', { class: 'mappings', 'data-testid': 'tunnel-mappings' });
|
|
41
|
+
|
|
42
|
+
const modeControl = selectControl({
|
|
43
|
+
value: '',
|
|
44
|
+
options: [
|
|
45
|
+
['stable', t('tunnelModeStable')],
|
|
46
|
+
['ephemeral', t('tunnelModeEphemeral')],
|
|
47
|
+
],
|
|
48
|
+
ariaLabel: t('tunnelMode'),
|
|
49
|
+
onChange: (value) => {
|
|
50
|
+
draft.set('addressMode', value);
|
|
51
|
+
drawer.clearFeedback();
|
|
52
|
+
update(ctx.state());
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
const modeField = field({
|
|
56
|
+
label: t('tunnelMode'),
|
|
57
|
+
hint: t('tunnelModeHint'),
|
|
58
|
+
override: t('overrideDot'),
|
|
59
|
+
control: modeControl,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const keyNameInput = textInput({
|
|
63
|
+
value: '',
|
|
64
|
+
ariaLabel: t('tunnelKeyName'),
|
|
65
|
+
placeholder: 'tailcat-webui',
|
|
66
|
+
onInput: (value) => {
|
|
67
|
+
draft.set('keyName', value);
|
|
68
|
+
drawer.clearFeedback();
|
|
69
|
+
update(ctx.state());
|
|
70
|
+
},
|
|
71
|
+
testid: 'tunnel-key-name',
|
|
72
|
+
});
|
|
73
|
+
const keyNameField = field({
|
|
74
|
+
label: t('tunnelKeyName'),
|
|
75
|
+
hint: t('tunnelKeyNameHint'),
|
|
76
|
+
override: t('overrideDot'),
|
|
77
|
+
control: keyNameInput,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const emptyRows = h('p', { class: 'mapping-empty' }, t('tunnelMappingEmpty'));
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @param {{ virtual: string, target: string }} row - editable row.
|
|
84
|
+
* @param {number} index - row position.
|
|
85
|
+
* @returns {HTMLElement} one mapping row.
|
|
86
|
+
*/
|
|
87
|
+
function buildRow(row, index) {
|
|
88
|
+
const virtual = textInput({
|
|
89
|
+
value: row.virtual,
|
|
90
|
+
ariaLabel: t('tunnelMappingVirtual'),
|
|
91
|
+
placeholder: '13080',
|
|
92
|
+
inputMode: 'numeric',
|
|
93
|
+
onInput: (value) => {
|
|
94
|
+
local.rows[index].virtual = value;
|
|
95
|
+
draft.set('extraTargets', serializeRows());
|
|
96
|
+
drawer.clearFeedback();
|
|
97
|
+
update(ctx.state());
|
|
98
|
+
},
|
|
99
|
+
testid: `mapping-virtual-${index}`,
|
|
100
|
+
});
|
|
101
|
+
const target = textInput({
|
|
102
|
+
value: row.target,
|
|
103
|
+
ariaLabel: t('tunnelMappingTarget'),
|
|
104
|
+
placeholder: t('tunnelMappingTargetPlaceholder'),
|
|
105
|
+
onInput: (value) => {
|
|
106
|
+
local.rows[index].target = value;
|
|
107
|
+
draft.set('extraTargets', serializeRows());
|
|
108
|
+
drawer.clearFeedback();
|
|
109
|
+
update(ctx.state());
|
|
110
|
+
},
|
|
111
|
+
testid: `mapping-target-${index}`,
|
|
112
|
+
});
|
|
113
|
+
const remove = iconButton({
|
|
114
|
+
label: t('tunnelMappingRemove'),
|
|
115
|
+
onClick: () => {
|
|
116
|
+
local.rows.splice(index, 1);
|
|
117
|
+
draft.set('extraTargets', serializeRows());
|
|
118
|
+
drawer.clearFeedback();
|
|
119
|
+
rebuildRows();
|
|
120
|
+
update(ctx.state());
|
|
121
|
+
},
|
|
122
|
+
testid: `mapping-remove-${index}`,
|
|
123
|
+
});
|
|
124
|
+
const node = h('div', { class: 'mapping-row' },
|
|
125
|
+
virtual,
|
|
126
|
+
h('span', { class: 'mapping-colon', 'aria-hidden': 'true' }, ':'),
|
|
127
|
+
target,
|
|
128
|
+
remove);
|
|
129
|
+
rowControls[index] = { node, virtual, target };
|
|
130
|
+
return node;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function rebuildRows() {
|
|
134
|
+
rowControls.length = 0;
|
|
135
|
+
if (local.rows.length === 0) {
|
|
136
|
+
rowsEl.replaceChildren(emptyRows);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
rowsEl.replaceChildren(...local.rows.map((row, index) => buildRow(row, index)));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Refresh row inputs in place without taking focus from a field being edited.
|
|
144
|
+
*/
|
|
145
|
+
function paintRowValues() {
|
|
146
|
+
local.rows.forEach((row, index) => {
|
|
147
|
+
const controls = rowControls[index];
|
|
148
|
+
if (!controls) return;
|
|
149
|
+
setValue(controls.virtual, row.virtual);
|
|
150
|
+
setValue(controls.target, row.target);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @returns {string[]} canonical mappings, incomplete rows included as text.
|
|
156
|
+
*/
|
|
157
|
+
function serializeRows() {
|
|
158
|
+
return local.rows.map((row) => buildMapping(row.virtual, row.target));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const addButton = button({
|
|
162
|
+
label: t('tunnelMappingAdd'),
|
|
163
|
+
variant: 'secondary',
|
|
164
|
+
testid: 'tunnel-mapping-add',
|
|
165
|
+
onClick: () => {
|
|
166
|
+
local.rows.push({ virtual: '', target: '' });
|
|
167
|
+
draft.set('extraTargets', serializeRows());
|
|
168
|
+
drawer.clearFeedback();
|
|
169
|
+
rebuildRows();
|
|
170
|
+
const controls = rowControls[local.rows.length - 1];
|
|
171
|
+
if (controls) controls.virtual.focus();
|
|
172
|
+
update(ctx.state());
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
const mappingsDot = h('span', {
|
|
177
|
+
class: 'override-dot',
|
|
178
|
+
role: 'img',
|
|
179
|
+
'aria-label': t('overrideDot'),
|
|
180
|
+
title: t('overrideDot'),
|
|
181
|
+
hidden: true,
|
|
182
|
+
});
|
|
183
|
+
const mappingsBlock = h('div', { class: 'field-block' },
|
|
184
|
+
h('h4', { class: 'subhead' }, h('span', { class: 'field-label-line' }, mappingsDot, t('tunnelMappings'))),
|
|
185
|
+
h('p', { class: 'field-hint' }, t('tunnelMappingsHint')),
|
|
186
|
+
alertBox({ tone: 'warn', children: t('tunnelMappingsWarning') }),
|
|
187
|
+
rowsEl,
|
|
188
|
+
addButton);
|
|
189
|
+
|
|
190
|
+
drawer.body.append(modeField, keyNameField, mappingsBlock);
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* @param {{ virtual: string, target: string }[]} rows - editable rows.
|
|
194
|
+
* @returns {string | null} validation message, or null.
|
|
195
|
+
*/
|
|
196
|
+
function validate(rows) {
|
|
197
|
+
const seen = new Set();
|
|
198
|
+
for (const row of rows) {
|
|
199
|
+
const virtual = String(row.virtual).trim();
|
|
200
|
+
const target = String(row.target).trim();
|
|
201
|
+
if (virtual === '' && target === '') continue;
|
|
202
|
+
if (virtual === '' || target === '') return t('mappingIncomplete');
|
|
203
|
+
if (!/^\d+$/.test(virtual) || Number(virtual) < 1 || Number(virtual) > 65535) return t('mappingInvalidPort');
|
|
204
|
+
if (seen.has(virtual)) return t('mappingDuplicate');
|
|
205
|
+
seen.add(virtual);
|
|
206
|
+
}
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function save() {
|
|
211
|
+
const patch = {};
|
|
212
|
+
if (draft.isDirty('addressMode')) patch.addressMode = draft.value('addressMode');
|
|
213
|
+
if (draft.isDirty('keyName')) {
|
|
214
|
+
const keyName = String(draft.value('keyName') ?? '').trim();
|
|
215
|
+
if (keyName === '') {
|
|
216
|
+
drawer.setFeedback('error', t('keyNameRequired'));
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
patch.keyName = keyName;
|
|
220
|
+
}
|
|
221
|
+
if (draft.isDirty('extraTargets')) {
|
|
222
|
+
const message = validate(local.rows);
|
|
223
|
+
if (message) {
|
|
224
|
+
drawer.setFeedback('error', message);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
patch.extraTargets = local.rows
|
|
228
|
+
.map((row) => ({ virtual: String(row.virtual).trim(), target: String(row.target).trim() }))
|
|
229
|
+
.filter((row) => row.virtual !== '' && row.target !== '')
|
|
230
|
+
.map((row) => buildMapping(row.virtual, row.target));
|
|
231
|
+
}
|
|
232
|
+
if (Object.keys(patch).length === 0) return;
|
|
233
|
+
const result = await ctx.act('settings', { settings: patch });
|
|
234
|
+
if (!result.ok) {
|
|
235
|
+
drawer.setFeedback('error', failureText(result.error, t, 'saveFailed'));
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
draft.clear();
|
|
239
|
+
drawer.setFeedback('ok', t('saved'));
|
|
240
|
+
update(ctx.state());
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function reset() {
|
|
244
|
+
const state = ctx.state();
|
|
245
|
+
local.rows = targetsOf(state).map(parseMapping);
|
|
246
|
+
rebuildRows();
|
|
247
|
+
draft.reset(settingsOf(state));
|
|
248
|
+
drawer.clearFeedback();
|
|
249
|
+
update(state);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
drawer.onSave(() => {
|
|
253
|
+
void save();
|
|
254
|
+
});
|
|
255
|
+
drawer.onReset(reset);
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* @param {object | null} state - latest snapshot.
|
|
259
|
+
*/
|
|
260
|
+
function update(state) {
|
|
261
|
+
if (!state) return;
|
|
262
|
+
const settings = settingsOf(state);
|
|
263
|
+
draft.sync(state);
|
|
264
|
+
if (local.rows === null) local.rows = targetsOf(state).map(parseMapping);
|
|
265
|
+
if (!draft.isDirty('extraTargets')) {
|
|
266
|
+
const next = targetsOf(state).map(parseMapping);
|
|
267
|
+
const same = JSON.stringify(next) === JSON.stringify(local.rows);
|
|
268
|
+
if (!same) {
|
|
269
|
+
local.rows = next;
|
|
270
|
+
const editing = rowsEl.contains(document.activeElement);
|
|
271
|
+
if (editing && rowControls.length === local.rows.length) paintRowValues();
|
|
272
|
+
else rebuildRows();
|
|
273
|
+
} else if (rowControls.length === 0) {
|
|
274
|
+
rebuildRows();
|
|
275
|
+
} else if (!rowsEl.contains(document.activeElement)) {
|
|
276
|
+
paintRowValues();
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const mode = draft.value('addressMode') ?? settings.addressMode ?? 'stable';
|
|
281
|
+
setSelect(modeControl.selectEl, mode);
|
|
282
|
+
setValue(keyNameInput, draft.value('keyName') ?? settings.keyName ?? '');
|
|
283
|
+
setDisabled(keyNameInput, mode === 'ephemeral');
|
|
284
|
+
|
|
285
|
+
const keyName = draft.value('keyName') ?? settings.keyName ?? '';
|
|
286
|
+
const count = local.rows.length;
|
|
287
|
+
const modeLabel = mode === 'ephemeral' ? t('summaryTunnelEphemeral') : t('summaryTunnelKey', { key: keyName });
|
|
288
|
+
const countLabel = count > 0 ? t('mappingCount', { n: count }) : t('mappingCountZero');
|
|
289
|
+
const sources = settings.sources && typeof settings.sources === 'object' ? settings.sources : {};
|
|
290
|
+
const overridden = KEYS.some((key) => sources[key] === 'panel');
|
|
291
|
+
setOverride(modeField, sources.addressMode === 'panel');
|
|
292
|
+
setOverride(keyNameField, sources.keyName === 'panel');
|
|
293
|
+
if (mappingsDot.hidden === (sources.extraTargets === 'panel')) mappingsDot.hidden = sources.extraTargets !== 'panel';
|
|
294
|
+
const dirtyKeys = draft.dirtyKeys();
|
|
295
|
+
const busy = ctx.busy() === 'settings';
|
|
296
|
+
drawer.renderChrome({
|
|
297
|
+
summary: `${modeLabel}${t('summaryJoin')}${countLabel}`,
|
|
298
|
+
overridden,
|
|
299
|
+
dirty: dirtyKeys.length > 0,
|
|
300
|
+
restart: dirtyKeys.length > 0,
|
|
301
|
+
address: dirtyKeys.includes('addressMode') || dirtyKeys.includes('keyName'),
|
|
302
|
+
busy,
|
|
303
|
+
busyText: t('saving'),
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
update(ctx.state());
|
|
308
|
+
return { el: drawer.el, update, shell: drawer };
|
|
309
|
+
}
|