dnsm 0.1.9

Toolkit for covert data exfiltration using DNS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
<script>
  import { onMount, onDestroy } from "svelte";
  import { link } from "svelte-spa-router";
  import { validateMailbox, randomMailbox } from "../lib/utils.js";

  export let params = {};

  let ws = null;
  let reconnectTimer = null;
  let reconnectDelay = 1000;
  const MAX_DELAY = 20000;
  let autoscroll = true;
  let autoReconnect = true;
  let messages = [];
  let status = { text: "Idle", level: "idle" };
  let host = location.hostname === "localhost" || location.hostname === "127.0.0.1" ? "127.0.0.1:8787" : "ws.dnsm.re";
  let mailbox = "";
  let zone = "k.dnsm.re";
  let wasmLoaded = false;
  let wasmModule = null;
  let genMessage = "";
  let genDomains = [];
  let genError = "";
  let genTimer = null;
  let generatorExpanded = false;
  let digExpanded = false;
  let expandedMessages = new Set();

  const COLLAPSE_LINE_THRESHOLD = 12;
  const COLLAPSE_CHAR_THRESHOLD = 1000;

  function isLongContent(content) {
    return content.length > COLLAPSE_CHAR_THRESHOLD ||
      content.split('\n').length > COLLAPSE_LINE_THRESHOLD;
  }

  function toggleExpand(id) {
    if (expandedMessages.has(id)) {
      expandedMessages.delete(id);
    } else {
      expandedMessages.add(id);
    }
    expandedMessages = expandedMessages;
  }

  $: parentZone = zone.split('.').slice(1).join('.') || zone;
  $: mailboxZone = `m.${parentZone}`;

  function toggleGenerator() {
    generatorExpanded = !generatorExpanded;
  }

  function toggleDig() {
    digExpanded = !digExpanded;
  }

  function setStatus(text, level = "idle") {
    status = { text, level };
  }

  function normalizeHttp(base, fallback) {
    const h = (base || "").trim();
    if (!h) return fallback;
    if (h.startsWith("http://") || h.startsWith("https://")) return h;
    if (h.includes("dnsm.re")) return "https://" + h;
    const scheme = location.protocol === "https:" ? "https://" : "http://";
    return scheme + h;
  }

  function toWsUrl(httpBase) {
    const u = new URL(httpBase);
    u.protocol = u.protocol === "https:" ? "wss:" : "ws:";
    return u.toString().replace(/\/$/, "");
  }

  function toHex(bytes) {
    let out = "";
    for (let i = 0; i < bytes.length; i++) {
      if (i && i % 16 === 0) out += "\n";
      else if (i && i % 2 === 0) out += " ";
      out += bytes[i].toString(16).padStart(2, "0");
    }
    return out;
  }

  function decodePayload(b64, messageType) {
    if (messageType === "ping") {
      return { type: "ping", content: "", raw: "" };
    }
    try {
      const bin = atob(b64);
      const bytes = new Uint8Array(bin.length);
      for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i) & 0xff;

      let text = null;
      try {
        text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
      } catch {}

      const printable = text && text.length > 0 && !/[\x00-\x08\x0b\x0c\x0e-\x1f]/.test(text);
      if (text && printable) {
        const trimmed = text.trim();
        if (
          (trimmed.startsWith("{") && trimmed.endsWith("}")) ||
          (trimmed.startsWith("[") && trimmed.endsWith("]"))
        ) {
          try {
            const parsed = JSON.parse(text);
            return {
              type: "json",
              content: JSON.stringify(parsed, null, 2),
              raw: b64,
            };
          } catch {}
        }
        return { type: "text", content: text, raw: b64 };
      }

      return { type: "binary", content: toHex(bytes), raw: b64 };
    } catch (e) {
      return { type: "invalid", content: `Invalid base64: ${e}`, raw: b64 };
    }
  }

  function formatDate(date) {
    const d = String(date.getDate()).padStart(2, "0");
    const m = String(date.getMonth() + 1).padStart(2, "0");
    const y = date.getFullYear();
    const H = String(date.getHours()).padStart(2, "0");
    const M = String(date.getMinutes()).padStart(2, "0");
    const S = String(date.getSeconds()).padStart(2, "0");
    return `${d}.${m}.${y}, ${H}:${M}:${S}`;
  }

  function computeReceivedMeta(value) {
    const numeric = typeof value === "number" ? value : Number(value);
    if (!Number.isFinite(numeric)) {
      const raw =
        value === null || value === undefined ? "Unknown" : String(value);
      return { label: "Unknown time", tooltip: raw };
    }
    const date = new Date(numeric);
    if (Number.isNaN(date.getTime())) {
      const raw =
        value === null || value === undefined ? "Unknown" : String(value);
      return { label: "Unknown time", tooltip: raw };
    }
    return { label: formatDate(date), tooltip: date.toISOString() };
  }

  function formatReceivedAt(value) {
    return computeReceivedMeta(value).label;
  }

  function receivedAtTooltip(value) {
    return computeReceivedMeta(value).tooltip;
  }

  async function fetchBacklog(httpBase, mb) {
    try {
      const url = `${httpBase.replace(/\/$/, "")}/api/mailbox/${mb}/messages`;
      const res = await fetch(url, { method: "GET" });
      if (!res.ok) {
        setStatus(`HTTP ${res.status}: failed to fetch`, "warn");
        return [];
      }
      const arr = await res.json();
      return Array.isArray(arr) ? arr : [];
    } catch (e) {
      setStatus(`HTTP error: ${e}`, "warn");
      return [];
    }
  }

  function scheduleReconnect() {
    if (!autoReconnect) return;
    if (reconnectTimer) clearTimeout(reconnectTimer);
    reconnectTimer = setTimeout(connect, reconnectDelay);
    reconnectDelay = Math.min(reconnectDelay * 2, MAX_DELAY);
    setStatus(
      `Reconnecting in ${Math.round(reconnectDelay / 1000)}s...`,
      "warn"
    );
  }

  let isConnected = false;
  let isIntentionalDisconnect = false;
  let isConnecting = false;

  async function connect() {
    const mb = validateMailbox(mailbox);
    if (!mb) {
      setStatus("Mailbox must be 12 lowercase hex chars", "bad");
      return;
    }

    // Prevent concurrent connection attempts
    if (isConnecting) {
      return;
    }

    isConnecting = true;
    isIntentionalDisconnect = true; // We're intentionally switching connections
    mailbox = mb;
    setStatus("Connecting...", "warn");
    messages = [];

    if (
      ws &&
      (ws.readyState === WebSocket.OPEN ||
        ws.readyState === WebSocket.CONNECTING)
    ) {
      try {
        ws.close();
      } catch {}
    }
    if (reconnectTimer) {
      clearTimeout(reconnectTimer);
      reconnectTimer = null;
    }
    reconnectDelay = 1000;

    const fallback =
      location.hostname && location.protocol.startsWith("http")
        ? `${location.protocol}//${location.host}`
        : "http://localhost:8787";
    const httpBase = normalizeHttp(host, fallback);
    const wsBase = toWsUrl(httpBase);

    const backlog = await fetchBacklog(httpBase, mb);
    messages = backlog.reverse().map((m) => ({
      ...m,
      payload: decodePayload(m.data_b64, m.message_type),
    }));

    try {
      ws = new WebSocket(`${wsBase}/ws/${mb}`);
    } catch (e) {
      setStatus(`WS init failed: ${e}`, "bad");
      isConnecting = false;
      scheduleReconnect();
      return;
    }

    ws.addEventListener("open", () => {
      setStatus("Connected to WebSocket server", "good");
      isConnected = true;
      isConnecting = false;
      isIntentionalDisconnect = false; // Reset the flag on successful connection
      reconnectDelay = 1000; // Reset delay on successful connection
    });
    ws.addEventListener("close", () => {
      setStatus("Disconnected", "warn");
      isConnected = false;
      isConnecting = false;

      // Only auto-reconnect if this wasn't an intentional disconnect
      if (!isIntentionalDisconnect) {
        scheduleReconnect();
      } else {
        // Reset the flag for future disconnects
        isIntentionalDisconnect = false;
      }
    });
    ws.addEventListener("error", () => {
      setStatus("WebSocket error", "bad");
      isConnecting = false;
    });
    ws.addEventListener("message", (ev) => {
      try {
        const m = JSON.parse(ev.data);
        const msg = { ...m, payload: decodePayload(m.data_b64, m.message_type) };
        messages = [...messages, msg];
        if (autoscroll) {
          setTimeout(() => {
            const stream = document.getElementById("stream");
            if (stream) stream.scrollTop = stream.scrollHeight;
          }, 100);
        }
      } catch {}
    });
  }

  function clearMessages() {
    messages = [];
  }

  async function generateRandomMailbox() {
    mailbox = randomMailbox();
    // Reconnect with the new mailbox
    await connect();
  }

  async function copyText(content) {
    try {
      await navigator.clipboard.writeText(content);
    } catch (e) {
      console.error("Copy failed:", e);
    }
  }

  function applyQueryParams() {
    // Parse query params from hash (e.g., #/inbox?mailbox=abc&host=xyz)
    const hashParts = location.hash.split("?");
    const queryString = hashParts.length > 1 ? hashParts[1] : "";
    const p = new URLSearchParams(queryString);
    const hostParam = p.get("host");
    const mailboxParam = p.get("mailbox");
    const auto = p.get("auto");
    if (hostParam) host = hostParam;
    if (mailboxParam) mailbox = mailboxParam;
    if (auto === "0" || auto === "false") autoReconnect = false;
    return (hostParam && mailboxParam) || auto === "1" || auto === "true";
  }

  async function loadWasm() {
    try {
      if (!wasmModule) {
        wasmModule = await import("../lib/pkg-web/dnsm.js");
        await wasmModule.default();
      }
      wasmLoaded = true;
    } catch (e) {
      console.error("Failed to load Wasm module:", e);
    }
  }

  function ensureMailboxForGen() {
    const v = (mailbox || "").trim();
    if (v === "") return "";
    const ok = validateMailbox(v);
    if (!ok) return null;
    return ok;
  }

  async function generateDomains() {
    genError = "";
    genDomains = [];
    if (!wasmLoaded) await loadWasm();
    const mb = ensureMailboxForGen();
    if (mb === null) {
      genError = "Mailbox must be exactly 12 lowercase hex characters";
      return;
    }
    const z = (zone || "").trim();
    const msg = (genMessage || "").toString();
    if (!z || !msg) {
      return;
    }
    try {
      const arr = wasmModule.domains_for_string_with_mailbox(msg, z, mb);
      genDomains = Array.from(arr);
    } catch (e) {
      genError = String(e);
    }
  }

  // Auto-generate on changes with a small debounce
  $: (async () => {
    if (!wasmLoaded || !generatorExpanded) return;
    if (genTimer) {
      clearTimeout(genTimer);
      genTimer = null;
    }
    const msgOk = (genMessage || "").trim().length > 0;
    const zOk = (zone || "").trim().length > 0;
    const mb = ensureMailboxForGen();
    if (!msgOk || !zOk || mb === null) {
      if (!msgOk) {
        genDomains = [];
        genError = "";
      }
      return;
    }
    genTimer = setTimeout(() => {
      generateDomains();
    }, 100);
  })();

  async function copyAllGenerated() {
    try {
      await navigator.clipboard.writeText(genDomains.join("\n"));
    } catch (e) {
      genError = `Copy failed: ${e}`;
    }
  }

  onMount(() => {
    // Route param takes priority (e.g. #/inbox/b5b051a225ca)
    if (params.id && validateMailbox(params.id)) {
      mailbox = params.id;
    }

    if (!mailbox) applyQueryParams();

    // Fallback: dev mailbox or random
    if (!mailbox) {
      mailbox = import.meta.env.DEV ? "d00d00d00d00" : randomMailbox();
    }

    // Preload WASM for generator
    loadWasm();

    // Auto-connect on load
    connect();
  });

  onDestroy(() => {
    // Disable auto-reconnect when component is being destroyed
    autoReconnect = false;
    isConnecting = false;
    isIntentionalDisconnect = true;

    if (ws) {
      try {
        ws.close();
      } catch {}
    }
    if (reconnectTimer) {
      clearTimeout(reconnectTimer);
      reconnectTimer = null;
    }
  });
</script>

<div class="wrap">
  <header>
    <div class="brand">
      <a href="#/" use:link class="logo"
        >dnsm <span class="muted">// inbox</span></a
      >
    </div>
    <div class="stat" title="Connection status">
      <span
        class="dot"
        class:good={status.level === "good"}
        class:warn={status.level === "warn"}
        class:bad={status.level === "bad"}
      ></span>
      <span>{status.text}</span>
    </div>
  </header>

  <section class="panel" aria-label="Connection">
    <div class="controls">
      <label for="host">WebSocket Server</label>
      <input
        id="host"
        type="text"
        spellcheck="false"
        placeholder="localhost:8787 or ws.example.com"
        bind:value={host}
        on:keydown={(e) => e.key === "Enter" && connect()}
      />
      <label for="mailbox">Mailbox</label>
      <input
        id="mailbox"
        type="text"
        spellcheck="false"
        placeholder="12 hex chars, e.g. 50373ff32343"
        maxlength="12"
        bind:value={mailbox}
        on:keydown={(e) => e.key === "Enter" && connect()}
      />
    </div>

    <div
      class="collapsible-header"
      role="button"
      tabindex="0"
      on:click={toggleGenerator}
      on:keydown={(e) => (e.key === 'Enter' || e.key === ' ') && toggleGenerator()}
    >
      <span class="collapsible-title">Domain Generator</span>
      <svg
        class="chevron"
        class:expanded={generatorExpanded}
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
      >
        <polyline points="6 9 12 15 18 9"></polyline>
      </svg>
    </div>

    {#if generatorExpanded}
      <div class="generator-section">
        <div class="controls" style="margin-bottom: 14px;">
          <label for="gen-zone">Zone</label>
          <input
            id="gen-zone"
            type="text"
            spellcheck="false"
            placeholder="e.g. k.example.com"
            bind:value={zone}
          />
          <label for="gen-message">Message</label>
          <textarea
            id="gen-message"
            class="msg-input"
            placeholder="Type or paste your message..."
            bind:value={genMessage}
          ></textarea>
        </div>
        <div class="toolbar" style="margin-bottom: 10px;">
          <span class="muted small"
            >{wasmLoaded
              ? "Auto-generating as you type"
              : "Loading Wasm..."}</span
          >
          <div class="spacer"></div>
          <button
            class="btn small"
            on:click={copyAllGenerated}
            disabled={genDomains.length === 0}>Copy All</button
          >
        </div>
        {#if genError}
          <p class="hint" style="color: var(--danger); margin-top: 0; margin-bottom: 10px;">{genError}</p>
        {/if}
        <div class="gen-out">
          {#if genDomains.length === 0}
            <div class="empty">
              No domains yet. Enter zone and message. Mailbox: <code
                >{mailbox || "(none)"}</code
              >
            </div>
          {:else}
            <ol class="gen-list">
              {#each genDomains as d}
                <li><code>{d}</code></li>
              {/each}
            </ol>
          {/if}
        </div>
      </div>
    {/if}

    <div class="toolbar" style="margin-top: 10px;">
      <button
        on:click={generateRandomMailbox}
        class="btn btn-secondary small"
        title="Generate random mailbox">Random Mailbox</button
      >
      <div class="spacer"></div>
      <button
        on:click={() => (autoscroll = !autoscroll)}
        class="btn small"
        class:btn-secondary={!autoscroll}
        title="Toggle autoscroll"
      >
        Autoscroll: {autoscroll ? "On" : "Off"}
      </button>
      <button
        on:click={() => (autoReconnect = !autoReconnect)}
        class="btn small"
        class:btn-secondary={!autoReconnect}
        title="Toggle auto-reconnect"
      >
        Auto-Reconnect: {autoReconnect ? "On" : "Off"}
      </button>
      <button
        on:click={clearMessages}
        class="btn btn-secondary small"
        title="Clear messages"
        disabled={messages.length === 0}>Clear</button
      >
    </div>
  </section>

  <section class="stream" id="stream" aria-live="polite">
    <div class="stream-inner">
    {#if messages.length === 0}
      <div class="empty">
        No messages yet. Send some data via dnsm with mailbox.
      </div>
    {:else}
      {#each messages as msg (msg.id)}
        <div class="msg" class:msg-ping={msg.payload.type === "ping"}>
          <div class="row">
            <div class="meta">
              <div class="meta-line">
                {#if msg.message_hex}
                  <span class="meta-label">Message ID</span>
                  <span class="meta-value mono mono-break"
                    >{msg.message_hex}</span
                  >
                  <span class="meta-sep">•</span>
                {/if}
                <span class="meta-label">Received</span>
                <span
                  class="meta-value"
                  title={receivedAtTooltip(msg.received_at)}
                >
                  {formatReceivedAt(msg.received_at)}
                </span>
                <span class="meta-sep">•</span>
                <span class="meta-label">Type</span>
                <span class="meta-value">
                  <span class="badge" class:badge-ping={msg.payload.type === "ping"}>{msg.payload.type === "ping" ? "Ping" : msg.payload.type}</span>
                </span>
                {#if msg.peer_ip}
                  <span class="meta-sep">•</span>
                  <span class="meta-label">Peer</span>
                  <span class="meta-value mono">{msg.peer_ip}</span>
                {/if}
              </div>
            </div>
          </div>
          {#if msg.payload.type !== "ping"}
            {@const long = isLongContent(msg.payload.content)}
            {@const expanded = !long || expandedMessages.has(msg.id)}
            <div class="payload-section">
              <div class="payload-wrap" class:collapsed={!expanded}>
                {#if msg.payload.type === "json"}
                  <pre class="payload mono">{msg.payload.content}</pre>
                {:else if msg.payload.type === "binary"}
                  <pre class="payload mono">{msg.payload.content}</pre>
                {:else}
                  <div class="payload">{msg.payload.content}</div>
                {/if}
                {#if !expanded}
                  <div class="fade-overlay"></div>
                {/if}
              </div>
              <div class="toolbar" style="margin-top: 8px;">
                {#if long}
                  <button
                    on:click={() => toggleExpand(msg.id)}
                    class="iconbtn small"
                    title={expanded ? "Collapse message" : "Expand message"}
                  >
                    <svg
                      class="icon chevron-icon"
                      class:expanded
                      viewBox="0 0 24 24"
                      fill="none"
                      stroke="currentColor"
                      stroke-width="2"
                      stroke-linecap="round"
                      stroke-linejoin="round"
                      aria-hidden="true"
                    >
                      <polyline points="6 9 12 15 18 9"></polyline>
                    </svg>
                    {expanded ? 'Show less' : 'Show more'}
                  </button>
                {/if}
                <button
                  on:click={() => copyText(msg.payload.content)}
                  class="iconbtn small"
                  title="Copy as text"
                >
                  <svg
                    class="icon"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="1.7"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                    aria-hidden="true"
                  >
                    <rect x="9" y="9" width="13" height="13" rx="2"></rect>
                    <path d="M5 15V5a2 2 0 0 1 2-2h10" />
                  </svg>
                  Copy
                </button>
              </div>
            </div>
          {/if}
        </div>
      {/each}
    {/if}
    </div>
  </section>

  {#if mailbox && validateMailbox(mailbox)}
    <div class="dig-section">
      <div
        class="dig-header"
        role="button"
        tabindex="0"
        on:click={toggleDig}
        on:keydown={(e) => (e.key === 'Enter' || e.key === ' ') && toggleDig()}
      >
        <span class="dig-title">Retrieve via DNS</span>
        <svg
          class="chevron"
          class:expanded={digExpanded}
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
        >
          <polyline points="6 9 12 15 18 9"></polyline>
        </svg>
      </div>
      {#if digExpanded}
        <div class="dig-content">
          <div class="dig-cmd">
            <code>dig @{parentZone} -p 53 {mailbox}.{mailboxZone} TXT +tcp +short</code>
            <button
              class="dig-copy-btn"
              on:click={() => copyText(`dig @${parentZone} -p 53 ${mailbox}.${mailboxZone} TXT +tcp +short`)}
              title="Copy command"
              aria-label="Copy dig command"
            >
              <svg
                class="icon"
                viewBox="0 0 24 24"
                fill="none"
                stroke="currentColor"
                stroke-width="1.7"
                stroke-linecap="round"
                stroke-linejoin="round"
                aria-hidden="true"
              >
                <rect x="9" y="9" width="13" height="13" rx="2"></rect>
                <path d="M5 15V5a2 2 0 0 1 2-2h10" />
              </svg>
            </button>
          </div>
        </div>
      {/if}
    </div>
  {/if}

  <p class="hint small">
    API: GET <code>/api/mailbox/&#123;mailbox&#125;/messages</code>, WS
    <code>/ws/&#123;mailbox&#125;</code>. Returns messages in newest-first
    fashion + live push on new rows.
  </p>
</div>

<style>
  .collapsible-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    margin-top: 10px;
    background: linear-gradient(135deg, #0a0e13 0%, #0d1218 100%);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    user-select: none;
    transition: all 0.2s ease;
    outline: none;
  }

  .collapsible-header:hover {
    border-color: rgba(29, 242, 166, 0.3);
    background: linear-gradient(135deg, #0c1015 0%, #0f141a 100%);
    box-shadow: inset 0 0 0 1px rgba(29, 242, 166, 0.1);
  }

  .collapsible-header:focus-visible {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(29, 242, 166, 0.15);
  }

  .collapsible-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--text);
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .collapsible-title::before {
    content: '⚙️';
    font-size: 14px;
  }

  .chevron {
    width: 18px;
    height: 18px;
    color: var(--accent);
    transition: all 0.2s ease;
  }

  .collapsible-header:hover .chevron {
    color: var(--accent-2);
  }

  .chevron.expanded {
    transform: rotate(180deg);
  }

  .generator-section {
    margin-top: 12px;
    padding: 16px;
    background: linear-gradient(180deg, #080c10 0%, #0a0e13 100%);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3);
  }

  .stream {
    max-height: 60vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .stream-inner {
    display: grid;
    gap: 12px;
    padding-right: 8px;
  }

  .msg {
    border: 1px solid rgba(35, 52, 64, 0.7);
    border-radius: var(--radius);
    background: #0b1319;
    padding: 12px 16px;
    box-shadow: inset 0 0 0 1px rgba(16, 27, 36, 0.4);
  }

  .msg-ping {
    border-color: rgba(139, 92, 246, 0.35);
    background: linear-gradient(135deg, #0d0b19 0%, #0b1319 100%);
    box-shadow: inset 0 0 0 1px rgba(139, 92, 246, 0.1), 0 0 12px rgba(139, 92, 246, 0.06);
  }

  .badge-ping {
    color: #c4b5fd !important;
    background: rgba(139, 92, 246, 0.15) !important;
    border-color: rgba(139, 92, 246, 0.3) !important;
  }

  .msg .row {
    margin-bottom: 8px;
  }

  .msg .row:last-child {
    margin-bottom: 0;
  }

  .meta {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-family: var(--font-mono);
  }

  .meta-line {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: baseline;
  }

  .meta-label {
    text-transform: uppercase;
    font-size: 10px;
    letter-spacing: 0.08em;
    color: #6e7c8d;
    white-space: nowrap;
  }

  .meta-value {
    font-size: 12px;
    color: #d6e2f1;
    white-space: nowrap;
  }

  .meta-value.mono {
    font-family: var(--font-mono);
  }

  .meta-value.mono-break {
    font-family: var(--font-mono);
    white-space: normal;
    word-break: break-all;
  }

  .meta-sep {
    color: #354250;
  }

  .meta-line .badge {
    font-size: 10px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    padding: 2px 6px;
  }

  .payload-section {
    margin-top: 8px;
  }

  .payload-wrap {
    position: relative;
  }

  .payload-wrap.collapsed {
    max-height: 200px;
    overflow: hidden;
  }

  .fade-overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 60px;
    background: linear-gradient(transparent, #0b1319);
    pointer-events: none;
  }

  .chevron-icon {
    transition: transform 0.2s ease;
  }

  .chevron-icon.expanded {
    transform: rotate(180deg);
  }

  .iconbtn {
    cursor: pointer;
    color: var(--muted);
    border: 1px solid var(--border);
    background: #0c1217;
    border-radius: var(--radius-sm);
    padding: 6px 8px;
    display: inline-flex;
    gap: 8px;
    align-items: center;
    font-family: var(--font-mono);
    font-size: 12px;
    transition: all 0.2s ease;
  }

  .iconbtn:hover {
    color: var(--accent);
    border-color: #204b3f;
    box-shadow: inset 0 0 0 1px rgba(29, 242, 166, 0.15);
  }

  .icon {
    width: 14px;
    height: 14px;
    display: inline-block;
    flex-shrink: 0;
  }

  .logo {
    text-decoration: none;
    color: var(--accent);
    transition: opacity 0.2s ease;
  }

  .logo:hover {
    opacity: 0.8;
  }

  .msg-input {
    min-height: 120px;
    resize: vertical;
    background: #0b1219;
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 12px;
    outline: none;
    transition:
      border-color 0.15s ease,
      box-shadow 0.15s ease;
    font-family: var(--font-mono);
  }

  .msg-input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(29, 242, 166, 0.15);
  }

  .gen-out {
    margin-top: 10px;
    background: #0b1016;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px;
    max-height: 320px;
    overflow: auto;
  }

  .gen-list {
    margin: 0;
    padding-left: 20px;
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--text);
  }

  .dig-section {
    margin-top: 16px;
    margin-bottom: 12px;
  }

  .dig-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    background: linear-gradient(135deg, #0a0e13 0%, #0d1218 100%);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    user-select: none;
    transition: all 0.2s ease;
    outline: none;
  }

  .dig-header:hover {
    border-color: rgba(29, 242, 166, 0.3);
    background: linear-gradient(135deg, #0c1015 0%, #0f141a 100%);
    box-shadow: inset 0 0 0 1px rgba(29, 242, 166, 0.1);
  }

  .dig-header:focus-visible {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(29, 242, 166, 0.15);
  }

  .dig-header .chevron {
    width: 18px;
    height: 18px;
    color: var(--accent);
    transition: all 0.2s ease;
  }

  .dig-header:hover .chevron {
    color: var(--accent-2);
  }

  .dig-header .chevron.expanded {
    transform: rotate(180deg);
  }

  .dig-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--text);
  }

  .dig-content {
    margin-top: 8px;
    padding: 10px 12px;
    background: linear-gradient(180deg, #080c10 0%, #0a0e13 100%);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3);
  }

  .dig-cmd {
    display: flex;
    align-items: center;
    gap: 8px;
    background: #0b1016;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 8px 10px;
  }

  .dig-cmd code {
    flex: 1;
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--accent);
    word-break: break-all;
  }

  .dig-copy-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    border-radius: 4px;
    transition: all 0.2s ease;
    flex-shrink: 0;
  }

  .dig-copy-btn:hover {
    color: var(--accent);
    background: rgba(29, 242, 166, 0.08);
  }

  .dig-copy-btn .icon {
    width: 14px;
    height: 14px;
    display: block;
  }
</style>