@myrialabs/clopen 0.2.14 → 0.2.16

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.
Files changed (51) hide show
  1. package/backend/engine/adapters/opencode/server.ts +72 -33
  2. package/backend/snapshot/helpers.ts +8 -0
  3. package/backend/tunnel/cloudflared/binary.ts +153 -0
  4. package/backend/tunnel/cloudflared/index.ts +21 -0
  5. package/backend/tunnel/cloudflared/tunnel.ts +389 -0
  6. package/backend/tunnel/global-tunnel-manager.ts +538 -152
  7. package/backend/tunnel/project-tunnel-manager.ts +6 -14
  8. package/backend/tunnel/tunnel-config.ts +234 -0
  9. package/backend/ws/tunnel/config.ts +108 -0
  10. package/backend/ws/tunnel/index.ts +3 -1
  11. package/backend/ws/tunnel/operations.ts +290 -43
  12. package/bin/clopen.ts +1 -1
  13. package/bun.lock +0 -4
  14. package/frontend/App.svelte +2 -1
  15. package/frontend/components/chat/ChatInterface.svelte +6 -1
  16. package/frontend/components/chat/formatters/Tools.svelte +3 -1
  17. package/frontend/components/chat/input/components/EngineModelPicker.svelte +12 -1
  18. package/frontend/components/chat/message/ChatMessage.svelte +0 -29
  19. package/frontend/components/chat/message/ChatMessages.svelte +2 -2
  20. package/frontend/components/chat/message/MessageBubble.svelte +0 -6
  21. package/frontend/components/chat/message/MessageHeader.svelte +0 -16
  22. package/frontend/components/chat/tools/SkillTool.svelte +21 -0
  23. package/frontend/components/chat/tools/index.ts +1 -0
  24. package/frontend/components/chat/widgets/ContextIndicator.svelte +186 -0
  25. package/frontend/components/history/HistoryModal.svelte +3 -3
  26. package/frontend/components/settings/SettingsModal.svelte +5 -0
  27. package/frontend/components/settings/SettingsView.svelte +4 -0
  28. package/frontend/components/settings/model/EngineModelPicker.svelte +9 -0
  29. package/frontend/components/settings/tunnel/TunnelSettings.svelte +1060 -0
  30. package/frontend/components/tunnel/TunnelActive.svelte +132 -71
  31. package/frontend/components/tunnel/TunnelButton.svelte +4 -4
  32. package/frontend/components/tunnel/TunnelInactive.svelte +38 -65
  33. package/frontend/components/tunnel/TunnelModal.svelte +55 -2
  34. package/frontend/components/workspace/PanelHeader.svelte +4 -0
  35. package/frontend/components/workspace/WorkspaceLayout.svelte +31 -1
  36. package/frontend/components/workspace/panels/ChatPanel.svelte +6 -0
  37. package/frontend/services/favicon.service.ts +103 -0
  38. package/frontend/services/terminal/project.service.ts +52 -0
  39. package/frontend/stores/features/tunnel-config.svelte.ts +205 -0
  40. package/frontend/stores/features/tunnel.svelte.ts +194 -80
  41. package/frontend/stores/ui/settings-modal.svelte.ts +7 -0
  42. package/frontend/utils/chat/message-grouper.ts +33 -5
  43. package/frontend/utils/chat/message-processor.ts +2 -1
  44. package/frontend/utils/chat/tool-handler.ts +38 -3
  45. package/frontend/utils/context-manager.ts +35 -237
  46. package/package.json +1 -2
  47. package/shared/types/messaging/index.ts +1 -0
  48. package/shared/types/messaging/tool.ts +24 -1
  49. package/shared/utils/ws-client.ts +23 -1
  50. package/frontend/components/chat/modal/TokenUsageModal.svelte +0 -124
  51. package/frontend/components/chat/widgets/TokenUsage.svelte +0 -78
@@ -0,0 +1,1060 @@
1
+ <script lang="ts">
2
+ import { onMount, onDestroy } from 'svelte';
3
+ import Icon from '../../common/display/Icon.svelte';
4
+ import Dialog from '../../common/overlay/Dialog.svelte';
5
+ import TunnelQRCode from '../../tunnel/TunnelQRCode.svelte';
6
+ import { tunnelConfigStore, type RemoteConfigItem, type LocalConfigItem } from '$frontend/stores/features/tunnel-config.svelte';
7
+ import { tunnelStore } from '$frontend/stores/features/tunnel.svelte';
8
+ import { addNotification } from '$frontend/stores/ui/notification.svelte';
9
+ import { debug } from '$shared/utils/logger';
10
+ import ws from '$frontend/utils/ws';
11
+
12
+ // --- Remote state ---
13
+ let remoteLabel = $state('');
14
+ let remoteToken = $state('');
15
+ let showRemoteToken = $state(false);
16
+ let showRemoteAddForm = $state(false);
17
+ let remoteRemoveTargetId = $state<string | null>(null);
18
+ let removingRemoteId = $state<string | null>(null);
19
+
20
+ // --- Local state ---
21
+ let localName = $state('');
22
+ let showLocalCreateForm = $state(false);
23
+ let localDeleteTargetId = $state<string | null>(null);
24
+ let deletingLocalId = $state<string | null>(null);
25
+
26
+ // Local auth
27
+ type LoginStep = 'idle' | 'loading' | 'waiting-url' | 'zone-prompt' | 'success' | 'error';
28
+ let loginStep = $state<LoginStep>('idle');
29
+ let loginUrl = $state<string | null>(null);
30
+ let loginError = $state<string | null>(null);
31
+ let loginUrlCopied = $state(false);
32
+ let isAuthenticated = $state(false);
33
+ let authorizedZone = $state<string | null>(null);
34
+ let zoneInput = $state('');
35
+
36
+ // Copy & QR
37
+ let copiedUrl = $state<string | null>(null);
38
+ let qrUrl = $state('');
39
+
40
+ // Local domain add
41
+ let domainConfigId = $state<string | null>(null);
42
+ let domainSubdomain = $state('');
43
+ let domainService = $state('');
44
+ let isAddingDomain = $state(false);
45
+
46
+ // Derived
47
+ const remotes = $derived(tunnelConfigStore.remotes);
48
+ const locals = $derived(tunnelConfigStore.locals);
49
+ const isSaving = $derived(tunnelConfigStore.isSaving);
50
+ const storeError = $derived(tunnelConfigStore.error);
51
+
52
+ // Event listener cleanup
53
+ const cleanups: Array<() => void> = [];
54
+
55
+ /** Sync isActive flags on config store based on actual tunnel status from backend */
56
+ async function syncActiveStates() {
57
+ try {
58
+ await tunnelStore.checkStatus();
59
+ const activeTunnels = tunnelStore.tunnels;
60
+
61
+ // Sync remote active states
62
+ for (const remote of tunnelConfigStore.remotes) {
63
+ const isRunning = activeTunnels.some((t) => t.configId === remote.id && t.type === 'remote');
64
+ tunnelConfigStore.setRemoteActive(remote.id, isRunning);
65
+ }
66
+
67
+ // Sync local active states
68
+ for (const local of tunnelConfigStore.locals) {
69
+ const isRunning = activeTunnels.some((t) => t.configId === local.id && t.type === 'local');
70
+ tunnelConfigStore.setLocalActive(local.id, isRunning);
71
+ }
72
+ } catch {
73
+ // Status check failed
74
+ }
75
+ }
76
+
77
+ onMount(async () => {
78
+ // Listen for login events
79
+ cleanups.push(
80
+ ws.on('tunnel:local:login-url', (data: { url: string }) => {
81
+ loginUrl = data.url;
82
+ loginStep = 'waiting-url';
83
+ }),
84
+ ws.on('tunnel:local:login-complete', () => {
85
+ loginStep = 'zone-prompt';
86
+ isAuthenticated = true;
87
+ }),
88
+ ws.on('tunnel:local:login-error', (data: { message: string }) => {
89
+ loginError = data.message;
90
+ loginStep = 'error';
91
+ }),
92
+ ws.on('tunnel:remote:ingress-update', (data: { configId: string; ingress: Array<{ hostname?: string; service: string }> }) => {
93
+ tunnelConfigStore.updateRemoteIngress(data.configId, data.ingress);
94
+ }),
95
+ ws.on('tunnel:status-changed', (_data: { tunnels: Array<{ configId?: string; type: string }> }) => {
96
+ // Re-sync active states when tunnel status changes
97
+ syncActiveStates();
98
+ })
99
+ );
100
+
101
+ // Load configs and check auth
102
+ await tunnelConfigStore.refresh();
103
+ try {
104
+ const authResult = await tunnelStore.checkAuth();
105
+ isAuthenticated = authResult.authenticated;
106
+ authorizedZone = authResult.zone;
107
+ } catch {
108
+ // Auth check failed
109
+ }
110
+
111
+ // Sync active states with actual backend status
112
+ await syncActiveStates();
113
+ });
114
+
115
+ onDestroy(() => {
116
+ for (const cleanup of cleanups) cleanup();
117
+ cleanups.length = 0;
118
+
119
+ if (loginStep === 'loading' || loginStep === 'waiting-url') {
120
+ tunnelStore.cancelLogin();
121
+ }
122
+ });
123
+
124
+ // --- Remote handlers ---
125
+
126
+ async function handleAddRemote() {
127
+ if (!remoteLabel.trim() || !remoteToken.trim()) return;
128
+ try {
129
+ await tunnelConfigStore.addRemote(remoteLabel.trim(), remoteToken.trim());
130
+ remoteLabel = '';
131
+ remoteToken = '';
132
+ showRemoteToken = false;
133
+ showRemoteAddForm = false;
134
+ addNotification({ type: 'success', title: 'Added', message: 'Remote tunnel config added' });
135
+ } catch {
136
+ // Error handled by store
137
+ }
138
+ }
139
+
140
+ async function handleRemoveRemote() {
141
+ if (!remoteRemoveTargetId) return;
142
+ const id = remoteRemoveTargetId;
143
+ remoteRemoveTargetId = null;
144
+ removingRemoteId = id;
145
+ try {
146
+ await tunnelConfigStore.removeRemote(id);
147
+ addNotification({ type: 'success', title: 'Removed', message: 'Remote tunnel config removed' });
148
+ } catch {
149
+ // Error handled by store
150
+ }
151
+ removingRemoteId = null;
152
+ }
153
+
154
+ async function handleStartRemote(config: RemoteConfigItem) {
155
+ try {
156
+ await tunnelStore.startRemoteTunnel(config.id);
157
+ tunnelConfigStore.setRemoteActive(config.id, true);
158
+ addNotification({ type: 'success', title: 'Started', message: `Remote tunnel "${config.label}" started` });
159
+ } catch (error) {
160
+ tunnelConfigStore.setRemoteActive(config.id, false);
161
+ const msg = error instanceof Error ? error.message : 'Failed to start';
162
+ addNotification({ type: 'error', title: 'Error', message: msg });
163
+ }
164
+ }
165
+
166
+ async function handleStopRemote(config: RemoteConfigItem) {
167
+ try {
168
+ await tunnelStore.stopRemoteTunnel(config.id);
169
+ tunnelConfigStore.setRemoteActive(config.id, false);
170
+ addNotification({ type: 'success', title: 'Stopped', message: `Remote tunnel "${config.label}" stopped` });
171
+ } catch (error) {
172
+ const msg = error instanceof Error ? error.message : 'Failed to stop';
173
+ addNotification({ type: 'error', title: 'Error', message: msg });
174
+ }
175
+ }
176
+
177
+ // --- Local auth handlers ---
178
+
179
+ function startLogin() {
180
+ loginStep = 'loading';
181
+ loginUrl = null;
182
+ loginError = null;
183
+ tunnelStore.startLogin();
184
+ }
185
+
186
+ function cancelLogin() {
187
+ tunnelStore.cancelLogin();
188
+ loginStep = 'idle';
189
+ loginUrl = null;
190
+ }
191
+
192
+ async function copyLoginUrl() {
193
+ if (!loginUrl) return;
194
+ try {
195
+ await navigator.clipboard.writeText(loginUrl);
196
+ loginUrlCopied = true;
197
+ setTimeout(() => { loginUrlCopied = false; }, 2000);
198
+ } catch {
199
+ // Silently fail
200
+ }
201
+ }
202
+
203
+ async function handleSetZone() {
204
+ if (!zoneInput.trim()) return;
205
+ try {
206
+ await tunnelStore.setZone(zoneInput.trim());
207
+ authorizedZone = zoneInput.trim();
208
+ zoneInput = '';
209
+ loginStep = 'idle';
210
+ addNotification({ type: 'success', title: 'Authenticated', message: `Cloudflare login successful for ${authorizedZone}` });
211
+ } catch {
212
+ addNotification({ type: 'error', title: 'Error', message: 'Failed to save zone' });
213
+ }
214
+ }
215
+
216
+ async function handleLogout() {
217
+ try {
218
+ await tunnelStore.logout();
219
+ isAuthenticated = false;
220
+ authorizedZone = null;
221
+ loginStep = 'idle';
222
+ addNotification({ type: 'info', title: 'Logged out', message: 'Cloudflare credentials removed' });
223
+ } catch {
224
+ addNotification({ type: 'error', title: 'Error', message: 'Failed to logout' });
225
+ }
226
+ }
227
+
228
+ // --- Local tunnel handlers ---
229
+
230
+ async function handleCreateLocal() {
231
+ if (!localName.trim()) return;
232
+ try {
233
+ await tunnelConfigStore.createLocal(localName.trim());
234
+ localName = '';
235
+ showLocalCreateForm = false;
236
+ addNotification({ type: 'success', title: 'Created', message: 'Local tunnel created' });
237
+ } catch {
238
+ // Error handled by store
239
+ }
240
+ }
241
+
242
+ async function handleDeleteLocal() {
243
+ if (!localDeleteTargetId) return;
244
+ const id = localDeleteTargetId;
245
+ localDeleteTargetId = null;
246
+ deletingLocalId = id;
247
+ try {
248
+ await tunnelConfigStore.deleteLocal(id);
249
+ addNotification({ type: 'success', title: 'Deleted', message: 'Local tunnel deleted' });
250
+ } catch {
251
+ // Error handled by store
252
+ }
253
+ deletingLocalId = null;
254
+ }
255
+
256
+ async function handleStartLocal(config: LocalConfigItem) {
257
+ try {
258
+ await tunnelStore.startLocalTunnel(config.id);
259
+ tunnelConfigStore.setLocalActive(config.id, true);
260
+ addNotification({ type: 'success', title: 'Started', message: `Local tunnel "${config.name}" started` });
261
+ } catch (error) {
262
+ tunnelConfigStore.setLocalActive(config.id, false);
263
+ const msg = error instanceof Error ? error.message : 'Failed to start';
264
+ addNotification({ type: 'error', title: 'Error', message: msg });
265
+ }
266
+ }
267
+
268
+ async function handleStopLocal(config: LocalConfigItem) {
269
+ try {
270
+ await tunnelStore.stopLocalTunnel(config.id);
271
+ tunnelConfigStore.setLocalActive(config.id, false);
272
+ addNotification({ type: 'success', title: 'Stopped', message: `Local tunnel "${config.name}" stopped` });
273
+ } catch (error) {
274
+ const msg = error instanceof Error ? error.message : 'Failed to stop';
275
+ addNotification({ type: 'error', title: 'Error', message: msg });
276
+ }
277
+ }
278
+
279
+ async function handleAddDomain(configId: string) {
280
+ if (!domainSubdomain.trim() || !domainService.trim() || !authorizedZone) return;
281
+ const hostname = `${domainSubdomain.trim()}.${authorizedZone}`;
282
+ isAddingDomain = true;
283
+ try {
284
+ const result = await tunnelConfigStore.addIngress(configId, hostname, domainService.trim());
285
+ domainSubdomain = '';
286
+ domainService = '';
287
+ domainConfigId = null;
288
+ if (result.dnsRouted) {
289
+ addNotification({ type: 'success', title: 'Added', message: `Subdomain ${hostname} added and DNS routed` });
290
+ } else if (result.dnsError) {
291
+ addNotification({ type: 'warning', title: 'Added', message: `Subdomain ${hostname} added, but DNS routing failed: ${result.dnsError}` });
292
+ } else {
293
+ addNotification({ type: 'success', title: 'Added', message: `Subdomain ${hostname} added` });
294
+ }
295
+ } catch {
296
+ // Error handled by store
297
+ }
298
+ isAddingDomain = false;
299
+ }
300
+
301
+ async function handleRemoveDomain(configId: string, hostname: string) {
302
+ try {
303
+ await tunnelConfigStore.removeIngress(configId, hostname);
304
+ } catch {
305
+ // Error handled by store
306
+ }
307
+ }
308
+
309
+ async function copyUrl(url: string) {
310
+ try {
311
+ await navigator.clipboard.writeText(url);
312
+ copiedUrl = url;
313
+ addNotification({ type: 'success', title: 'Copied', message: 'URL copied to clipboard' });
314
+ setTimeout(() => { copiedUrl = null; }, 2000);
315
+ } catch (error) {
316
+ debug.error('tunnel', 'Failed to copy:', error);
317
+ addNotification({ type: 'error', title: 'Error', message: 'Failed to copy URL' });
318
+ }
319
+ }
320
+
321
+ function getRemoteLoadingState(configId: string) {
322
+ return tunnelStore.getLoadingState(configId);
323
+ }
324
+
325
+ function getLocalLoadingState(configId: string) {
326
+ return tunnelStore.getLoadingState(configId);
327
+ }
328
+ </script>
329
+
330
+ <div class="py-1">
331
+ <h3 class="text-base font-bold text-slate-900 dark:text-slate-100 mb-1.5">Tunnel</h3>
332
+ <p class="text-sm text-slate-600 dark:text-slate-500 mb-5">Cloudflare tunnel services for persistent domains</p>
333
+
334
+ <div class="space-y-6">
335
+ <!-- ========== REMOTELY-MANAGED ========== -->
336
+ <div class="rounded-xl border border-slate-200 dark:border-slate-700/50 bg-white dark:bg-slate-800/50 overflow-hidden">
337
+ <!-- Header -->
338
+ <div class="flex items-center justify-between px-5 py-4 border-b border-slate-200 dark:border-slate-700/50">
339
+ <div class="flex items-center gap-3">
340
+ <div class="flex items-center justify-center w-9 h-9 rounded-lg bg-orange-500/10 dark:bg-orange-500/15">
341
+ <Icon name="lucide:cloud" class="w-5 h-5 text-orange-600 dark:text-orange-400" />
342
+ </div>
343
+ <div>
344
+ <h4 class="font-semibold text-slate-900 dark:text-slate-100">Remotely-Managed</h4>
345
+ <p class="text-xs text-slate-500 dark:text-slate-400">Tunnels configured via Cloudflare Dashboard</p>
346
+ </div>
347
+ </div>
348
+
349
+ <div class="flex items-center gap-2">
350
+ {#if remotes.length > 0}
351
+ <span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium bg-orange-100 dark:bg-orange-900/30 text-orange-700 dark:text-orange-400">
352
+ {remotes.length} configured
353
+ </span>
354
+ {/if}
355
+ <a
356
+ href="https://dash.cloudflare.com/?to=/:account/tunnels"
357
+ target="_blank"
358
+ rel="noopener noreferrer"
359
+ class="flex items-center gap-1 px-2 py-1 text-xs font-medium rounded text-slate-500 dark:text-slate-400 hover:text-orange-600 dark:hover:text-orange-400 hover:bg-orange-50 dark:hover:bg-orange-900/20 transition-colors"
360
+ title="Open Cloudflare Dashboard"
361
+ >
362
+ <Icon name="lucide:external-link" class="w-3.5 h-3.5" />
363
+ Dashboard
364
+ </a>
365
+ </div>
366
+ </div>
367
+
368
+ <!-- Body -->
369
+ <div class="px-5 py-4 space-y-4">
370
+ <!-- Existing remote configs -->
371
+ {#if remotes.length > 0}
372
+ <div class="space-y-2">
373
+ {#each remotes as config (config.id)}
374
+ {@const loadState = getRemoteLoadingState(config.id)}
375
+ <div class="p-3 rounded-lg bg-slate-50 dark:bg-slate-800/80 border border-slate-200 dark:border-slate-700/50">
376
+ <div class="flex items-center justify-between gap-3">
377
+ <div class="flex items-center gap-2.5 min-w-0">
378
+ <div class="w-2 h-2 rounded-full {config.isActive ? 'bg-green-500' : 'bg-slate-300 dark:bg-slate-600'}"></div>
379
+ <span class="text-sm font-semibold text-slate-900 dark:text-slate-100 truncate">{config.label}</span>
380
+ </div>
381
+ <div class="flex items-center gap-2 shrink-0">
382
+ {#if loadState.isLoading}
383
+ <div class="flex items-center gap-1.5 text-xs text-slate-500">
384
+ <div class="w-3.5 h-3.5 border-2 border-violet-500 border-t-transparent rounded-full animate-spin"></div>
385
+ Starting...
386
+ </div>
387
+ {:else if config.isActive}
388
+ <button
389
+ type="button"
390
+ class="flex items-center gap-1 px-2 py-1 text-xs font-medium rounded text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-900/20 transition-colors cursor-pointer"
391
+ onclick={() => handleStopRemote(config)}
392
+ >
393
+ <Icon name="lucide:circle-stop" class="w-3.5 h-3.5" />
394
+ Stop
395
+ </button>
396
+ {:else}
397
+ <button
398
+ type="button"
399
+ class="flex items-center gap-1 px-2 py-1 text-xs font-medium rounded text-green-600 dark:text-green-400 hover:bg-green-50 dark:hover:bg-green-900/20 transition-colors cursor-pointer"
400
+ onclick={() => handleStartRemote(config)}
401
+ >
402
+ <Icon name="lucide:play" class="w-3.5 h-3.5" />
403
+ Start
404
+ </button>
405
+ {/if}
406
+ <button
407
+ type="button"
408
+ class="flex items-center gap-1 px-2 py-1 text-xs font-medium rounded text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-900/20 transition-colors cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed"
409
+ onclick={() => { remoteRemoveTargetId = config.id; }}
410
+ disabled={config.isActive || removingRemoteId === config.id}
411
+ title={config.isActive ? 'Stop the tunnel first' : 'Delete tunnel'}
412
+ >
413
+ {#if removingRemoteId === config.id}
414
+ <div class="w-3.5 h-3.5 border-2 border-red-400 border-t-transparent rounded-full animate-spin"></div>
415
+ {:else}
416
+ <Icon name="lucide:trash-2" class="w-3.5 h-3.5" />
417
+ {/if}
418
+ </button>
419
+ </div>
420
+ </div>
421
+ <!-- Remote ingress list (only shown when active) -->
422
+ {#if config.isActive}
423
+ {@const hostRules = config.ingress?.filter((r) => r.hostname) ?? []}
424
+ {#if hostRules.length > 0}
425
+ <div class="mt-2 pt-2 border-t border-slate-200 dark:border-slate-700/30 space-y-2">
426
+ {#each hostRules as rule}
427
+ {@const ruleUrl = `https://${rule.hostname}`}
428
+ <div>
429
+ <div class="flex items-start justify-between gap-2">
430
+ <a
431
+ href={ruleUrl}
432
+ target="_blank"
433
+ rel="noopener noreferrer"
434
+ class="flex items-center gap-2 text-sm font-medium text-violet-600 dark:text-violet-400 hover:underline break-all"
435
+ >
436
+ <Icon name="lucide:globe" class="w-3.5 h-3.5 text-orange-500 shrink-0" />
437
+ {rule.hostname}
438
+ </a>
439
+ <div class="flex items-center gap-1 shrink-0">
440
+ <button
441
+ type="button"
442
+ class="flex p-1.5 rounded transition-colors cursor-pointer {copiedUrl === ruleUrl
443
+ ? 'text-green-600 dark:text-green-400 bg-green-100 dark:bg-green-900/20'
444
+ : 'text-slate-400 hover:text-slate-600 dark:hover:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-700'}"
445
+ onclick={() => copyUrl(ruleUrl)}
446
+ title="Copy URL"
447
+ >
448
+ <Icon name={copiedUrl === ruleUrl ? 'lucide:check' : 'lucide:copy'} class="w-3.5 h-3.5" />
449
+ </button>
450
+ <button
451
+ type="button"
452
+ class="flex p-1.5 rounded transition-colors cursor-pointer {qrUrl === ruleUrl
453
+ ? 'text-violet-600 dark:text-violet-400 bg-violet-100 dark:bg-violet-900/20'
454
+ : 'text-slate-400 hover:text-slate-600 dark:hover:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-700'}"
455
+ onclick={() => { qrUrl = qrUrl === ruleUrl ? '' : ruleUrl; }}
456
+ title="Show QR Code"
457
+ >
458
+ <Icon name="lucide:qr-code" class="w-3.5 h-3.5" />
459
+ </button>
460
+ </div>
461
+ </div>
462
+ <div class="flex items-center gap-2 pl-5.5 text-xs text-slate-500 dark:text-slate-400">
463
+ <Icon name="lucide:arrow-right" class="w-3 h-3 shrink-0" />
464
+ <span class="font-mono">{rule.service}</span>
465
+ </div>
466
+ {#if qrUrl === ruleUrl}
467
+ <div class="mt-2 ml-5.5 p-3 rounded-lg border border-slate-200 dark:border-slate-700 bg-slate-50 dark:bg-slate-800/50">
468
+ <TunnelQRCode value={ruleUrl} />
469
+ </div>
470
+ {/if}
471
+ </div>
472
+ {/each}
473
+ </div>
474
+ {:else}
475
+ <div class="mt-2 pt-2 border-t border-slate-200 dark:border-slate-700/30">
476
+ <p class="text-sm text-slate-400 dark:text-slate-500 italic">No domains connected yet. Configure ingress rules in Cloudflare Dashboard.</p>
477
+ </div>
478
+ {/if}
479
+ {/if}
480
+ {#if loadState.error}
481
+ <p class="text-xs text-red-500 dark:text-red-400 mt-2">{loadState.error}</p>
482
+ {/if}
483
+ </div>
484
+ {/each}
485
+ </div>
486
+ {/if}
487
+
488
+ <!-- Add remote button / form -->
489
+ {#if !showRemoteAddForm}
490
+ <button
491
+ type="button"
492
+ class="flex items-center gap-2 w-full px-4 py-2 text-sm font-medium rounded-lg border border-dashed border-slate-300 dark:border-slate-600 text-slate-600 dark:text-slate-400 hover:border-violet-400 hover:text-violet-600 dark:hover:text-violet-400 transition-colors cursor-pointer justify-center"
493
+ onclick={() => { showRemoteAddForm = true; }}
494
+ >
495
+ <Icon name="lucide:plus" class="w-4 h-4" />
496
+ Add Remote Tunnel
497
+ </button>
498
+ {:else}
499
+ <div class="space-y-3 p-4 rounded-lg border border-violet-200 dark:border-violet-800/50 bg-violet-50/50 dark:bg-violet-900/10">
500
+ <!-- Guide -->
501
+ <div class="flex gap-2.5 p-3 rounded-lg bg-white dark:bg-slate-900/50 border border-slate-200 dark:border-slate-700/50">
502
+ <Icon name="lucide:info" class="w-4 h-4 shrink-0 mt-0.5 text-violet-600 dark:text-violet-400" />
503
+ <div class="text-xs text-slate-600 dark:text-slate-400 space-y-1.5">
504
+ <p class="font-medium text-slate-700 dark:text-slate-300">How to get a tunnel token:</p>
505
+ <ol class="list-decimal list-inside space-y-0.5">
506
+ <li>Go to <a href="https://dash.cloudflare.com/?to=/:account/tunnels" target="_blank" rel="noopener noreferrer" class="text-violet-600 dark:text-violet-400 underline underline-offset-2">Cloudflare Dashboard</a></li>
507
+ <li>Navigate to Networking &rarr; Tunnels</li>
508
+ <li>Click <strong>Create a tunnel</strong></li>
509
+ <li>Enter a <strong>Tunnel name</strong></li>
510
+ <li>Copy the tunnel token</li>
511
+ </ol>
512
+ </div>
513
+ </div>
514
+
515
+ <!-- Label input -->
516
+ <div class="space-y-2">
517
+ <label for="remote-label" class="block text-sm font-semibold text-slate-700 dark:text-slate-300">
518
+ Label
519
+ </label>
520
+ <input
521
+ id="remote-label"
522
+ type="text"
523
+ bind:value={remoteLabel}
524
+ placeholder="e.g. My Dev Tunnel"
525
+ class="w-full px-3 py-2 text-sm rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-900 text-slate-900 dark:text-slate-100 placeholder:text-slate-400 focus:outline-none focus:ring-2 focus:ring-violet-500/40 focus:border-violet-500"
526
+ />
527
+ </div>
528
+
529
+ <!-- Token input -->
530
+ <div class="space-y-2">
531
+ <label for="remote-token" class="block text-sm font-semibold text-slate-700 dark:text-slate-300">
532
+ Tunnel Token
533
+ </label>
534
+ <div class="relative">
535
+ <input
536
+ id="remote-token"
537
+ type={showRemoteToken ? 'text' : 'password'}
538
+ bind:value={remoteToken}
539
+ placeholder="eyJhIjoiNWE3ZjY0Nz..."
540
+ class="w-full px-3 py-2 pr-10 text-sm font-mono rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-900 text-slate-900 dark:text-slate-100 placeholder:text-slate-400 focus:outline-none focus:ring-2 focus:ring-violet-500/40 focus:border-violet-500"
541
+ />
542
+ <button
543
+ type="button"
544
+ class="absolute right-2 top-1/2 -translate-y-1/2 p-1 text-slate-400 hover:text-slate-600 dark:hover:text-slate-300 transition-colors cursor-pointer"
545
+ onclick={() => { showRemoteToken = !showRemoteToken; }}
546
+ title={showRemoteToken ? 'Hide token' : 'Show token'}
547
+ >
548
+ <Icon name={showRemoteToken ? 'lucide:eye-off' : 'lucide:eye'} class="w-4 h-4" />
549
+ </button>
550
+ </div>
551
+ </div>
552
+
553
+ <!-- Buttons -->
554
+ <div class="flex gap-2">
555
+ <button
556
+ type="button"
557
+ class="flex-1 flex items-center justify-center gap-2 px-4 py-2 text-sm font-medium rounded-lg bg-violet-600 text-white hover:bg-violet-700 transition-colors disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer"
558
+ onclick={handleAddRemote}
559
+ disabled={isSaving || !remoteLabel.trim() || !remoteToken.trim()}
560
+ >
561
+ {#if isSaving}
562
+ <div class="w-3.5 h-3.5 border-2 border-white/30 border-t-white rounded-full animate-spin"></div>
563
+ Saving...
564
+ {:else}
565
+ <Icon name="lucide:plus" class="w-3.5 h-3.5" />
566
+ Add
567
+ {/if}
568
+ </button>
569
+ <button
570
+ type="button"
571
+ class="px-4 py-2 text-sm font-medium rounded-lg border border-slate-300 dark:border-slate-600 text-slate-600 dark:text-slate-400 hover:bg-slate-100 dark:hover:bg-slate-700 transition-colors cursor-pointer"
572
+ onclick={() => { showRemoteAddForm = false; remoteLabel = ''; remoteToken = ''; showRemoteToken = false; }}
573
+ >
574
+ Cancel
575
+ </button>
576
+ </div>
577
+ </div>
578
+ {/if}
579
+ </div>
580
+ </div>
581
+
582
+ <!-- ========== LOCALLY-MANAGED ========== -->
583
+ <div class="rounded-xl border border-slate-200 dark:border-slate-700/50 bg-white dark:bg-slate-800/50 overflow-hidden">
584
+ <!-- Header -->
585
+ <div class="flex items-center justify-between px-5 py-4 border-b border-slate-200 dark:border-slate-700/50">
586
+ <div class="flex items-center gap-3">
587
+ <div class="flex items-center justify-center w-9 h-9 rounded-lg bg-blue-500/10 dark:bg-blue-500/15">
588
+ <Icon name="lucide:server" class="w-5 h-5 text-blue-600 dark:text-blue-400" />
589
+ </div>
590
+ <div>
591
+ <h4 class="font-semibold text-slate-900 dark:text-slate-100">Locally-Managed</h4>
592
+ <p class="text-xs text-slate-500 dark:text-slate-400">Tunnels configured and managed from Clopen</p>
593
+ </div>
594
+ </div>
595
+
596
+ {#if locals.length > 0}
597
+ <span class="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-400">
598
+ {locals.length} configured
599
+ </span>
600
+ {/if}
601
+ </div>
602
+
603
+ <!-- Body -->
604
+ <div class="px-5 py-4 space-y-4">
605
+ <!-- Auth status -->
606
+ {#if !isAuthenticated}
607
+ <div class="p-4 rounded-lg border border-amber-200 dark:border-amber-800/50 bg-amber-50/50 dark:bg-amber-900/10">
608
+ <div class="flex items-start gap-3">
609
+ <Icon name="lucide:key" class="w-5 h-5 text-amber-600 dark:text-amber-400 shrink-0 mt-0.5" />
610
+ <div class="flex-1">
611
+ <p class="text-sm font-semibold text-slate-900 dark:text-slate-100 mb-1">Cloudflare Login Required</p>
612
+ <p class="text-xs text-slate-600 dark:text-slate-400 mb-3">
613
+ Locally-managed tunnels require authentication with your Cloudflare account.
614
+ </p>
615
+
616
+ {#if loginStep === 'idle'}
617
+ <button
618
+ type="button"
619
+ class="flex items-center gap-2 px-4 py-2 text-sm font-medium rounded-lg bg-amber-600 text-white hover:bg-amber-700 transition-colors cursor-pointer"
620
+ onclick={startLogin}
621
+ >
622
+ <Icon name="lucide:log-in" class="w-4 h-4" />
623
+ Login to Cloudflare
624
+ </button>
625
+ {:else if loginStep === 'loading'}
626
+ <div class="flex items-center gap-2 text-sm text-slate-500">
627
+ <Icon name="lucide:loader" class="w-4 h-4 animate-spin" />
628
+ Starting authentication...
629
+ </div>
630
+ {:else if loginStep === 'waiting-url'}
631
+ <div class="space-y-3">
632
+ <p class="text-xs text-slate-600 dark:text-slate-400">
633
+ Open the URL below in your browser, log in to Cloudflare, and <strong>select the domain</strong> you want to use with tunnels.
634
+ </p>
635
+ <!-- Auth URL -->
636
+ <div class="bg-white dark:bg-slate-800 rounded-lg px-3 py-2 text-xs font-mono text-slate-700 dark:text-slate-300 break-all border border-slate-200 dark:border-slate-700">
637
+ {loginUrl}
638
+ </div>
639
+ <div class="flex gap-2">
640
+ <button
641
+ type="button"
642
+ class="flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium rounded-md transition-colors cursor-pointer
643
+ {loginUrlCopied
644
+ ? 'bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-300'
645
+ : 'bg-slate-100 dark:bg-slate-700 text-slate-700 dark:text-slate-300 hover:bg-slate-200 dark:hover:bg-slate-600'}"
646
+ onclick={copyLoginUrl}
647
+ >
648
+ {#if loginUrlCopied}
649
+ <Icon name="lucide:check" class="w-3 h-3" />
650
+ Copied
651
+ {:else}
652
+ <Icon name="lucide:copy" class="w-3 h-3" />
653
+ Copy URL
654
+ {/if}
655
+ </button>
656
+ <a
657
+ href={loginUrl}
658
+ target="_blank"
659
+ rel="noopener noreferrer"
660
+ class="flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium rounded-md bg-violet-100 dark:bg-violet-900/30 text-violet-700 dark:text-violet-300 hover:bg-violet-200 dark:hover:bg-violet-800/40 transition-colors"
661
+ >
662
+ <Icon name="lucide:external-link" class="w-3 h-3" />
663
+ Open in Browser
664
+ </a>
665
+ <button
666
+ type="button"
667
+ class="flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium rounded-md bg-slate-100 dark:bg-slate-700 text-slate-500 dark:text-slate-400 hover:bg-slate-200 dark:hover:bg-slate-600 transition-colors cursor-pointer"
668
+ onclick={cancelLogin}
669
+ >
670
+ Cancel
671
+ </button>
672
+ </div>
673
+ <p class="text-2xs text-slate-400 dark:text-slate-500">
674
+ Waiting for browser authorization...
675
+ </p>
676
+ </div>
677
+ {:else if loginStep === 'zone-prompt'}
678
+ <div class="space-y-3">
679
+ <div class="flex items-center gap-2 text-sm text-green-600 dark:text-green-400 mb-2">
680
+ <Icon name="lucide:circle-check" class="w-4 h-4" />
681
+ Login successful!
682
+ </div>
683
+ <p class="text-xs text-slate-600 dark:text-slate-400">
684
+ Enter the domain you selected during Cloudflare authorization. This will be used for subdomain configuration.
685
+ </p>
686
+ <div class="flex gap-2">
687
+ <input
688
+ type="text"
689
+ bind:value={zoneInput}
690
+ placeholder="example.com"
691
+ class="flex-1 px-3 py-1.5 text-xs rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-900 text-slate-900 dark:text-slate-100 placeholder:text-slate-400 focus:outline-none focus:ring-2 focus:ring-violet-500/40 focus:border-violet-500"
692
+ onkeydown={(e) => { if (e.key === 'Enter') handleSetZone(); }}
693
+ />
694
+ <button
695
+ type="button"
696
+ class="px-4 py-1.5 text-xs font-medium rounded-lg bg-violet-600 text-white hover:bg-violet-700 transition-colors disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer"
697
+ onclick={handleSetZone}
698
+ disabled={!zoneInput.trim()}
699
+ >
700
+ Confirm
701
+ </button>
702
+ </div>
703
+ </div>
704
+ {:else if loginStep === 'error'}
705
+ <div class="space-y-2">
706
+ <div class="flex items-center gap-2 text-sm text-red-600 dark:text-red-400">
707
+ <Icon name="lucide:circle-alert" class="w-4 h-4" />
708
+ {loginError ?? 'Authentication failed'}
709
+ </div>
710
+ <button
711
+ type="button"
712
+ class="flex items-center gap-2 px-3 py-1.5 text-xs font-medium rounded-lg bg-slate-100 dark:bg-slate-700 text-slate-700 dark:text-slate-300 hover:bg-slate-200 dark:hover:bg-slate-600 transition-colors cursor-pointer"
713
+ onclick={() => { loginStep = 'idle'; loginError = null; }}
714
+ >
715
+ Try Again
716
+ </button>
717
+ </div>
718
+ {/if}
719
+ </div>
720
+ </div>
721
+ </div>
722
+ {:else}
723
+ <!-- Authenticated indicator with zone + logout + info -->
724
+ <div class="rounded-lg bg-green-50 dark:bg-green-900/10 border border-green-200 dark:border-green-800/30">
725
+ <div class="flex items-center justify-between gap-3 px-3 pt-2">
726
+ <div class="flex items-center gap-2">
727
+ <Icon name="lucide:circle-check" class="w-4 h-4 text-green-600 dark:text-green-400" />
728
+ <span class="text-xs text-green-700 dark:text-green-300 font-medium">
729
+ Cloudflare authenticated
730
+ {#if authorizedZone}
731
+ <span class="text-green-600/70 dark:text-green-400/70">({authorizedZone})</span>
732
+ {/if}
733
+ </span>
734
+ </div>
735
+ <button
736
+ type="button"
737
+ class="flex items-center gap-1 px-2 py-1 text-xs font-medium rounded text-slate-500 dark:text-slate-400 hover:text-red-600 dark:hover:text-red-400 hover:bg-red-50 dark:hover:bg-red-900/20 transition-colors cursor-pointer"
738
+ onclick={handleLogout}
739
+ >
740
+ <Icon name="lucide:log-out" class="w-3 h-3" />
741
+ Logout
742
+ </button>
743
+ </div>
744
+ <div class="px-3 pb-2">
745
+ <p class="text-2xs text-green-600/60 dark:text-green-400/50">
746
+ One domain per login. For multiple domains, use Remotely-Managed tunnels.
747
+ </p>
748
+ </div>
749
+ </div>
750
+
751
+ {#if !authorizedZone}
752
+ <!-- Zone prompt for already-authenticated users who haven't set zone -->
753
+ <div class="p-3 rounded-lg border border-amber-200 dark:border-amber-800/50 bg-amber-50/50 dark:bg-amber-900/10">
754
+ <p class="text-xs text-slate-600 dark:text-slate-400 mb-2">
755
+ Enter the domain authorized with your Cloudflare account to enable subdomain configuration.
756
+ </p>
757
+ <div class="flex gap-2">
758
+ <input
759
+ type="text"
760
+ bind:value={zoneInput}
761
+ placeholder="example.com"
762
+ class="flex-1 px-3 py-1.5 text-xs rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-900 text-slate-900 dark:text-slate-100 placeholder:text-slate-400 focus:outline-none focus:ring-2 focus:ring-violet-500/40 focus:border-violet-500"
763
+ onkeydown={(e) => { if (e.key === 'Enter') handleSetZone(); }}
764
+ />
765
+ <button
766
+ type="button"
767
+ class="px-4 py-1.5 text-xs font-medium rounded-lg bg-violet-600 text-white hover:bg-violet-700 transition-colors disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer"
768
+ onclick={handleSetZone}
769
+ disabled={!zoneInput.trim()}
770
+ >
771
+ Set Domain
772
+ </button>
773
+ </div>
774
+ </div>
775
+ {/if}
776
+ {/if}
777
+
778
+ <!-- Existing local configs -->
779
+ {#if locals.length > 0}
780
+ <div class="space-y-3">
781
+ {#each locals as config (config.id)}
782
+ {@const loadState = getLocalLoadingState(config.id)}
783
+ <div class="rounded-lg border border-slate-200 dark:border-slate-700/50 overflow-hidden">
784
+ <!-- Config header -->
785
+ <div class="flex items-center justify-between gap-3 p-3 bg-slate-50 dark:bg-slate-800/80">
786
+ <div class="flex items-center gap-2.5 min-w-0">
787
+ <div class="w-2 h-2 rounded-full {config.isActive ? 'bg-green-500' : 'bg-slate-300 dark:bg-slate-600'}"></div>
788
+ <span class="text-sm font-semibold text-slate-900 dark:text-slate-100 truncate">{config.name}</span>
789
+ </div>
790
+ <div class="flex items-center gap-2 shrink-0">
791
+ {#if loadState.isLoading}
792
+ <div class="flex items-center gap-1.5 text-xs text-slate-500">
793
+ <div class="w-3.5 h-3.5 border-2 border-blue-500 border-t-transparent rounded-full animate-spin"></div>
794
+ Starting...
795
+ </div>
796
+ {:else if config.isActive}
797
+ <button
798
+ type="button"
799
+ class="flex items-center gap-1 px-2 py-1 text-xs font-medium rounded text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-900/20 transition-colors cursor-pointer"
800
+ onclick={() => handleStopLocal(config)}
801
+ >
802
+ <Icon name="lucide:circle-stop" class="w-3.5 h-3.5" />
803
+ Stop
804
+ </button>
805
+ {:else if config.ingress.length > 0}
806
+ <button
807
+ type="button"
808
+ class="flex items-center gap-1 px-2 py-1 text-xs font-medium rounded text-green-600 dark:text-green-400 hover:bg-green-50 dark:hover:bg-green-900/20 transition-colors cursor-pointer"
809
+ onclick={() => handleStartLocal(config)}
810
+ >
811
+ <Icon name="lucide:play" class="w-3.5 h-3.5" />
812
+ Start
813
+ </button>
814
+ {/if}
815
+ <button
816
+ type="button"
817
+ class="flex items-center gap-1 px-2 py-1 text-xs font-medium rounded text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-900/20 transition-colors cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed"
818
+ onclick={() => { localDeleteTargetId = config.id; }}
819
+ disabled={config.isActive || deletingLocalId === config.id}
820
+ title={config.isActive ? 'Stop the tunnel first' : 'Delete tunnel'}
821
+ >
822
+ {#if deletingLocalId === config.id}
823
+ <div class="w-3.5 h-3.5 border-2 border-red-400 border-t-transparent rounded-full animate-spin"></div>
824
+ {:else}
825
+ <Icon name="lucide:trash-2" class="w-3.5 h-3.5" />
826
+ {/if}
827
+ </button>
828
+ </div>
829
+ </div>
830
+
831
+ {#if loadState.error}
832
+ <div class="px-3 py-2 bg-red-50 dark:bg-red-900/10 border-t border-red-200 dark:border-red-800/30">
833
+ <p class="text-xs text-red-500 dark:text-red-400">{loadState.error}</p>
834
+ </div>
835
+ {/if}
836
+
837
+ <!-- Domains -->
838
+ <div class="px-3 py-2.5 space-y-2 border-t border-slate-200 dark:border-slate-700/50">
839
+ {#if config.ingress.length > 0}
840
+ {#each config.ingress as rule}
841
+ {@const ruleUrl = `https://${rule.hostname}`}
842
+ <div>
843
+ <div class="flex items-start justify-between gap-2">
844
+ <a
845
+ href={ruleUrl}
846
+ target="_blank"
847
+ rel="noopener noreferrer"
848
+ class="flex items-center gap-2 text-sm font-medium text-violet-600 dark:text-violet-400 hover:underline break-all"
849
+ >
850
+ <Icon name="lucide:globe" class="w-3.5 h-3.5 text-blue-500 shrink-0" />
851
+ {rule.hostname}
852
+ </a>
853
+ <div class="flex items-center gap-1 shrink-0">
854
+ <button
855
+ type="button"
856
+ class="flex p-1.5 rounded transition-colors cursor-pointer {copiedUrl === ruleUrl
857
+ ? 'text-green-600 dark:text-green-400 bg-green-100 dark:bg-green-900/20'
858
+ : 'text-slate-400 hover:text-slate-600 dark:hover:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-700'}"
859
+ onclick={() => copyUrl(ruleUrl)}
860
+ title="Copy URL"
861
+ >
862
+ <Icon name={copiedUrl === ruleUrl ? 'lucide:check' : 'lucide:copy'} class="w-3.5 h-3.5" />
863
+ </button>
864
+ <button
865
+ type="button"
866
+ class="flex p-1.5 rounded transition-colors cursor-pointer {qrUrl === ruleUrl
867
+ ? 'text-violet-600 dark:text-violet-400 bg-violet-100 dark:bg-violet-900/20'
868
+ : 'text-slate-400 hover:text-slate-600 dark:hover:text-slate-300 hover:bg-slate-100 dark:hover:bg-slate-700'}"
869
+ onclick={() => { qrUrl = qrUrl === ruleUrl ? '' : ruleUrl; }}
870
+ title="Show QR Code"
871
+ >
872
+ <Icon name="lucide:qr-code" class="w-3.5 h-3.5" />
873
+ </button>
874
+ {#if !config.isActive}
875
+ <button
876
+ type="button"
877
+ class="flex p-1.5 text-red-400 hover:text-red-600 dark:hover:text-red-300 hover:bg-red-50 dark:hover:bg-red-900/20 rounded transition-colors shrink-0 cursor-pointer"
878
+ onclick={() => handleRemoveDomain(config.id, rule.hostname)}
879
+ title="Remove subdomain"
880
+ >
881
+ <Icon name="lucide:x" class="w-3.5 h-3.5" />
882
+ </button>
883
+ {/if}
884
+ </div>
885
+ </div>
886
+ <div class="flex items-center gap-2 pl-5.5 text-xs text-slate-500 dark:text-slate-400">
887
+ <Icon name="lucide:arrow-right" class="w-3 h-3 shrink-0" />
888
+ <span class="font-mono">{rule.service}</span>
889
+ </div>
890
+ {#if qrUrl === ruleUrl}
891
+ <div class="mt-2 ml-5.5 p-3 rounded-lg border border-slate-200 dark:border-slate-700 bg-slate-50 dark:bg-slate-800/50">
892
+ <TunnelQRCode value={ruleUrl} />
893
+ </div>
894
+ {/if}
895
+ </div>
896
+ {/each}
897
+ {:else}
898
+ <p class="text-sm text-slate-400 dark:text-slate-500 italic">No subdomains configured yet</p>
899
+ {/if}
900
+
901
+ <!-- Add subdomain -->
902
+ {#if !config.isActive && authorizedZone}
903
+ {#if domainConfigId === config.id}
904
+ <div class="space-y-3 pt-1">
905
+ <div class="space-y-2">
906
+ <label class="block text-sm font-semibold text-slate-700 dark:text-slate-300">Subdomain</label>
907
+ <div class="flex">
908
+ <input
909
+ type="text"
910
+ bind:value={domainSubdomain}
911
+ placeholder="dev"
912
+ class="w-full px-3 py-2 text-sm border border-slate-300 dark:border-slate-600 rounded-l-lg bg-white dark:bg-slate-900 text-slate-900 dark:text-slate-100 placeholder:text-slate-400 focus:outline-none focus:ring-2 focus:ring-violet-500/40 focus:border-violet-500 focus:z-10"
913
+ />
914
+ <span class="inline-flex items-center px-3 py-2 text-sm font-mono text-slate-500 dark:text-slate-400 bg-slate-100 dark:bg-slate-800 border border-l-0 border-slate-300 dark:border-slate-600 rounded-r-lg">
915
+ .{authorizedZone}
916
+ </span>
917
+ </div>
918
+ </div>
919
+ <div class="space-y-2">
920
+ <label class="block text-sm font-semibold text-slate-700 dark:text-slate-300">Service URL</label>
921
+ <input
922
+ type="text"
923
+ bind:value={domainService}
924
+ placeholder="http://localhost:3000"
925
+ class="w-full px-3 py-2 text-sm rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-900 text-slate-900 dark:text-slate-100 placeholder:text-slate-400 focus:outline-none focus:ring-2 focus:ring-violet-500/40 focus:border-violet-500"
926
+ />
927
+ </div>
928
+ <div class="flex gap-2">
929
+ <button
930
+ type="button"
931
+ class="flex-1 flex items-center justify-center gap-2 px-4 py-2 text-sm font-medium rounded-lg bg-blue-600 text-white hover:bg-blue-700 transition-colors disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer"
932
+ onclick={() => handleAddDomain(config.id)}
933
+ disabled={!domainSubdomain.trim() || !domainService.trim() || isAddingDomain}
934
+ >
935
+ {#if isAddingDomain}
936
+ <div class="w-3.5 h-3.5 border-2 border-white/30 border-t-white rounded-full animate-spin"></div>
937
+ Adding...
938
+ {:else}
939
+ <Icon name="lucide:plus" class="w-3.5 h-3.5" />
940
+ Add Subdomain
941
+ {/if}
942
+ </button>
943
+ <button
944
+ type="button"
945
+ class="px-4 py-2 text-sm font-medium rounded-lg border border-slate-300 dark:border-slate-600 text-slate-600 dark:text-slate-400 hover:bg-slate-100 dark:hover:bg-slate-700 transition-colors cursor-pointer"
946
+ onclick={() => { domainConfigId = null; domainSubdomain = ''; domainService = ''; }}
947
+ >
948
+ Cancel
949
+ </button>
950
+ </div>
951
+ </div>
952
+ {:else}
953
+ <button
954
+ type="button"
955
+ class="flex items-center gap-2 w-full mt-3 px-4 py-2 text-xs font-medium rounded-lg border border-dashed border-slate-300 dark:border-slate-600 text-slate-600 dark:text-slate-400 hover:border-blue-400 hover:text-blue-600 dark:hover:text-blue-400 transition-colors cursor-pointer justify-center"
956
+ onclick={() => { domainConfigId = config.id; }}
957
+ >
958
+ <Icon name="lucide:plus" class="w-4 h-4" />
959
+ Add Subdomain
960
+ </button>
961
+ {/if}
962
+ {:else if !config.isActive && !authorizedZone}
963
+ <p class="text-xs text-amber-500 dark:text-amber-400 pt-1">Set your authorized domain above to add subdomains</p>
964
+ {/if}
965
+ </div>
966
+ </div>
967
+ {/each}
968
+ </div>
969
+ {/if}
970
+
971
+ <!-- Add local tunnel button / form -->
972
+ {#if isAuthenticated && authorizedZone}
973
+ {#if !showLocalCreateForm}
974
+ <button
975
+ type="button"
976
+ class="flex items-center gap-2 w-full px-4 py-2 text-sm font-medium rounded-lg border border-dashed border-slate-300 dark:border-slate-600 text-slate-600 dark:text-slate-400 hover:border-blue-400 hover:text-blue-600 dark:hover:text-blue-400 transition-colors cursor-pointer justify-center"
977
+ onclick={() => { showLocalCreateForm = true; }}
978
+ >
979
+ <Icon name="lucide:plus" class="w-4 h-4" />
980
+ Add Local Tunnel
981
+ </button>
982
+ {:else}
983
+ <div class="space-y-3 p-4 rounded-lg border border-blue-200 dark:border-blue-800/50 bg-blue-50/50 dark:bg-blue-900/10">
984
+ <div class="space-y-2">
985
+ <label for="local-name" class="block text-sm font-semibold text-slate-700 dark:text-slate-300">
986
+ Tunnel Name
987
+ </label>
988
+ <input
989
+ id="local-name"
990
+ type="text"
991
+ bind:value={localName}
992
+ placeholder="e.g. my-dev-tunnel"
993
+ class="w-full px-3 py-2 text-sm rounded-lg border border-slate-300 dark:border-slate-600 bg-white dark:bg-slate-900 text-slate-900 dark:text-slate-100 placeholder:text-slate-400 focus:outline-none focus:ring-2 focus:ring-violet-500/40 focus:border-violet-500"
994
+ />
995
+ <p class="text-2xs text-slate-400 dark:text-slate-500">
996
+ A unique name for the tunnel (lowercase, no spaces)
997
+ </p>
998
+ </div>
999
+ <div class="flex gap-2">
1000
+ <button
1001
+ type="button"
1002
+ class="flex-1 flex items-center justify-center gap-2 px-4 py-2 text-sm font-medium rounded-lg bg-blue-600 text-white hover:bg-blue-700 transition-colors disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer"
1003
+ onclick={handleCreateLocal}
1004
+ disabled={isSaving || !localName.trim()}
1005
+ >
1006
+ {#if isSaving}
1007
+ <div class="w-3.5 h-3.5 border-2 border-white/30 border-t-white rounded-full animate-spin"></div>
1008
+ Adding...
1009
+ {:else}
1010
+ <Icon name="lucide:plus" class="w-3.5 h-3.5" />
1011
+ Add
1012
+ {/if}
1013
+ </button>
1014
+ <button
1015
+ type="button"
1016
+ class="px-4 py-2 text-sm font-medium rounded-lg border border-slate-300 dark:border-slate-600 text-slate-600 dark:text-slate-400 hover:bg-slate-100 dark:hover:bg-slate-700 transition-colors cursor-pointer"
1017
+ onclick={() => { showLocalCreateForm = false; localName = ''; }}
1018
+ >
1019
+ Cancel
1020
+ </button>
1021
+ </div>
1022
+ </div>
1023
+ {/if}
1024
+ {/if}
1025
+ </div>
1026
+ </div>
1027
+ </div>
1028
+
1029
+ <!-- Store error -->
1030
+ {#if storeError}
1031
+ <div class="mt-4 flex items-center gap-2 p-3 rounded-lg bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800/50">
1032
+ <Icon name="lucide:circle-alert" class="w-4 h-4 text-red-600 dark:text-red-400 shrink-0" />
1033
+ <span class="text-sm text-red-700 dark:text-red-300">{storeError}</span>
1034
+ </div>
1035
+ {/if}
1036
+ </div>
1037
+
1038
+ <!-- Remove remote dialog -->
1039
+ <Dialog
1040
+ bind:isOpen={() => remoteRemoveTargetId !== null, (v) => { if (!v) remoteRemoveTargetId = null; }}
1041
+ onClose={() => { remoteRemoveTargetId = null; }}
1042
+ type="warning"
1043
+ title="Remove Remote Tunnel"
1044
+ message="Are you sure you want to remove this remote tunnel config? The tunnel token will be deleted. If the tunnel is running, it will be stopped."
1045
+ confirmText="Remove"
1046
+ cancelText="Cancel"
1047
+ onConfirm={handleRemoveRemote}
1048
+ />
1049
+
1050
+ <!-- Delete local dialog -->
1051
+ <Dialog
1052
+ bind:isOpen={() => localDeleteTargetId !== null, (v) => { if (!v) localDeleteTargetId = null; }}
1053
+ onClose={() => { localDeleteTargetId = null; }}
1054
+ type="warning"
1055
+ title="Delete Local Tunnel"
1056
+ message="Are you sure you want to delete this tunnel? The tunnel and its configuration will be permanently removed from Cloudflare."
1057
+ confirmText="Delete"
1058
+ cancelText="Cancel"
1059
+ onConfirm={handleDeleteLocal}
1060
+ />