mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-17 13:01:24 +00:00
Enhances the OAuth authorization experience with modern, branded feedback pages and introduces loading overlays for popups. Refines popup window handling for consistency and security. Removes detailed OneDrive token logging and JWT inspection for cleaner logs and better privacy.
558 lines
16 KiB
JavaScript
558 lines
16 KiB
JavaScript
/**
|
|
* Netcatty Electron Main Process
|
|
*
|
|
* This is the main entry point for the Electron application.
|
|
* All major functionality has been extracted into separate bridge modules:
|
|
*
|
|
* - sshBridge.cjs: SSH connections and session management
|
|
* - sftpBridge.cjs: SFTP file operations
|
|
* - localFsBridge.cjs: Local filesystem operations
|
|
* - transferBridge.cjs: File transfers with progress
|
|
* - portForwardingBridge.cjs: SSH port forwarding tunnels
|
|
* - terminalBridge.cjs: Local shell, telnet, and mosh sessions
|
|
* - windowManager.cjs: Electron window management
|
|
*/
|
|
|
|
// Handle environment setup
|
|
if (process.env.ELECTRON_RUN_AS_NODE) {
|
|
delete process.env.ELECTRON_RUN_AS_NODE;
|
|
}
|
|
|
|
// Handle uncaught exceptions for EPIPE errors
|
|
process.on('uncaughtException', (err) => {
|
|
if (err.code === 'EPIPE' || err.code === 'ERR_STREAM_DESTROYED') {
|
|
console.warn('Ignored stream error:', err.code);
|
|
return;
|
|
}
|
|
console.error('Uncaught exception:', err);
|
|
throw err;
|
|
});
|
|
|
|
// Load Electron
|
|
let electronModule;
|
|
try {
|
|
electronModule = require("node:electron");
|
|
} catch {
|
|
electronModule = require("electron");
|
|
}
|
|
|
|
const { app, BrowserWindow, Menu, protocol } = electronModule || {};
|
|
if (!app || !BrowserWindow) {
|
|
throw new Error("Failed to load Electron runtime. Ensure the app is launched with the Electron binary.");
|
|
}
|
|
|
|
const path = require("node:path");
|
|
const os = require("node:os");
|
|
const fs = require("node:fs");
|
|
|
|
try {
|
|
protocol?.registerSchemesAsPrivileged?.([
|
|
{
|
|
scheme: "app",
|
|
privileges: {
|
|
standard: true,
|
|
secure: true,
|
|
supportFetchAPI: true,
|
|
corsEnabled: true,
|
|
stream: true,
|
|
},
|
|
},
|
|
]);
|
|
} catch (err) {
|
|
console.warn("[Main] Failed to register app:// scheme privileges:", err);
|
|
}
|
|
|
|
// Apply ssh2 protocol patch needed for OpenSSH sk-* signature layouts.
|
|
|
|
// Import bridge modules
|
|
const sshBridge = require("./bridges/sshBridge.cjs");
|
|
const sftpBridge = require("./bridges/sftpBridge.cjs");
|
|
const localFsBridge = require("./bridges/localFsBridge.cjs");
|
|
const transferBridge = require("./bridges/transferBridge.cjs");
|
|
const portForwardingBridge = require("./bridges/portForwardingBridge.cjs");
|
|
const terminalBridge = require("./bridges/terminalBridge.cjs");
|
|
const oauthBridge = require("./bridges/oauthBridge.cjs");
|
|
const githubAuthBridge = require("./bridges/githubAuthBridge.cjs");
|
|
const googleAuthBridge = require("./bridges/googleAuthBridge.cjs");
|
|
const onedriveAuthBridge = require("./bridges/onedriveAuthBridge.cjs");
|
|
const cloudSyncBridge = require("./bridges/cloudSyncBridge.cjs");
|
|
const windowManager = require("./bridges/windowManager.cjs");
|
|
|
|
// GPU settings
|
|
// NOTE: Do not disable Chromium sandbox by default.
|
|
// If you need to debug with sandbox disabled, set NETCATTY_NO_SANDBOX=1.
|
|
if (process.env.NETCATTY_NO_SANDBOX === "1") {
|
|
app.commandLine.appendSwitch("no-sandbox");
|
|
}
|
|
// Force hardware acceleration even on blocklisted GPUs (macs sometimes fall back to software)
|
|
app.commandLine.appendSwitch("ignore-gpu-blocklist");
|
|
app.commandLine.appendSwitch("ignore-gpu-blacklist"); // Some Chromium builds use this alias; keep both for safety
|
|
app.commandLine.appendSwitch("enable-gpu-rasterization");
|
|
app.commandLine.appendSwitch("enable-zero-copy");
|
|
|
|
// Silence noisy DevTools Autofill CDP errors (Electron's backend doesn't expose this domain)
|
|
app.on("web-contents-created", (_event, contents) => {
|
|
if (contents.getType() !== "devtools") return;
|
|
// Drop console output from Autofill requests in DevTools frontend
|
|
contents.on("did-finish-load", () => {
|
|
contents
|
|
.executeJavaScript(`
|
|
(() => {
|
|
const block = (methodName) => {
|
|
const original = console[methodName];
|
|
if (!original) return;
|
|
console[methodName] = (...args) => {
|
|
if (args.some(arg => typeof arg === "string" && arg.includes("Autofill."))) return;
|
|
original(...args);
|
|
};
|
|
};
|
|
block("error");
|
|
block("warn");
|
|
})();
|
|
`)
|
|
.catch(() => {});
|
|
});
|
|
contents.on("console-message", (event, _level, message, _line, sourceId) => {
|
|
if (sourceId?.startsWith("devtools://") && message.includes("Autofill.")) {
|
|
event.preventDefault();
|
|
}
|
|
});
|
|
});
|
|
|
|
// Application configuration
|
|
const devServerUrl = process.env.VITE_DEV_SERVER_URL;
|
|
// Never treat a packaged app as "dev" even if the user has VITE_DEV_SERVER_URL set globally.
|
|
const isDev = !app.isPackaged && !!devServerUrl;
|
|
const effectiveDevServerUrl = isDev ? devServerUrl : undefined;
|
|
const preload = path.join(__dirname, "preload.cjs");
|
|
const isMac = process.platform === "darwin";
|
|
const appIcon = path.join(__dirname, "../public/icon.png");
|
|
const electronDir = __dirname;
|
|
|
|
const APP_PROTOCOL_HEADERS = {
|
|
// Required for crossOriginIsolated / SharedArrayBuffer.
|
|
// Mirrors the dev-server headers in `vite.config.ts`.
|
|
"Cross-Origin-Opener-Policy": "same-origin",
|
|
"Cross-Origin-Embedder-Policy": "credentialless",
|
|
};
|
|
|
|
const DIST_MIME_TYPES = {
|
|
".html": "text/html",
|
|
".js": "text/javascript",
|
|
".mjs": "text/javascript",
|
|
".css": "text/css",
|
|
".json": "application/json",
|
|
".png": "image/png",
|
|
".jpg": "image/jpeg",
|
|
".jpeg": "image/jpeg",
|
|
".gif": "image/gif",
|
|
".svg": "image/svg+xml",
|
|
".ico": "image/x-icon",
|
|
".woff": "font/woff",
|
|
".woff2": "font/woff2",
|
|
".ttf": "font/ttf",
|
|
".eot": "application/vnd.ms-fontobject",
|
|
".wav": "audio/wav",
|
|
".mp3": "audio/mpeg",
|
|
".mp4": "video/mp4",
|
|
".webm": "video/webm",
|
|
".wasm": "application/wasm",
|
|
};
|
|
|
|
function resolveContentType(filePath) {
|
|
const ext = path.extname(filePath).toLowerCase();
|
|
return DIST_MIME_TYPES[ext] || "application/octet-stream";
|
|
}
|
|
|
|
function isPathInside(parentPath, childPath) {
|
|
const parent = path.resolve(parentPath);
|
|
const child = path.resolve(childPath);
|
|
if (child === parent) return true;
|
|
return child.startsWith(`${parent}${path.sep}`);
|
|
}
|
|
|
|
function resolveDistPath() {
|
|
return path.join(electronDir, "../dist");
|
|
}
|
|
|
|
function registerAppProtocol() {
|
|
if (!protocol?.handle) return;
|
|
|
|
try {
|
|
protocol.handle("app", async (request) => {
|
|
const notFound = () =>
|
|
new Response("Not Found", {
|
|
status: 404,
|
|
headers: { ...APP_PROTOCOL_HEADERS, "Content-Type": "text/plain" },
|
|
});
|
|
|
|
try {
|
|
const url = new URL(request.url);
|
|
let pathname = url.pathname || "/";
|
|
try {
|
|
pathname = decodeURIComponent(pathname);
|
|
} catch {
|
|
// keep undecoded
|
|
}
|
|
|
|
if (!pathname || pathname === "/") pathname = "/index.html";
|
|
|
|
const distPath = path.resolve(resolveDistPath());
|
|
const relative = pathname.replace(/^\/+/, "");
|
|
let fullPath = path.resolve(distPath, relative);
|
|
|
|
if (!isPathInside(distPath, fullPath)) {
|
|
return new Response("Forbidden", {
|
|
status: 403,
|
|
headers: { ...APP_PROTOCOL_HEADERS, "Content-Type": "text/plain" },
|
|
});
|
|
}
|
|
|
|
// SPA fallback: for extension-less paths, serve index.html.
|
|
if (!path.extname(fullPath)) {
|
|
fullPath = path.resolve(distPath, "index.html");
|
|
}
|
|
|
|
const file = await fs.promises.readFile(fullPath);
|
|
return new Response(file, {
|
|
status: 200,
|
|
headers: {
|
|
...APP_PROTOCOL_HEADERS,
|
|
"Content-Type": resolveContentType(fullPath),
|
|
},
|
|
});
|
|
} catch (err) {
|
|
return notFound();
|
|
}
|
|
});
|
|
} catch (err) {
|
|
console.error("[Main] Failed to register app:// protocol handler:", err);
|
|
}
|
|
}
|
|
|
|
function focusMainWindow() {
|
|
try {
|
|
const wins = BrowserWindow.getAllWindows();
|
|
const win = wins && wins.length ? wins[0] : null;
|
|
if (!win) return false;
|
|
|
|
try {
|
|
if (win.isMinimized && win.isMinimized()) win.restore();
|
|
} catch {}
|
|
try {
|
|
win.show();
|
|
} catch {}
|
|
try {
|
|
win.focus();
|
|
} catch {}
|
|
try {
|
|
app.focus({ steal: true });
|
|
} catch {}
|
|
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Shared state
|
|
const sessions = new Map();
|
|
const sftpClients = new Map();
|
|
const keyRoot = path.join(os.homedir(), ".netcatty", "keys");
|
|
let cloudSyncSessionPassword = null;
|
|
const CLOUD_SYNC_PASSWORD_FILE = "netcatty_cloud_sync_master_password_v1";
|
|
|
|
// Key management helpers
|
|
const ensureKeyDir = () => {
|
|
try {
|
|
fs.mkdirSync(keyRoot, { recursive: true, mode: 0o700 });
|
|
} catch (err) {
|
|
console.warn("Unable to ensure key cache dir", err);
|
|
}
|
|
};
|
|
|
|
const writeKeyToDisk = (keyId, privateKey) => {
|
|
if (!privateKey) return null;
|
|
ensureKeyDir();
|
|
const filename = `${keyId || "temp"}.pem`;
|
|
const target = path.join(keyRoot, filename);
|
|
const normalized = privateKey.endsWith("\n") ? privateKey : `${privateKey}\n`;
|
|
try {
|
|
fs.writeFileSync(target, normalized, { mode: 0o600 });
|
|
return target;
|
|
} catch (err) {
|
|
console.error("Failed to persist private key", err);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
// Track if bridges are registered
|
|
let bridgesRegistered = false;
|
|
|
|
/**
|
|
* Register all IPC bridges with Electron
|
|
*/
|
|
const registerBridges = (win) => {
|
|
if (bridgesRegistered) return;
|
|
bridgesRegistered = true;
|
|
|
|
const { ipcMain } = electronModule;
|
|
const { safeStorage } = electronModule;
|
|
|
|
const getCloudSyncPasswordPath = () => {
|
|
try {
|
|
return path.join(app.getPath("userData"), CLOUD_SYNC_PASSWORD_FILE);
|
|
} catch {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
const readPersistedCloudSyncPassword = () => {
|
|
try {
|
|
if (!safeStorage?.isEncryptionAvailable?.()) return null;
|
|
const filePath = getCloudSyncPasswordPath();
|
|
if (!filePath || !fs.existsSync(filePath)) return null;
|
|
const base64 = fs.readFileSync(filePath, "utf8");
|
|
if (!base64) return null;
|
|
const buf = Buffer.from(base64, "base64");
|
|
const decrypted = safeStorage.decryptString(buf);
|
|
return typeof decrypted === "string" && decrypted.length ? decrypted : null;
|
|
} catch (err) {
|
|
console.warn("[CloudSync] Failed to read persisted password:", err?.message || err);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
const persistCloudSyncPassword = (password) => {
|
|
try {
|
|
if (!safeStorage?.isEncryptionAvailable?.()) return false;
|
|
const filePath = getCloudSyncPasswordPath();
|
|
if (!filePath) return false;
|
|
const encrypted = safeStorage.encryptString(password);
|
|
fs.writeFileSync(filePath, encrypted.toString("base64"), { mode: 0o600 });
|
|
return true;
|
|
} catch (err) {
|
|
console.warn("[CloudSync] Failed to persist password:", err?.message || err);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
const clearPersistedCloudSyncPassword = () => {
|
|
try {
|
|
const filePath = getCloudSyncPasswordPath();
|
|
if (filePath && fs.existsSync(filePath)) {
|
|
fs.unlinkSync(filePath);
|
|
}
|
|
} catch (err) {
|
|
console.warn("[CloudSync] Failed to clear persisted password:", err?.message || err);
|
|
}
|
|
};
|
|
|
|
// Initialize bridges with shared dependencies
|
|
const deps = {
|
|
sessions,
|
|
sftpClients,
|
|
electronModule,
|
|
};
|
|
|
|
sshBridge.init(deps);
|
|
sftpBridge.init(deps);
|
|
transferBridge.init(deps);
|
|
terminalBridge.init(deps);
|
|
|
|
// Register all IPC handlers
|
|
sshBridge.registerHandlers(ipcMain);
|
|
sftpBridge.registerHandlers(ipcMain);
|
|
localFsBridge.registerHandlers(ipcMain);
|
|
transferBridge.registerHandlers(ipcMain);
|
|
portForwardingBridge.registerHandlers(ipcMain);
|
|
terminalBridge.registerHandlers(ipcMain);
|
|
oauthBridge.setupOAuthBridge(ipcMain);
|
|
githubAuthBridge.registerHandlers(ipcMain);
|
|
googleAuthBridge.registerHandlers(ipcMain, electronModule);
|
|
onedriveAuthBridge.registerHandlers(ipcMain, electronModule);
|
|
cloudSyncBridge.registerHandlers(ipcMain);
|
|
|
|
// Settings window handler
|
|
ipcMain.handle("netcatty:settings:open", async () => {
|
|
try {
|
|
await windowManager.openSettingsWindow(electronModule, {
|
|
preload,
|
|
devServerUrl: effectiveDevServerUrl,
|
|
isDev,
|
|
appIcon,
|
|
isMac,
|
|
electronDir,
|
|
});
|
|
return true;
|
|
} catch (err) {
|
|
console.error("[Main] Failed to open settings window:", err);
|
|
return false;
|
|
}
|
|
});
|
|
|
|
// Cloud sync master password (stored in-memory + persisted via safeStorage)
|
|
ipcMain.handle("netcatty:cloudSync:session:setPassword", async (_event, password) => {
|
|
cloudSyncSessionPassword = typeof password === "string" && password.length ? password : null;
|
|
if (cloudSyncSessionPassword) {
|
|
persistCloudSyncPassword(cloudSyncSessionPassword);
|
|
} else {
|
|
clearPersistedCloudSyncPassword();
|
|
}
|
|
return true;
|
|
});
|
|
|
|
ipcMain.handle("netcatty:cloudSync:session:getPassword", async () => {
|
|
if (cloudSyncSessionPassword) return cloudSyncSessionPassword;
|
|
const persisted = readPersistedCloudSyncPassword();
|
|
cloudSyncSessionPassword = persisted;
|
|
return persisted;
|
|
});
|
|
|
|
ipcMain.handle("netcatty:cloudSync:session:clearPassword", async () => {
|
|
cloudSyncSessionPassword = null;
|
|
clearPersistedCloudSyncPassword();
|
|
return true;
|
|
});
|
|
|
|
// Open external URL in default browser
|
|
ipcMain.handle("netcatty:openExternal", async (_event, url) => {
|
|
const { shell } = electronModule;
|
|
if (url && typeof url === 'string' && (url.startsWith('http://') || url.startsWith('https://'))) {
|
|
await shell.openExternal(url);
|
|
}
|
|
});
|
|
|
|
// App information for About/Application screens
|
|
ipcMain.handle("netcatty:app:getInfo", async () => {
|
|
return {
|
|
name: app.getName(),
|
|
version: app.getVersion(),
|
|
platform: process.platform,
|
|
};
|
|
});
|
|
|
|
console.log('[Main] All bridges registered successfully');
|
|
};
|
|
|
|
/**
|
|
* Create the main application window
|
|
*/
|
|
async function createWindow() {
|
|
const win = await windowManager.createWindow(electronModule, {
|
|
preload,
|
|
devServerUrl: effectiveDevServerUrl,
|
|
isDev,
|
|
appIcon,
|
|
isMac,
|
|
electronDir,
|
|
onRegisterBridge: registerBridges,
|
|
});
|
|
|
|
return win;
|
|
}
|
|
|
|
function showStartupError(err) {
|
|
const title = "Netcatty";
|
|
const code = err && typeof err === "object" ? err.code : null;
|
|
const message =
|
|
code === "ENOENT"
|
|
? "Renderer files are missing. Please reinstall or rebuild Netcatty."
|
|
: "Failed to load the UI. Please relaunch Netcatty.";
|
|
|
|
try {
|
|
electronModule.dialog?.showErrorBox?.(title, message);
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
// Application lifecycle
|
|
app.whenReady().then(() => {
|
|
registerAppProtocol();
|
|
|
|
// Set dock icon on macOS
|
|
if (isMac && appIcon && app.dock?.setIcon) {
|
|
try {
|
|
app.dock.setIcon(appIcon);
|
|
} catch (err) {
|
|
console.warn("Failed to set dock icon", err);
|
|
}
|
|
}
|
|
|
|
// Build and set application menu
|
|
const menu = windowManager.buildAppMenu(Menu, app, isMac);
|
|
Menu.setApplicationMenu(menu);
|
|
|
|
app.on("browser-window-created", (_event, win) => {
|
|
try {
|
|
const mainWin = windowManager.getMainWindow();
|
|
const settingsWin = windowManager.getSettingsWindow();
|
|
const isPrimary = win === mainWin || win === settingsWin;
|
|
if (!isPrimary) {
|
|
win.setMenuBarVisibility(false);
|
|
win.autoHideMenuBar = true;
|
|
win.setMenu(null);
|
|
if (appIcon && win.setIcon) win.setIcon(appIcon);
|
|
}
|
|
} catch {
|
|
// ignore
|
|
}
|
|
});
|
|
|
|
// Create the main window
|
|
void createWindow().catch((err) => {
|
|
console.error("[Main] Failed to create main window:", err);
|
|
showStartupError(err);
|
|
try {
|
|
app.quit();
|
|
} catch {}
|
|
});
|
|
|
|
// Re-create window on macOS dock click
|
|
app.on("activate", () => {
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
void createWindow().catch((err) => {
|
|
console.error("[Main] Failed to create window on activate:", err);
|
|
showStartupError(err);
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
// Ensure single-instance behavior focuses existing window
|
|
try {
|
|
const gotLock = app.requestSingleInstanceLock();
|
|
if (!gotLock) {
|
|
app.quit();
|
|
} else {
|
|
app.on("second-instance", () => {
|
|
focusMainWindow();
|
|
});
|
|
}
|
|
} catch {}
|
|
|
|
// Cleanup on all windows closed
|
|
app.on("window-all-closed", () => {
|
|
if (process.platform !== "darwin") {
|
|
app.quit();
|
|
}
|
|
});
|
|
|
|
// Cleanup all PTY sessions before quitting to prevent node-pty assertion errors
|
|
app.on("will-quit", () => {
|
|
try {
|
|
terminalBridge.cleanupAllSessions();
|
|
} catch (err) {
|
|
console.warn("Error during terminal cleanup:", err);
|
|
}
|
|
});
|
|
|
|
// Export for testing
|
|
module.exports = {
|
|
sessions,
|
|
sftpClients,
|
|
ensureKeyDir,
|
|
writeKeyToDisk,
|
|
};
|