@lethevimlet/sshift 0.5.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "opencode-attention",
3
+ "version": "1.0.0",
4
+ "description": "Flashes SSH tabs when opencode is waiting for user input",
5
+ "type": "sshift-plugin"
6
+ }
@@ -18,11 +18,17 @@ const socketIO = require('socket.io');
18
18
  const selfsigned = require('selfsigned');
19
19
 
20
20
  // Import utilities
21
- const { ensureConfig, loadConfig, getPort, getBindAddress, getEnableHttps, getDataDir, isPasswordSet } = require('./utils/config');
21
+ const { ensureConfig, loadConfig, getPort, getBindAddress, getEnableHttps, getCertPath, getKeyPath, getDataDir, isPasswordSet } = require('./utils/config');
22
22
 
23
23
  // Import services
24
24
  const { sshManager, sftpManager } = require('./services');
25
25
 
26
+ // Import tab manager
27
+ const tabManager = require('./utils/tab-manager');
28
+
29
+ // Import plugin manager
30
+ const pluginManager = require('./plugins/plugin-manager');
31
+
26
32
  // Import endpoints
27
33
  const { rest, ws } = require('./endpoints');
28
34
 
@@ -35,6 +41,26 @@ const SSL_KEY_FILE = 'ssl-key.pem';
35
41
  * @returns {Promise<Object>} Certificate and private key
36
42
  */
37
43
  async function getSSLCredentials() {
44
+ const customCertPath = getCertPath();
45
+ const customKeyPath = getKeyPath();
46
+
47
+ if (customCertPath && customKeyPath) {
48
+ console.log('[HTTPS] Using custom certificate from config:');
49
+ console.log('[HTTPS] Cert:', customCertPath);
50
+ console.log('[HTTPS] Key:', customKeyPath);
51
+ try {
52
+ return {
53
+ cert: fs.readFileSync(customCertPath, 'utf8'),
54
+ key: fs.readFileSync(customKeyPath, 'utf8')
55
+ };
56
+ } catch (err) {
57
+ console.error('[HTTPS] Failed to read custom certificate files:', err.message);
58
+ console.error('[HTTPS] Falling back to self-signed certificate');
59
+ }
60
+ } else if (customCertPath || customKeyPath) {
61
+ console.warn('[HTTPS] Both certPath and keyPath must be set in config. Only one was provided; falling back to self-signed certificate.');
62
+ }
63
+
38
64
  const dataDir = getDataDir();
39
65
  const certPath = path.join(dataDir, SSL_CERT_FILE);
40
66
  const keyPath = path.join(dataDir, SSL_KEY_FILE);
@@ -170,6 +196,15 @@ async function initializeServer() {
170
196
  // Set the socket.io instance for services that need it
171
197
  sshManager.setIO(io);
172
198
 
199
+ // Initialize plugin system
200
+ const config = loadConfig();
201
+ pluginManager.init({
202
+ io,
203
+ sshManager,
204
+ tabManager,
205
+ config,
206
+ });
207
+
173
208
  // Middleware
174
209
  app.use(express.json());
175
210
 
@@ -249,6 +284,9 @@ async function initializeServer() {
249
284
  accent: currentAccent
250
285
  });
251
286
 
287
+ // Sync flash state for tabs that need attention
288
+ pluginManager.syncFlashState(socket);
289
+
252
290
  // Register all WebSocket handlers
253
291
  ws.registerAllWSHandlers(socket, io);
254
292
 
@@ -0,0 +1,256 @@
1
+ /**
2
+ * Plugin System - Core Plugin Manager
3
+ *
4
+ * Provides a hook-based architecture for plugins to observe and react to
5
+ * SSH session data, terminal output, and other events.
6
+ *
7
+ * Plugin API available to plugins:
8
+ * - onSessionConnect(sessionId, sessionInfo) - SSH session established
9
+ * - onSessionDisconnect(sessionId) - SSH session closed
10
+ * - onData(sessionId, data) - Raw terminal output data
11
+ * - onTerminalLine(sessionId, line) - Individual terminal lines
12
+ *
13
+ * Plugin actions (via context object):
14
+ * - ctx.flashTab(sessionId, options) - Flash a tab to get attention
15
+ * - ctx.stopFlashTab(sessionId) - Stop flashing a tab
16
+ * - ctx.emitToSession(sessionId, event, data) - Send WS event to session clients
17
+ * - ctx.emitToAll(event, data) - Broadcast WS event to all clients
18
+ * - ctx.writeToSession(sessionId, data) - Write data to SSH session stdin
19
+ * - ctx.getTerminalState(sessionId) - Get current terminal state
20
+ * - ctx.getActiveSessions() - Get list of active session IDs
21
+ * - ctx.getConfig() - Get current server config
22
+ */
23
+
24
+ const path = require('path');
25
+ const fs = require('fs');
26
+
27
+ class PluginManager {
28
+ constructor() {
29
+ this.plugins = new Map();
30
+ this.hooks = {
31
+ onSessionConnect: [],
32
+ onSessionDisconnect: [],
33
+ onData: [],
34
+ onTerminalLine: [],
35
+ };
36
+ this.io = null;
37
+ this.sshManager = null;
38
+ this.tabManager = null;
39
+ this.config = null;
40
+ this._lineBuffers = new Map();
41
+ this.flashingSessions = new Map(); // sessionId -> options
42
+ }
43
+
44
+ init({ io, sshManager, tabManager, config }) {
45
+ this.io = io;
46
+ this.sshManager = sshManager;
47
+ this.tabManager = tabManager;
48
+ this.config = config;
49
+ this._loadPlugins();
50
+ }
51
+
52
+ _loadPlugins() {
53
+ const pluginConfigs = this.config.plugins || [];
54
+
55
+ for (const pluginConfig of pluginConfigs) {
56
+ if (pluginConfig.enabled === false) {
57
+ console.log(`[PLUGINS] Skipping disabled plugin: ${pluginConfig.name}`);
58
+ continue;
59
+ }
60
+ this._loadPlugin(pluginConfig);
61
+ }
62
+ }
63
+
64
+ _loadPlugin(pluginConfig) {
65
+ const { name } = pluginConfig;
66
+
67
+ const pluginDir = path.join(__dirname, name);
68
+ const builtinDir = path.join(__dirname, '..', '..', '..', 'plugins', name);
69
+
70
+ let pluginPath;
71
+ if (fs.existsSync(path.join(pluginDir, 'index.js'))) {
72
+ pluginPath = path.join(pluginDir, 'index.js');
73
+ } else if (fs.existsSync(path.join(builtinDir, 'index.js'))) {
74
+ pluginPath = path.join(builtinDir, 'index.js');
75
+ } else {
76
+ try {
77
+ pluginPath = require.resolve(name, { paths: [path.join(__dirname, '..', '..', '..')] });
78
+ } catch (e) {
79
+ console.error(`[PLUGINS] Plugin "${name}" not found in builtin or external paths`);
80
+ return;
81
+ }
82
+ }
83
+
84
+ try {
85
+ const PluginClass = require(pluginPath);
86
+ const ctx = this._createContext(pluginConfig);
87
+ const instance = typeof PluginClass === 'function'
88
+ ? new PluginClass(ctx, pluginConfig.config || {})
89
+ : PluginClass;
90
+
91
+ this.plugins.set(name, {
92
+ instance,
93
+ config: pluginConfig,
94
+ path: pluginPath,
95
+ });
96
+
97
+ for (const [hookName, hookFn] of Object.entries(this.hooks)) {
98
+ if (typeof instance[hookName] === 'function') {
99
+ hookFn.push({ pluginName: name, fn: instance[hookName].bind(instance) });
100
+ console.log(`[PLUGINS] Registered hook ${hookName} for plugin "${name}"`);
101
+ }
102
+ }
103
+
104
+ if (typeof instance.init === 'function') {
105
+ instance.init(ctx, pluginConfig.config || {});
106
+ }
107
+
108
+ console.log(`[PLUGINS] Loaded plugin: ${name} from ${pluginPath}`);
109
+ } catch (err) {
110
+ console.error(`[PLUGINS] Failed to load plugin "${name}":`, err.message);
111
+ }
112
+ }
113
+
114
+ _createContext(pluginConfig) {
115
+ const self = this;
116
+ return {
117
+ flashTab(sessionId, options = {}) {
118
+ if (!self.io) return;
119
+ self.flashingSessions.set(sessionId, options);
120
+ self.io.to(`session-${sessionId}`).emit('tab-flash', {
121
+ sessionId,
122
+ ...options,
123
+ });
124
+ self.io.emit('tab-flash', { sessionId, ...options });
125
+ },
126
+
127
+ stopFlashTab(sessionId) {
128
+ if (!self.io) return;
129
+ self.flashingSessions.delete(sessionId);
130
+ self.io.to(`session-${sessionId}`).emit('tab-flash-stop', { sessionId });
131
+ self.io.emit('tab-flash-stop', { sessionId });
132
+ },
133
+
134
+ emitToSession(sessionId, event, data) {
135
+ if (!self.io) return;
136
+ self.io.to(`session-${sessionId}`).emit(event, data);
137
+ },
138
+
139
+ emitToAll(event, data) {
140
+ if (!self.io) return;
141
+ self.io.emit(event, data);
142
+ },
143
+
144
+ writeToSession(sessionId, data) {
145
+ if (self.sshManager) {
146
+ self.sshManager.write(sessionId, data);
147
+ }
148
+ },
149
+
150
+ getTerminalState(sessionId) {
151
+ if (self.sshManager) {
152
+ return self.sshManager.getTerminalState(sessionId);
153
+ }
154
+ return null;
155
+ },
156
+
157
+ getActiveSessions() {
158
+ if (self.sshManager) {
159
+ return Array.from(self.sshManager.sessions.keys());
160
+ }
161
+ return [];
162
+ },
163
+
164
+ getConfig() {
165
+ return self.config;
166
+ },
167
+
168
+ getPluginConfig() {
169
+ return pluginConfig.config || {};
170
+ },
171
+ };
172
+ }
173
+
174
+ _processLineBuffer(sessionId, dataStr) {
175
+ if (!this._lineBuffers.has(sessionId)) {
176
+ this._lineBuffers.set(sessionId, '');
177
+ }
178
+ let buffer = this._lineBuffers.get(sessionId) + dataStr;
179
+
180
+ const lines = [];
181
+ let lastNewline = buffer.lastIndexOf('\n');
182
+ if (lastNewline !== -1) {
183
+ const rawLines = buffer.substring(0, lastNewline);
184
+ buffer = buffer.substring(lastNewline + 1);
185
+ const split = rawLines.split('\n');
186
+ lines.push(...split);
187
+ }
188
+ this._lineBuffers.set(sessionId, buffer);
189
+
190
+ for (const line of lines) {
191
+ const stripped = this._stripAnsi(line);
192
+ for (const hook of this.hooks.onTerminalLine) {
193
+ try {
194
+ hook.fn(sessionId, stripped, line);
195
+ } catch (err) {
196
+ console.error(`[PLUGINS] Error in onTerminalLine hook (${hook.pluginName}):`, err.message);
197
+ }
198
+ }
199
+ }
200
+ }
201
+
202
+ _stripAnsi(str) {
203
+ return str.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, '')
204
+ .replace(/\x1b\][^\x07]*\x07/g, '')
205
+ .replace(/\x1b\[\?[0-9]+[hl]/g, '')
206
+ .replace(/\x1b[\(\)]B/g, '')
207
+ .replace(/\x1b[0-9;]*[a-zA-Z]/g, '')
208
+ .replace(/\x1b[^[\]()?P]/g, '')
209
+ .replace(/[\x00-\x08\x0b\x0c\x0d\x0e-\x1f\x7f]/g, '')
210
+ .trim();
211
+ }
212
+
213
+ emit(hookName, ...args) {
214
+ const hooks = this.hooks[hookName];
215
+ if (!hooks) return;
216
+ for (const hook of hooks) {
217
+ try {
218
+ hook.fn(...args);
219
+ } catch (err) {
220
+ console.error(`[PLUGINS] Error in ${hookName} hook (${hook.pluginName}):`, err.message);
221
+ }
222
+ }
223
+ }
224
+
225
+ onData(sessionId, data) {
226
+ this.emit('onData', sessionId, data);
227
+ this._processLineBuffer(sessionId, data);
228
+ }
229
+
230
+ onSessionConnect(sessionId, sessionInfo) {
231
+ this.emit('onSessionConnect', sessionId, sessionInfo);
232
+ }
233
+
234
+ onSessionDisconnect(sessionId) {
235
+ this._lineBuffers.delete(sessionId);
236
+ this.flashingSessions.delete(sessionId);
237
+ this.emit('onSessionDisconnect', sessionId);
238
+ }
239
+
240
+ getFlashingSessions() {
241
+ return Array.from(this.flashingSessions.entries()).map(([sessionId, options]) => ({
242
+ sessionId,
243
+ ...options,
244
+ }));
245
+ }
246
+
247
+ syncFlashState(socket) {
248
+ if (!this.io) return;
249
+ const flashing = this.getFlashingSessions();
250
+ for (const flash of flashing) {
251
+ socket.emit('tab-flash', flash);
252
+ }
253
+ }
254
+ }
255
+
256
+ module.exports = new PluginManager();
@@ -1,6 +1,7 @@
1
1
  const { Client } = require('ssh2');
2
2
  const { Terminal } = require('@xterm/headless');
3
3
  const { SerializeAddon } = require('@xterm/addon-serialize');
4
+ const pluginManager = require('../plugins/plugin-manager');
4
5
 
5
6
  class SSHManager {
6
7
  constructor() {
@@ -132,6 +133,14 @@ class SSHManager {
132
133
  socket.join(`session-${sessionId}`);
133
134
  console.log(`[SSH] Shell started: ${sessionId}`);
134
135
 
136
+ // Notify plugins about new session
137
+ pluginManager.onSessionConnect(sessionId, {
138
+ host: options.host,
139
+ port: options.port || 22,
140
+ username: options.username,
141
+ name: options.name || 'SSH',
142
+ });
143
+
135
144
  // Handle stream data with batching to reduce socket events
136
145
  // This prevents performance issues when multiple clients are connected
137
146
  stream.on('data', (data) => {
@@ -216,6 +225,9 @@ class SSHManager {
216
225
  const session = this.sessions.get(sessionId);
217
226
  if (!session) return;
218
227
 
228
+ // Notify plugins about terminal output
229
+ pluginManager.onData(sessionId, dataStr);
230
+
219
231
  // Add data to buffer
220
232
  session.dataBuffer += dataStr;
221
233
  session.lastDataTime = Date.now();
@@ -507,6 +519,8 @@ class SSHManager {
507
519
  session.terminal.dispose();
508
520
  }
509
521
  this.sessions.delete(sessionId);
522
+ // Notify plugins about disconnect
523
+ pluginManager.onSessionDisconnect(sessionId);
510
524
  }
511
525
  }
512
526
 
@@ -39,6 +39,9 @@ const defaultConfig = {
39
39
  sticky: true,
40
40
  sshKeepaliveInterval: 15000,
41
41
  sshKeepaliveCountMax: 500,
42
+ certPath: null,
43
+ keyPath: null,
44
+ plugins: [],
42
45
  bookmarks: [],
43
46
  folders: [],
44
47
  passwordHash: null
@@ -222,6 +225,24 @@ function getEnableHttps() {
222
225
  return config.enableHttps !== false; // Default to true
223
226
  }
224
227
 
228
+ /**
229
+ * Get custom certificate path from config
230
+ * @returns {string|null} Path to custom certificate file, or null
231
+ */
232
+ function getCertPath() {
233
+ const config = loadConfig();
234
+ return config.certPath || null;
235
+ }
236
+
237
+ /**
238
+ * Get custom private key path from config
239
+ * @returns {string|null} Path to custom private key file, or null
240
+ */
241
+ function getKeyPath() {
242
+ const config = loadConfig();
243
+ return config.keyPath || null;
244
+ }
245
+
225
246
  /**
226
247
  * Get the data directory for persistent storage (SSL certs, etc.)
227
248
  * Searches the same paths as config to find a writable data directory.
@@ -280,6 +301,8 @@ module.exports = {
280
301
  getMobileKeysBarEnabled,
281
302
  getLayouts,
282
303
  getEnableHttps,
304
+ getCertPath,
305
+ getKeyPath,
283
306
  getDataDir,
284
307
  hashPassword,
285
308
  isPasswordSet,
@@ -1920,6 +1920,144 @@ body {
1920
1920
  color: var(--text-primary);
1921
1921
  }
1922
1922
 
1923
+ .tab.flashing {
1924
+ animation: tab-flash 1s ease-in-out infinite;
1925
+ transition: none;
1926
+ }
1927
+
1928
+ @keyframes tab-flash {
1929
+ 0%, 100% {
1930
+ background: transparent;
1931
+ border-bottom-color: var(--accent-primary);
1932
+ color: var(--accent-primary);
1933
+ }
1934
+ 50% {
1935
+ background: var(--accent-primary);
1936
+ color: #fff;
1937
+ border-bottom-color: var(--accent-primary);
1938
+ }
1939
+ }
1940
+
1941
+ .tab.active.flashing {
1942
+ animation: tab-flash-active 1s ease-in-out infinite;
1943
+ transition: none;
1944
+ }
1945
+
1946
+ @keyframes tab-flash-active {
1947
+ 0%, 100% {
1948
+ background: var(--bg-tertiary);
1949
+ border-bottom-color: var(--accent-primary);
1950
+ color: var(--accent-primary);
1951
+ }
1952
+ 50% {
1953
+ background: var(--accent-primary);
1954
+ color: #fff;
1955
+ border-bottom-color: var(--accent-primary);
1956
+ }
1957
+ }
1958
+
1959
+ .mobile-tabs-toggle.flashing {
1960
+ animation: mobile-toggle-flash 1s ease-in-out infinite;
1961
+ transition: none;
1962
+ }
1963
+
1964
+ @keyframes mobile-toggle-flash {
1965
+ 0%, 100% {
1966
+ background: var(--bg-tertiary);
1967
+ border-color: var(--accent-primary);
1968
+ box-shadow: 0 0 0 1px var(--accent-primary);
1969
+ }
1970
+ 50% {
1971
+ background: var(--accent-primary);
1972
+ border-color: var(--accent-primary);
1973
+ color: #fff;
1974
+ box-shadow: 0 0 8px var(--accent-primary);
1975
+ }
1976
+ }
1977
+
1978
+ .mobile-tab-option.flashing {
1979
+ animation: mobile-option-flash 1s ease-in-out infinite;
1980
+ transition: none;
1981
+ }
1982
+
1983
+ @keyframes mobile-option-flash {
1984
+ 0%, 100% {
1985
+ background: transparent;
1986
+ color: var(--accent-primary);
1987
+ }
1988
+ 50% {
1989
+ background: var(--accent-primary);
1990
+ color: #fff;
1991
+ }
1992
+ }
1993
+
1994
+ [data-theme="light"] .tab.flashing {
1995
+ animation: tab-flash-light 1s ease-in-out infinite;
1996
+ }
1997
+
1998
+ @keyframes tab-flash-light {
1999
+ 0%, 100% {
2000
+ background: transparent;
2001
+ border-bottom-color: var(--accent-primary);
2002
+ color: var(--accent-primary);
2003
+ }
2004
+ 50% {
2005
+ background: var(--accent-primary);
2006
+ color: #fff;
2007
+ border-bottom-color: var(--accent-primary);
2008
+ }
2009
+ }
2010
+
2011
+ [data-theme="light"] .tab.active.flashing {
2012
+ animation: tab-flash-active-light 1s ease-in-out infinite;
2013
+ }
2014
+
2015
+ @keyframes tab-flash-active-light {
2016
+ 0%, 100% {
2017
+ background: var(--bg-tertiary);
2018
+ border-bottom-color: var(--accent-primary);
2019
+ color: var(--accent-primary);
2020
+ }
2021
+ 50% {
2022
+ background: var(--accent-primary);
2023
+ color: #fff;
2024
+ border-bottom-color: var(--accent-primary);
2025
+ }
2026
+ }
2027
+
2028
+ [data-theme="light"] .mobile-tabs-toggle.flashing {
2029
+ animation: mobile-toggle-flash-light 1s ease-in-out infinite;
2030
+ }
2031
+
2032
+ @keyframes mobile-toggle-flash-light {
2033
+ 0%, 100% {
2034
+ background: var(--bg-tertiary);
2035
+ border-color: var(--accent-primary);
2036
+ box-shadow: 0 0 0 1px var(--accent-primary);
2037
+ }
2038
+ 50% {
2039
+ background: var(--accent-primary);
2040
+ border-color: var(--accent-primary);
2041
+ color: #fff;
2042
+ box-shadow: 0 0 8px var(--accent-primary);
2043
+ }
2044
+ }
2045
+
2046
+ [data-theme="light"] .mobile-tab-option.flashing {
2047
+ animation: mobile-option-flash-light 1s ease-in-out infinite;
2048
+ }
2049
+
2050
+ @keyframes mobile-option-flash-light {
2051
+ 0%, 100% {
2052
+ background: transparent;
2053
+ color: var(--accent-primary);
2054
+ }
2055
+ 50% {
2056
+ background: var(--accent-primary);
2057
+ color: #fff;
2058
+ }
2059
+ }
2060
+
1923
2061
  .tab-icon {
1924
2062
  font-size: 12px;
1925
2063
  }