|
| 1 | +const { app, BrowserWindow, ipcMain } = require("electron"); |
| 2 | +const path = require("node:path"); |
| 3 | +const pty = require("node-pty"); |
| 4 | +const fs = require("node:fs"); |
| 5 | +const yaml = require("js-yaml"); |
| 6 | + |
| 7 | +// Keep a global reference of the window object |
| 8 | +let mainWindow; |
| 9 | +let ptyProcesses = []; // Array of terminal processes |
| 10 | +let config; // Will be loaded later |
| 11 | + |
| 12 | +// This method will be called when Electron has finished initialization |
| 13 | +app.whenReady().then(() => { |
| 14 | + createWindow(); |
| 15 | +}); |
| 16 | + |
| 17 | +ipcMain.on("renderer-ready", () => { |
| 18 | + initializeAllTerminals(); |
| 19 | +}); |
| 20 | + |
| 21 | +// IPC handlers for terminal communication |
| 22 | +ipcMain.on("terminal-input", (_event, { index, data }) => { |
| 23 | + if (ptyProcesses[index]) { |
| 24 | + ptyProcesses[index].write(data); |
| 25 | + } else { |
| 26 | + // Fallback if process died |
| 27 | + mainWindow?.webContents?.send( |
| 28 | + `terminal-data-${index}`, |
| 29 | + "\r\n\x1b[91mSSH session not available\x1b[0m\r\n$ ", |
| 30 | + ); |
| 31 | + } |
| 32 | +}); |
| 33 | + |
| 34 | +ipcMain.on("terminal-resize", (_event, { index, cols, rows }) => { |
| 35 | + if (ptyProcesses[index]) { |
| 36 | + ptyProcesses[index].resize(cols, rows); |
| 37 | + } |
| 38 | +}); |
| 39 | + |
| 40 | +ipcMain.on("toggle-dev-tools", () => { |
| 41 | + if (mainWindow) { |
| 42 | + if (mainWindow.webContents.isDevToolsOpened()) { |
| 43 | + mainWindow.webContents.closeDevTools(); |
| 44 | + } else { |
| 45 | + mainWindow.webContents.openDevTools(); |
| 46 | + } |
| 47 | + } |
| 48 | +}); |
| 49 | + |
| 50 | +function createWindow() { |
| 51 | + // Create the browser window |
| 52 | + mainWindow = new BrowserWindow({ |
| 53 | + width: 1400, |
| 54 | + height: 1000, |
| 55 | + webPreferences: { |
| 56 | + nodeIntegration: true, |
| 57 | + contextIsolation: false, |
| 58 | + enableRemoteModule: true, |
| 59 | + }, |
| 60 | + icon: path.join(__dirname, "assets/ssh.png"), // Optional: add an icon |
| 61 | + titleBarStyle: "default", |
| 62 | + show: false, // Don't show until ready |
| 63 | + }); |
| 64 | + |
| 65 | + // Load the index.html file |
| 66 | + mainWindow.loadFile("index.html"); |
| 67 | + mainWindow.removeMenu(); |
| 68 | + |
| 69 | + // Show window when ready to prevent visual flash |
| 70 | + mainWindow.once("ready-to-show", () => { |
| 71 | + mainWindow.show(); |
| 72 | + // Load and send config to renderer |
| 73 | + try { |
| 74 | + config = yaml.load( |
| 75 | + fs.readFileSync(path.join(__dirname, "config.yaml"), "utf8"), |
| 76 | + ); |
| 77 | + mainWindow.webContents.send("config", config); |
| 78 | + } catch (error) { |
| 79 | + console.error("Failed to load config:", error); |
| 80 | + mainWindow.webContents.send("config-error", error.message); |
| 81 | + } |
| 82 | + // Wait for renderer ready before initializing PTY |
| 83 | + }); |
| 84 | + |
| 85 | + // Emitted when the window is closed |
| 86 | + mainWindow.on("closed", () => { |
| 87 | + // Clean up PTY processes |
| 88 | + ptyProcesses.forEach((process, _index) => { |
| 89 | + if (process && !process.killed) { |
| 90 | + process.kill(); |
| 91 | + } |
| 92 | + }); |
| 93 | + ptyProcesses = []; |
| 94 | + // Dereference the window object |
| 95 | + mainWindow = null; |
| 96 | + }); |
| 97 | +} |
| 98 | + |
| 99 | +function initializeAllTerminals() { |
| 100 | + const shell = "ssh"; |
| 101 | + console.log("Using shell:", shell); |
| 102 | + |
| 103 | + for (let i = 0; i < config.hosts.length; i++) { |
| 104 | + try { |
| 105 | + initializeTerminal(i, shell, config.hosts[i]); |
| 106 | + } catch (error) { |
| 107 | + console.error(`Failed to spawn SSH session ${i} process:`, error); |
| 108 | + mainWindow?.webContents?.send( |
| 109 | + `terminal-data-${i}`, |
| 110 | + `\r\n\x1b[91mFailed to start SSH session ${i + 1}: ${error.message}\x1b[0m\r\n`, |
| 111 | + ); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// Initialize PTY processes |
| 117 | +function initializeTerminal(index, shell, host) { |
| 118 | + // Use node-pty for proper pseudo-terminal functionality |
| 119 | + const ptyProcess = pty.spawn(shell, [host], { |
| 120 | + name: "xterm-256color", |
| 121 | + cols: 80, |
| 122 | + rows: 24, |
| 123 | + cwd: process.env.HOME || process.env.USERPROFILE || "/", |
| 124 | + env: process.env, |
| 125 | + }); |
| 126 | + |
| 127 | + // Send data from PTY to renderer |
| 128 | + ptyProcess.onData((data) => { |
| 129 | + mainWindow?.webContents?.send(`terminal-data-${index}`, data); |
| 130 | + }); |
| 131 | + |
| 132 | + // Handle process exit |
| 133 | + ptyProcess.onExit(({ exitCode, signal }) => { |
| 134 | + mainWindow?.webContents?.send(`terminal-exit-${index}`, { |
| 135 | + code: exitCode, |
| 136 | + signal, |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + ptyProcesses[index] = ptyProcess; |
| 141 | +} |
0 commit comments