|
| 1 | +/* |
| 2 | + * Copyright 2021 The Kubernetes Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * This file defines a channel between the Electron renderer process |
| 19 | + * and Electron main process. |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +import Debug from 'debug' |
| 24 | +import { EventEmitter } from 'events' |
| 25 | +import { IpcMain, IpcRenderer, WebContents } from 'electron' |
| 26 | + |
| 27 | +import { Channel, ReadyState } from './channel' |
| 28 | +import { onConnection, disableBashSessions } from './server' |
| 29 | + |
| 30 | +interface Args { |
| 31 | + execUUID: string |
| 32 | +} |
| 33 | + |
| 34 | +const debugMain = Debug('plugin-bash-like/pty/electron/main') |
| 35 | +const debugRenderer = Debug('plugin-bash-like/pty/electron/renderer') |
| 36 | + |
| 37 | +function messageChannel(execUUID) { |
| 38 | + return `/plugin-bash-like/pty/message/${execUUID}` |
| 39 | +} |
| 40 | + |
| 41 | +class ElectronMainSideChannel extends EventEmitter implements Channel { |
| 42 | + /** is the channel alive? */ |
| 43 | + public readonly isAlive = true |
| 44 | + |
| 45 | + public readonly readyState = ReadyState.OPEN |
| 46 | + |
| 47 | + private ipcMain: IpcMain |
| 48 | + private otherSide: WebContents |
| 49 | + |
| 50 | + private messageChannel: string |
| 51 | + |
| 52 | + private handleMessage = (_, msg: string) => { |
| 53 | + // debugMain('got message', msg) |
| 54 | + this.emit('message', msg) |
| 55 | + } |
| 56 | + |
| 57 | + public async init(args: Args, otherSide: WebContents) { |
| 58 | + debugMain('main side init', args.execUUID) |
| 59 | + |
| 60 | + const { ipcMain } = await import('electron') |
| 61 | + this.ipcMain = ipcMain |
| 62 | + this.otherSide = otherSide |
| 63 | + |
| 64 | + this.messageChannel = messageChannel(args.execUUID) |
| 65 | + ipcMain.on(this.messageChannel, this.handleMessage) |
| 66 | + |
| 67 | + await onConnection(await disableBashSessions())(this) |
| 68 | + } |
| 69 | + |
| 70 | + /** Forcibly close the channel */ |
| 71 | + public close() { |
| 72 | + this.emit('exit') |
| 73 | + this.ipcMain.off(this.messageChannel, this.handleMessage) |
| 74 | + } |
| 75 | + |
| 76 | + /** Send a message over the channel */ |
| 77 | + public send(msg: string | Buffer) { |
| 78 | + this.otherSide.send(this.messageChannel, msg) |
| 79 | + } |
| 80 | + |
| 81 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 82 | + public removeEventListener(eventType: string, handler: any) { |
| 83 | + this.off(eventType, handler) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +export default class ElectronRendererSideChannel extends EventEmitter implements Channel { |
| 88 | + /** is the channel alive? */ |
| 89 | + public readonly isAlive = true |
| 90 | + |
| 91 | + public readonly readyState = ReadyState.OPEN |
| 92 | + |
| 93 | + private ipcRenderer: IpcRenderer |
| 94 | + private messageChannel: string |
| 95 | + |
| 96 | + private handleMessage = (_, msg: string) => { |
| 97 | + this.emit('message', msg) |
| 98 | + } |
| 99 | + |
| 100 | + public async init(execUUID: string) { |
| 101 | + const { ipcRenderer } = await import('electron') |
| 102 | + this.ipcRenderer = ipcRenderer |
| 103 | + debugRenderer('renderer side init') |
| 104 | + |
| 105 | + this.messageChannel = messageChannel(execUUID) |
| 106 | + |
| 107 | + ipcRenderer.once(`/exec/response/${execUUID}`, (_, _msg: string) => { |
| 108 | + const msg = JSON.parse(_msg) |
| 109 | + debugRenderer('renderer side init exec response', msg) |
| 110 | + if (msg.returnValue === true) { |
| 111 | + this.ipcRenderer.on(this.messageChannel, this.handleMessage) |
| 112 | + this.emit('open') |
| 113 | + } |
| 114 | + }) |
| 115 | + |
| 116 | + ipcRenderer.send( |
| 117 | + '/exec/invoke', |
| 118 | + JSON.stringify({ |
| 119 | + module: 'plugin-bash-like', |
| 120 | + main: 'initMainPty', |
| 121 | + hash: execUUID, |
| 122 | + args: { |
| 123 | + execUUID |
| 124 | + } |
| 125 | + }) |
| 126 | + ) |
| 127 | + } |
| 128 | + |
| 129 | + /** Forcibly close the channel */ |
| 130 | + public close() { |
| 131 | + this.send('exit') |
| 132 | + this.ipcRenderer.off(this.messageChannel, this.handleMessage) |
| 133 | + } |
| 134 | + |
| 135 | + /** Send a message over the channel */ |
| 136 | + public send(msg: string | Buffer) { |
| 137 | + this.ipcRenderer.send(this.messageChannel, msg.toString()) |
| 138 | + } |
| 139 | + |
| 140 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 141 | + public removeEventListener(eventType: string, handler: any) { |
| 142 | + this.off(eventType, handler) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +export async function initMainPty(args: Args, otherSide: WebContents) { |
| 147 | + debugMain('initMainPty', args) |
| 148 | + try { |
| 149 | + await new ElectronMainSideChannel().init(args, otherSide) |
| 150 | + return true |
| 151 | + } catch (err) { |
| 152 | + console.error('Error starting up pty', err) |
| 153 | + return false |
| 154 | + } |
| 155 | +} |
0 commit comments