mirror of
https://github.com/tale/headplane.git
synced 2026-08-12 06:46:51 +00:00
feat: support resizing and other xterm.js addons
This commit is contained in:
@@ -8,7 +8,7 @@ export interface Command {
|
||||
payload: unknown;
|
||||
}
|
||||
|
||||
interface SSHConnectCommand extends Command {
|
||||
export interface SSHConnectCommand extends Command {
|
||||
op: 'ssh_conn';
|
||||
payload: {
|
||||
sessionId: string;
|
||||
@@ -18,14 +18,31 @@ interface SSHConnectCommand extends Command {
|
||||
};
|
||||
}
|
||||
|
||||
interface SSHCloseCommand extends Command {
|
||||
export interface SSHCloseCommand extends Command {
|
||||
op: 'ssh_term';
|
||||
payload: {
|
||||
sessionId: string;
|
||||
};
|
||||
}
|
||||
|
||||
type AgentCommand = SSHConnectCommand | SSHCloseCommand;
|
||||
export interface SSHResizeCommand extends Command {
|
||||
op: 'ssh_resize';
|
||||
payload: {
|
||||
sessionId: string;
|
||||
width: number;
|
||||
height: number;
|
||||
};
|
||||
}
|
||||
|
||||
export interface SSHDataCommand extends Command {
|
||||
op: 'ssh_data';
|
||||
payload: {
|
||||
sessionId: string;
|
||||
data: Uint8Array;
|
||||
};
|
||||
}
|
||||
|
||||
type AgentCommand = SSHConnectCommand | SSHCloseCommand | SSHResizeCommand;
|
||||
|
||||
export async function dispatchCommand(
|
||||
dispatcher: Writable,
|
||||
|
||||
@@ -13,7 +13,7 @@ export type ChannelType = 0 | 1 | 2;
|
||||
interface SSHFrame {
|
||||
sessionId: string;
|
||||
channel: ChannelType;
|
||||
payload: Blob | ArrayBufferLike | string;
|
||||
payload: Buffer;
|
||||
}
|
||||
|
||||
export async function encodeSSHFrame(frame: SSHFrame) {
|
||||
@@ -23,17 +23,9 @@ export async function encodeSSHFrame(frame: SSHFrame) {
|
||||
return;
|
||||
}
|
||||
|
||||
const payload = Buffer.isBuffer(frame.payload)
|
||||
? frame.payload
|
||||
: typeof frame.payload === 'string'
|
||||
? Buffer.from(frame.payload, 'utf8')
|
||||
: frame.payload instanceof Blob
|
||||
? Buffer.from(await frame.payload.arrayBuffer())
|
||||
: Buffer.from(frame.payload);
|
||||
|
||||
// Size can only hold 4 bytes
|
||||
if (payload.length > 0xffffffff) {
|
||||
log.error('agent', 'SSH payload too large: %d bytes', payload.length);
|
||||
if (frame.payload.length > 0xffffffff) {
|
||||
log.error('agent', 'SSH payload too large: %d bytes', frame.payload.length);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -42,7 +34,7 @@ export async function encodeSSHFrame(frame: SSHFrame) {
|
||||
1 + // Version
|
||||
1 + // Channel Type
|
||||
(1 + sid.length) + // Session ID length + SID
|
||||
(4 + payload.length); // Payload length + Payload
|
||||
(4 + frame.payload.length); // Payload length + Payload
|
||||
|
||||
const buf = Buffer.alloc(frameSize);
|
||||
buf.write(MAGIC, 0, 'utf8');
|
||||
@@ -53,8 +45,8 @@ export async function encodeSSHFrame(frame: SSHFrame) {
|
||||
const offset = 7 + sid.length;
|
||||
sid.copy(buf, 7);
|
||||
|
||||
buf.writeUInt32BE(payload.length, offset);
|
||||
payload.copy(buf, offset + 4);
|
||||
buf.writeUInt32BE(frame.payload.length, offset);
|
||||
frame.payload.copy(buf, offset + 4);
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
+49
-7
@@ -1,10 +1,17 @@
|
||||
import { ChildProcess } from 'node:child_process';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import type { Readable, Writable } from 'node:stream';
|
||||
import { decode } from 'cborg';
|
||||
import { Context } from 'hono';
|
||||
import { WSContext, WSEvents } from 'hono/ws';
|
||||
import log from '~/utils/log';
|
||||
import { dispatchCommand, dispatchWeb } from './dispatcher';
|
||||
import {
|
||||
Command,
|
||||
SSHDataCommand,
|
||||
SSHResizeCommand,
|
||||
dispatchCommand,
|
||||
dispatchWeb,
|
||||
} from './dispatcher';
|
||||
import { decodeSSHFrame, encodeSSHFrame } from './encoder';
|
||||
|
||||
interface SSHConnection {
|
||||
@@ -134,13 +141,48 @@ export class SSHMultiplexer {
|
||||
return;
|
||||
}
|
||||
|
||||
const encodedFrame = await encodeSSHFrame({
|
||||
sessionId,
|
||||
channel: 0, // stdin
|
||||
payload: event.data,
|
||||
});
|
||||
const wsData = Buffer.isBuffer(event.data)
|
||||
? event.data
|
||||
: typeof event.data === 'string'
|
||||
? Buffer.from(event.data, 'utf8')
|
||||
: event.data instanceof Blob
|
||||
? Buffer.from(await event.data.arrayBuffer())
|
||||
: Buffer.from(event.data);
|
||||
|
||||
this.sshInput.write(encodedFrame);
|
||||
const obj = decode(wsData) as Command;
|
||||
if (obj.op === 'ssh_data') {
|
||||
const data = obj as SSHDataCommand;
|
||||
if (data.payload.sessionId !== sessionId) {
|
||||
log.warn(
|
||||
'agent',
|
||||
'Received data for mismatched SSH session %s',
|
||||
data.payload.sessionId,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const encodedFrame = await encodeSSHFrame({
|
||||
sessionId,
|
||||
channel: 0, // stdin
|
||||
payload: Buffer.from(data.payload.data),
|
||||
});
|
||||
|
||||
this.sshInput.write(encodedFrame);
|
||||
}
|
||||
|
||||
if (obj.op === 'ssh_resize') {
|
||||
const resize = obj as SSHResizeCommand;
|
||||
if (resize.payload.sessionId !== sessionId) {
|
||||
log.warn(
|
||||
'agent',
|
||||
'Received resize for mismatched SSH session %s',
|
||||
resize.payload.sessionId,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
await dispatchCommand(this.control, resize);
|
||||
}
|
||||
},
|
||||
|
||||
onClose: async (_, ws) => {
|
||||
|
||||
Reference in New Issue
Block a user