mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-27 11:56:20 +00:00
- Add host-rendered native IDE primitives (PluginCodeEditor/Monaco, PluginDataGrid/@tanstack/react-virtual, PluginSplitPane, PluginErCanvas) with Shadow DOM slot projection (zero iframes, zero plugin JS in renderer) - Expand Remote DOM budgets, expose full 22 Netcatty semantic CSS tokens, add dblclick/contextmenu/scroll/selection events, cursor-anchored PluginPopover, database theme icon, and full-bleed chrome flag - Add dynamic user-approved TCP/TLS byte streams (incl. localhost opt-in blocking internal control ports), in-band STARTTLS upgrade with cert fingerprint confirmation, durable revocable Vault SSH host bindings, plugin-owned encrypted secrets, native ui.openFile/saveFile streaming handles, 4 MiB clipboard, and prefix-filtered storage.keys - Optimize plugin view image pipeline with pooled sandboxed rasterizer window (auto-destroyed on idle/error/dispose), LRU version+hash cache in PluginViewResources, transient failure eviction in HostDrawnViewHost, renderer-side session deduplication, and RemoteImageElement cache + retry - Temporarily enable plugin host by default in hostBootstrap.cjs (marked TODO(revert), opt-out via NETCATTY_PLUGIN_DEV=0)
122 lines
4.8 KiB
JavaScript
122 lines
4.8 KiB
JavaScript
"use strict";
|
|
|
|
const fs = require("node:fs");
|
|
const net = require("node:net");
|
|
|
|
function failure(_code, message) { return new TypeError(message); }
|
|
|
|
function canonicalizeStreamEndpoint(value) {
|
|
if (typeof value !== "string" || value.length > 512) {
|
|
throw failure("invalidArgument", "Byte stream endpoint is invalid");
|
|
}
|
|
// URL's permissive non-special scheme parser accepts paths, userinfo, and
|
|
// alternate IPv4 spellings. Require a complete unambiguous authority instead.
|
|
const match = /^(tcp|tls):\/\/(\[[0-9a-fA-F:.]+\]|[A-Za-z0-9.-]+):([0-9]{1,5})$/u.exec(value);
|
|
if (!match) throw failure("invalidArgument", "Byte streams require an exact TCP or TLS host and port");
|
|
let host = match[2].toLowerCase();
|
|
const port = Number(match[3]);
|
|
if (port < 1 || port > 65535 || String(port) !== match[3]) {
|
|
throw failure("invalidArgument", "Byte stream port is invalid");
|
|
}
|
|
if (host.startsWith("[")) {
|
|
host = host.slice(1, -1);
|
|
if (net.isIP(host) !== 6) throw failure("invalidArgument", "Byte stream IPv6 address is invalid");
|
|
host = new URL(`http://[${host}]`).hostname.slice(1, -1);
|
|
} else if (!net.isIP(host)) {
|
|
if (host.length > 253 || /^[0-9.]+$/u.test(host) || host.split(".").some((label) => (
|
|
!/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/u.test(label)
|
|
))) throw failure("invalidArgument", "Byte stream hostname is invalid");
|
|
}
|
|
return Object.freeze({
|
|
endpoint: `${match[1]}://${net.isIP(host) === 6 ? `[${host}]` : host}:${port}`,
|
|
transport: match[1], host, port,
|
|
});
|
|
}
|
|
|
|
function normalizeStreamAddress(address) {
|
|
if (net.isIP(address) === 4) return address;
|
|
if (net.isIP(address) !== 6) return null;
|
|
const canonical = new URL(`http://[${address.split("%")[0]}]`).hostname.slice(1, -1);
|
|
if (!canonical.startsWith("::ffff:")) return canonical;
|
|
const words = canonical.slice(7).split(":");
|
|
const n = (parseInt(words[0], 16) * 65536) + parseInt(words[1], 16);
|
|
return [n >>> 24, (n >>> 16) & 255, (n >>> 8) & 255, n & 255].join(".");
|
|
}
|
|
|
|
function isLoopbackAddress(address) {
|
|
const normalized = normalizeStreamAddress(address);
|
|
if (net.isIP(normalized) === 4) {
|
|
return normalized.startsWith("127.");
|
|
}
|
|
return normalized === "::1";
|
|
}
|
|
|
|
function isUnconditionallyBlockedAddress(address) {
|
|
const normalized = normalizeStreamAddress(address);
|
|
if (net.isIP(normalized) === 4) {
|
|
const [a, b] = normalized.split(".").map(Number);
|
|
return a === 0 || a >= 224 || (a === 169 && b === 254);
|
|
}
|
|
if (net.isIP(normalized) !== 6) return true;
|
|
if (normalized === "::1") return false;
|
|
return normalized.startsWith("::") || normalized.startsWith("ff")
|
|
|| /^fe[89ab]/u.test(normalized) || normalized.startsWith("2002:")
|
|
|| normalized.startsWith("2001:0:");
|
|
}
|
|
|
|
function isLocalControlAddress(address) {
|
|
return isLoopbackAddress(address) || isUnconditionallyBlockedAddress(address);
|
|
}
|
|
|
|
function getNetcattyInternalControlPorts() {
|
|
const ports = new Set();
|
|
const discoveryFile = process.env.NETCATTY_TOOL_CLI_DISCOVERY_FILE;
|
|
if (typeof discoveryFile === "string" && discoveryFile.length > 0) {
|
|
try {
|
|
const parsed = JSON.parse(fs.readFileSync(discoveryFile, "utf8"));
|
|
if (Number.isInteger(parsed?.port) && parsed.port > 0 && parsed.port <= 65535) {
|
|
ports.add(parsed.port);
|
|
}
|
|
} catch { /* discovery file may not exist */ }
|
|
}
|
|
const viteUrl = process.env.VITE_DEV_SERVER_URL;
|
|
if (typeof viteUrl === "string" && viteUrl.length > 0) {
|
|
try {
|
|
const parsed = new URL(viteUrl);
|
|
const port = Number(parsed.port || (parsed.protocol === "https:" ? 443 : 80));
|
|
if (Number.isInteger(port) && port > 0 && port <= 65535) ports.add(port);
|
|
} catch { /* ignore invalid dev server URL */ }
|
|
}
|
|
for (const arg of [...(process.argv ?? []), ...(process.execArgv ?? [])]) {
|
|
const match = /^--(?:remote-debugging-port|inspect|inspect-brk)=(?:[^:]*:)?(\d{1,5})$/u.exec(String(arg));
|
|
if (match) {
|
|
const port = Number(match[1]);
|
|
if (port > 0 && port <= 65535) ports.add(port);
|
|
}
|
|
}
|
|
// In the live Electron main process, every active TCP net.Server belongs to
|
|
// Netcatty's own internal bridges (MCP/CLI RPC, external MCP HTTP, OAuth, etc.).
|
|
if (process.versions?.electron && typeof process._getActiveHandles === "function") {
|
|
try {
|
|
for (const handle of process._getActiveHandles()) {
|
|
if (handle instanceof net.Server && handle.listening) {
|
|
const addr = handle.address();
|
|
if (addr && typeof addr === "object" && Number.isInteger(addr.port) && addr.port > 0) {
|
|
ports.add(addr.port);
|
|
}
|
|
}
|
|
}
|
|
} catch { /* ignore handle inspection errors */ }
|
|
}
|
|
return ports;
|
|
}
|
|
|
|
module.exports = {
|
|
canonicalizeStreamEndpoint,
|
|
getNetcattyInternalControlPorts,
|
|
isLocalControlAddress,
|
|
isLoopbackAddress,
|
|
isUnconditionallyBlockedAddress,
|
|
normalizeStreamAddress,
|
|
};
|