Skip to content

Commit 5984c56

Browse files
committed
Prevent multiple pty instances from being created
1 parent 7faf88f commit 5984c56

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/connect/http-routes/handlers/create-command.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,33 @@ import { Router } from 'express';
33

44
import { SocketServer } from '../../socket-server.js';
55

6-
export function createCommandHandler(command: string, args?: string): Router {
6+
export enum ConnectCommand {
7+
TERMINAL = 'terminal',
8+
APPLY = 'apply',
9+
PLAN = 'plan',
10+
IMPORT = 'import'
11+
}
12+
13+
const CommandInfo = {
14+
[ConnectCommand.TERMINAL]: {
15+
args: [],
16+
},
17+
[ConnectCommand.APPLY]: {
18+
args: ['-c', 'codify apply']
19+
}
20+
21+
}
22+
23+
export function createCommandHandler(command: ConnectCommand): Router {
24+
// if (!Object.values(ConnectCommand).includes(command)) {
25+
// throw new Error(`Unknown command ${command}. Please check code`);
26+
// }
27+
//
28+
// const commandInfo = CommandInfo[command];
29+
// if (!commandInfo) {
30+
// throw new Error(`Command info not provided for ${command}. Please check code`);
31+
// }
32+
733
const router = Router({
834
mergeParams: true,
935
});
@@ -22,11 +48,15 @@ export function createCommandHandler(command: string, args?: string): Router {
2248
return res.status(400).json({ error: 'SessionId does not exist' });
2349
}
2450

25-
const {ws, server} = session;
51+
const { ws, server } = session;
2652
if (!ws) {
2753
return res.status(400).json({ error: 'SessionId not open' });
2854
}
2955

56+
if (session.pty) {
57+
return res.status(304).json({ status: 'Already started' })
58+
}
59+
3060
const pty = spawn('zsh', [], {
3161
name: 'xterm-color',
3262
cols: 80,
@@ -35,6 +65,8 @@ export function createCommandHandler(command: string, args?: string): Router {
3565
env: process.env
3666
});
3767

68+
session.pty = pty;
69+
3870
pty.onData((data) => {
3971
ws.send(Buffer.from(data, 'utf8'));
4072
});
@@ -48,6 +80,9 @@ export function createCommandHandler(command: string, args?: string): Router {
4880
ws.terminate();
4981
server.close();
5082
})
83+
84+
85+
return res.status(204).json({});
5186
});
5287

5388
return router;

src/connect/http-routes/router.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ router.use('/', defaultHandler);
99
router.use('/apply', createCommandHandler('apply'));
1010
router.use('/plan', createCommandHandler('plan'));
1111
router.use('/import', createCommandHandler('import'));
12+
router.use('/terminal', createCommandHandler(''));
1213

1314
export default router;

src/connect/socket-server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IPty } from '@homebridge/node-pty-prebuilt-multiarch';
12
import { Server as HttpServer, IncomingMessage } from 'node:http';
23
import { Duplex } from 'node:stream';
34
import WebSocket, { WebSocketServer } from 'ws';
@@ -7,6 +8,7 @@ import { config } from '../config.js';
78
export interface Session {
89
server: WebSocketServer;
910
ws?: WebSocket;
11+
pty?: IPty;
1012
}
1113

1214
let instance: SocketServer | undefined;
@@ -67,7 +69,7 @@ export class SocketServer {
6769
return;
6870
}
6971

70-
if (/*!this.validateOrigin(request.headers.origin ?? request.headers.referer ?? '') ||*/ !this.validateConnectionSecret(request)) {
72+
if (/*! this.validateOrigin(request.headers.origin ?? request.headers.referer ?? '') || */ !this.validateConnectionSecret(request)) {
7173
console.error('Unauthorized request. Connection code:', request.headers['sec-websocket-protocol']);
7274
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n')
7375
socket.destroy();

0 commit comments

Comments
 (0)