mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-29 13:50:35 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
82e64a60d8 | ||
|
|
3c4f561beb | ||
|
|
d04bf9ce72 |
No files matched your search
@@ -46,6 +46,8 @@
|
||||
<a href="./README.md">English</a> · <a href="./README.zh-CN.md">简体中文</a> · <a href="./README.zh-TW.md">繁體中文</a> · <a href="./README.ja-JP.md">日本語</a>
|
||||
</p>
|
||||
|
||||
> **お知らせ:** プラグインシステムおよび現在の公式プラグイン(Netcatty Database、Netcatty Monitor)は、まだ不安定な段階にあります。ご利用の際はご注意ください。問題があればいつでも [Issue を作成](https://github.com/binaricat/Netcatty/issues)してください。できるだけ早く対応します。詳しくは[お知らせ](https://github.com/binaricat/Netcatty/discussions/4554)をご覧ください。
|
||||
|
||||
---
|
||||
|
||||
<img width="3358" height="1984" alt="image" src="docs/assets/readme/overview.webp" />
|
||||
@@ -84,6 +86,8 @@ Catty Agent にサーバーの健全性チェックを依頼すると、適切
|
||||
<a name="plugin-system"></a>
|
||||
# 🧩 プラグインシステム — Netcatty を安全に拡張
|
||||
|
||||
> **お知らせ:** プラグインシステムおよび現在の公式プラグイン(Netcatty Database、Netcatty Monitor)は、まだ不安定な段階にあります。ご利用の際はご注意ください。問題があればいつでも [Issue を作成](https://github.com/binaricat/Netcatty/issues)してください。できるだけ早く対応します。詳しくは[お知らせ](https://github.com/binaricat/Netcatty/discussions/4554)をご覧ください。
|
||||
|
||||
> **SSH 鍵やホストレンダラーを公開することなく機能を追加できます。** Netcatty のセキュリティ優先プラグイン基盤は、通常プラグインをオフラインのサンドボックス内で実行し、**ホストレンダリング UI**(Closed Shadow DOM + ネイティブ Monaco、DataGrid、BI/メトリクスチャート、Vault SSH トンネルプロキシ)のみで画面を描画します。
|
||||
|
||||
### ✨ 主な特徴
|
||||
|
||||
@@ -46,6 +46,8 @@
|
||||
<a href="./README.md">English</a> · <a href="./README.zh-CN.md">简体中文</a> · <a href="./README.zh-TW.md">繁體中文</a> · <a href="./README.ja-JP.md">日本語</a>
|
||||
</p>
|
||||
|
||||
> **Notice:** The plugin system and the current official plugins (Netcatty Database and Netcatty Monitor) are still in an unstable stage. Please keep that in mind while using them. If you run into a problem, feel free to [open an Issue](https://github.com/binaricat/Netcatty/issues) — I'll fix it as soon as I can. See the [announcement](https://github.com/binaricat/Netcatty/discussions/4554).
|
||||
|
||||
---
|
||||
|
||||
<img width="3358" height="1984" alt="image" src="docs/assets/readme/overview.webp" />
|
||||
@@ -86,6 +88,8 @@ Watch Catty Agent orchestrate a Docker Swarm cluster across two servers in one c
|
||||
<a name="plugin-system"></a>
|
||||
# 🧩 Plugin System — Extend Netcatty Safely
|
||||
|
||||
> **Notice:** The plugin system and the current official plugins (Netcatty Database and Netcatty Monitor) are still in an unstable stage. Please keep that in mind while using them. If you run into a problem, feel free to [open an Issue](https://github.com/binaricat/Netcatty/issues) — I'll fix it as soon as I can. See the [announcement](https://github.com/binaricat/Netcatty/discussions/4554).
|
||||
|
||||
> **Build and install rich extensions without exposing your SSH keys or host renderer.** Netcatty's security-first plugin platform runs ordinary extensions inside an offline, sandboxed runtime and renders custom views exclusively through **Host-Rendered UI** (closed Shadow DOM + native Monaco, DataGrid, BI/Metric charts, and Vault SSH tunnel brokers).
|
||||
|
||||
### ✨ Highlights
|
||||
|
||||
@@ -46,6 +46,8 @@
|
||||
<a href="./README.md">English</a> · <a href="./README.zh-CN.md">简体中文</a> · <a href="./README.zh-TW.md">繁體中文</a> · <a href="./README.ja-JP.md">日本語</a>
|
||||
</p>
|
||||
|
||||
> **说明:** 插件系统和现有的官方插件(Netcatty 数据库、Netcatty 监控)目前仍处于不稳定阶段,使用时请多加留意。如果你遇到问题,欢迎随时[提交 Issue](https://github.com/binaricat/Netcatty/issues),我会尽快修复。详见[公告](https://github.com/binaricat/Netcatty/discussions/4554)。
|
||||
|
||||
---
|
||||
|
||||
<img width="3358" height="1984" alt="image" src="docs/assets/readme/overview.webp" />
|
||||
@@ -84,6 +86,8 @@
|
||||
<a name="plugin-system"></a>
|
||||
# 🧩 插件系统 — 安全扩展你的工作台
|
||||
|
||||
> **说明:** 插件系统和现有的官方插件(Netcatty 数据库、Netcatty 监控)目前仍处于不稳定阶段,使用时请多加留意。如果你遇到问题,欢迎随时[提交 Issue](https://github.com/binaricat/Netcatty/issues),我会尽快修复。详见[公告](https://github.com/binaricat/Netcatty/discussions/4554)。
|
||||
|
||||
> **在不暴露 SSH 密钥与主进程渲染器的前提下自由扩展 Netcatty。** Netcatty 的安全优先插件平台将普通插件隔离在离线沙箱中运行,并通过**宿主渲染 UI**(Closed Shadow DOM + 原生 Monaco 编辑器、虚拟化数据网格、实时监控/BI 图表与 Vault SSH 隧道代理)呈现原生手感界面。
|
||||
|
||||
### ✨ 核心亮点
|
||||
|
||||
@@ -46,6 +46,8 @@
|
||||
<a href="./README.md">English</a> · <a href="./README.zh-CN.md">简体中文</a> · <a href="./README.zh-TW.md">繁體中文</a> · <a href="./README.ja-JP.md">日本語</a>
|
||||
</p>
|
||||
|
||||
> **說明:** 外掛系統與現有的官方外掛(Netcatty 資料庫、Netcatty 監控)目前仍處於不穩定階段,使用時請多加留意。若遇到問題,歡迎隨時[提交 Issue](https://github.com/binaricat/Netcatty/issues),我會盡快修復。詳見[公告](https://github.com/binaricat/Netcatty/discussions/4554)。
|
||||
|
||||
---
|
||||
|
||||
<img width="3358" height="1984" alt="image" src="docs/assets/readme/overview.webp" />
|
||||
@@ -84,6 +86,8 @@
|
||||
<a name="plugin-system"></a>
|
||||
# 🧩 外掛系統 —— 安全擴充你的工作台
|
||||
|
||||
> **說明:** 外掛系統與現有的官方外掛(Netcatty 資料庫、Netcatty 監控)目前仍處於不穩定階段,使用時請多加留意。若遇到問題,歡迎隨時[提交 Issue](https://github.com/binaricat/Netcatty/issues),我會盡快修復。詳見[公告](https://github.com/binaricat/Netcatty/discussions/4554)。
|
||||
|
||||
> **在不暴露 SSH 金鑰與宿主渲染器的前提下自由擴充 Netcatty。** Netcatty 的安全優先外掛平台將一般外掛隔離於離線沙箱中執行,並透過**宿主渲染 UI**(Closed Shadow DOM + 原生 Monaco 編輯器、虛擬化資料格、即時監控/BI 圖表與 Vault SSH 通道代理)呈現原生體驗。
|
||||
|
||||
### ✨ 核心亮點
|
||||
|
||||
@@ -16,5 +16,19 @@ export const useClipboardBackend = () => {
|
||||
return await bridge.writeClipboardText(text) === true;
|
||||
}, []);
|
||||
|
||||
return { readClipboardText, writeClipboardText };
|
||||
const readClipboardImagePng = useCallback(async (): Promise<Uint8Array | null> => {
|
||||
const bridge = netcattyBridge.get();
|
||||
if (bridge?.readClipboardImagePng) return await bridge.readClipboardImagePng();
|
||||
|
||||
// Browser fallback when the native clipboard bridge is unavailable.
|
||||
const items = await navigator.clipboard?.read();
|
||||
for (const item of items ?? []) {
|
||||
if (!item.types.includes("image/png")) continue;
|
||||
const blob = await item.getType("image/png");
|
||||
return new Uint8Array(await blob.arrayBuffer());
|
||||
}
|
||||
return null;
|
||||
}, []);
|
||||
|
||||
return { readClipboardText, writeClipboardText, readClipboardImagePng };
|
||||
};
|
||||
@@ -9,6 +9,8 @@ import React, {
|
||||
} from 'react';
|
||||
import { AlertCircle, UploadCloud } from 'lucide-react';
|
||||
import { useI18n } from '../../application/i18n/I18nProvider';
|
||||
import { useClipboardBackend } from '../../application/state/useClipboardBackend';
|
||||
import { buildRdpClipboardItemData, nextRdpClipboardGuardState, selectRdpClipboardData } from '../../domain/rdpClipboard';
|
||||
import {
|
||||
computeMonitorVirtualBounds,
|
||||
mapViewportToVirtualDesktopCoords,
|
||||
@@ -186,6 +188,7 @@ export const RdpCanvasView = React.memo(forwardRef<RemoteDesktopCanvasHandle, Rd
|
||||
ref,
|
||||
) {
|
||||
const { t } = useI18n();
|
||||
const { readClipboardImagePng } = useClipboardBackend();
|
||||
const containerRef = useRef<HTMLDivElement | null>(null);
|
||||
const viewportRef = useRef<HTMLDivElement | null>(null);
|
||||
const canvasRef = useRef<HTMLCanvasElement | null>(null);
|
||||
@@ -199,7 +202,9 @@ export const RdpCanvasView = React.memo(forwardRef<RemoteDesktopCanvasHandle, Rd
|
||||
const modRef = useRef<IronRdpModule | null>(null);
|
||||
const audioPlayerRef = useRef<RdpAudioPlayer | null>(null);
|
||||
const lastAltRightUpRef = useRef(0);
|
||||
const lastRemoteClipboardTextRef = useRef('');
|
||||
const lastClipboardGuardStateRef = useRef<ReturnType<typeof nextRdpClipboardGuardState>>({
|
||||
lastText: '', lastPngFingerprint: null,
|
||||
});
|
||||
const uploadedFilesRef = useRef<File[]>([]);
|
||||
const uploadTransferIdRef = useRef<string>('');
|
||||
const uploadedHighWaterByIndexRef = useRef<Map<number, number>>(new Map());
|
||||
@@ -601,21 +606,31 @@ export const RdpCanvasView = React.memo(forwardRef<RemoteDesktopCanvasHandle, Rd
|
||||
const mod = modRef.current;
|
||||
if (!session || !mod) return;
|
||||
try {
|
||||
const text = await navigator.clipboard?.readText();
|
||||
if (text && (force || text !== lastRemoteClipboardTextRef.current)) {
|
||||
lastRemoteClipboardTextRef.current = text;
|
||||
const data = new mod.ClipboardData();
|
||||
try {
|
||||
data.addText('text/plain', text);
|
||||
await session.onClipboardPaste(data);
|
||||
} finally {
|
||||
try { data.free(); } catch { /* ignore */ }
|
||||
}
|
||||
// A denied or unsupported read of one format must not block the other.
|
||||
const [textResult, pngResult] = await Promise.allSettled([
|
||||
Promise.resolve().then(() => navigator.clipboard?.readText()),
|
||||
readClipboardImagePng(),
|
||||
]);
|
||||
const { text, pngBytes } = selectRdpClipboardData({
|
||||
text: textResult.status === 'fulfilled' ? textResult.value : null,
|
||||
pngBytes: pngResult.status === 'fulfilled' ? pngResult.value : null,
|
||||
...lastClipboardGuardStateRef.current,
|
||||
force,
|
||||
});
|
||||
if (!text && !pngBytes) return;
|
||||
const data = new mod.ClipboardData();
|
||||
try {
|
||||
if (text) data.addText('text/plain', text);
|
||||
if (pngBytes) data.addBinary('image/png', pngBytes);
|
||||
await session.onClipboardPaste(data);
|
||||
lastClipboardGuardStateRef.current = nextRdpClipboardGuardState({ text, pngBytes });
|
||||
} finally {
|
||||
try { data.free(); } catch { /* ignore */ }
|
||||
}
|
||||
} catch {
|
||||
// Clipboard permission or empty
|
||||
}
|
||||
}, []);
|
||||
}, [readClipboardImagePng]);
|
||||
|
||||
const syncClipboardNow = useCallback(() => syncLocalClipboardToRdp(true), [syncLocalClipboardToRdp]);
|
||||
|
||||
@@ -1168,18 +1183,20 @@ export const RdpCanvasView = React.memo(forwardRef<RemoteDesktopCanvasHandle, Rd
|
||||
pngBytes = val;
|
||||
}
|
||||
}
|
||||
lastClipboardGuardStateRef.current = nextRdpClipboardGuardState({ text: plainText, pngBytes });
|
||||
if (pngBytes && typeof ClipboardItem !== 'undefined' && navigator.clipboard?.write) {
|
||||
const pngCopy = new Uint8Array(pngBytes.byteLength);
|
||||
pngCopy.set(pngBytes);
|
||||
const blob = new Blob([pngCopy], { type: 'image/png' });
|
||||
void navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })]).catch(() => {
|
||||
if (plainText) {
|
||||
lastRemoteClipboardTextRef.current = plainText;
|
||||
void navigator.clipboard?.writeText(plainText).catch(() => {});
|
||||
const data = buildRdpClipboardItemData({ text: plainText, pngBytes });
|
||||
void navigator.clipboard.write([new ClipboardItem(data)]).catch(async () => {
|
||||
if (plainText !== null) {
|
||||
// Some clipboards reject mixed formats; keep the PNG if possible.
|
||||
try {
|
||||
await navigator.clipboard.write([new ClipboardItem({ 'image/png': data['image/png'] })]);
|
||||
} catch {
|
||||
await navigator.clipboard?.writeText(plainText).catch(() => {});
|
||||
}
|
||||
}
|
||||
});
|
||||
} else if (plainText !== null) {
|
||||
lastRemoteClipboardTextRef.current = plainText;
|
||||
void navigator.clipboard?.writeText(plainText).catch(() => {});
|
||||
}
|
||||
} catch {
|
||||
|
||||
@@ -301,7 +301,8 @@ export function getCommandToRecordOnEnter(
|
||||
/**
|
||||
* Keystrokes that remove the text a snippet acceptance replaces.
|
||||
* Line-start keeps the shell kill-line (Ctrl+U, or backspaces on Windows).
|
||||
* Mid-line and program read-prompts delete only the word being typed.
|
||||
* Matching label prefixes replace the longest suffix starting at a word boundary.
|
||||
* Otherwise, mid-line and program read-prompts delete only the word being typed.
|
||||
* POSIX word delete sends DEL, the tty erase character. Ctrl-H is literal
|
||||
* input to canonical `read` when erase is ^?, while readline still deletes
|
||||
* on DEL. Windows line editing keeps backspace.
|
||||
@@ -313,18 +314,35 @@ export function computeSnippetAcceptClearSequence(options: {
|
||||
os: string;
|
||||
promptText: string;
|
||||
snippetKind?: "snippet" | "script";
|
||||
snippetLabel?: string;
|
||||
}): string {
|
||||
const line = options.line ?? "";
|
||||
if (line.length === 0) return "";
|
||||
|
||||
let matchedLabelSuffix: string | undefined;
|
||||
if (options.snippetKind !== "script" && options.snippetLabel) {
|
||||
const label = options.snippetLabel.toLowerCase();
|
||||
// Scan from the start so the first boundary match is the longest suffix.
|
||||
for (let start = 0; start < line.length; start += 1) {
|
||||
if (start > 0 && !/\s/.test(line[start - 1])) continue;
|
||||
const suffix = line.slice(start);
|
||||
if (label.startsWith(suffix.toLowerCase())) {
|
||||
matchedLabelSuffix = suffix;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const { currentWord, wordIndex } = parseCommandLine(line);
|
||||
const replaceWord = options.snippetKind !== "script"
|
||||
&& currentWord.length > 0
|
||||
&& line.endsWith(currentWord)
|
||||
&& (wordIndex > 0 || isProgramInputPromptText(options.promptText));
|
||||
const replaceWord = matchedLabelSuffix !== undefined
|
||||
? matchedLabelSuffix !== line
|
||||
: options.snippetKind !== "script"
|
||||
&& currentWord.length > 0
|
||||
&& line.endsWith(currentWord)
|
||||
&& (wordIndex > 0 || isProgramInputPromptText(options.promptText));
|
||||
if (replaceWord) {
|
||||
const eraseChar = isWindowsShellLineInput(options.os, options.promptText) ? "\b" : "\x7f";
|
||||
return eraseChar.repeat(Array.from(currentWord).length);
|
||||
return eraseChar.repeat(Array.from(matchedLabelSuffix ?? currentWord).length);
|
||||
}
|
||||
|
||||
return isWindowsShellLineInput(options.os, options.promptText)
|
||||
|
||||
@@ -1340,7 +1340,7 @@ export function useTerminalAutocomplete(
|
||||
[handleCommandHistoryCleared],
|
||||
);
|
||||
|
||||
/** Accept a snippet: clear the matched word (or the whole line for a script
|
||||
/** Accept a snippet: clear the matched label or word (or the whole line for a script
|
||||
* or a shell prompt), then run it via onAcceptSnippet. */
|
||||
const acceptSnippet = useCallback((snippet: Snippet): boolean => {
|
||||
const term = termRef.current;
|
||||
@@ -1358,6 +1358,7 @@ export function useTerminalAutocomplete(
|
||||
os: hostOsRef.current,
|
||||
promptText: prompt.promptText,
|
||||
snippetKind: snippet.kind,
|
||||
snippetLabel: snippet.label,
|
||||
});
|
||||
if (clearSequence) writeToTerminal(clearSequence);
|
||||
}
|
||||
|
||||
@@ -387,6 +387,102 @@ test("snippet accept deletes only the mid-line word (#3622)", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("snippet accept clears a whole-line label prefix (#4549)", () => {
|
||||
for (const snippetKind of [undefined, "snippet"] as const) {
|
||||
for (const line of ["Check size", "Check si", "check size"]) {
|
||||
for (const os of ["linux", "windows"]) {
|
||||
assert.equal(
|
||||
computeSnippetAcceptClearSequence({
|
||||
line,
|
||||
os,
|
||||
promptText: os === "windows" ? "C:\\Users\\me>" : "user@host:~$ ",
|
||||
snippetKind,
|
||||
snippetLabel: "Check size",
|
||||
}),
|
||||
os === "windows" ? "\b".repeat(line.length) : "\x15",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("snippet accept deletes the longest matching label suffix at a word boundary (#4549)", () => {
|
||||
for (const os of ["linux", "windows"]) {
|
||||
for (const suffix of ["Check size", "Check si", "check size", "Check \u{1F680}"]) {
|
||||
assert.equal(
|
||||
computeSnippetAcceptClearSequence({
|
||||
line: `git pull ${suffix}`,
|
||||
os,
|
||||
promptText: os === "windows" ? "C:\\Users\\me>" : "user@host:~$ ",
|
||||
snippetKind: "snippet",
|
||||
snippetLabel: suffix === "Check \u{1F680}" ? suffix : "Check size",
|
||||
}),
|
||||
(os === "windows" ? "\b" : "\x7f").repeat(Array.from(suffix).length),
|
||||
);
|
||||
}
|
||||
}
|
||||
assert.equal(
|
||||
computeSnippetAcceptClearSequence({
|
||||
line: "git pull\tCheck\tsize",
|
||||
os: "linux",
|
||||
promptText: "user@host:~$ ",
|
||||
snippetLabel: "Check\tsize",
|
||||
}),
|
||||
"\x7f".repeat("Check\tsize".length),
|
||||
);
|
||||
assert.equal(
|
||||
computeSnippetAcceptClearSequence({
|
||||
line: "git pull use use",
|
||||
os: "linux",
|
||||
promptText: "user@host:~$ ",
|
||||
snippetLabel: "use use more",
|
||||
}),
|
||||
"\x7f".repeat("use use".length),
|
||||
);
|
||||
});
|
||||
|
||||
test("snippet accept preserves word deletion with a single-word or unmatched label (#3622, #4549)", () => {
|
||||
for (const snippetLabel of [undefined, "use", "user name", "Check size"]) {
|
||||
assert.equal(
|
||||
computeSnippetAcceptClearSequence({
|
||||
line: "git pull use",
|
||||
os: "linux",
|
||||
promptText: "user@host:~$ ",
|
||||
snippetLabel,
|
||||
}),
|
||||
"\x7f".repeat(3),
|
||||
);
|
||||
}
|
||||
for (const line of ["size", "git pull size", "git pull xCheck size"]) {
|
||||
assert.equal(
|
||||
computeSnippetAcceptClearSequence({
|
||||
line,
|
||||
os: "linux",
|
||||
promptText: "user@host:~$ ",
|
||||
snippetLabel: "Check size",
|
||||
}),
|
||||
line === "size" ? "\x15" : "\x7f".repeat(4),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("script snippet accept ignores matching labels and clears the whole line (#4549)", () => {
|
||||
for (const os of ["linux", "windows"]) {
|
||||
for (const promptText of ["user@host:~$ ", "Email: "]) {
|
||||
assert.equal(
|
||||
computeSnippetAcceptClearSequence({
|
||||
line: "git pull Check size",
|
||||
os,
|
||||
promptText,
|
||||
snippetKind: "script",
|
||||
snippetLabel: "Check size",
|
||||
}),
|
||||
os === "windows" ? "\b".repeat("git pull Check size".length) : "\x15",
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("script snippet accept still clears the whole line (#3622)", () => {
|
||||
assert.equal(
|
||||
computeSnippetAcceptClearSequence({
|
||||
@@ -453,4 +549,4 @@ test("computeAutocompleteAcceptWrite clears with backspaces on a mislabeled Wind
|
||||
}),
|
||||
"\b\b\buv run tkauto video sync-fs",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,157 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { buildRdpClipboardItemData, fingerprintRdpClipboardPng, nextRdpClipboardGuardState, selectRdpClipboardData } from './rdpClipboard';
|
||||
|
||||
const pngBytes = new Uint8Array([0x89, 0x50, 0x4e, 0x47]);
|
||||
const emptyState = { lastText: '', lastPngFingerprint: null };
|
||||
|
||||
test('mixed remote clipboard data preserves both local formats and does not echo on refocus', async () => {
|
||||
const snapshot = { text: 'caption', pngBytes };
|
||||
const data = buildRdpClipboardItemData(snapshot);
|
||||
assert.deepEqual(Object.keys(data).sort(), ['image/png', 'text/plain']);
|
||||
assert.equal(data['image/png'].type, 'image/png');
|
||||
assert.equal(data['text/plain'].type, 'text/plain');
|
||||
const localSnapshot = {
|
||||
text: await data['text/plain'].text(),
|
||||
pngBytes: new Uint8Array(await data['image/png'].arrayBuffer()),
|
||||
};
|
||||
assert.deepEqual(localSnapshot, snapshot);
|
||||
assert.deepEqual(selectRdpClipboardData({
|
||||
...nextRdpClipboardGuardState(snapshot),
|
||||
...localSnapshot,
|
||||
}), { text: null, pngBytes: null, pngFingerprint: null });
|
||||
});
|
||||
|
||||
test('local clipboard item data preserves image-only, text-only and empty text formats', async () => {
|
||||
const imageData = buildRdpClipboardItemData({ text: null, pngBytes });
|
||||
assert.deepEqual(Object.keys(imageData), ['image/png']);
|
||||
assert.deepEqual(new Uint8Array(await imageData['image/png'].arrayBuffer()), pngBytes);
|
||||
for (const text of ['hello', '']) {
|
||||
const textData = buildRdpClipboardItemData({ text, pngBytes: null });
|
||||
assert.deepEqual(Object.keys(textData), ['text/plain']);
|
||||
assert.equal(await textData['text/plain'].text(), text);
|
||||
}
|
||||
});
|
||||
|
||||
test('image-only RDP clipboard data is sent', () => {
|
||||
assert.deepEqual(selectRdpClipboardData({ ...emptyState, text: '', pngBytes }), {
|
||||
text: null,
|
||||
pngBytes,
|
||||
pngFingerprint: fingerprintRdpClipboardPng(pngBytes),
|
||||
});
|
||||
});
|
||||
|
||||
test('the last pushed or received PNG is skipped unless forced', () => {
|
||||
const input = {
|
||||
...emptyState,
|
||||
text: null,
|
||||
pngBytes: new Uint8Array(pngBytes),
|
||||
lastPngFingerprint: fingerprintRdpClipboardPng(pngBytes),
|
||||
};
|
||||
assert.equal(selectRdpClipboardData(input).pngBytes, null);
|
||||
assert.deepEqual(selectRdpClipboardData({ ...input, force: true }).pngBytes, pngBytes);
|
||||
assert.notEqual(fingerprintRdpClipboardPng(new Uint8Array([1, 2, 3, 4])), input.lastPngFingerprint);
|
||||
});
|
||||
|
||||
test('text-only RDP clipboard behavior is unchanged', () => {
|
||||
const input = { ...emptyState, text: 'hello', pngBytes: null };
|
||||
assert.equal(selectRdpClipboardData(input).text, 'hello');
|
||||
assert.equal(selectRdpClipboardData({ ...input, lastText: 'hello' }).text, null);
|
||||
assert.equal(selectRdpClipboardData({ ...input, lastText: 'hello', force: true }).text, 'hello');
|
||||
});
|
||||
|
||||
test('text and PNG are selected together', () => {
|
||||
const result = selectRdpClipboardData({ ...emptyState, text: 'caption', pngBytes });
|
||||
assert.equal(result.text, 'caption');
|
||||
assert.equal(result.pngBytes, pngBytes);
|
||||
});
|
||||
|
||||
test('a changed clipboard snapshot sends every present format', () => {
|
||||
const result = selectRdpClipboardData({
|
||||
...emptyState,
|
||||
text: 'new text',
|
||||
pngBytes,
|
||||
lastPngFingerprint: fingerprintRdpClipboardPng(pngBytes),
|
||||
});
|
||||
assert.equal(result.text, 'new text');
|
||||
assert.equal(result.pngBytes, pngBytes);
|
||||
const imageResult = selectRdpClipboardData({
|
||||
...emptyState,
|
||||
text: 'same text',
|
||||
lastText: 'same text',
|
||||
pngBytes,
|
||||
});
|
||||
assert.equal(imageResult.text, 'same text');
|
||||
assert.equal(imageResult.pngBytes, pngBytes);
|
||||
});
|
||||
|
||||
test('text then image then the same text replaces each clipboard snapshot', () => {
|
||||
const imageResult = selectRdpClipboardData({
|
||||
lastText: 'B', lastPngFingerprint: null, text: null, pngBytes,
|
||||
});
|
||||
const pngFingerprint = fingerprintRdpClipboardPng(pngBytes);
|
||||
assert.deepEqual(imageResult, { text: null, pngBytes, pngFingerprint });
|
||||
const imageState = nextRdpClipboardGuardState(imageResult);
|
||||
assert.deepEqual(imageState, { lastText: '', lastPngFingerprint: pngFingerprint });
|
||||
|
||||
const textResult = selectRdpClipboardData({ ...imageState, text: 'B', pngBytes: null });
|
||||
assert.deepEqual(textResult, { text: 'B', pngBytes: null, pngFingerprint: null });
|
||||
const textState = nextRdpClipboardGuardState(textResult);
|
||||
assert.deepEqual(textState, { lastText: 'B', lastPngFingerprint: null });
|
||||
assert.deepEqual(selectRdpClipboardData({ ...textState, text: null, pngBytes }), imageResult);
|
||||
});
|
||||
|
||||
test('text-only replacement sends unchanged text when the previous snapshot included a PNG', () => {
|
||||
assert.deepEqual(selectRdpClipboardData({
|
||||
lastText: 'B',
|
||||
lastPngFingerprint: fingerprintRdpClipboardPng(pngBytes),
|
||||
text: 'B',
|
||||
pngBytes: null,
|
||||
}), { text: 'B', pngBytes: null, pngFingerprint: null });
|
||||
});
|
||||
|
||||
test('image-only replacement sends the same PNG when the previous snapshot included text', () => {
|
||||
const pngFingerprint = fingerprintRdpClipboardPng(pngBytes);
|
||||
assert.deepEqual(selectRdpClipboardData({
|
||||
lastText: 'B',
|
||||
lastPngFingerprint: pngFingerprint,
|
||||
text: null,
|
||||
pngBytes,
|
||||
}), { text: null, pngBytes, pngFingerprint });
|
||||
});
|
||||
|
||||
test('an identical text and PNG snapshot is skipped unless forced', () => {
|
||||
const pngFingerprint = fingerprintRdpClipboardPng(pngBytes);
|
||||
const input = {
|
||||
lastText: 'B',
|
||||
lastPngFingerprint: pngFingerprint,
|
||||
text: 'B',
|
||||
pngBytes: new Uint8Array(pngBytes),
|
||||
};
|
||||
assert.deepEqual(selectRdpClipboardData(input), {
|
||||
text: null, pngBytes: null, pngFingerprint: null,
|
||||
});
|
||||
assert.deepEqual(selectRdpClipboardData({ ...input, force: true }), {
|
||||
text: 'B', pngBytes, pngFingerprint,
|
||||
});
|
||||
assert.deepEqual(nextRdpClipboardGuardState(input), {
|
||||
lastText: 'B', lastPngFingerprint: pngFingerprint,
|
||||
});
|
||||
});
|
||||
|
||||
test('an empty clipboard snapshot clears both guard fields and sends nothing', () => {
|
||||
const input = { text: undefined, pngBytes: new Uint8Array() };
|
||||
assert.deepEqual(nextRdpClipboardGuardState(input), emptyState);
|
||||
assert.deepEqual(selectRdpClipboardData({ ...emptyState, ...input }), {
|
||||
text: null, pngBytes: null, pngFingerprint: null,
|
||||
});
|
||||
});
|
||||
|
||||
test('empty clipboard formats are ignored even when forced', () => {
|
||||
assert.deepEqual(selectRdpClipboardData({
|
||||
...emptyState,
|
||||
text: undefined,
|
||||
pngBytes: new Uint8Array(),
|
||||
force: true,
|
||||
}), { text: null, pngBytes: null, pngFingerprint: null });
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
export function buildRdpClipboardItemData({ text, pngBytes }: {
|
||||
text: string | null;
|
||||
pngBytes: Uint8Array | null;
|
||||
}): Record<string, Blob> {
|
||||
const data: Record<string, Blob> = {};
|
||||
if (pngBytes) {
|
||||
const pngCopy = new Uint8Array(pngBytes.byteLength);
|
||||
pngCopy.set(pngBytes);
|
||||
data['image/png'] = new Blob([pngCopy], { type: 'image/png' });
|
||||
}
|
||||
if (text !== null) {
|
||||
data['text/plain'] = new Blob([text], { type: 'text/plain' });
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
export function fingerprintRdpClipboardPng(bytes: Uint8Array): string {
|
||||
// FNV-1a plus the byte length is sufficient for clipboard echo detection.
|
||||
let hash = 0x811c9dc5;
|
||||
for (const byte of bytes) {
|
||||
hash = Math.imul(hash ^ byte, 0x01000193);
|
||||
}
|
||||
return `${bytes.length}:${(hash >>> 0).toString(16)}`;
|
||||
}
|
||||
|
||||
export function nextRdpClipboardGuardState({ text, pngBytes }: {
|
||||
text: string | null | undefined;
|
||||
pngBytes: Uint8Array | null;
|
||||
}) {
|
||||
return {
|
||||
lastText: text || '',
|
||||
lastPngFingerprint: pngBytes?.length ? fingerprintRdpClipboardPng(pngBytes) : null,
|
||||
};
|
||||
}
|
||||
|
||||
export function selectRdpClipboardData({
|
||||
text,
|
||||
pngBytes,
|
||||
lastText,
|
||||
lastPngFingerprint,
|
||||
force = false,
|
||||
}: {
|
||||
text: string | null | undefined;
|
||||
pngBytes: Uint8Array | null;
|
||||
lastText: string;
|
||||
lastPngFingerprint: string | null;
|
||||
force?: boolean;
|
||||
}) {
|
||||
const current = nextRdpClipboardGuardState({ text, pngBytes });
|
||||
if (!force && current.lastText === lastText && current.lastPngFingerprint === lastPngFingerprint) {
|
||||
return { text: null, pngBytes: null, pngFingerprint: null };
|
||||
}
|
||||
return {
|
||||
text: current.lastText || null,
|
||||
pngBytes: current.lastPngFingerprint !== null ? pngBytes : null,
|
||||
pngFingerprint: current.lastPngFingerprint,
|
||||
};
|
||||
}
|
||||
@@ -237,16 +237,11 @@ function createClipboardImageFileName(date = new Date()) {
|
||||
return `netcatty-paste-${formatClipboardImageTimestamp(date)}.png`;
|
||||
}
|
||||
|
||||
async function readClipboardImage({
|
||||
async function readClipboardImagePngBytes({
|
||||
clipboard,
|
||||
fsImpl = fs,
|
||||
tempDirBridge,
|
||||
nativeImageImpl = null,
|
||||
now = () => new Date(),
|
||||
} = {}) {
|
||||
if (!clipboard || !tempDirBridge?.getTempFilePath) {
|
||||
return null;
|
||||
}
|
||||
if (!clipboard) return null;
|
||||
|
||||
try {
|
||||
let content = null;
|
||||
@@ -297,7 +292,24 @@ async function readClipboardImage({
|
||||
}
|
||||
|
||||
if (!Buffer.isBuffer(content) || content.length === 0) return null;
|
||||
return content;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function readClipboardImage({
|
||||
clipboard,
|
||||
fsImpl = fs,
|
||||
tempDirBridge,
|
||||
nativeImageImpl = null,
|
||||
now = () => new Date(),
|
||||
} = {}) {
|
||||
if (!clipboard || !tempDirBridge?.getTempFilePath) return null;
|
||||
|
||||
try {
|
||||
const content = await readClipboardImagePngBytes({ clipboard, nativeImageImpl });
|
||||
if (!content) return null;
|
||||
const name = createClipboardImageFileName(now());
|
||||
const imagePath = tempDirBridge.getTempFilePath(name);
|
||||
await fsImpl.promises.writeFile(imagePath, content);
|
||||
@@ -381,4 +393,5 @@ module.exports = {
|
||||
parseClipboardTextFilePaths,
|
||||
readClipboardFiles,
|
||||
readClipboardImage,
|
||||
readClipboardImagePngBytes,
|
||||
};
|
||||
@@ -11,6 +11,7 @@ const {
|
||||
parseClipboardTextFilePaths,
|
||||
readClipboardFiles,
|
||||
readClipboardImage,
|
||||
readClipboardImagePngBytes,
|
||||
} = require("./clipboardFiles.cjs");
|
||||
|
||||
const createFs = (entries) => ({
|
||||
@@ -158,6 +159,59 @@ test("creates stable clipboard image file names", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("reads native clipboard PNG bytes without a temp directory", async () => {
|
||||
const content = Buffer.from([0x89, 0x50, 0x4e, 0x47]);
|
||||
const result = await readClipboardImagePngBytes({
|
||||
clipboard: {
|
||||
readImage: () => ({ isEmpty: () => false, toPNG: () => content }),
|
||||
read: () => assert.fail("native image read should take precedence"),
|
||||
},
|
||||
});
|
||||
|
||||
assert.deepEqual(result, content);
|
||||
});
|
||||
|
||||
test("PNG bytes helper returns null for empty native images or PNG buffers", async () => {
|
||||
assert.equal(await readClipboardImagePngBytes({
|
||||
clipboard: {
|
||||
readImage: () => ({
|
||||
isEmpty: () => true,
|
||||
toPNG: () => assert.fail("empty image must not be encoded"),
|
||||
}),
|
||||
},
|
||||
}), null);
|
||||
assert.equal(await readClipboardImagePngBytes({
|
||||
clipboard: { readImage: () => ({ toPNG: () => Buffer.alloc(0) }) },
|
||||
}), null);
|
||||
});
|
||||
|
||||
test("PNG bytes helper returns null for missing or failing clipboard reads", async () => {
|
||||
assert.equal(await readClipboardImagePngBytes(), null);
|
||||
assert.equal(await readClipboardImagePngBytes({
|
||||
clipboard: { readImage: () => { throw new Error("clipboard unavailable"); } },
|
||||
}), null);
|
||||
});
|
||||
|
||||
test("PNG bytes helper converts async clipboard images through nativeImage", async () => {
|
||||
const rawBytes = Buffer.from("jpeg");
|
||||
const pngBytes = Buffer.from("png");
|
||||
const result = await readClipboardImagePngBytes({
|
||||
clipboard: {
|
||||
read: async () => [{
|
||||
types: ["image/jpeg"],
|
||||
getType: async () => new Blob([rawBytes], { type: "image/jpeg" }),
|
||||
}],
|
||||
},
|
||||
nativeImageImpl: {
|
||||
createFromBuffer: (bytes) => {
|
||||
assert.deepEqual(bytes, rawBytes);
|
||||
return { isEmpty: () => false, toPNG: () => pngBytes };
|
||||
},
|
||||
},
|
||||
});
|
||||
assert.deepEqual(result, pngBytes);
|
||||
});
|
||||
|
||||
test("writes clipboard image to Netcatty temp directory", async () => {
|
||||
const writes = [];
|
||||
const content = Buffer.from([1, 2, 3]);
|
||||
@@ -346,6 +400,7 @@ test("readClipboardImage and hasClipboardImage support Electron 44+ async W3C cl
|
||||
};
|
||||
|
||||
assert.equal(await hasClipboardImage({ clipboard }), true);
|
||||
assert.deepEqual(await readClipboardImagePngBytes({ clipboard }), pngBytes);
|
||||
const saved = await readClipboardImage({
|
||||
clipboard,
|
||||
fsImpl: {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
let cloudSyncSessionPassword = null;
|
||||
const perfTrace = require("../perfTrace.cjs");
|
||||
const { readClipboardFiles, readClipboardImage, hasClipboardImage } = require("../bridges/clipboardFiles.cjs");
|
||||
const { readClipboardFiles, readClipboardImage, readClipboardImagePngBytes, hasClipboardImage } = require("../bridges/clipboardFiles.cjs");
|
||||
const { registerSystemNotificationHandlers } = require("../bridges/systemNotification.cjs");
|
||||
const {
|
||||
installWindowAttention,
|
||||
@@ -1207,6 +1207,10 @@ function createBridgeRegistrar(context) {
|
||||
return await readClipboardImage({ clipboard, fsImpl: fs, tempDirBridge });
|
||||
});
|
||||
|
||||
ipcMain.handle("netcatty:clipboard:readImagePng", async () => {
|
||||
return await readClipboardImagePngBytes({ clipboard });
|
||||
});
|
||||
|
||||
ipcMain.handle("netcatty:clipboard:hasImage", async () => {
|
||||
return await hasClipboardImage({ clipboard });
|
||||
});
|
||||
|
||||
@@ -2047,6 +2047,9 @@ function createPreloadApi(ctx) {
|
||||
readClipboardImage: async () => {
|
||||
return ipcRenderer.invoke("netcatty:clipboard:readImage");
|
||||
},
|
||||
readClipboardImagePng: async () => {
|
||||
return ipcRenderer.invoke("netcatty:clipboard:readImagePng");
|
||||
},
|
||||
hasClipboardImage: async () => {
|
||||
return ipcRenderer.invoke("netcatty:clipboard:hasImage");
|
||||
},
|
||||
|
||||
+1
@@ -170,6 +170,7 @@ declare global {
|
||||
writeClipboardText?(text: string): Promise<boolean>;
|
||||
readClipboardFiles?(): Promise<Array<{ path: string; name: string; isDirectory: boolean; size?: number }>>;
|
||||
readClipboardImage?(): Promise<{ path: string; name: string; mediaType: string; size?: number } | null>;
|
||||
readClipboardImagePng?(): Promise<Uint8Array | null>;
|
||||
hasClipboardImage?(): Promise<boolean>;
|
||||
|
||||
// Credential encryption (field-level safeStorage for sensitive data at rest)
|
||||
|
||||
Reference in new issue
Block a user