| 1 | |
| 2 | |
| 3 | from pathlib import Path |
| 4 | import sys |
| 5 | |
| 6 | root = Path(sys.argv[1]) if len(sys.argv) > 1 else Path('/usr/share/novnc') |
| 7 | path = root / 'core/rfb.js' |
| 8 | source = path.read_text() |
| 9 | original = path.with_name('rfb.cage-original.js') |
| 10 | if 'cagePasteText(text)' in source: |
| 11 | if not original.exists(): |
| 12 | raise SystemExit('Restore the package rfb.js before applying a new clipboard patch') |
| 13 | source = original.read_text() |
| 14 | else: |
| 15 | original.write_text(source) |
| 16 | ui = root / 'app/ui.js' |
| 17 | u = ui.read_text() if ui.exists() else '' |
| 18 | u = u.replace('cage=2', 'cage=3') |
| 19 | u = u.replace('"../core/rfb.js"', '"../core/rfb.js?cage=3"') |
| 20 | page = root / 'vnc.html' |
| 21 | h = (page.read_text() if page.exists() else '').replace('cage=2', 'cage=3').replace('"./app/ui.js"', '"./app/ui.js?cage=3"') |
| 22 | h = h.replace('src="cage-clipboard.js"', 'src="cage-clipboard.js?cage=3"') |
| 23 | |
| 24 | def replace_once(old, new): |
| 25 | global source |
| 26 | if source.count(old) != 1: |
| 27 | raise SystemExit('Unsupported noVNC version: Cage input/clipboard patch did not apply') |
| 28 | source = source.replace(old, new) |
| 29 | |
| 30 | |
| 31 | |
| 32 | replace_once(''' focus(options) { |
| 33 | this._canvas.focus(options); |
| 34 | } |
| 35 | |
| 36 | blur() { |
| 37 | this._canvas.blur(); |
| 38 | }''', ''' cageSetKeyboardTarget(target) { |
| 39 | this._keyboard.ungrab(); |
| 40 | this._cageKeyboardTarget = target || this._canvas; |
| 41 | this._keyboard = new Keyboard(this._cageKeyboardTarget); |
| 42 | this._keyboard.onkeyevent = this._handleKeyEvent.bind(this); |
| 43 | if (this._rfbConnectionState === 'connected' && !this._viewOnly) { |
| 44 | this._keyboard.grab(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | focus(options) { |
| 49 | (this._cageKeyboardTarget || this._canvas).focus(options); |
| 50 | } |
| 51 | |
| 52 | blur() { |
| 53 | (this._cageKeyboardTarget || this._canvas).blur(); |
| 54 | }''') |
| 55 | |
| 56 | replace_once(' clipboardPasteFrom(text) {', ''' // Cage: serialize the clipboard notification, data transfer and paste. |
| 57 | // A server fence confirms it has processed the provided clipboard data. |
| 58 | _cageQueueInput(action) { |
| 59 | const result = (this._cageInputChain || Promise.resolve()).then(action); |
| 60 | this._cageInputChain = result; |
| 61 | const clear = () => { |
| 62 | if (this._cageInputChain === result) this._cageInputChain = null; |
| 63 | }; |
| 64 | // A failed text transfer also cancels keys/clicks already queued behind |
| 65 | // it, so an Enter cannot submit a form with the committed text missing. |
| 66 | result.then(clear, clear); |
| 67 | return result; |
| 68 | } |
| 69 | |
| 70 | cageRunInput(action) { |
| 71 | return this._cageQueueInput(() => this._cageRunInput(action)); |
| 72 | } |
| 73 | |
| 74 | async _cageRunInput(action) { |
| 75 | if (this._rfbConnectionState !== 'connected' || this._viewOnly || !this._supportsFence) |
| 76 | throw new Error('Input transport is not ready'); |
| 77 | // Flush preceding VNC clicks/keys before a browser text insertion. |
| 78 | await new Promise((resolve, reject) => { |
| 79 | const id = 'cage-input-' + (this._cageInputSequence = (this._cageInputSequence || 0) + 1); |
| 80 | const timer = setTimeout(() => { |
| 81 | this._cageInputFence = null; |
| 82 | reject(new Error('Input fence timed out')); |
| 83 | }, 4000); |
| 84 | this._cageInputFence = { id, timer, resolve }; |
| 85 | RFB.messages.clientFence(this._sock, (1 << 31) | 1, id); |
| 86 | }); |
| 87 | if (this._rfbConnectionState !== 'connected') throw new Error('Input disconnected'); |
| 88 | return action((...key) => this._cageSendKey(...key)); |
| 89 | } |
| 90 | |
| 91 | cagePasteContent(content, pasteImage, valid, pasteText) { |
| 92 | // Reserve the queue before asynchronous host clipboard/image decoding. |
| 93 | // Keep a rejection handler attached even if an earlier input fails. |
| 94 | const pending = Promise.resolve(content).then(value => ({value}), error => ({error})); |
| 95 | return this._cageQueueInput(async () => { |
| 96 | const {value, error} = await pending; |
| 97 | if (error) throw error; |
| 98 | if (!valid()) throw new Error('Input target changed'); |
| 99 | if (value.png || value.files || value.fileSelection) return this._cageRunInput(() => pasteImage(value)); |
| 100 | if (pasteText) return this._cageRunInput(() => pasteText(value.text || '')); |
| 101 | return this._cagePasteText(value.text || ''); |
| 102 | }); |
| 103 | } |
| 104 | |
| 105 | cagePasteText(text) { |
| 106 | return this._cageQueueInput(() => this._cagePasteText(text)); |
| 107 | } |
| 108 | |
| 109 | _cagePasteText(text) { |
| 110 | return new Promise((resolve, reject) => { |
| 111 | if (this._rfbConnectionState !== 'connected' || this._viewOnly || |
| 112 | !this._supportsFence || |
| 113 | !this._clipboardServerCapabilitiesActions[extendedClipboardActionNotify] || |
| 114 | !this._clipboardServerCapabilitiesActions[extendedClipboardActionProvide]) { |
| 115 | reject(new Error('Clipboard transport is not ready')); |
| 116 | return; |
| 117 | } |
| 118 | const id = 'cage-paste-' + (this._cagePasteSequence = (this._cagePasteSequence || 0) + 1); |
| 119 | const timer = setTimeout(() => { |
| 120 | if (this._cagePasteJob?.id === id) this._cagePasteJob = null; |
| 121 | reject(new Error('Remote application did not accept the paste')); |
| 122 | }, 4000); |
| 123 | this._cagePasteJob = {id, timer, resolve, phase: 'data'}; |
| 124 | this.clipboardPasteFrom(text); |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | clipboardPasteFrom(text) {''') |
| 129 | |
| 130 | replace_once(''' sendKey(keysym, code, down) { |
| 131 | if (this._rfbConnectionState''', ''' sendKey(keysym, code, down) { |
| 132 | if (this._cageInputChain) { |
| 133 | this._cageQueueInput(() => this._cageSendKey(keysym, code, down)); |
| 134 | return; |
| 135 | } |
| 136 | this._cageSendKey(keysym, code, down); |
| 137 | } |
| 138 | |
| 139 | _cageSendKey(keysym, code, down) { |
| 140 | if (this._rfbConnectionState''') |
| 141 | replace_once(''' this.sendKey(keysym, code, true); |
| 142 | this.sendKey(keysym, code, false);''', ''' this._cageSendKey(keysym, code, true); |
| 143 | this._cageSendKey(keysym, code, false);''') |
| 144 | replace_once(''' const scancode = XtScancode[code];''', ''' // Track modifiers when they actually reach the wire, including keys |
| 145 | // waiting behind text transfers. Host key state may already be newer. |
| 146 | if (keysym >= 0xffe1 && keysym <= 0xffee) { |
| 147 | this._cageModifiers ||= new Map(); |
| 148 | if (down) this._cageModifiers.set(code, keysym); |
| 149 | else this._cageModifiers.delete(code); |
| 150 | } |
| 151 | const scancode = XtScancode[code];''') |
| 152 | replace_once(''' _sendMouse(x, y, mask) {''', ''' _sendMouse(x, y, mask) { |
| 153 | if (this._cageInputChain) { |
| 154 | this._cageQueueInput(() => this._cageSendMouse(x, y, mask)); |
| 155 | return; |
| 156 | } |
| 157 | this._cageSendMouse(x, y, mask); |
| 158 | } |
| 159 | |
| 160 | _cageSendMouse(x, y, mask) {''') |
| 161 | provide = 'RFB.messages.extendedClipboardProvide(this._sock, [extendedClipboardFormatText], [this._clipboardText]);' |
| 162 | replace_once(provide, provide + ''' |
| 163 | if (this._cagePasteJob?.phase === 'data') { |
| 164 | this._cagePasteJob.phase = 'fence'; |
| 165 | RFB.messages.clientFence(this._sock, (1 << 31) | 1, this._cagePasteJob.id); |
| 166 | }''') |
| 167 | replace_once(''' if (!(flags & (1<<31))) { |
| 168 | return this._fail("Unexpected fence response"); |
| 169 | }''', ''' if (!(flags & (1<<31))) { |
| 170 | const input = this._cageInputFence; |
| 171 | if (input && payload === input.id) { |
| 172 | clearTimeout(input.timer); |
| 173 | this._cageInputFence = null; |
| 174 | input.resolve(); |
| 175 | return true; |
| 176 | } |
| 177 | if (payload.startsWith('cage-input-')) return true; |
| 178 | const job = this._cagePasteJob; |
| 179 | if (job && job.phase === 'fence' && payload === job.id) { |
| 180 | // Punctuation can commit while Shift is still physically held. |
| 181 | // Paste with a plain Ctrl+V, then restore the wire modifiers; |
| 182 | // queued host key releases will follow in their normal order. |
| 183 | const modifiers = [...(this._cageModifiers || [])]; |
| 184 | for (const [code, sym] of modifiers) this._cageSendKey(sym, code, false); |
| 185 | this._cageSendKey(0xffe3, 'ControlLeft', true); |
| 186 | this._cageSendKey(0x76, 'KeyV', true); |
| 187 | this._cageSendKey(0x76, 'KeyV', false); |
| 188 | this._cageSendKey(0xffe3, 'ControlLeft', false); |
| 189 | for (const [code, sym] of modifiers) this._cageSendKey(sym, code, true); |
| 190 | job.phase = 'keys'; |
| 191 | RFB.messages.clientFence(this._sock, (1 << 31) | 1, job.id + '-keys'); |
| 192 | return true; |
| 193 | } |
| 194 | if (job && job.phase === 'keys' && payload === job.id + '-keys') { |
| 195 | clearTimeout(job.timer); |
| 196 | this._cagePasteJob = null; |
| 197 | job.resolve(); |
| 198 | return true; |
| 199 | } |
| 200 | // A late acknowledgement after a timeout must not disconnect. |
| 201 | if (payload.startsWith('cage-paste-')) return true; |
| 202 | return this._fail("Unexpected fence response"); |
| 203 | }''') |
| 204 | path.write_text(source) |
| 205 | if ui.exists(): ui.write_text(u) |
| 206 | if page.exists(): page.write_text(h) |