mirror of
https://github.com/koush/scrypted.git
synced 2026-02-13 10:22:57 +00:00
core: refactor console on top of pty
This commit is contained in:
@@ -12,7 +12,7 @@ import { MediaCore } from './media-core';
|
||||
import { newScript, ScriptCore, ScriptCoreNativeId } from './script-core';
|
||||
import { TerminalService, TerminalServiceNativeId } from './terminal-service';
|
||||
import { UsersCore, UsersNativeId } from './user';
|
||||
import { ReplService, ReplServiceNativeId } from './repl-service';
|
||||
import { ConsoleServiceNativeId, PluginSocketService, ReplServiceNativeId } from './plugin-socket-service';
|
||||
|
||||
const { systemManager, deviceManager, endpointManager } = sdk;
|
||||
|
||||
@@ -31,7 +31,7 @@ interface RoutedHttpRequest extends HttpRequest {
|
||||
params: { [key: string]: string };
|
||||
}
|
||||
|
||||
class ScryptedCore extends ScryptedDeviceBase implements HttpRequestHandler, EngineIOHandler, DeviceProvider, Settings {
|
||||
class ScryptedCore extends ScryptedDeviceBase implements HttpRequestHandler, DeviceProvider, Settings {
|
||||
router: any = Router();
|
||||
publicRouter: any = Router();
|
||||
mediaCore: MediaCore;
|
||||
@@ -39,7 +39,8 @@ class ScryptedCore extends ScryptedDeviceBase implements HttpRequestHandler, Eng
|
||||
aggregateCore: AggregateCore;
|
||||
automationCore: AutomationCore;
|
||||
users: UsersCore;
|
||||
replService: ReplService;
|
||||
consoleService: PluginSocketService;
|
||||
replService: PluginSocketService;
|
||||
terminalService: TerminalService;
|
||||
localAddresses: string[];
|
||||
storageSettings = new StorageSettings(this, {
|
||||
@@ -185,7 +186,9 @@ class ScryptedCore extends ScryptedDeviceBase implements HttpRequestHandler, Eng
|
||||
if (nativeId === TerminalServiceNativeId)
|
||||
return this.terminalService ||= new TerminalService();
|
||||
if (nativeId === ReplServiceNativeId)
|
||||
return this.replService ||= new ReplService();
|
||||
return this.replService ||= new PluginSocketService(ReplServiceNativeId, 'repl');
|
||||
if (nativeId === ConsoleServiceNativeId)
|
||||
return this.consoleService ||= new PluginSocketService(ConsoleServiceNativeId, 'console');
|
||||
}
|
||||
|
||||
async releaseDevice(id: string, nativeId: string): Promise<void> {
|
||||
@@ -198,35 +201,6 @@ class ScryptedCore extends ScryptedDeviceBase implements HttpRequestHandler, Eng
|
||||
return check;
|
||||
}
|
||||
|
||||
async checkService(request: HttpRequest, ws: WebSocket, name: string): Promise<boolean> {
|
||||
// only allow admin users to access these services.
|
||||
if (request.aclId)
|
||||
return false;
|
||||
const check = this.checkEngineIoEndpoint(request, name);
|
||||
if (!check)
|
||||
return false;
|
||||
const deviceId = request.url.substr(check.length).split('/')[0];
|
||||
const plugins = await systemManager.getComponent('plugins');
|
||||
const { nativeId, pluginId } = await plugins.getDeviceInfo(deviceId);
|
||||
const port = await plugins.getRemoteServicePort(pluginId, name);
|
||||
const socket = net.connect(port);
|
||||
socket.on('close', () => ws.close());
|
||||
socket.on('data', data => ws.send(data));
|
||||
socket.resume();
|
||||
socket.write(nativeId?.toString() || 'undefined');
|
||||
ws.onclose = () => socket.destroy();
|
||||
ws.onmessage = message => socket.write(message.data);
|
||||
return true;
|
||||
}
|
||||
|
||||
async onConnection(request: HttpRequest, ws: WebSocket): Promise<void> {
|
||||
if (await this.checkService(request, ws, 'console')) {
|
||||
return;
|
||||
}
|
||||
|
||||
ws.close();
|
||||
}
|
||||
|
||||
async handlePublicFinal(request: HttpRequest, response: HttpResponse) {
|
||||
// need to strip off the query.
|
||||
const incomingPathname = request.url.split('?')[0];
|
||||
|
||||
@@ -4,29 +4,20 @@ import { once } from 'events';
|
||||
import net from 'net';
|
||||
|
||||
export const ReplServiceNativeId = 'replservice';
|
||||
export const ConsoleServiceNativeId = 'consoleservice';
|
||||
|
||||
export class ReplService extends ScryptedDeviceBase implements StreamService {
|
||||
constructor(nativeId?: ScryptedNativeId) {
|
||||
super(ReplServiceNativeId);
|
||||
export class PluginSocketService extends ScryptedDeviceBase implements StreamService {
|
||||
constructor(nativeId: ScryptedNativeId, public serviceName: string) {
|
||||
super(nativeId);
|
||||
}
|
||||
|
||||
/*
|
||||
* The input to this stream can send buffers for normal terminal data and strings
|
||||
* for control messages. Control messages are JSON-formatted.
|
||||
*
|
||||
* The current implemented control messages:
|
||||
*
|
||||
* Start: { "interactive": boolean, "cmd": string[] }
|
||||
* Resize: { "dim": { "cols": number, "rows": number } }
|
||||
* EOF: { "eof": true }
|
||||
*/
|
||||
async connectStream(input?: AsyncGenerator<Buffer | string, void>, options?: any): Promise<AsyncGenerator<Buffer, void>> {
|
||||
const pluginId = options?.pluginId as string;
|
||||
if (!pluginId)
|
||||
throw new Error('must provide pluginId');
|
||||
|
||||
const plugins = await sdk.systemManager.getComponent('plugins');
|
||||
const replPort: number = await plugins.getRemoteServicePort(pluginId, 'repl');
|
||||
const replPort: number = await plugins.getRemoteServicePort(pluginId, this.serviceName);
|
||||
|
||||
const socket = net.connect(replPort);
|
||||
await once(socket, 'connect');
|
||||
@@ -61,6 +52,8 @@ export class ReplService extends ScryptedDeviceBase implements StreamService {
|
||||
yield await queue.dequeue();
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
}
|
||||
finally {
|
||||
socket.destroy();
|
||||
}
|
||||
Reference in New Issue
Block a user