@lethevimlet/sshift 0.2.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.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1146 -0
  3. package/example.config.json +162 -0
  4. package/package.json +74 -0
  5. package/src/server/endpoints/index.js +12 -0
  6. package/src/server/endpoints/rest/bookmarks.js +106 -0
  7. package/src/server/endpoints/rest/config.js +65 -0
  8. package/src/server/endpoints/rest/folders.js +127 -0
  9. package/src/server/endpoints/rest/index.js +32 -0
  10. package/src/server/endpoints/rest/sessions.js +113 -0
  11. package/src/server/endpoints/rest/system.js +211 -0
  12. package/src/server/endpoints/ws/index.js +46 -0
  13. package/src/server/endpoints/ws/layout.js +43 -0
  14. package/src/server/endpoints/ws/sftp.js +156 -0
  15. package/src/server/endpoints/ws/ssh.js +180 -0
  16. package/src/server/endpoints/ws/tabs.js +211 -0
  17. package/src/server/index.js +127 -0
  18. package/src/server/services/index.js +12 -0
  19. package/src/server/services/sftp-manager.js +266 -0
  20. package/src/server/services/ssh-manager.js +527 -0
  21. package/src/server/utils/config.js +171 -0
  22. package/src/server/utils/env-loader.js +29 -0
  23. package/src/server/utils/index.js +14 -0
  24. package/src/server/utils/tab-manager.js +266 -0
  25. package/src/webapp/css/style.css +3819 -0
  26. package/src/webapp/index.html +1008 -0
  27. package/src/webapp/js/app.js +8308 -0
  28. package/src/webapp/layouts.json +128 -0
  29. package/src/webapp/libs/font-awesome/css/all.min.css +9 -0
  30. package/src/webapp/libs/font-awesome/webfonts/fa-brands-400.ttf +0 -0
  31. package/src/webapp/libs/font-awesome/webfonts/fa-brands-400.woff2 +0 -0
  32. package/src/webapp/libs/font-awesome/webfonts/fa-regular-400.ttf +0 -0
  33. package/src/webapp/libs/font-awesome/webfonts/fa-regular-400.woff2 +0 -0
  34. package/src/webapp/libs/font-awesome/webfonts/fa-solid-900.ttf +0 -0
  35. package/src/webapp/libs/font-awesome/webfonts/fa-solid-900.woff2 +0 -0
  36. package/src/webapp/libs/lucide/lucide.min.js +12 -0
  37. package/src/webapp/libs/xterm/xterm-addon-fit.js +2 -0
  38. package/src/webapp/libs/xterm/xterm-addon-search.js +2 -0
  39. package/src/webapp/libs/xterm/xterm-addon-serialize.js +2 -0
  40. package/src/webapp/libs/xterm/xterm-addon-web-links.js +2 -0
  41. package/src/webapp/libs/xterm/xterm.css +209 -0
  42. package/src/webapp/libs/xterm/xterm.js +2 -0
  43. package/sshift +269 -0
package/sshift ADDED
@@ -0,0 +1,269 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * sshift - Web-based SSH & SFTP Terminal Client
5
+ *
6
+ * This is the main entry point for the sshift application.
7
+ * Run this file to start the server.
8
+ *
9
+ * Usage:
10
+ * ./sshift # Start server on default port (8022)
11
+ * ./sshift --port 8080 # Start server on custom port
12
+ * ./sshift --bind 127.0.0.1 # Bind to specific address
13
+ * ./sshift --dev # Start in development mode (port 3000)
14
+ * ./sshift --stop # Stop running instance
15
+ * ./sshift --restart # Restart running instance
16
+ * ./sshift --status # Check if sshift is running
17
+ * ./sshift --help # Show help message
18
+ *
19
+ * Environment Variables:
20
+ * PORT=8080 ./sshift # Start server on custom port (overrides config)
21
+ * BIND=127.0.0.1 ./sshift # Bind to specific address (overrides config)
22
+ * NODE_ENV=development ./sshift # Start in dev mode (port 3000, or from config)
23
+ *
24
+ * Port Priority:
25
+ * 1. --port CLI argument (highest priority)
26
+ * 2. PORT environment variable
27
+ * 3. config.json port/devPort based on NODE_ENV
28
+ * 4. Default: 8022 (production), 3000 (development)
29
+ */
30
+
31
+ 'use strict';
32
+
33
+ const path = require('path');
34
+ const fs = require('fs');
35
+ const { spawn } = require('child_process');
36
+
37
+ // Get the directory where this script is located
38
+ const scriptDir = __dirname;
39
+ const serverPath = path.join(scriptDir, 'src', 'server', 'index.js');
40
+ const pidFile = path.join(scriptDir, '.sshift.pid');
41
+
42
+ // CLI argument parsing
43
+ const args = process.argv.slice(2);
44
+ const options = {
45
+ port: null,
46
+ bind: null,
47
+ dev: false,
48
+ stop: false,
49
+ restart: false,
50
+ status: false,
51
+ help: false
52
+ };
53
+
54
+ // Parse arguments
55
+ for (let i = 0; i < args.length; i++) {
56
+ const arg = args[i];
57
+ if (arg === '--port' || arg === '-p') {
58
+ options.port = parseInt(args[++i], 10);
59
+ if (isNaN(options.port)) {
60
+ console.error('Error: --port requires a valid port number');
61
+ process.exit(1);
62
+ }
63
+ } else if (arg === '--bind' || arg === '-b') {
64
+ options.bind = args[++i];
65
+ if (!options.bind) {
66
+ console.error('Error: --bind requires an address');
67
+ process.exit(1);
68
+ }
69
+ } else if (arg === '--dev' || arg === '-d') {
70
+ options.dev = true;
71
+ } else if (arg === '--stop' || arg === '-s') {
72
+ options.stop = true;
73
+ } else if (arg === '--restart' || arg === '-r') {
74
+ options.restart = true;
75
+ } else if (arg === '--status') {
76
+ options.status = true;
77
+ } else if (arg === '--help' || arg === '-h') {
78
+ options.help = true;
79
+ } else {
80
+ console.error(`Error: Unknown argument: ${arg}`);
81
+ console.error('Use --help for usage information');
82
+ process.exit(1);
83
+ }
84
+ }
85
+
86
+ // Show help
87
+ if (options.help) {
88
+ console.log(`
89
+ sshift - Web-based SSH & SFTP Terminal Client
90
+
91
+ Usage:
92
+ ./sshift Start server on default port (8022)
93
+ ./sshift --port 8080 Start server on custom port
94
+ ./sshift --bind 127.0.0.1 Bind to specific address
95
+ ./sshift --dev Start in development mode (port 3000)
96
+ ./sshift --stop Stop running instance
97
+ ./sshift --restart Restart running instance
98
+ ./sshift --status Check if sshift is running
99
+ ./sshift --help Show this help message
100
+
101
+ Options:
102
+ -p, --port <port> Server port (default: 8022, dev: 3000)
103
+ -b, --bind <address> Bind address (default: 0.0.0.0)
104
+ -d, --dev Development mode
105
+ -s, --stop Stop running instance
106
+ -r, --restart Restart running instance
107
+ --status Check running status
108
+ -h, --help Show this help message
109
+
110
+ Environment Variables:
111
+ PORT=8080 Override port
112
+ BIND=127.0.0.1 Override bind address
113
+ NODE_ENV=development Development mode
114
+
115
+ Examples:
116
+ ./sshift # Start on default port
117
+ ./sshift --port 9000 # Start on port 9000
118
+ ./sshift --dev # Start in dev mode
119
+ ./sshift --bind 127.0.0.1 # Bind to localhost only
120
+ ./sshift --stop # Stop running instance
121
+ ./sshift --restart # Restart running instance
122
+ PORT=8080 ./sshift # Start on port 8080 (env var)
123
+ `);
124
+ process.exit(0);
125
+ }
126
+
127
+ // Check if index.js exists
128
+ if (!fs.existsSync(serverPath)) {
129
+ console.error('Error: index.js not found');
130
+ console.error('Make sure you are running sshift from the installation directory');
131
+ process.exit(1);
132
+ }
133
+
134
+ // Helper: Check if sshift is running
135
+ function isRunning() {
136
+ if (fs.existsSync(pidFile)) {
137
+ try {
138
+ const pid = parseInt(fs.readFileSync(pidFile, 'utf8').trim(), 10);
139
+ if (isNaN(pid)) return { running: false };
140
+
141
+ // Check if process exists
142
+ try {
143
+ process.kill(pid, 0);
144
+ return { running: true, pid };
145
+ } catch (e) {
146
+ // Process not running, clean up stale PID file
147
+ fs.unlinkSync(pidFile);
148
+ return { running: false };
149
+ }
150
+ } catch (e) {
151
+ return { running: false };
152
+ }
153
+ }
154
+ return { running: false };
155
+ }
156
+
157
+ // Helper: Stop running instance
158
+ function stopInstance() {
159
+ const status = isRunning();
160
+
161
+ if (!status.running) {
162
+ console.log('sshift is not running');
163
+ return true;
164
+ }
165
+
166
+ console.log(`Stopping sshift (PID: ${status.pid})...`);
167
+
168
+ try {
169
+ process.kill(status.pid, 'SIGTERM');
170
+
171
+ // Wait for process to stop (up to 10 seconds)
172
+ let attempts = 0;
173
+ while (attempts < 10) {
174
+ try {
175
+ process.kill(status.pid, 0);
176
+ // Still running, wait
177
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1000);
178
+ attempts++;
179
+ } catch (e) {
180
+ // Process stopped
181
+ break;
182
+ }
183
+ }
184
+
185
+ // Force kill if still running
186
+ try {
187
+ process.kill(status.pid, 0);
188
+ console.log('Process did not stop gracefully, force killing...');
189
+ process.kill(status.pid, 'SIGKILL');
190
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1000);
191
+ } catch (e) {
192
+ // Already stopped
193
+ }
194
+
195
+ // Clean up PID file
196
+ if (fs.existsSync(pidFile)) {
197
+ fs.unlinkSync(pidFile);
198
+ }
199
+
200
+ console.log('sshift stopped successfully');
201
+ return true;
202
+ } catch (e) {
203
+ console.error(`Error stopping sshift: ${e.message}`);
204
+ return false;
205
+ }
206
+ }
207
+
208
+ // Handle --status
209
+ if (options.status) {
210
+ const status = isRunning();
211
+ if (status.running) {
212
+ console.log(`sshift is running (PID: ${status.pid})`);
213
+ process.exit(0);
214
+ } else {
215
+ console.log('sshift is not running');
216
+ process.exit(1);
217
+ }
218
+ }
219
+
220
+ // Handle --stop
221
+ if (options.stop) {
222
+ const success = stopInstance();
223
+ process.exit(success ? 0 : 1);
224
+ }
225
+
226
+ // Handle --restart
227
+ if (options.restart) {
228
+ console.log('Restarting sshift...');
229
+ stopInstance();
230
+ // Continue to start below
231
+ }
232
+
233
+ // For start operations, change to script directory
234
+ process.chdir(scriptDir);
235
+
236
+ // Load environment variables from .env files
237
+ // Priority: .env/.env.local > .env.local > .env/.env > .env
238
+ const envPaths = [
239
+ path.join(scriptDir, '.env', '.env.local'),
240
+ path.join(scriptDir, '.env.local'),
241
+ path.join(scriptDir, '.env', '.env'),
242
+ path.join(scriptDir, '.env')
243
+ ];
244
+
245
+ // Load dotenv if available
246
+ try {
247
+ const dotenv = require('dotenv');
248
+ envPaths.forEach(envPath => {
249
+ if (fs.existsSync(envPath)) {
250
+ dotenv.config({ path: envPath });
251
+ }
252
+ });
253
+ } catch (e) {
254
+ // dotenv not available, environment variables will be loaded by server.js
255
+ }
256
+
257
+ // Set environment variables from CLI options
258
+ if (options.port !== null) {
259
+ process.env.PORT = options.port.toString();
260
+ }
261
+ if (options.bind) {
262
+ process.env.BIND = options.bind;
263
+ }
264
+ if (options.dev) {
265
+ process.env.NODE_ENV = 'development';
266
+ }
267
+
268
+ // Start the server
269
+ require(serverPath);