webssh2-server
Version:
A Websocket to SSH2 gateway using xterm.js, socket.io, ssh2
114 lines (113 loc) • 2.96 kB
TypeScript
import type { EventEmitter } from 'node:events';
import type { Config } from '../types/config.js';
import type { AuthCredentials } from '../types/contracts/v1/socket.js';
import type { SSHCtor } from '../types/ssh.js';
export interface SSHConfig {
host: string;
port: number;
username: string;
password?: string;
privateKey?: string;
passphrase?: string;
algorithms?: Record<string, unknown>;
readyTimeout?: number;
keepaliveInterval?: number;
}
export interface ShellOptions {
term?: string;
rows?: number;
cols?: number;
width?: number;
height?: number;
}
export interface ExecOptions {
pty?: boolean;
term?: string;
rows?: number;
cols?: number;
width?: number;
height?: number;
}
export interface Connection {
id: string;
status: 'connecting' | 'connected' | 'disconnected' | 'error';
connectedAt?: Date;
error?: string;
}
export type Stream = EventEmitter & {
write?: (data: string) => void;
end?: () => void;
stderr?: EventEmitter;
signal?: (signal: string) => void;
close?: () => void;
};
export interface ConnectionResult {
success: boolean;
connection?: Connection;
error?: string;
}
export interface StreamResult {
success: boolean;
stream?: Stream;
error?: string;
}
/**
* Creates SSH configuration from credentials
* @param credentials - Authentication credentials
* @param config - Server configuration
* @returns SSH configuration
* @pure
*/
export declare function createSSHConfig(credentials: AuthCredentials, config: Config): SSHConfig;
/**
* Parses SSH error for user-friendly message
* @param error - SSH error
* @returns User-friendly error message
* @pure
*/
export declare function parseSSHError(error: unknown): string;
/**
* Generates unique connection ID
* @returns Connection ID
* @pure
*/
export declare function generateConnectionId(): string;
/**
* SSH connection adapter for managing SSH connections
* Handles all SSH I/O operations
*/
export declare class SSHConnectionAdapter {
private sshClient;
private connection;
private readonly config;
private readonly SSHConnectionClass;
constructor(config: Config, SSHConnectionClass: SSHCtor);
/**
* Connect to SSH server
*/
connect(credentials: AuthCredentials): Promise<ConnectionResult>;
/**
* Create SSH shell
*/
shell(options: ShellOptions, env?: Record<string, string> | null): Promise<StreamResult>;
/**
* Execute command over SSH
*/
exec(command: string, options: ExecOptions, env?: Record<string, string>): Promise<StreamResult>;
/**
* Resize terminal
*/
resizeTerminal(rows: number, cols: number): void;
/**
* End SSH connection
*/
end(): void;
/**
* Get connection status
*/
getConnection(): Connection | null;
/**
* Check if connected
*/
isConnected(): boolean;
}