| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | import { toUnsigned32bit, toSigned32bit } from './util/int.js'; |
| 11 | import * as Log from './util/logging.js'; |
| 12 | import { encodeUTF8, decodeUTF8 } from './util/strings.js'; |
| 13 | import { dragThreshold, supportsWebCodecsH264Decode } from './util/browser.js'; |
| 14 | import { clientToElement } from './util/element.js'; |
| 15 | import { setCapture } from './util/events.js'; |
| 16 | import EventTargetMixin from './util/eventtarget.js'; |
| 17 | import Display from "./display.js"; |
| 18 | import CageBrowserDisplay from "./cage-browser-display.js"; |
| 19 | import CageWheel, { cageWheelCapability, cageWheelEncoding, cageWheelPacket } from "./cage-wheel.js"; |
| 20 | import Inflator from "./inflator.js"; |
| 21 | import Deflator from "./deflator.js"; |
| 22 | import Keyboard from "./input/keyboard.js"; |
| 23 | import GestureHandler from "./input/gesturehandler.js"; |
| 24 | import Cursor from "./util/cursor.js"; |
| 25 | import Websock from "./websock.js"; |
| 26 | import KeyTable from "./input/keysym.js"; |
| 27 | import XtScancode from "./input/xtscancodes.js"; |
| 28 | import { encodings } from "./encodings.js"; |
| 29 | import RSAAESAuthenticationState from "./ra2.js"; |
| 30 | import legacyCrypto from "./crypto/crypto.js"; |
| 31 | |
| 32 | import RawDecoder from "./decoders/raw.js"; |
| 33 | import CopyRectDecoder from "./decoders/copyrect.js"; |
| 34 | import RREDecoder from "./decoders/rre.js"; |
| 35 | import HextileDecoder from "./decoders/hextile.js"; |
| 36 | import ZlibDecoder from './decoders/zlib.js'; |
| 37 | import TightDecoder from "./decoders/tight.js"; |
| 38 | import TightPNGDecoder from "./decoders/tightpng.js"; |
| 39 | import ZRLEDecoder from "./decoders/zrle.js"; |
| 40 | import JPEGDecoder from "./decoders/jpeg.js"; |
| 41 | import H264Decoder from "./decoders/h264.js"; |
| 42 | |
| 43 | |
| 44 | const DISCONNECT_TIMEOUT = 3; |
| 45 | const DEFAULT_BACKGROUND = 'rgb(40, 40, 40)'; |
| 46 | |
| 47 | |
| 48 | const MOUSE_MOVE_DELAY = 17; |
| 49 | |
| 50 | |
| 51 | const WHEEL_STEP = 50; |
| 52 | const WHEEL_LINE_HEIGHT = 19; |
| 53 | |
| 54 | |
| 55 | const GESTURE_ZOOMSENS = 75; |
| 56 | const GESTURE_SCRLSENS = 50; |
| 57 | const DOUBLE_TAP_TIMEOUT = 1000; |
| 58 | const DOUBLE_TAP_THRESHOLD = 50; |
| 59 | |
| 60 | |
| 61 | const securityTypeNone = 1; |
| 62 | const securityTypeVNCAuth = 2; |
| 63 | const securityTypeRA2ne = 6; |
| 64 | const securityTypeTight = 16; |
| 65 | const securityTypeVeNCrypt = 19; |
| 66 | const securityTypeXVP = 22; |
| 67 | const securityTypeARD = 30; |
| 68 | const securityTypeMSLogonII = 113; |
| 69 | |
| 70 | |
| 71 | const securityTypeUnixLogon = 129; |
| 72 | |
| 73 | |
| 74 | const securityTypePlain = 256; |
| 75 | |
| 76 | |
| 77 | const extendedClipboardFormatText = 1; |
| 78 | |
| 79 | const extendedClipboardFormatRtf = 1 << 1; |
| 80 | const extendedClipboardFormatHtml = 1 << 2; |
| 81 | const extendedClipboardFormatDib = 1 << 3; |
| 82 | const extendedClipboardFormatFiles = 1 << 4; |
| 83 | |
| 84 | |
| 85 | |
| 86 | const extendedClipboardActionCaps = 1 << 24; |
| 87 | const extendedClipboardActionRequest = 1 << 25; |
| 88 | const extendedClipboardActionPeek = 1 << 26; |
| 89 | const extendedClipboardActionNotify = 1 << 27; |
| 90 | const extendedClipboardActionProvide = 1 << 28; |
| 91 | |
| 92 | export default class RFB extends EventTargetMixin { |
| 93 | constructor(target, urlOrChannel, options) { |
| 94 | if (!target) { |
| 95 | throw new Error("Must specify target"); |
| 96 | } |
| 97 | if (!urlOrChannel) { |
| 98 | throw new Error("Must specify URL, WebSocket or RTCDataChannel"); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | if (!window.isSecureContext) { |
| 104 | Log.Error("noVNC requires a secure context (TLS). Expect crashes!"); |
| 105 | } |
| 106 | |
| 107 | super(); |
| 108 | |
| 109 | this._target = target; |
| 110 | |
| 111 | if (typeof urlOrChannel === "string") { |
| 112 | this._url = urlOrChannel; |
| 113 | } else { |
| 114 | this._url = null; |
| 115 | this._rawChannel = urlOrChannel; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | options = options || {}; |
| 120 | this._rfbCredentials = options.credentials || {}; |
| 121 | this._shared = 'shared' in options ? !!options.shared : true; |
| 122 | this._repeaterID = options.repeaterID || ''; |
| 123 | this._wsProtocols = options.wsProtocols || []; |
| 124 | |
| 125 | |
| 126 | this._rfbConnectionState = ''; |
| 127 | this._rfbInitState = ''; |
| 128 | this._rfbAuthScheme = -1; |
| 129 | this._rfbCleanDisconnect = true; |
| 130 | this._rfbRSAAESAuthenticationState = null; |
| 131 | |
| 132 | |
| 133 | this._rfbVersion = 0; |
| 134 | this._rfbMaxVersion = 3.8; |
| 135 | this._rfbTightVNC = false; |
| 136 | this._rfbVeNCryptState = 0; |
| 137 | this._rfbXvpVer = 0; |
| 138 | |
| 139 | this._fbWidth = 0; |
| 140 | this._fbHeight = 0; |
| 141 | |
| 142 | this._fbName = ""; |
| 143 | |
| 144 | this._capabilities = { power: false }; |
| 145 | |
| 146 | this._supportsFence = false; |
| 147 | |
| 148 | this._supportsContinuousUpdates = false; |
| 149 | this._enabledContinuousUpdates = false; |
| 150 | |
| 151 | this._supportsSetDesktopSize = false; |
| 152 | this._screenID = 0; |
| 153 | this._screenFlags = 0; |
| 154 | this._pendingRemoteResize = false; |
| 155 | this._lastResize = 0; |
| 156 | |
| 157 | this._qemuExtKeyEventSupported = false; |
| 158 | |
| 159 | this._extendedPointerEventSupported = false; |
| 160 | |
| 161 | this._clipboardText = null; |
| 162 | this._clipboardServerCapabilitiesActions = {}; |
| 163 | this._clipboardServerCapabilitiesFormats = {}; |
| 164 | |
| 165 | |
| 166 | this._sock = null; |
| 167 | this._display = null; |
| 168 | this._flushing = false; |
| 169 | this._keyboard = null; |
| 170 | this._gestures = null; |
| 171 | this._resizeObserver = null; |
| 172 | |
| 173 | |
| 174 | this._disconnTimer = null; |
| 175 | this._resizeTimeout = null; |
| 176 | this._mouseMoveTimer = null; |
| 177 | |
| 178 | |
| 179 | this._decoders = {}; |
| 180 | |
| 181 | this._FBU = { |
| 182 | rects: 0, |
| 183 | x: 0, |
| 184 | y: 0, |
| 185 | width: 0, |
| 186 | height: 0, |
| 187 | encoding: null, |
| 188 | }; |
| 189 | |
| 190 | |
| 191 | this._mousePos = {}; |
| 192 | this._mouseButtonMask = 0; |
| 193 | this._mouseLastMoveTime = 0; |
| 194 | this._viewportDragging = false; |
| 195 | this._viewportDragPos = {}; |
| 196 | this._viewportHasMoved = false; |
| 197 | this._accumulatedWheelDeltaX = 0; |
| 198 | this._accumulatedWheelDeltaY = 0; |
| 199 | |
| 200 | this._cageBrowserDisplay = !!options.cageBrowserDisplay; |
| 201 | this._cagePixelWheel = false; |
| 202 | this._cageWheel = new CageWheel((x, y, dx, dy) => this._cageSendWheel(x, y, dx, dy)); |
| 203 | |
| 204 | |
| 205 | this._gestureLastTapTime = null; |
| 206 | this._gestureFirstDoubleTapEv = null; |
| 207 | this._gestureLastMagnitudeX = 0; |
| 208 | this._gestureLastMagnitudeY = 0; |
| 209 | |
| 210 | |
| 211 | this._eventHandlers = { |
| 212 | focusCanvas: this._focusCanvas.bind(this), |
| 213 | handleResize: this._handleResize.bind(this), |
| 214 | handleMouse: this._handleMouse.bind(this), |
| 215 | handleWheel: this._handleWheel.bind(this), |
| 216 | handleGesture: this._handleGesture.bind(this), |
| 217 | handleRSAAESCredentialsRequired: this._handleRSAAESCredentialsRequired.bind(this), |
| 218 | handleRSAAESServerVerification: this._handleRSAAESServerVerification.bind(this), |
| 219 | }; |
| 220 | |
| 221 | |
| 222 | Log.Debug(">> RFB.constructor"); |
| 223 | |
| 224 | |
| 225 | this._screen = document.createElement('div'); |
| 226 | this._screen.style.display = 'flex'; |
| 227 | this._screen.style.width = '100%'; |
| 228 | this._screen.style.height = '100%'; |
| 229 | this._screen.style.overflow = 'auto'; |
| 230 | this._screen.style.background = DEFAULT_BACKGROUND; |
| 231 | this._canvas = document.createElement('canvas'); |
| 232 | this._canvas.style.margin = 'auto'; |
| 233 | |
| 234 | this._canvas.style.outline = 'none'; |
| 235 | this._canvas.width = 0; |
| 236 | this._canvas.height = 0; |
| 237 | this._canvas.tabIndex = -1; |
| 238 | this._screen.appendChild(this._canvas); |
| 239 | |
| 240 | |
| 241 | this._cursor = new Cursor(options.cageBrowserDisplay || options.cageCursorFallback ? 'default' : 'none'); |
| 242 | |
| 243 | XXX: |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | this._cursorImage = RFB.cursors.none; |
| 253 | |
| 254 | |
| 255 | this._decoders[encodings.encodingRaw] = new RawDecoder(); |
| 256 | this._decoders[encodings.encodingCopyRect] = new CopyRectDecoder(); |
| 257 | this._decoders[encodings.encodingRRE] = new RREDecoder(); |
| 258 | this._decoders[encodings.encodingHextile] = new HextileDecoder(); |
| 259 | this._decoders[encodings.encodingZlib] = new ZlibDecoder(); |
| 260 | this._decoders[encodings.encodingTight] = new TightDecoder(); |
| 261 | this._decoders[encodings.encodingTightPNG] = new TightPNGDecoder(); |
| 262 | this._decoders[encodings.encodingZRLE] = new ZRLEDecoder(); |
| 263 | this._decoders[encodings.encodingJPEG] = new JPEGDecoder(); |
| 264 | this._decoders[encodings.encodingH264] = new H264Decoder(); |
| 265 | |
| 266 | |
| 267 | |
| 268 | try { |
| 269 | const DisplayClass = options.cageBrowserDisplay ? CageBrowserDisplay : Display; |
| 270 | this._display = new DisplayClass(this._canvas); |
| 271 | } catch (exc) { |
| 272 | Log.Error("Display exception: " + exc); |
| 273 | throw exc; |
| 274 | } |
| 275 | |
| 276 | this._keyboard = new Keyboard(this._canvas); |
| 277 | this._keyboard.onkeyevent = this._handleKeyEvent.bind(this); |
| 278 | this._remoteCapsLock = null; |
| 279 | this._remoteNumLock = null; |
| 280 | |
| 281 | this._gestures = new GestureHandler(); |
| 282 | |
| 283 | this._sock = new Websock(); |
| 284 | this._sock.on('open', this._socketOpen.bind(this)); |
| 285 | this._sock.on('close', this._socketClose.bind(this)); |
| 286 | this._sock.on('message', this._handleMessage.bind(this)); |
| 287 | this._sock.on('error', this._socketError.bind(this)); |
| 288 | |
| 289 | this._expectedClientWidth = null; |
| 290 | this._expectedClientHeight = null; |
| 291 | this._resizeObserver = new ResizeObserver(this._eventHandlers.handleResize); |
| 292 | |
| 293 | |
| 294 | this._updateConnectionState('connecting'); |
| 295 | |
| 296 | Log.Debug("<< RFB.constructor"); |
| 297 | |
| 298 | |
| 299 | |
| 300 | this.dragViewport = false; |
| 301 | this.focusOnClick = true; |
| 302 | |
| 303 | this._viewOnly = false; |
| 304 | this._clipViewport = false; |
| 305 | this._clippingViewport = false; |
| 306 | this._scaleViewport = false; |
| 307 | this._resizeSession = false; |
| 308 | |
| 309 | this._showDotCursor = false; |
| 310 | if (options.showDotCursor !== undefined) { |
| 311 | Log.Warn("Specifying showDotCursor as a RFB constructor argument is deprecated"); |
| 312 | this._showDotCursor = options.showDotCursor; |
| 313 | } |
| 314 | |
| 315 | this._qualityLevel = 6; |
| 316 | this._compressionLevel = 2; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | |
| 321 | get viewOnly() { return this._viewOnly; } |
| 322 | set viewOnly(viewOnly) { |
| 323 | this._viewOnly = viewOnly; |
| 324 | |
| 325 | if (this._rfbConnectionState === "connecting" || |
| 326 | this._rfbConnectionState === "connected") { |
| 327 | if (viewOnly) { |
| 328 | this._keyboard.ungrab(); |
| 329 | } else { |
| 330 | this._keyboard.grab(); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | get capabilities() { return this._capabilities; } |
| 336 | |
| 337 | get clippingViewport() { return this._clippingViewport; } |
| 338 | _setClippingViewport(on) { |
| 339 | if (on === this._clippingViewport) { |
| 340 | return; |
| 341 | } |
| 342 | this._clippingViewport = on; |
| 343 | this.dispatchEvent(new CustomEvent("clippingviewport", |
| 344 | { detail: this._clippingViewport })); |
| 345 | } |
| 346 | |
| 347 | get touchButton() { return 0; } |
| 348 | set touchButton(button) { Log.Warn("Using old API!"); } |
| 349 | |
| 350 | get clipViewport() { return this._clipViewport; } |
| 351 | set clipViewport(viewport) { |
| 352 | this._clipViewport = viewport; |
| 353 | this._updateClip(); |
| 354 | } |
| 355 | |
| 356 | get scaleViewport() { return this._scaleViewport; } |
| 357 | set scaleViewport(scale) { |
| 358 | this._scaleViewport = scale; |
| 359 | |
| 360 | |
| 361 | if (scale && this._clipViewport) { |
| 362 | this._updateClip(); |
| 363 | } |
| 364 | this._updateScale(); |
| 365 | if (!scale && this._clipViewport) { |
| 366 | this._updateClip(); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | get resizeSession() { return this._resizeSession; } |
| 371 | set resizeSession(resize) { |
| 372 | this._resizeSession = resize; |
| 373 | if (resize) { |
| 374 | this._requestRemoteResize(); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | get showDotCursor() { return this._showDotCursor; } |
| 379 | set showDotCursor(show) { |
| 380 | this._showDotCursor = show; |
| 381 | this._refreshCursor(); |
| 382 | } |
| 383 | |
| 384 | get background() { return this._screen.style.background; } |
| 385 | set background(cssValue) { this._screen.style.background = cssValue; } |
| 386 | |
| 387 | get qualityLevel() { |
| 388 | return this._qualityLevel; |
| 389 | } |
| 390 | set qualityLevel(qualityLevel) { |
| 391 | if (!Number.isInteger(qualityLevel) || qualityLevel < 0 || qualityLevel > 9) { |
| 392 | Log.Error("qualityLevel must be an integer between 0 and 9"); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | if (this._qualityLevel === qualityLevel) { |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | this._qualityLevel = qualityLevel; |
| 401 | |
| 402 | if (this._rfbConnectionState === 'connected') { |
| 403 | this._sendEncodings(); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | get compressionLevel() { |
| 408 | return this._compressionLevel; |
| 409 | } |
| 410 | set compressionLevel(compressionLevel) { |
| 411 | if (!Number.isInteger(compressionLevel) || compressionLevel < 0 || compressionLevel > 9) { |
| 412 | Log.Error("compressionLevel must be an integer between 0 and 9"); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | if (this._compressionLevel === compressionLevel) { |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | this._compressionLevel = compressionLevel; |
| 421 | |
| 422 | if (this._rfbConnectionState === 'connected') { |
| 423 | this._sendEncodings(); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | |
| 428 | |
| 429 | disconnect() { |
| 430 | this._updateConnectionState('disconnecting'); |
| 431 | this._sock.off('error'); |
| 432 | this._sock.off('message'); |
| 433 | this._sock.off('open'); |
| 434 | if (this._rfbRSAAESAuthenticationState !== null) { |
| 435 | this._rfbRSAAESAuthenticationState.disconnect(); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | approveServer() { |
| 440 | if (this._rfbRSAAESAuthenticationState !== null) { |
| 441 | this._rfbRSAAESAuthenticationState.approveServer(); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | sendCredentials(creds) { |
| 446 | this._rfbCredentials = creds; |
| 447 | this._resumeAuthentication(); |
| 448 | } |
| 449 | |
| 450 | sendCtrlAltDel() { |
| 451 | if (this._rfbConnectionState !== 'connected' || this._viewOnly) { return; } |
| 452 | Log.Info("Sending Ctrl-Alt-Del"); |
| 453 | |
| 454 | this.sendKey(KeyTable.XK_Control_L, "ControlLeft", true); |
| 455 | this.sendKey(KeyTable.XK_Alt_L, "AltLeft", true); |
| 456 | this.sendKey(KeyTable.XK_Delete, "Delete", true); |
| 457 | this.sendKey(KeyTable.XK_Delete, "Delete", false); |
| 458 | this.sendKey(KeyTable.XK_Alt_L, "AltLeft", false); |
| 459 | this.sendKey(KeyTable.XK_Control_L, "ControlLeft", false); |
| 460 | } |
| 461 | |
| 462 | machineShutdown() { |
| 463 | this._xvpOp(1, 2); |
| 464 | } |
| 465 | |
| 466 | machineReboot() { |
| 467 | this._xvpOp(1, 3); |
| 468 | } |
| 469 | |
| 470 | machineReset() { |
| 471 | this._xvpOp(1, 4); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | |
| 476 | sendKey(keysym, code, down) { |
| 477 | if (this._rfbConnectionState !== 'connected' || this._viewOnly) { return; } |
| 478 | |
| 479 | if (down === undefined) { |
| 480 | this.sendKey(keysym, code, true); |
| 481 | this.sendKey(keysym, code, false); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | const scancode = XtScancode[code]; |
| 486 | |
| 487 | if (this._qemuExtKeyEventSupported && scancode) { |
| 488 | |
| 489 | keysym = keysym || 0; |
| 490 | |
| 491 | Log.Info("Sending key (" + (down ? "down" : "up") + "): keysym " + keysym + ", scancode " + scancode); |
| 492 | |
| 493 | RFB.messages.QEMUExtendedKeyEvent(this._sock, keysym, down, scancode); |
| 494 | } else { |
| 495 | if (!keysym) { |
| 496 | return; |
| 497 | } |
| 498 | Log.Info("Sending keysym (" + (down ? "down" : "up") + "): " + keysym); |
| 499 | RFB.messages.keyEvent(this._sock, keysym, down ? 1 : 0); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | focus(options) { |
| 504 | this._canvas.focus(options); |
| 505 | } |
| 506 | |
| 507 | blur() { |
| 508 | this._canvas.blur(); |
| 509 | } |
| 510 | |
| 511 | clipboardPasteFrom(text) { |
| 512 | if (this._rfbConnectionState !== 'connected' || this._viewOnly) { return; } |
| 513 | |
| 514 | if (this._clipboardServerCapabilitiesFormats[extendedClipboardFormatText] && |
| 515 | this._clipboardServerCapabilitiesActions[extendedClipboardActionNotify]) { |
| 516 | |
| 517 | this._clipboardText = text; |
| 518 | RFB.messages.extendedClipboardNotify(this._sock, [extendedClipboardFormatText]); |
| 519 | } else { |
| 520 | let length, i; |
| 521 | let data; |
| 522 | |
| 523 | length = 0; |
| 524 | |
| 525 | for (let codePoint of text) { |
| 526 | length++; |
| 527 | } |
| 528 | |
| 529 | data = new Uint8Array(length); |
| 530 | |
| 531 | i = 0; |
| 532 | for (let codePoint of text) { |
| 533 | let code = codePoint.codePointAt(0); |
| 534 | |
| 535 | |
| 536 | if (code > 0xff) { |
| 537 | code = 0x3f; |
| 538 | } |
| 539 | |
| 540 | data[i++] = code; |
| 541 | } |
| 542 | |
| 543 | RFB.messages.clientCutText(this._sock, data); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | getImageData() { |
| 548 | return this._display.getImageData(); |
| 549 | } |
| 550 | |
| 551 | toDataURL(type, encoderOptions) { |
| 552 | return this._display.toDataURL(type, encoderOptions); |
| 553 | } |
| 554 | |
| 555 | toBlob(callback, type, quality) { |
| 556 | return this._display.toBlob(callback, type, quality); |
| 557 | } |
| 558 | |
| 559 | |
| 560 | |
| 561 | _connect() { |
| 562 | Log.Debug(">> RFB.connect"); |
| 563 | |
| 564 | if (this._url) { |
| 565 | Log.Info(`connecting to ${this._url}`); |
| 566 | this._sock.open(this._url, this._wsProtocols); |
| 567 | } else { |
| 568 | Log.Info(`attaching ${this._rawChannel} to Websock`); |
| 569 | this._sock.attach(this._rawChannel); |
| 570 | |
| 571 | if (this._sock.readyState === 'closed') { |
| 572 | throw Error("Cannot use already closed WebSocket/RTCDataChannel"); |
| 573 | } |
| 574 | |
| 575 | if (this._sock.readyState === 'open') { |
| 576 | FIXME: |
| 577 | |
| 578 | |
| 579 | this._socketOpen(); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | |
| 584 | this._target.appendChild(this._screen); |
| 585 | |
| 586 | this._gestures.attach(this._canvas); |
| 587 | |
| 588 | this._cursor.attach(this._canvas); |
| 589 | this._refreshCursor(); |
| 590 | |
| 591 | |
| 592 | this._resizeObserver.observe(this._screen); |
| 593 | |
| 594 | |
| 595 | this._canvas.addEventListener("mousedown", this._eventHandlers.focusCanvas); |
| 596 | this._canvas.addEventListener("touchstart", this._eventHandlers.focusCanvas); |
| 597 | |
| 598 | |
| 599 | this._canvas.addEventListener('mousedown', this._eventHandlers.handleMouse); |
| 600 | this._canvas.addEventListener('mouseup', this._eventHandlers.handleMouse); |
| 601 | this._canvas.addEventListener('mousemove', this._eventHandlers.handleMouse); |
| 602 | |
| 603 | this._canvas.addEventListener('click', this._eventHandlers.handleMouse); |
| 604 | |
| 605 | |
| 606 | this._canvas.addEventListener('contextmenu', this._eventHandlers.handleMouse); |
| 607 | |
| 608 | |
| 609 | this._canvas.addEventListener("wheel", this._eventHandlers.handleWheel); |
| 610 | |
| 611 | |
| 612 | this._canvas.addEventListener("gesturestart", this._eventHandlers.handleGesture); |
| 613 | this._canvas.addEventListener("gesturemove", this._eventHandlers.handleGesture); |
| 614 | this._canvas.addEventListener("gestureend", this._eventHandlers.handleGesture); |
| 615 | |
| 616 | Log.Debug("<< RFB.connect"); |
| 617 | } |
| 618 | |
| 619 | _disconnect() { |
| 620 | Log.Debug(">> RFB.disconnect"); |
| 621 | this._cursor.detach(); |
| 622 | this._canvas.removeEventListener("gesturestart", this._eventHandlers.handleGesture); |
| 623 | this._canvas.removeEventListener("gesturemove", this._eventHandlers.handleGesture); |
| 624 | this._canvas.removeEventListener("gestureend", this._eventHandlers.handleGesture); |
| 625 | this._canvas.removeEventListener("wheel", this._eventHandlers.handleWheel); |
| 626 | this._canvas.removeEventListener('mousedown', this._eventHandlers.handleMouse); |
| 627 | this._canvas.removeEventListener('mouseup', this._eventHandlers.handleMouse); |
| 628 | this._canvas.removeEventListener('mousemove', this._eventHandlers.handleMouse); |
| 629 | this._canvas.removeEventListener('click', this._eventHandlers.handleMouse); |
| 630 | this._canvas.removeEventListener('contextmenu', this._eventHandlers.handleMouse); |
| 631 | this._canvas.removeEventListener("mousedown", this._eventHandlers.focusCanvas); |
| 632 | this._canvas.removeEventListener("touchstart", this._eventHandlers.focusCanvas); |
| 633 | this._resizeObserver.disconnect(); |
| 634 | this._keyboard.ungrab(); |
| 635 | this._gestures.detach(); |
| 636 | this._sock.close(); |
| 637 | try { |
| 638 | this._target.removeChild(this._screen); |
| 639 | } catch (e) { |
| 640 | if (e.name === 'NotFoundError') { |
| 641 | |
| 642 | |
| 643 | } else { |
| 644 | throw e; |
| 645 | } |
| 646 | } |
| 647 | clearTimeout(this._resizeTimeout); |
| 648 | clearTimeout(this._mouseMoveTimer); |
| 649 | this._cageWheel.stop(); |
| 650 | Log.Debug("<< RFB.disconnect"); |
| 651 | } |
| 652 | |
| 653 | _socketOpen() { |
| 654 | if ((this._rfbConnectionState === 'connecting') && |
| 655 | (this._rfbInitState === '')) { |
| 656 | this._rfbInitState = 'ProtocolVersion'; |
| 657 | Log.Debug("Starting VNC handshake"); |
| 658 | } else { |
| 659 | this._fail("Unexpected server connection while " + |
| 660 | this._rfbConnectionState); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | _socketClose(e) { |
| 665 | Log.Debug("WebSocket on-close event"); |
| 666 | let msg = ""; |
| 667 | if (e.code) { |
| 668 | msg = "(code: " + e.code; |
| 669 | if (e.reason) { |
| 670 | msg += ", reason: " + e.reason; |
| 671 | } |
| 672 | msg += ")"; |
| 673 | } |
| 674 | switch (this._rfbConnectionState) { |
| 675 | case 'connecting': |
| 676 | this._fail("Connection closed " + msg); |
| 677 | break; |
| 678 | case 'connected': |
| 679 | |
| 680 | this._updateConnectionState('disconnecting'); |
| 681 | this._updateConnectionState('disconnected'); |
| 682 | break; |
| 683 | case 'disconnecting': |
| 684 | |
| 685 | this._updateConnectionState('disconnected'); |
| 686 | break; |
| 687 | case 'disconnected': |
| 688 | this._fail("Unexpected server disconnect " + |
| 689 | "when already disconnected " + msg); |
| 690 | break; |
| 691 | default: |
| 692 | this._fail("Unexpected server disconnect before connecting " + |
| 693 | msg); |
| 694 | break; |
| 695 | } |
| 696 | this._sock.off('close'); |
| 697 | |
| 698 | this._rawChannel = null; |
| 699 | } |
| 700 | |
| 701 | _socketError(e) { |
| 702 | Log.Warn("WebSocket on-error event"); |
| 703 | } |
| 704 | |
| 705 | _focusCanvas(event) { |
| 706 | if (!this.focusOnClick) { |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | this.focus({ preventScroll: true }); |
| 711 | } |
| 712 | |
| 713 | _setDesktopName(name) { |
| 714 | this._fbName = name; |
| 715 | this.dispatchEvent(new CustomEvent( |
| 716 | "desktopname", |
| 717 | { detail: { name: this._fbName } })); |
| 718 | } |
| 719 | |
| 720 | _saveExpectedClientSize() { |
| 721 | this._expectedClientWidth = this._screen.clientWidth; |
| 722 | this._expectedClientHeight = this._screen.clientHeight; |
| 723 | } |
| 724 | |
| 725 | _currentClientSize() { |
| 726 | return [this._screen.clientWidth, this._screen.clientHeight]; |
| 727 | } |
| 728 | |
| 729 | _clientHasExpectedSize() { |
| 730 | const [currentWidth, currentHeight] = this._currentClientSize(); |
| 731 | return currentWidth == this._expectedClientWidth && |
| 732 | currentHeight == this._expectedClientHeight; |
| 733 | } |
| 734 | |
| 735 | |
| 736 | _handleResize() { |
| 737 | |
| 738 | if (this._clientHasExpectedSize()) { |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | |
| 743 | window.requestAnimationFrame(() => { |
| 744 | this._updateClip(); |
| 745 | this._updateScale(); |
| 746 | this._saveExpectedClientSize(); |
| 747 | }); |
| 748 | |
| 749 | |
| 750 | |
| 751 | this._requestRemoteResize(); |
| 752 | } |
| 753 | |
| 754 | |
| 755 | |
| 756 | _updateClip() { |
| 757 | const curClip = this._display.clipViewport; |
| 758 | let newClip = this._clipViewport; |
| 759 | |
| 760 | if (this._scaleViewport) { |
| 761 | |
| 762 | newClip = false; |
| 763 | } |
| 764 | |
| 765 | if (curClip !== newClip) { |
| 766 | this._display.clipViewport = newClip; |
| 767 | } |
| 768 | |
| 769 | if (newClip) { |
| 770 | |
| 771 | |
| 772 | const size = this._screenSize(); |
| 773 | this._display.viewportChangeSize(size.w, size.h); |
| 774 | this._fixScrollbars(); |
| 775 | this._setClippingViewport(size.w < this._display.width || |
| 776 | size.h < this._display.height); |
| 777 | } else { |
| 778 | this._setClippingViewport(false); |
| 779 | } |
| 780 | |
| 781 | |
| 782 | |
| 783 | if (curClip !== newClip) { |
| 784 | this._saveExpectedClientSize(); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | _updateScale() { |
| 789 | if (!this._scaleViewport) { |
| 790 | this._display.scale = 1.0; |
| 791 | } else { |
| 792 | const size = this._screenSize(); |
| 793 | this._display.autoscale(size.w, size.h); |
| 794 | } |
| 795 | this._fixScrollbars(); |
| 796 | } |
| 797 | |
| 798 | |
| 799 | |
| 800 | _requestRemoteResize() { |
| 801 | if (!this._resizeSession) { |
| 802 | return; |
| 803 | } |
| 804 | if (this._viewOnly) { |
| 805 | return; |
| 806 | } |
| 807 | if (!this._supportsSetDesktopSize) { |
| 808 | return; |
| 809 | } |
| 810 | |
| 811 | |
| 812 | if (this._pendingRemoteResize) { |
| 813 | return; |
| 814 | } |
| 815 | |
| 816 | |
| 817 | if ((Date.now() - this._lastResize) < 100) { |
| 818 | clearTimeout(this._resizeTimeout); |
| 819 | this._resizeTimeout = setTimeout(this._requestRemoteResize.bind(this), |
| 820 | 100 - (Date.now() - this._lastResize)); |
| 821 | return; |
| 822 | } |
| 823 | this._resizeTimeout = null; |
| 824 | |
| 825 | const size = this._screenSize(); |
| 826 | |
| 827 | |
| 828 | if (size.w === this._fbWidth && size.h === this._fbHeight) { |
| 829 | return; |
| 830 | } |
| 831 | |
| 832 | this._pendingRemoteResize = true; |
| 833 | this._lastResize = Date.now(); |
| 834 | RFB.messages.setDesktopSize(this._sock, |
| 835 | Math.floor(size.w), Math.floor(size.h), |
| 836 | this._screenID, this._screenFlags); |
| 837 | |
| 838 | Log.Debug('Requested new desktop size: ' + |
| 839 | size.w + 'x' + size.h); |
| 840 | } |
| 841 | |
| 842 | |
| 843 | _screenSize() { |
| 844 | let r = this._screen.getBoundingClientRect(); |
| 845 | return { w: r.width, h: r.height }; |
| 846 | } |
| 847 | |
| 848 | _fixScrollbars() { |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | const orig = this._screen.style.overflow; |
| 854 | this._screen.style.overflow = 'hidden'; |
| 855 | |
| 856 | |
| 857 | this._screen.getBoundingClientRect(); |
| 858 | this._screen.style.overflow = orig; |
| 859 | } |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | _updateConnectionState(state) { |
| 869 | const oldstate = this._rfbConnectionState; |
| 870 | |
| 871 | if (state === oldstate) { |
| 872 | Log.Debug("Already in state '" + state + "', ignoring"); |
| 873 | return; |
| 874 | } |
| 875 | |
| 876 | |
| 877 | if (oldstate === 'disconnected') { |
| 878 | Log.Error("Tried changing state of a disconnected RFB object"); |
| 879 | return; |
| 880 | } |
| 881 | |
| 882 | |
| 883 | switch (state) { |
| 884 | case 'connected': |
| 885 | if (oldstate !== 'connecting') { |
| 886 | Log.Error("Bad transition to connected state, " + |
| 887 | "previous connection state: " + oldstate); |
| 888 | return; |
| 889 | } |
| 890 | break; |
| 891 | |
| 892 | case 'disconnected': |
| 893 | if (oldstate !== 'disconnecting') { |
| 894 | Log.Error("Bad transition to disconnected state, " + |
| 895 | "previous connection state: " + oldstate); |
| 896 | return; |
| 897 | } |
| 898 | break; |
| 899 | |
| 900 | case 'connecting': |
| 901 | if (oldstate !== '') { |
| 902 | Log.Error("Bad transition to connecting state, " + |
| 903 | "previous connection state: " + oldstate); |
| 904 | return; |
| 905 | } |
| 906 | break; |
| 907 | |
| 908 | case 'disconnecting': |
| 909 | if (oldstate !== 'connected' && oldstate !== 'connecting') { |
| 910 | Log.Error("Bad transition to disconnecting state, " + |
| 911 | "previous connection state: " + oldstate); |
| 912 | return; |
| 913 | } |
| 914 | break; |
| 915 | |
| 916 | default: |
| 917 | Log.Error("Unknown connection state: " + state); |
| 918 | return; |
| 919 | } |
| 920 | |
| 921 | |
| 922 | |
| 923 | this._rfbConnectionState = state; |
| 924 | |
| 925 | Log.Debug("New state '" + state + "', was '" + oldstate + "'."); |
| 926 | |
| 927 | if (this._disconnTimer && state !== 'disconnecting') { |
| 928 | Log.Debug("Clearing disconnect timer"); |
| 929 | clearTimeout(this._disconnTimer); |
| 930 | this._disconnTimer = null; |
| 931 | |
| 932 | |
| 933 | this._sock.off('close'); |
| 934 | } |
| 935 | |
| 936 | switch (state) { |
| 937 | case 'connecting': |
| 938 | this._connect(); |
| 939 | break; |
| 940 | |
| 941 | case 'connected': |
| 942 | this.dispatchEvent(new CustomEvent("connect", { detail: {} })); |
| 943 | break; |
| 944 | |
| 945 | case 'disconnecting': |
| 946 | this._disconnect(); |
| 947 | |
| 948 | this._disconnTimer = setTimeout(() => { |
| 949 | Log.Error("Disconnection timed out."); |
| 950 | this._updateConnectionState('disconnected'); |
| 951 | }, DISCONNECT_TIMEOUT * 1000); |
| 952 | break; |
| 953 | |
| 954 | case 'disconnected': |
| 955 | this.dispatchEvent(new CustomEvent( |
| 956 | "disconnect", { detail: |
| 957 | { clean: this._rfbCleanDisconnect } })); |
| 958 | break; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | _fail(details) { |
| 968 | switch (this._rfbConnectionState) { |
| 969 | case 'disconnecting': |
| 970 | Log.Error("Failed when disconnecting: " + details); |
| 971 | break; |
| 972 | case 'connected': |
| 973 | Log.Error("Failed while connected: " + details); |
| 974 | break; |
| 975 | case 'connecting': |
| 976 | Log.Error("Failed when connecting: " + details); |
| 977 | break; |
| 978 | default: |
| 979 | Log.Error("RFB failure: " + details); |
| 980 | break; |
| 981 | } |
| 982 | this._rfbCleanDisconnect = false; |
| 983 | |
| 984 | |
| 985 | this._updateConnectionState('disconnecting'); |
| 986 | this._updateConnectionState('disconnected'); |
| 987 | |
| 988 | return false; |
| 989 | } |
| 990 | |
| 991 | _setCapability(cap, val) { |
| 992 | this._capabilities[cap] = val; |
| 993 | this.dispatchEvent(new CustomEvent("capabilities", |
| 994 | { detail: { capabilities: this._capabilities } })); |
| 995 | } |
| 996 | |
| 997 | _handleMessage() { |
| 998 | if (this._sock.rQwait("message", 1)) { |
| 999 | Log.Warn("handleMessage called on an empty receive queue"); |
| 1000 | return; |
| 1001 | } |
| 1002 | |
| 1003 | switch (this._rfbConnectionState) { |
| 1004 | case 'disconnected': |
| 1005 | Log.Error("Got data while disconnected"); |
| 1006 | break; |
| 1007 | case 'connected': |
| 1008 | while (true) { |
| 1009 | if (this._flushing) { |
| 1010 | break; |
| 1011 | } |
| 1012 | if (!this._normalMsg()) { |
| 1013 | break; |
| 1014 | } |
| 1015 | if (this._sock.rQwait("message", 1)) { |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
| 1019 | break; |
| 1020 | case 'connecting': |
| 1021 | while (this._rfbConnectionState === 'connecting') { |
| 1022 | if (!this._initMsg()) { |
| 1023 | break; |
| 1024 | } |
| 1025 | } |
| 1026 | break; |
| 1027 | default: |
| 1028 | Log.Error("Got data while in an invalid state"); |
| 1029 | break; |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | _handleKeyEvent(keysym, code, down, numlock, capslock) { |
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | if (code == 'CapsLock' && down) { |
| 1040 | this._remoteCapsLock = null; |
| 1041 | } |
| 1042 | if (this._remoteCapsLock !== null && capslock !== null && this._remoteCapsLock !== capslock && down) { |
| 1043 | Log.Debug("Fixing remote caps lock"); |
| 1044 | |
| 1045 | this.sendKey(KeyTable.XK_Caps_Lock, 'CapsLock', true); |
| 1046 | this.sendKey(KeyTable.XK_Caps_Lock, 'CapsLock', false); |
| 1047 | |
| 1048 | |
| 1049 | this._remoteCapsLock = null; |
| 1050 | } |
| 1051 | |
| 1052 | |
| 1053 | if (code == 'NumLock' && down) { |
| 1054 | this._remoteNumLock = null; |
| 1055 | } |
| 1056 | if (this._remoteNumLock !== null && numlock !== null && this._remoteNumLock !== numlock && down) { |
| 1057 | Log.Debug("Fixing remote num lock"); |
| 1058 | this.sendKey(KeyTable.XK_Num_Lock, 'NumLock', true); |
| 1059 | this.sendKey(KeyTable.XK_Num_Lock, 'NumLock', false); |
| 1060 | this._remoteNumLock = null; |
| 1061 | } |
| 1062 | this.sendKey(keysym, code, down); |
| 1063 | } |
| 1064 | |
| 1065 | static _convertButtonMask(buttons) { |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | const buttonMaskMap = { |
| 1079 | 0: 1 << 0, |
| 1080 | 1: 1 << 2, |
| 1081 | 2: 1 << 1, |
| 1082 | 3: 1 << 7, |
| 1083 | 4: 1 << 8, |
| 1084 | }; |
| 1085 | |
| 1086 | let bmask = 0; |
| 1087 | for (let i = 0; i < 5; i++) { |
| 1088 | if (buttons & (1 << i)) { |
| 1089 | bmask |= buttonMaskMap[i]; |
| 1090 | } |
| 1091 | } |
| 1092 | return bmask; |
| 1093 | } |
| 1094 | |
| 1095 | _handleMouse(ev) { |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | if (ev.type === 'click') { |
| 1102 | |
| 1103 | |
| 1104 | |
| 1105 | |
| 1106 | |
| 1107 | if (ev.target !== this._canvas) { |
| 1108 | return; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | FIXME: |
| 1113 | |
| 1114 | ev.stopPropagation(); |
| 1115 | ev.preventDefault(); |
| 1116 | |
| 1117 | if ((ev.type === 'click') || (ev.type === 'contextmenu')) { |
| 1118 | return; |
| 1119 | } |
| 1120 | |
| 1121 | let pos = clientToElement(ev.clientX, ev.clientY, |
| 1122 | this._canvas); |
| 1123 | |
| 1124 | let bmask = RFB._convertButtonMask(ev.buttons); |
| 1125 | |
| 1126 | let down = ev.type == 'mousedown'; |
| 1127 | switch (ev.type) { |
| 1128 | case 'mousedown': |
| 1129 | case 'mouseup': |
| 1130 | if (this.dragViewport) { |
| 1131 | if (down && !this._viewportDragging) { |
| 1132 | this._viewportDragging = true; |
| 1133 | this._viewportDragPos = {'x': pos.x, 'y': pos.y}; |
| 1134 | this._viewportHasMoved = false; |
| 1135 | |
| 1136 | this._flushMouseMoveTimer(pos.x, pos.y); |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | this._mouseButtonMask = bmask; |
| 1141 | break; |
| 1142 | } else { |
| 1143 | this._viewportDragging = false; |
| 1144 | |
| 1145 | |
| 1146 | |
| 1147 | if (this._viewportHasMoved) { |
| 1148 | this._mouseButtonMask = bmask; |
| 1149 | break; |
| 1150 | } |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | |
| 1155 | this._sendMouse(pos.x, pos.y, this._mouseButtonMask); |
| 1156 | } |
| 1157 | } |
| 1158 | if (down) { |
| 1159 | setCapture(this._canvas); |
| 1160 | } |
| 1161 | this._handleMouseButton(pos.x, pos.y, bmask); |
| 1162 | break; |
| 1163 | case 'mousemove': |
| 1164 | if (this._viewportDragging) { |
| 1165 | const deltaX = this._viewportDragPos.x - pos.x; |
| 1166 | const deltaY = this._viewportDragPos.y - pos.y; |
| 1167 | |
| 1168 | if (this._viewportHasMoved || (Math.abs(deltaX) > dragThreshold || |
| 1169 | Math.abs(deltaY) > dragThreshold)) { |
| 1170 | this._viewportHasMoved = true; |
| 1171 | |
| 1172 | this._viewportDragPos = {'x': pos.x, 'y': pos.y}; |
| 1173 | this._display.viewportChangePos(deltaX, deltaY); |
| 1174 | } |
| 1175 | |
| 1176 | |
| 1177 | break; |
| 1178 | } |
| 1179 | this._handleMouseMove(pos.x, pos.y); |
| 1180 | break; |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | _handleMouseButton(x, y, bmask) { |
| 1185 | |
| 1186 | this._flushMouseMoveTimer(x, y); |
| 1187 | |
| 1188 | this._mouseButtonMask = bmask; |
| 1189 | this._sendMouse(x, y, this._mouseButtonMask); |
| 1190 | } |
| 1191 | |
| 1192 | _handleMouseMove(x, y) { |
| 1193 | this._mousePos = { 'x': x, 'y': y }; |
| 1194 | |
| 1195 | |
| 1196 | if (this._mouseMoveTimer == null) { |
| 1197 | |
| 1198 | const timeSinceLastMove = Date.now() - this._mouseLastMoveTime; |
| 1199 | if (timeSinceLastMove > MOUSE_MOVE_DELAY) { |
| 1200 | this._sendMouse(x, y, this._mouseButtonMask); |
| 1201 | this._mouseLastMoveTime = Date.now(); |
| 1202 | } else { |
| 1203 | |
| 1204 | this._mouseMoveTimer = setTimeout(() => { |
| 1205 | this._handleDelayedMouseMove(); |
| 1206 | }, MOUSE_MOVE_DELAY - timeSinceLastMove); |
| 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | _handleDelayedMouseMove() { |
| 1212 | this._mouseMoveTimer = null; |
| 1213 | this._sendMouse(this._mousePos.x, this._mousePos.y, |
| 1214 | this._mouseButtonMask); |
| 1215 | this._mouseLastMoveTime = Date.now(); |
| 1216 | } |
| 1217 | |
| 1218 | _sendMouse(x, y, mask) { |
| 1219 | if (this._rfbConnectionState !== 'connected') { return; } |
| 1220 | if (this._viewOnly) { return; } |
| 1221 | |
| 1222 | |
| 1223 | if (mask & 0x8000) { |
| 1224 | throw new Error("Illegal mouse button mask (mask: " + mask + ")"); |
| 1225 | } |
| 1226 | |
| 1227 | let extendedMouseButtons = mask & 0x7f80; |
| 1228 | |
| 1229 | if (this._extendedPointerEventSupported && extendedMouseButtons) { |
| 1230 | RFB.messages.extendedPointerEvent(this._sock, this._display.absX(x), |
| 1231 | this._display.absY(y), mask); |
| 1232 | } else { |
| 1233 | RFB.messages.pointerEvent(this._sock, this._display.absX(x), |
| 1234 | this._display.absY(y), mask); |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | _handleWheel(ev) { |
| 1239 | if (this._rfbConnectionState !== 'connected') { return; } |
| 1240 | if (this._viewOnly) { return; } |
| 1241 | |
| 1242 | ev.stopPropagation(); |
| 1243 | ev.preventDefault(); |
| 1244 | |
| 1245 | let pos = clientToElement(ev.clientX, ev.clientY, |
| 1246 | this._canvas); |
| 1247 | |
| 1248 | let bmask = RFB._convertButtonMask(ev.buttons); |
| 1249 | let dX = ev.deltaX; |
| 1250 | let dY = ev.deltaY; |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | if (this._cagePixelWheel && bmask === 0) { |
| 1255 | const unit = ev.deltaMode === 1 ? WHEEL_LINE_HEIGHT : |
| 1256 | ev.deltaMode === 2 ? this._canvas.clientHeight : 1; |
| 1257 | this._cageWheel.add(pos.x, pos.y, dX * unit, dY * unit); |
| 1258 | return; |
| 1259 | } |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | |
| 1266 | if (ev.deltaMode !== 0) { |
| 1267 | dX *= WHEEL_LINE_HEIGHT; |
| 1268 | dY *= WHEEL_LINE_HEIGHT; |
| 1269 | } |
| 1270 | |
| 1271 | |
| 1272 | |
| 1273 | |
| 1274 | this._accumulatedWheelDeltaX += dX; |
| 1275 | this._accumulatedWheelDeltaY += dY; |
| 1276 | |
| 1277 | |
| 1278 | |
| 1279 | |
| 1280 | if (Math.abs(this._accumulatedWheelDeltaX) >= WHEEL_STEP) { |
| 1281 | if (this._accumulatedWheelDeltaX < 0) { |
| 1282 | this._handleMouseButton(pos.x, pos.y, bmask | 1 << 5); |
| 1283 | this._handleMouseButton(pos.x, pos.y, bmask); |
| 1284 | } else if (this._accumulatedWheelDeltaX > 0) { |
| 1285 | this._handleMouseButton(pos.x, pos.y, bmask | 1 << 6); |
| 1286 | this._handleMouseButton(pos.x, pos.y, bmask); |
| 1287 | } |
| 1288 | |
| 1289 | this._accumulatedWheelDeltaX = 0; |
| 1290 | } |
| 1291 | if (Math.abs(this._accumulatedWheelDeltaY) >= WHEEL_STEP) { |
| 1292 | if (this._accumulatedWheelDeltaY < 0) { |
| 1293 | this._handleMouseButton(pos.x, pos.y, bmask | 1 << 3); |
| 1294 | this._handleMouseButton(pos.x, pos.y, bmask); |
| 1295 | } else if (this._accumulatedWheelDeltaY > 0) { |
| 1296 | this._handleMouseButton(pos.x, pos.y, bmask | 1 << 4); |
| 1297 | this._handleMouseButton(pos.x, pos.y, bmask); |
| 1298 | } |
| 1299 | |
| 1300 | this._accumulatedWheelDeltaY = 0; |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | _cageSendWheel(x, y, dx, dy) { |
| 1305 | const send = () => { |
| 1306 | if (this._rfbConnectionState !== 'connected' || this._viewOnly) return; |
| 1307 | this._sock.sQpushBytes(cageWheelPacket(this._display.absX(x), this._display.absY(y), dx, dy)); |
| 1308 | this._sock.flush(); |
| 1309 | }; |
| 1310 | |
| 1311 | if (this._cageInputChain) this._cageQueueInput(send); |
| 1312 | else send(); |
| 1313 | } |
| 1314 | |
| 1315 | _fakeMouseMove(ev, elementX, elementY) { |
| 1316 | this._handleMouseMove(elementX, elementY); |
| 1317 | this._cursor.move(ev.detail.clientX, ev.detail.clientY); |
| 1318 | } |
| 1319 | |
| 1320 | _handleTapEvent(ev, bmask) { |
| 1321 | let pos = clientToElement(ev.detail.clientX, ev.detail.clientY, |
| 1322 | this._canvas); |
| 1323 | |
| 1324 | |
| 1325 | |
| 1326 | |
| 1327 | if ((this._gestureLastTapTime !== null) && |
| 1328 | ((Date.now() - this._gestureLastTapTime) < DOUBLE_TAP_TIMEOUT) && |
| 1329 | (this._gestureFirstDoubleTapEv.detail.type === ev.detail.type)) { |
| 1330 | let dx = this._gestureFirstDoubleTapEv.detail.clientX - ev.detail.clientX; |
| 1331 | let dy = this._gestureFirstDoubleTapEv.detail.clientY - ev.detail.clientY; |
| 1332 | let distance = Math.hypot(dx, dy); |
| 1333 | |
| 1334 | if (distance < DOUBLE_TAP_THRESHOLD) { |
| 1335 | pos = clientToElement(this._gestureFirstDoubleTapEv.detail.clientX, |
| 1336 | this._gestureFirstDoubleTapEv.detail.clientY, |
| 1337 | this._canvas); |
| 1338 | } else { |
| 1339 | this._gestureFirstDoubleTapEv = ev; |
| 1340 | } |
| 1341 | } else { |
| 1342 | this._gestureFirstDoubleTapEv = ev; |
| 1343 | } |
| 1344 | this._gestureLastTapTime = Date.now(); |
| 1345 | |
| 1346 | this._fakeMouseMove(this._gestureFirstDoubleTapEv, pos.x, pos.y); |
| 1347 | this._handleMouseButton(pos.x, pos.y, bmask); |
| 1348 | this._handleMouseButton(pos.x, pos.y, 0x0); |
| 1349 | } |
| 1350 | |
| 1351 | _handleGesture(ev) { |
| 1352 | let magnitude; |
| 1353 | |
| 1354 | let pos = clientToElement(ev.detail.clientX, ev.detail.clientY, |
| 1355 | this._canvas); |
| 1356 | switch (ev.type) { |
| 1357 | case 'gesturestart': |
| 1358 | switch (ev.detail.type) { |
| 1359 | case 'onetap': |
| 1360 | this._handleTapEvent(ev, 0x1); |
| 1361 | break; |
| 1362 | case 'twotap': |
| 1363 | this._handleTapEvent(ev, 0x4); |
| 1364 | break; |
| 1365 | case 'threetap': |
| 1366 | this._handleTapEvent(ev, 0x2); |
| 1367 | break; |
| 1368 | case 'drag': |
| 1369 | if (this.dragViewport) { |
| 1370 | this._viewportHasMoved = false; |
| 1371 | this._viewportDragging = true; |
| 1372 | this._viewportDragPos = {'x': pos.x, 'y': pos.y}; |
| 1373 | } else { |
| 1374 | this._fakeMouseMove(ev, pos.x, pos.y); |
| 1375 | this._handleMouseButton(pos.x, pos.y, 0x1); |
| 1376 | } |
| 1377 | break; |
| 1378 | case 'longpress': |
| 1379 | if (this.dragViewport) { |
| 1380 | |
| 1381 | |
| 1382 | |
| 1383 | this._viewportHasMoved = false; |
| 1384 | this._viewportDragPos = {'x': pos.x, 'y': pos.y}; |
| 1385 | } else { |
| 1386 | this._fakeMouseMove(ev, pos.x, pos.y); |
| 1387 | this._handleMouseButton(pos.x, pos.y, 0x4); |
| 1388 | } |
| 1389 | break; |
| 1390 | case 'twodrag': |
| 1391 | this._gestureLastMagnitudeX = ev.detail.magnitudeX; |
| 1392 | this._gestureLastMagnitudeY = ev.detail.magnitudeY; |
| 1393 | this._fakeMouseMove(ev, pos.x, pos.y); |
| 1394 | break; |
| 1395 | case 'pinch': |
| 1396 | this._gestureLastMagnitudeX = Math.hypot(ev.detail.magnitudeX, |
| 1397 | ev.detail.magnitudeY); |
| 1398 | this._fakeMouseMove(ev, pos.x, pos.y); |
| 1399 | break; |
| 1400 | } |
| 1401 | break; |
| 1402 | |
| 1403 | case 'gesturemove': |
| 1404 | switch (ev.detail.type) { |
| 1405 | case 'onetap': |
| 1406 | case 'twotap': |
| 1407 | case 'threetap': |
| 1408 | break; |
| 1409 | case 'drag': |
| 1410 | case 'longpress': |
| 1411 | if (this.dragViewport) { |
| 1412 | this._viewportDragging = true; |
| 1413 | const deltaX = this._viewportDragPos.x - pos.x; |
| 1414 | const deltaY = this._viewportDragPos.y - pos.y; |
| 1415 | |
| 1416 | if (this._viewportHasMoved || (Math.abs(deltaX) > dragThreshold || |
| 1417 | Math.abs(deltaY) > dragThreshold)) { |
| 1418 | this._viewportHasMoved = true; |
| 1419 | |
| 1420 | this._viewportDragPos = {'x': pos.x, 'y': pos.y}; |
| 1421 | this._display.viewportChangePos(deltaX, deltaY); |
| 1422 | } |
| 1423 | } else { |
| 1424 | this._fakeMouseMove(ev, pos.x, pos.y); |
| 1425 | } |
| 1426 | break; |
| 1427 | case 'twodrag': |
| 1428 | |
| 1429 | |
| 1430 | |
| 1431 | this._fakeMouseMove(ev, pos.x, pos.y); |
| 1432 | while ((ev.detail.magnitudeY - this._gestureLastMagnitudeY) > GESTURE_SCRLSENS) { |
| 1433 | this._handleMouseButton(pos.x, pos.y, 0x8); |
| 1434 | this._handleMouseButton(pos.x, pos.y, 0x0); |
| 1435 | this._gestureLastMagnitudeY += GESTURE_SCRLSENS; |
| 1436 | } |
| 1437 | while ((ev.detail.magnitudeY - this._gestureLastMagnitudeY) < -GESTURE_SCRLSENS) { |
| 1438 | this._handleMouseButton(pos.x, pos.y, 0x10); |
| 1439 | this._handleMouseButton(pos.x, pos.y, 0x0); |
| 1440 | this._gestureLastMagnitudeY -= GESTURE_SCRLSENS; |
| 1441 | } |
| 1442 | while ((ev.detail.magnitudeX - this._gestureLastMagnitudeX) > GESTURE_SCRLSENS) { |
| 1443 | this._handleMouseButton(pos.x, pos.y, 0x20); |
| 1444 | this._handleMouseButton(pos.x, pos.y, 0x0); |
| 1445 | this._gestureLastMagnitudeX += GESTURE_SCRLSENS; |
| 1446 | } |
| 1447 | while ((ev.detail.magnitudeX - this._gestureLastMagnitudeX) < -GESTURE_SCRLSENS) { |
| 1448 | this._handleMouseButton(pos.x, pos.y, 0x40); |
| 1449 | this._handleMouseButton(pos.x, pos.y, 0x0); |
| 1450 | this._gestureLastMagnitudeX -= GESTURE_SCRLSENS; |
| 1451 | } |
| 1452 | break; |
| 1453 | case 'pinch': |
| 1454 | |
| 1455 | |
| 1456 | |
| 1457 | this._fakeMouseMove(ev, pos.x, pos.y); |
| 1458 | magnitude = Math.hypot(ev.detail.magnitudeX, ev.detail.magnitudeY); |
| 1459 | if (Math.abs(magnitude - this._gestureLastMagnitudeX) > GESTURE_ZOOMSENS) { |
| 1460 | this._handleKeyEvent(KeyTable.XK_Control_L, "ControlLeft", true); |
| 1461 | while ((magnitude - this._gestureLastMagnitudeX) > GESTURE_ZOOMSENS) { |
| 1462 | this._handleMouseButton(pos.x, pos.y, 0x8); |
| 1463 | this._handleMouseButton(pos.x, pos.y, 0x0); |
| 1464 | this._gestureLastMagnitudeX += GESTURE_ZOOMSENS; |
| 1465 | } |
| 1466 | while ((magnitude - this._gestureLastMagnitudeX) < -GESTURE_ZOOMSENS) { |
| 1467 | this._handleMouseButton(pos.x, pos.y, 0x10); |
| 1468 | this._handleMouseButton(pos.x, pos.y, 0x0); |
| 1469 | this._gestureLastMagnitudeX -= GESTURE_ZOOMSENS; |
| 1470 | } |
| 1471 | } |
| 1472 | this._handleKeyEvent(KeyTable.XK_Control_L, "ControlLeft", false); |
| 1473 | break; |
| 1474 | } |
| 1475 | break; |
| 1476 | |
| 1477 | case 'gestureend': |
| 1478 | switch (ev.detail.type) { |
| 1479 | case 'onetap': |
| 1480 | case 'twotap': |
| 1481 | case 'threetap': |
| 1482 | case 'pinch': |
| 1483 | case 'twodrag': |
| 1484 | break; |
| 1485 | case 'drag': |
| 1486 | if (this.dragViewport) { |
| 1487 | this._viewportDragging = false; |
| 1488 | } else { |
| 1489 | this._fakeMouseMove(ev, pos.x, pos.y); |
| 1490 | this._handleMouseButton(pos.x, pos.y, 0x0); |
| 1491 | } |
| 1492 | break; |
| 1493 | case 'longpress': |
| 1494 | if (this._viewportHasMoved) { |
| 1495 | |
| 1496 | |
| 1497 | break; |
| 1498 | } |
| 1499 | |
| 1500 | if (this.dragViewport && !this._viewportHasMoved) { |
| 1501 | this._fakeMouseMove(ev, pos.x, pos.y); |
| 1502 | |
| 1503 | |
| 1504 | |
| 1505 | this._handleMouseButton(pos.x, pos.y, 0x4); |
| 1506 | this._handleMouseButton(pos.x, pos.y, 0x0); |
| 1507 | this._viewportDragging = false; |
| 1508 | } else { |
| 1509 | this._fakeMouseMove(ev, pos.x, pos.y); |
| 1510 | this._handleMouseButton(pos.x, pos.y, 0x0); |
| 1511 | } |
| 1512 | break; |
| 1513 | } |
| 1514 | break; |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | _flushMouseMoveTimer(x, y) { |
| 1519 | if (this._mouseMoveTimer !== null) { |
| 1520 | clearTimeout(this._mouseMoveTimer); |
| 1521 | this._mouseMoveTimer = null; |
| 1522 | this._sendMouse(x, y, this._mouseButtonMask); |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | _negotiateProtocolVersion() { |
| 1529 | if (this._sock.rQwait("version", 12)) { |
| 1530 | return false; |
| 1531 | } |
| 1532 | |
| 1533 | const sversion = this._sock.rQshiftStr(12).substr(4, 7); |
| 1534 | Log.Info("Server ProtocolVersion: " + sversion); |
| 1535 | let isRepeater = 0; |
| 1536 | switch (sversion) { |
| 1537 | case "000.000": |
| 1538 | isRepeater = 1; |
| 1539 | break; |
| 1540 | case "003.003": |
| 1541 | case "003.006": |
| 1542 | this._rfbVersion = 3.3; |
| 1543 | break; |
| 1544 | case "003.007": |
| 1545 | this._rfbVersion = 3.7; |
| 1546 | break; |
| 1547 | case "003.008": |
| 1548 | case "003.889": |
| 1549 | case "004.000": |
| 1550 | case "004.001": |
| 1551 | case "005.000": |
| 1552 | this._rfbVersion = 3.8; |
| 1553 | break; |
| 1554 | default: |
| 1555 | return this._fail("Invalid server version " + sversion); |
| 1556 | } |
| 1557 | |
| 1558 | if (isRepeater) { |
| 1559 | let repeaterID = "ID:" + this._repeaterID; |
| 1560 | while (repeaterID.length < 250) { |
| 1561 | repeaterID += "\0"; |
| 1562 | } |
| 1563 | this._sock.sQpushString(repeaterID); |
| 1564 | this._sock.flush(); |
| 1565 | return true; |
| 1566 | } |
| 1567 | |
| 1568 | if (this._rfbVersion > this._rfbMaxVersion) { |
| 1569 | this._rfbVersion = this._rfbMaxVersion; |
| 1570 | } |
| 1571 | |
| 1572 | const cversion = "00" + parseInt(this._rfbVersion, 10) + |
| 1573 | ".00" + ((this._rfbVersion * 10) % 10); |
| 1574 | this._sock.sQpushString("RFB " + cversion + "\n"); |
| 1575 | this._sock.flush(); |
| 1576 | Log.Debug('Sent ProtocolVersion: ' + cversion); |
| 1577 | |
| 1578 | this._rfbInitState = 'Security'; |
| 1579 | } |
| 1580 | |
| 1581 | _isSupportedSecurityType(type) { |
| 1582 | const clientTypes = [ |
| 1583 | securityTypeNone, |
| 1584 | securityTypeVNCAuth, |
| 1585 | securityTypeRA2ne, |
| 1586 | securityTypeTight, |
| 1587 | securityTypeVeNCrypt, |
| 1588 | securityTypeXVP, |
| 1589 | securityTypeARD, |
| 1590 | securityTypeMSLogonII, |
| 1591 | securityTypePlain, |
| 1592 | ]; |
| 1593 | |
| 1594 | return clientTypes.includes(type); |
| 1595 | } |
| 1596 | |
| 1597 | _negotiateSecurity() { |
| 1598 | if (this._rfbVersion >= 3.7) { |
| 1599 | |
| 1600 | const numTypes = this._sock.rQshift8(); |
| 1601 | if (this._sock.rQwait("security type", numTypes, 1)) { return false; } |
| 1602 | |
| 1603 | if (numTypes === 0) { |
| 1604 | this._rfbInitState = "SecurityReason"; |
| 1605 | this._securityContext = "no security types"; |
| 1606 | this._securityStatus = 1; |
| 1607 | return true; |
| 1608 | } |
| 1609 | |
| 1610 | const types = this._sock.rQshiftBytes(numTypes); |
| 1611 | Log.Debug("Server security types: " + types); |
| 1612 | |
| 1613 | |
| 1614 | |
| 1615 | this._rfbAuthScheme = -1; |
| 1616 | for (let type of types) { |
| 1617 | if (this._isSupportedSecurityType(type)) { |
| 1618 | this._rfbAuthScheme = type; |
| 1619 | break; |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | if (this._rfbAuthScheme === -1) { |
| 1624 | return this._fail("Unsupported security types (types: " + types + ")"); |
| 1625 | } |
| 1626 | |
| 1627 | this._sock.sQpush8(this._rfbAuthScheme); |
| 1628 | this._sock.flush(); |
| 1629 | } else { |
| 1630 | |
| 1631 | if (this._sock.rQwait("security scheme", 4)) { return false; } |
| 1632 | this._rfbAuthScheme = this._sock.rQshift32(); |
| 1633 | |
| 1634 | if (this._rfbAuthScheme == 0) { |
| 1635 | this._rfbInitState = "SecurityReason"; |
| 1636 | this._securityContext = "authentication scheme"; |
| 1637 | this._securityStatus = 1; |
| 1638 | return true; |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | this._rfbInitState = 'Authentication'; |
| 1643 | Log.Debug('Authenticating using scheme: ' + this._rfbAuthScheme); |
| 1644 | |
| 1645 | return true; |
| 1646 | } |
| 1647 | |
| 1648 | _handleSecurityReason() { |
| 1649 | if (this._sock.rQwait("reason length", 4)) { |
| 1650 | return false; |
| 1651 | } |
| 1652 | const strlen = this._sock.rQshift32(); |
| 1653 | let reason = ""; |
| 1654 | |
| 1655 | if (strlen > 0) { |
| 1656 | if (this._sock.rQwait("reason", strlen, 4)) { return false; } |
| 1657 | reason = this._sock.rQshiftStr(strlen); |
| 1658 | } |
| 1659 | |
| 1660 | if (reason !== "") { |
| 1661 | this.dispatchEvent(new CustomEvent( |
| 1662 | "securityfailure", |
| 1663 | { detail: { status: this._securityStatus, |
| 1664 | reason: reason } })); |
| 1665 | |
| 1666 | return this._fail("Security negotiation failed on " + |
| 1667 | this._securityContext + |
| 1668 | " (reason: " + reason + ")"); |
| 1669 | } else { |
| 1670 | this.dispatchEvent(new CustomEvent( |
| 1671 | "securityfailure", |
| 1672 | { detail: { status: this._securityStatus } })); |
| 1673 | |
| 1674 | return this._fail("Security negotiation failed on " + |
| 1675 | this._securityContext); |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | |
| 1680 | _negotiateXvpAuth() { |
| 1681 | if (this._rfbCredentials.username === undefined || |
| 1682 | this._rfbCredentials.password === undefined || |
| 1683 | this._rfbCredentials.target === undefined) { |
| 1684 | this.dispatchEvent(new CustomEvent( |
| 1685 | "credentialsrequired", |
| 1686 | { detail: { types: ["username", "password", "target"] } })); |
| 1687 | return false; |
| 1688 | } |
| 1689 | |
| 1690 | this._sock.sQpush8(this._rfbCredentials.username.length); |
| 1691 | this._sock.sQpush8(this._rfbCredentials.target.length); |
| 1692 | this._sock.sQpushString(this._rfbCredentials.username); |
| 1693 | this._sock.sQpushString(this._rfbCredentials.target); |
| 1694 | |
| 1695 | this._sock.flush(); |
| 1696 | |
| 1697 | this._rfbAuthScheme = securityTypeVNCAuth; |
| 1698 | |
| 1699 | return this._negotiateAuthentication(); |
| 1700 | } |
| 1701 | |
| 1702 | |
| 1703 | _negotiateVeNCryptAuth() { |
| 1704 | |
| 1705 | |
| 1706 | if (this._rfbVeNCryptState == 0) { |
| 1707 | if (this._sock.rQwait("vencrypt version", 2)) { return false; } |
| 1708 | |
| 1709 | const major = this._sock.rQshift8(); |
| 1710 | const minor = this._sock.rQshift8(); |
| 1711 | |
| 1712 | if (!(major == 0 && minor == 2)) { |
| 1713 | return this._fail("Unsupported VeNCrypt version " + major + "." + minor); |
| 1714 | } |
| 1715 | |
| 1716 | this._sock.sQpush8(0); |
| 1717 | this._sock.sQpush8(2); |
| 1718 | this._sock.flush(); |
| 1719 | this._rfbVeNCryptState = 1; |
| 1720 | } |
| 1721 | |
| 1722 | |
| 1723 | if (this._rfbVeNCryptState == 1) { |
| 1724 | if (this._sock.rQwait("vencrypt ack", 1)) { return false; } |
| 1725 | |
| 1726 | const res = this._sock.rQshift8(); |
| 1727 | |
| 1728 | if (res != 0) { |
| 1729 | return this._fail("VeNCrypt failure " + res); |
| 1730 | } |
| 1731 | |
| 1732 | this._rfbVeNCryptState = 2; |
| 1733 | } |
| 1734 | |
| 1735 | |
| 1736 | |
| 1737 | if (this._rfbVeNCryptState == 2) { |
| 1738 | if (this._sock.rQwait("vencrypt subtypes length", 1)) { return false; } |
| 1739 | |
| 1740 | const subtypesLength = this._sock.rQshift8(); |
| 1741 | if (subtypesLength < 1) { |
| 1742 | return this._fail("VeNCrypt subtypes empty"); |
| 1743 | } |
| 1744 | |
| 1745 | this._rfbVeNCryptSubtypesLength = subtypesLength; |
| 1746 | this._rfbVeNCryptState = 3; |
| 1747 | } |
| 1748 | |
| 1749 | |
| 1750 | if (this._rfbVeNCryptState == 3) { |
| 1751 | if (this._sock.rQwait("vencrypt subtypes", 4 * this._rfbVeNCryptSubtypesLength)) { return false; } |
| 1752 | |
| 1753 | const subtypes = []; |
| 1754 | for (let i = 0; i < this._rfbVeNCryptSubtypesLength; i++) { |
| 1755 | subtypes.push(this._sock.rQshift32()); |
| 1756 | } |
| 1757 | |
| 1758 | |
| 1759 | |
| 1760 | this._rfbAuthScheme = -1; |
| 1761 | for (let type of subtypes) { |
| 1762 | |
| 1763 | if (type === securityTypeVeNCrypt) { |
| 1764 | continue; |
| 1765 | } |
| 1766 | |
| 1767 | if (this._isSupportedSecurityType(type)) { |
| 1768 | this._rfbAuthScheme = type; |
| 1769 | break; |
| 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | if (this._rfbAuthScheme === -1) { |
| 1774 | return this._fail("Unsupported security types (types: " + subtypes + ")"); |
| 1775 | } |
| 1776 | |
| 1777 | this._sock.sQpush32(this._rfbAuthScheme); |
| 1778 | this._sock.flush(); |
| 1779 | |
| 1780 | this._rfbVeNCryptState = 4; |
| 1781 | return true; |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | _negotiatePlainAuth() { |
| 1786 | if (this._rfbCredentials.username === undefined || |
| 1787 | this._rfbCredentials.password === undefined) { |
| 1788 | this.dispatchEvent(new CustomEvent( |
| 1789 | "credentialsrequired", |
| 1790 | { detail: { types: ["username", "password"] } })); |
| 1791 | return false; |
| 1792 | } |
| 1793 | |
| 1794 | const user = encodeUTF8(this._rfbCredentials.username); |
| 1795 | const pass = encodeUTF8(this._rfbCredentials.password); |
| 1796 | |
| 1797 | this._sock.sQpush32(user.length); |
| 1798 | this._sock.sQpush32(pass.length); |
| 1799 | this._sock.sQpushString(user); |
| 1800 | this._sock.sQpushString(pass); |
| 1801 | this._sock.flush(); |
| 1802 | |
| 1803 | this._rfbInitState = "SecurityResult"; |
| 1804 | return true; |
| 1805 | } |
| 1806 | |
| 1807 | _negotiateStdVNCAuth() { |
| 1808 | if (this._sock.rQwait("auth challenge", 16)) { return false; } |
| 1809 | |
| 1810 | if (this._rfbCredentials.password === undefined) { |
| 1811 | this.dispatchEvent(new CustomEvent( |
| 1812 | "credentialsrequired", |
| 1813 | { detail: { types: ["password"] } })); |
| 1814 | return false; |
| 1815 | } |
| 1816 | |
| 1817 | |
| 1818 | const challenge = Array.prototype.slice.call(this._sock.rQshiftBytes(16)); |
| 1819 | const response = RFB.genDES(this._rfbCredentials.password, challenge); |
| 1820 | this._sock.sQpushBytes(response); |
| 1821 | this._sock.flush(); |
| 1822 | this._rfbInitState = "SecurityResult"; |
| 1823 | return true; |
| 1824 | } |
| 1825 | |
| 1826 | _negotiateARDAuth() { |
| 1827 | |
| 1828 | if (this._rfbCredentials.username === undefined || |
| 1829 | this._rfbCredentials.password === undefined) { |
| 1830 | this.dispatchEvent(new CustomEvent( |
| 1831 | "credentialsrequired", |
| 1832 | { detail: { types: ["username", "password"] } })); |
| 1833 | return false; |
| 1834 | } |
| 1835 | |
| 1836 | if (this._rfbCredentials.ardPublicKey != undefined && |
| 1837 | this._rfbCredentials.ardCredentials != undefined) { |
| 1838 | |
| 1839 | this._sock.sQpushBytes(this._rfbCredentials.ardCredentials); |
| 1840 | this._sock.sQpushBytes(this._rfbCredentials.ardPublicKey); |
| 1841 | this._sock.flush(); |
| 1842 | this._rfbCredentials.ardCredentials = null; |
| 1843 | this._rfbCredentials.ardPublicKey = null; |
| 1844 | this._rfbInitState = "SecurityResult"; |
| 1845 | return true; |
| 1846 | } |
| 1847 | |
| 1848 | if (this._sock.rQwait("read ard", 4)) { return false; } |
| 1849 | |
| 1850 | let generator = this._sock.rQshiftBytes(2); |
| 1851 | |
| 1852 | let keyLength = this._sock.rQshift16(); |
| 1853 | |
| 1854 | if (this._sock.rQwait("read ard keylength", keyLength*2, 4)) { return false; } |
| 1855 | |
| 1856 | |
| 1857 | let prime = this._sock.rQshiftBytes(keyLength); |
| 1858 | let serverPublicKey = this._sock.rQshiftBytes(keyLength); |
| 1859 | |
| 1860 | let clientKey = legacyCrypto.generateKey( |
| 1861 | { name: "DH", g: generator, p: prime }, false, ["deriveBits"]); |
| 1862 | this._negotiateARDAuthAsync(keyLength, serverPublicKey, clientKey); |
| 1863 | |
| 1864 | return false; |
| 1865 | } |
| 1866 | |
| 1867 | async _negotiateARDAuthAsync(keyLength, serverPublicKey, clientKey) { |
| 1868 | const clientPublicKey = legacyCrypto.exportKey("raw", clientKey.publicKey); |
| 1869 | const sharedKey = legacyCrypto.deriveBits( |
| 1870 | { name: "DH", public: serverPublicKey }, clientKey.privateKey, keyLength * 8); |
| 1871 | |
| 1872 | const username = encodeUTF8(this._rfbCredentials.username).substring(0, 63); |
| 1873 | const password = encodeUTF8(this._rfbCredentials.password).substring(0, 63); |
| 1874 | |
| 1875 | const credentials = window.crypto.getRandomValues(new Uint8Array(128)); |
| 1876 | for (let i = 0; i < username.length; i++) { |
| 1877 | credentials[i] = username.charCodeAt(i); |
| 1878 | } |
| 1879 | credentials[username.length] = 0; |
| 1880 | for (let i = 0; i < password.length; i++) { |
| 1881 | credentials[64 + i] = password.charCodeAt(i); |
| 1882 | } |
| 1883 | credentials[64 + password.length] = 0; |
| 1884 | |
| 1885 | const key = await legacyCrypto.digest("MD5", sharedKey); |
| 1886 | const cipher = await legacyCrypto.importKey( |
| 1887 | "raw", key, { name: "AES-ECB" }, false, ["encrypt"]); |
| 1888 | const encrypted = await legacyCrypto.encrypt({ name: "AES-ECB" }, cipher, credentials); |
| 1889 | |
| 1890 | this._rfbCredentials.ardCredentials = encrypted; |
| 1891 | this._rfbCredentials.ardPublicKey = clientPublicKey; |
| 1892 | |
| 1893 | this._resumeAuthentication(); |
| 1894 | } |
| 1895 | |
| 1896 | _negotiateTightUnixAuth() { |
| 1897 | if (this._rfbCredentials.username === undefined || |
| 1898 | this._rfbCredentials.password === undefined) { |
| 1899 | this.dispatchEvent(new CustomEvent( |
| 1900 | "credentialsrequired", |
| 1901 | { detail: { types: ["username", "password"] } })); |
| 1902 | return false; |
| 1903 | } |
| 1904 | |
| 1905 | this._sock.sQpush32(this._rfbCredentials.username.length); |
| 1906 | this._sock.sQpush32(this._rfbCredentials.password.length); |
| 1907 | this._sock.sQpushString(this._rfbCredentials.username); |
| 1908 | this._sock.sQpushString(this._rfbCredentials.password); |
| 1909 | this._sock.flush(); |
| 1910 | |
| 1911 | this._rfbInitState = "SecurityResult"; |
| 1912 | return true; |
| 1913 | } |
| 1914 | |
| 1915 | _negotiateTightTunnels(numTunnels) { |
| 1916 | const clientSupportedTunnelTypes = { |
| 1917 | 0: { vendor: 'TGHT', signature: 'NOTUNNEL' } |
| 1918 | }; |
| 1919 | const serverSupportedTunnelTypes = {}; |
| 1920 | |
| 1921 | for (let i = 0; i < numTunnels; i++) { |
| 1922 | const capCode = this._sock.rQshift32(); |
| 1923 | const capVendor = this._sock.rQshiftStr(4); |
| 1924 | const capSignature = this._sock.rQshiftStr(8); |
| 1925 | serverSupportedTunnelTypes[capCode] = { vendor: capVendor, signature: capSignature }; |
| 1926 | } |
| 1927 | |
| 1928 | Log.Debug("Server Tight tunnel types: " + serverSupportedTunnelTypes); |
| 1929 | |
| 1930 | |
| 1931 | |
| 1932 | |
| 1933 | if (serverSupportedTunnelTypes[1] && |
| 1934 | (serverSupportedTunnelTypes[1].vendor === "SICR") && |
| 1935 | (serverSupportedTunnelTypes[1].signature === "SCHANNEL")) { |
| 1936 | Log.Debug("Detected Siemens server. Assuming NOTUNNEL support."); |
| 1937 | serverSupportedTunnelTypes[0] = { vendor: 'TGHT', signature: 'NOTUNNEL' }; |
| 1938 | } |
| 1939 | |
| 1940 | |
| 1941 | if (serverSupportedTunnelTypes[0]) { |
| 1942 | if (serverSupportedTunnelTypes[0].vendor != clientSupportedTunnelTypes[0].vendor || |
| 1943 | serverSupportedTunnelTypes[0].signature != clientSupportedTunnelTypes[0].signature) { |
| 1944 | return this._fail("Client's tunnel type had the incorrect " + |
| 1945 | "vendor or signature"); |
| 1946 | } |
| 1947 | Log.Debug("Selected tunnel type: " + clientSupportedTunnelTypes[0]); |
| 1948 | this._sock.sQpush32(0); |
| 1949 | this._sock.flush(); |
| 1950 | return false; |
| 1951 | } else { |
| 1952 | return this._fail("Server wanted tunnels, but doesn't support " + |
| 1953 | "the notunnel type"); |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | _negotiateTightAuth() { |
| 1958 | if (!this._rfbTightVNC) { |
| 1959 | if (this._sock.rQwait("num tunnels", 4)) { return false; } |
| 1960 | const numTunnels = this._sock.rQshift32(); |
| 1961 | if (numTunnels > 0 && this._sock.rQwait("tunnel capabilities", 16 * numTunnels, 4)) { return false; } |
| 1962 | |
| 1963 | this._rfbTightVNC = true; |
| 1964 | |
| 1965 | if (numTunnels > 0) { |
| 1966 | this._negotiateTightTunnels(numTunnels); |
| 1967 | return false; |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | |
| 1972 | if (this._sock.rQwait("sub auth count", 4)) { return false; } |
| 1973 | const subAuthCount = this._sock.rQshift32(); |
| 1974 | if (subAuthCount === 0) { |
| 1975 | this._rfbInitState = 'SecurityResult'; |
| 1976 | return true; |
| 1977 | } |
| 1978 | |
| 1979 | if (this._sock.rQwait("sub auth capabilities", 16 * subAuthCount, 4)) { return false; } |
| 1980 | |
| 1981 | const clientSupportedTypes = { |
| 1982 | 'STDVNOAUTH__': 1, |
| 1983 | 'STDVVNCAUTH_': 2, |
| 1984 | 'TGHTULGNAUTH': 129 |
| 1985 | }; |
| 1986 | |
| 1987 | const serverSupportedTypes = []; |
| 1988 | |
| 1989 | for (let i = 0; i < subAuthCount; i++) { |
| 1990 | this._sock.rQshift32(); |
| 1991 | const capabilities = this._sock.rQshiftStr(12); |
| 1992 | serverSupportedTypes.push(capabilities); |
| 1993 | } |
| 1994 | |
| 1995 | Log.Debug("Server Tight authentication types: " + serverSupportedTypes); |
| 1996 | |
| 1997 | for (let authType in clientSupportedTypes) { |
| 1998 | if (serverSupportedTypes.indexOf(authType) != -1) { |
| 1999 | this._sock.sQpush32(clientSupportedTypes[authType]); |
| 2000 | this._sock.flush(); |
| 2001 | Log.Debug("Selected authentication type: " + authType); |
| 2002 | |
| 2003 | switch (authType) { |
| 2004 | case 'STDVNOAUTH__': |
| 2005 | this._rfbInitState = 'SecurityResult'; |
| 2006 | return true; |
| 2007 | case 'STDVVNCAUTH_': |
| 2008 | this._rfbAuthScheme = securityTypeVNCAuth; |
| 2009 | return true; |
| 2010 | case 'TGHTULGNAUTH': |
| 2011 | this._rfbAuthScheme = securityTypeUnixLogon; |
| 2012 | return true; |
| 2013 | default: |
| 2014 | return this._fail("Unsupported tiny auth scheme " + |
| 2015 | "(scheme: " + authType + ")"); |
| 2016 | } |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | return this._fail("No supported sub-auth types!"); |
| 2021 | } |
| 2022 | |
| 2023 | _handleRSAAESCredentialsRequired(event) { |
| 2024 | this.dispatchEvent(event); |
| 2025 | } |
| 2026 | |
| 2027 | _handleRSAAESServerVerification(event) { |
| 2028 | this.dispatchEvent(event); |
| 2029 | } |
| 2030 | |
| 2031 | _negotiateRA2neAuth() { |
| 2032 | if (this._rfbRSAAESAuthenticationState === null) { |
| 2033 | this._rfbRSAAESAuthenticationState = new RSAAESAuthenticationState(this._sock, () => this._rfbCredentials); |
| 2034 | this._rfbRSAAESAuthenticationState.addEventListener( |
| 2035 | "serververification", this._eventHandlers.handleRSAAESServerVerification); |
| 2036 | this._rfbRSAAESAuthenticationState.addEventListener( |
| 2037 | "credentialsrequired", this._eventHandlers.handleRSAAESCredentialsRequired); |
| 2038 | } |
| 2039 | this._rfbRSAAESAuthenticationState.checkInternalEvents(); |
| 2040 | if (!this._rfbRSAAESAuthenticationState.hasStarted) { |
| 2041 | this._rfbRSAAESAuthenticationState.negotiateRA2neAuthAsync() |
| 2042 | .catch((e) => { |
| 2043 | if (e.message !== "disconnect normally") { |
| 2044 | this._fail(e.message); |
| 2045 | } |
| 2046 | }) |
| 2047 | .then(() => { |
| 2048 | this._rfbInitState = "SecurityResult"; |
| 2049 | return true; |
| 2050 | }).finally(() => { |
| 2051 | this._rfbRSAAESAuthenticationState.removeEventListener( |
| 2052 | "serververification", this._eventHandlers.handleRSAAESServerVerification); |
| 2053 | this._rfbRSAAESAuthenticationState.removeEventListener( |
| 2054 | "credentialsrequired", this._eventHandlers.handleRSAAESCredentialsRequired); |
| 2055 | this._rfbRSAAESAuthenticationState = null; |
| 2056 | }); |
| 2057 | } |
| 2058 | return false; |
| 2059 | } |
| 2060 | |
| 2061 | _negotiateMSLogonIIAuth() { |
| 2062 | if (this._sock.rQwait("mslogonii dh param", 24)) { return false; } |
| 2063 | |
| 2064 | if (this._rfbCredentials.username === undefined || |
| 2065 | this._rfbCredentials.password === undefined) { |
| 2066 | this.dispatchEvent(new CustomEvent( |
| 2067 | "credentialsrequired", |
| 2068 | { detail: { types: ["username", "password"] } })); |
| 2069 | return false; |
| 2070 | } |
| 2071 | |
| 2072 | const g = this._sock.rQshiftBytes(8); |
| 2073 | const p = this._sock.rQshiftBytes(8); |
| 2074 | const A = this._sock.rQshiftBytes(8); |
| 2075 | const dhKey = legacyCrypto.generateKey({ name: "DH", g: g, p: p }, true, ["deriveBits"]); |
| 2076 | const B = legacyCrypto.exportKey("raw", dhKey.publicKey); |
| 2077 | const secret = legacyCrypto.deriveBits({ name: "DH", public: A }, dhKey.privateKey, 64); |
| 2078 | |
| 2079 | const key = legacyCrypto.importKey("raw", secret, { name: "DES-CBC" }, false, ["encrypt"]); |
| 2080 | const username = encodeUTF8(this._rfbCredentials.username).substring(0, 255); |
| 2081 | const password = encodeUTF8(this._rfbCredentials.password).substring(0, 63); |
| 2082 | let usernameBytes = new Uint8Array(256); |
| 2083 | let passwordBytes = new Uint8Array(64); |
| 2084 | window.crypto.getRandomValues(usernameBytes); |
| 2085 | window.crypto.getRandomValues(passwordBytes); |
| 2086 | for (let i = 0; i < username.length; i++) { |
| 2087 | usernameBytes[i] = username.charCodeAt(i); |
| 2088 | } |
| 2089 | usernameBytes[username.length] = 0; |
| 2090 | for (let i = 0; i < password.length; i++) { |
| 2091 | passwordBytes[i] = password.charCodeAt(i); |
| 2092 | } |
| 2093 | passwordBytes[password.length] = 0; |
| 2094 | usernameBytes = legacyCrypto.encrypt({ name: "DES-CBC", iv: secret }, key, usernameBytes); |
| 2095 | passwordBytes = legacyCrypto.encrypt({ name: "DES-CBC", iv: secret }, key, passwordBytes); |
| 2096 | this._sock.sQpushBytes(B); |
| 2097 | this._sock.sQpushBytes(usernameBytes); |
| 2098 | this._sock.sQpushBytes(passwordBytes); |
| 2099 | this._sock.flush(); |
| 2100 | this._rfbInitState = "SecurityResult"; |
| 2101 | return true; |
| 2102 | } |
| 2103 | |
| 2104 | _negotiateAuthentication() { |
| 2105 | switch (this._rfbAuthScheme) { |
| 2106 | case securityTypeNone: |
| 2107 | if (this._rfbVersion >= 3.8) { |
| 2108 | this._rfbInitState = 'SecurityResult'; |
| 2109 | } else { |
| 2110 | this._rfbInitState = 'ClientInitialisation'; |
| 2111 | } |
| 2112 | return true; |
| 2113 | |
| 2114 | case securityTypeXVP: |
| 2115 | return this._negotiateXvpAuth(); |
| 2116 | |
| 2117 | case securityTypeARD: |
| 2118 | return this._negotiateARDAuth(); |
| 2119 | |
| 2120 | case securityTypeVNCAuth: |
| 2121 | return this._negotiateStdVNCAuth(); |
| 2122 | |
| 2123 | case securityTypeTight: |
| 2124 | return this._negotiateTightAuth(); |
| 2125 | |
| 2126 | case securityTypeVeNCrypt: |
| 2127 | return this._negotiateVeNCryptAuth(); |
| 2128 | |
| 2129 | case securityTypePlain: |
| 2130 | return this._negotiatePlainAuth(); |
| 2131 | |
| 2132 | case securityTypeUnixLogon: |
| 2133 | return this._negotiateTightUnixAuth(); |
| 2134 | |
| 2135 | case securityTypeRA2ne: |
| 2136 | return this._negotiateRA2neAuth(); |
| 2137 | |
| 2138 | case securityTypeMSLogonII: |
| 2139 | return this._negotiateMSLogonIIAuth(); |
| 2140 | |
| 2141 | default: |
| 2142 | return this._fail("Unsupported auth scheme (scheme: " + |
| 2143 | this._rfbAuthScheme + ")"); |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | _handleSecurityResult() { |
| 2148 | if (this._sock.rQwait('VNC auth response ', 4)) { return false; } |
| 2149 | |
| 2150 | const status = this._sock.rQshift32(); |
| 2151 | |
| 2152 | if (status === 0) { |
| 2153 | this._rfbInitState = 'ClientInitialisation'; |
| 2154 | Log.Debug('Authentication OK'); |
| 2155 | return true; |
| 2156 | } else { |
| 2157 | if (this._rfbVersion >= 3.8) { |
| 2158 | this._rfbInitState = "SecurityReason"; |
| 2159 | this._securityContext = "security result"; |
| 2160 | this._securityStatus = status; |
| 2161 | return true; |
| 2162 | } else { |
| 2163 | this.dispatchEvent(new CustomEvent( |
| 2164 | "securityfailure", |
| 2165 | { detail: { status: status } })); |
| 2166 | |
| 2167 | return this._fail("Security handshake failed"); |
| 2168 | } |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | _negotiateServerInit() { |
| 2173 | if (this._sock.rQwait("server initialization", 24)) { return false; } |
| 2174 | |
| 2175 | |
| 2176 | const width = this._sock.rQshift16(); |
| 2177 | const height = this._sock.rQshift16(); |
| 2178 | |
| 2179 | |
| 2180 | const bpp = this._sock.rQshift8(); |
| 2181 | const depth = this._sock.rQshift8(); |
| 2182 | const bigEndian = this._sock.rQshift8(); |
| 2183 | const trueColor = this._sock.rQshift8(); |
| 2184 | |
| 2185 | const redMax = this._sock.rQshift16(); |
| 2186 | const greenMax = this._sock.rQshift16(); |
| 2187 | const blueMax = this._sock.rQshift16(); |
| 2188 | const redShift = this._sock.rQshift8(); |
| 2189 | const greenShift = this._sock.rQshift8(); |
| 2190 | const blueShift = this._sock.rQshift8(); |
| 2191 | this._sock.rQskipBytes(3); |
| 2192 | |
| 2193 | |
| 2194 | |
| 2195 | |
| 2196 | |
| 2197 | const nameLength = this._sock.rQshift32(); |
| 2198 | if (this._sock.rQwait('server init name', nameLength, 24)) { return false; } |
| 2199 | let name = this._sock.rQshiftStr(nameLength); |
| 2200 | name = decodeUTF8(name, true); |
| 2201 | |
| 2202 | if (this._rfbTightVNC) { |
| 2203 | if (this._sock.rQwait('TightVNC extended server init header', 8, 24 + nameLength)) { return false; } |
| 2204 | |
| 2205 | const numServerMessages = this._sock.rQshift16(); |
| 2206 | const numClientMessages = this._sock.rQshift16(); |
| 2207 | const numEncodings = this._sock.rQshift16(); |
| 2208 | this._sock.rQskipBytes(2); |
| 2209 | |
| 2210 | const totalMessagesLength = (numServerMessages + numClientMessages + numEncodings) * 16; |
| 2211 | if (this._sock.rQwait('TightVNC extended server init header', totalMessagesLength, 32 + nameLength)) { return false; } |
| 2212 | |
| 2213 | |
| 2214 | |
| 2215 | |
| 2216 | |
| 2217 | this._sock.rQskipBytes(16 * numServerMessages); |
| 2218 | |
| 2219 | |
| 2220 | this._sock.rQskipBytes(16 * numClientMessages); |
| 2221 | |
| 2222 | |
| 2223 | this._sock.rQskipBytes(16 * numEncodings); |
| 2224 | } |
| 2225 | |
| 2226 | |
| 2227 | |
| 2228 | Log.Info("Screen: " + width + "x" + height + |
| 2229 | ", bpp: " + bpp + ", depth: " + depth + |
| 2230 | ", bigEndian: " + bigEndian + |
| 2231 | ", trueColor: " + trueColor + |
| 2232 | ", redMax: " + redMax + |
| 2233 | ", greenMax: " + greenMax + |
| 2234 | ", blueMax: " + blueMax + |
| 2235 | ", redShift: " + redShift + |
| 2236 | ", greenShift: " + greenShift + |
| 2237 | ", blueShift: " + blueShift); |
| 2238 | |
| 2239 | |
| 2240 | this._setDesktopName(name); |
| 2241 | this._resize(width, height); |
| 2242 | |
| 2243 | if (!this._viewOnly) { this._keyboard.grab(); } |
| 2244 | |
| 2245 | this._fbDepth = 24; |
| 2246 | |
| 2247 | if (this._fbName === "Intel(r) AMT KVM") { |
| 2248 | Log.Warn("Intel AMT KVM only supports 8/16 bit depths. Using low color mode."); |
| 2249 | this._fbDepth = 8; |
| 2250 | } |
| 2251 | |
| 2252 | RFB.messages.pixelFormat(this._sock, this._fbDepth, true); |
| 2253 | this._sendEncodings(); |
| 2254 | RFB.messages.fbUpdateRequest(this._sock, false, 0, 0, this._fbWidth, this._fbHeight); |
| 2255 | |
| 2256 | this._updateConnectionState('connected'); |
| 2257 | return true; |
| 2258 | } |
| 2259 | |
| 2260 | _sendEncodings() { |
| 2261 | const encs = []; |
| 2262 | |
| 2263 | |
| 2264 | encs.push(encodings.encodingCopyRect); |
| 2265 | |
| 2266 | if (this._fbDepth == 24) { |
| 2267 | if (supportsWebCodecsH264Decode) { |
| 2268 | encs.push(encodings.encodingH264); |
| 2269 | } |
| 2270 | encs.push(encodings.encodingTight); |
| 2271 | encs.push(encodings.encodingTightPNG); |
| 2272 | encs.push(encodings.encodingZRLE); |
| 2273 | encs.push(encodings.encodingJPEG); |
| 2274 | encs.push(encodings.encodingHextile); |
| 2275 | encs.push(encodings.encodingRRE); |
| 2276 | encs.push(encodings.encodingZlib); |
| 2277 | } |
| 2278 | encs.push(encodings.encodingRaw); |
| 2279 | |
| 2280 | |
| 2281 | encs.push(encodings.pseudoEncodingQualityLevel0 + this._qualityLevel); |
| 2282 | encs.push(encodings.pseudoEncodingCompressLevel0 + this._compressionLevel); |
| 2283 | |
| 2284 | encs.push(encodings.pseudoEncodingDesktopSize); |
| 2285 | encs.push(encodings.pseudoEncodingLastRect); |
| 2286 | encs.push(encodings.pseudoEncodingQEMUExtendedKeyEvent); |
| 2287 | encs.push(encodings.pseudoEncodingQEMULedEvent); |
| 2288 | encs.push(encodings.pseudoEncodingExtendedDesktopSize); |
| 2289 | encs.push(encodings.pseudoEncodingXvp); |
| 2290 | encs.push(encodings.pseudoEncodingFence); |
| 2291 | encs.push(encodings.pseudoEncodingContinuousUpdates); |
| 2292 | encs.push(encodings.pseudoEncodingDesktopName); |
| 2293 | encs.push(encodings.pseudoEncodingExtendedClipboard); |
| 2294 | encs.push(encodings.pseudoEncodingExtendedMouseButtons); |
| 2295 | |
| 2296 | if (this._fbDepth == 24) { |
| 2297 | encs.push(encodings.pseudoEncodingVMwareCursor); |
| 2298 | encs.push(encodings.pseudoEncodingCursor); |
| 2299 | } |
| 2300 | |
| 2301 | |
| 2302 | if (this._cageBrowserDisplay) encs.push(cageWheelEncoding); |
| 2303 | |
| 2304 | RFB.messages.clientEncodings(this._sock, encs); |
| 2305 | } |
| 2306 | |
| 2307 | |
| 2308 | |
| 2309 | |
| 2310 | |
| 2311 | |
| 2312 | |
| 2313 | |
| 2314 | |
| 2315 | _initMsg() { |
| 2316 | switch (this._rfbInitState) { |
| 2317 | case 'ProtocolVersion': |
| 2318 | return this._negotiateProtocolVersion(); |
| 2319 | |
| 2320 | case 'Security': |
| 2321 | return this._negotiateSecurity(); |
| 2322 | |
| 2323 | case 'Authentication': |
| 2324 | return this._negotiateAuthentication(); |
| 2325 | |
| 2326 | case 'SecurityResult': |
| 2327 | return this._handleSecurityResult(); |
| 2328 | |
| 2329 | case 'SecurityReason': |
| 2330 | return this._handleSecurityReason(); |
| 2331 | |
| 2332 | case 'ClientInitialisation': |
| 2333 | this._sock.sQpush8(this._shared ? 1 : 0); |
| 2334 | this._sock.flush(); |
| 2335 | this._rfbInitState = 'ServerInitialisation'; |
| 2336 | return true; |
| 2337 | |
| 2338 | case 'ServerInitialisation': |
| 2339 | return this._negotiateServerInit(); |
| 2340 | |
| 2341 | default: |
| 2342 | return this._fail("Unknown init state (state: " + |
| 2343 | this._rfbInitState + ")"); |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | |
| 2348 | |
| 2349 | _resumeAuthentication() { |
| 2350 | |
| 2351 | |
| 2352 | setTimeout(this._initMsg.bind(this), 0); |
| 2353 | } |
| 2354 | |
| 2355 | _handleSetColourMapMsg() { |
| 2356 | Log.Debug("SetColorMapEntries"); |
| 2357 | |
| 2358 | return this._fail("Unexpected SetColorMapEntries message"); |
| 2359 | } |
| 2360 | |
| 2361 | _handleServerCutText() { |
| 2362 | Log.Debug("ServerCutText"); |
| 2363 | |
| 2364 | if (this._sock.rQwait("ServerCutText header", 7, 1)) { return false; } |
| 2365 | |
| 2366 | this._sock.rQskipBytes(3); |
| 2367 | |
| 2368 | let length = this._sock.rQshift32(); |
| 2369 | length = toSigned32bit(length); |
| 2370 | |
| 2371 | if (this._sock.rQwait("ServerCutText content", Math.abs(length), 8)) { return false; } |
| 2372 | |
| 2373 | if (length >= 0) { |
| 2374 | |
| 2375 | const text = this._sock.rQshiftStr(length); |
| 2376 | if (this._viewOnly) { |
| 2377 | return true; |
| 2378 | } |
| 2379 | |
| 2380 | this.dispatchEvent(new CustomEvent( |
| 2381 | "clipboard", |
| 2382 | { detail: { text: text } })); |
| 2383 | |
| 2384 | } else { |
| 2385 | |
| 2386 | length = Math.abs(length); |
| 2387 | const flags = this._sock.rQshift32(); |
| 2388 | let formats = flags & 0x0000FFFF; |
| 2389 | let actions = flags & 0xFF000000; |
| 2390 | |
| 2391 | let isCaps = (!!(actions & extendedClipboardActionCaps)); |
| 2392 | if (isCaps) { |
| 2393 | this._clipboardServerCapabilitiesFormats = {}; |
| 2394 | this._clipboardServerCapabilitiesActions = {}; |
| 2395 | |
| 2396 | |
| 2397 | for (let i = 0; i <= 15; i++) { |
| 2398 | let index = 1 << i; |
| 2399 | |
| 2400 | |
| 2401 | if ((formats & index)) { |
| 2402 | this._clipboardServerCapabilitiesFormats[index] = true; |
| 2403 | |
| 2404 | |
| 2405 | this._sock.rQshift32(); |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | |
| 2410 | for (let i = 24; i <= 31; i++) { |
| 2411 | let index = 1 << i; |
| 2412 | this._clipboardServerCapabilitiesActions[index] = !!(actions & index); |
| 2413 | } |
| 2414 | |
| 2415 | |
| 2416 | |
| 2417 | let clientActions = [ |
| 2418 | extendedClipboardActionCaps, |
| 2419 | extendedClipboardActionRequest, |
| 2420 | extendedClipboardActionPeek, |
| 2421 | extendedClipboardActionNotify, |
| 2422 | extendedClipboardActionProvide |
| 2423 | ]; |
| 2424 | RFB.messages.extendedClipboardCaps(this._sock, clientActions, {extendedClipboardFormatText: 0}); |
| 2425 | |
| 2426 | } else if (actions === extendedClipboardActionRequest) { |
| 2427 | if (this._viewOnly) { |
| 2428 | return true; |
| 2429 | } |
| 2430 | |
| 2431 | |
| 2432 | if (this._clipboardText != null && |
| 2433 | this._clipboardServerCapabilitiesActions[extendedClipboardActionProvide]) { |
| 2434 | |
| 2435 | if (formats & extendedClipboardFormatText) { |
| 2436 | RFB.messages.extendedClipboardProvide(this._sock, [extendedClipboardFormatText], [this._clipboardText]); |
| 2437 | } |
| 2438 | } |
| 2439 | |
| 2440 | } else if (actions === extendedClipboardActionPeek) { |
| 2441 | if (this._viewOnly) { |
| 2442 | return true; |
| 2443 | } |
| 2444 | |
| 2445 | if (this._clipboardServerCapabilitiesActions[extendedClipboardActionNotify]) { |
| 2446 | |
| 2447 | if (this._clipboardText != null) { |
| 2448 | RFB.messages.extendedClipboardNotify(this._sock, [extendedClipboardFormatText]); |
| 2449 | } else { |
| 2450 | RFB.messages.extendedClipboardNotify(this._sock, []); |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | } else if (actions === extendedClipboardActionNotify) { |
| 2455 | if (this._viewOnly) { |
| 2456 | return true; |
| 2457 | } |
| 2458 | |
| 2459 | if (this._clipboardServerCapabilitiesActions[extendedClipboardActionRequest]) { |
| 2460 | |
| 2461 | if (formats & extendedClipboardFormatText) { |
| 2462 | RFB.messages.extendedClipboardRequest(this._sock, [extendedClipboardFormatText]); |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | } else if (actions === extendedClipboardActionProvide) { |
| 2467 | if (this._viewOnly) { |
| 2468 | return true; |
| 2469 | } |
| 2470 | |
| 2471 | if (!(formats & extendedClipboardFormatText)) { |
| 2472 | return true; |
| 2473 | } |
| 2474 | |
| 2475 | this._clipboardText = null; |
| 2476 | |
| 2477 | FIXME: |
| 2478 | let zlibStream = this._sock.rQshiftBytes(length - 4); |
| 2479 | let streamInflator = new Inflator(); |
| 2480 | let textData = null; |
| 2481 | |
| 2482 | streamInflator.setInput(zlibStream); |
| 2483 | for (let i = 0; i <= 15; i++) { |
| 2484 | let format = 1 << i; |
| 2485 | |
| 2486 | if (formats & format) { |
| 2487 | |
| 2488 | let size = 0x00; |
| 2489 | let sizeArray = streamInflator.inflate(4); |
| 2490 | |
| 2491 | size |= (sizeArray[0] << 24); |
| 2492 | size |= (sizeArray[1] << 16); |
| 2493 | size |= (sizeArray[2] << 8); |
| 2494 | size |= (sizeArray[3]); |
| 2495 | let chunk = streamInflator.inflate(size); |
| 2496 | |
| 2497 | if (format === extendedClipboardFormatText) { |
| 2498 | textData = chunk; |
| 2499 | } |
| 2500 | } |
| 2501 | } |
| 2502 | streamInflator.setInput(null); |
| 2503 | |
| 2504 | if (textData !== null) { |
| 2505 | let tmpText = ""; |
| 2506 | for (let i = 0; i < textData.length; i++) { |
| 2507 | tmpText += String.fromCharCode(textData[i]); |
| 2508 | } |
| 2509 | textData = tmpText; |
| 2510 | |
| 2511 | textData = decodeUTF8(textData); |
| 2512 | if ((textData.length > 0) && "\0" === textData.charAt(textData.length - 1)) { |
| 2513 | textData = textData.slice(0, -1); |
| 2514 | } |
| 2515 | |
| 2516 | textData = textData.replaceAll("\r\n", "\n"); |
| 2517 | |
| 2518 | this.dispatchEvent(new CustomEvent( |
| 2519 | "clipboard", |
| 2520 | { detail: { text: textData } })); |
| 2521 | } |
| 2522 | } else { |
| 2523 | return this._fail("Unexpected action in extended clipboard message: " + actions); |
| 2524 | } |
| 2525 | } |
| 2526 | return true; |
| 2527 | } |
| 2528 | |
| 2529 | _handleServerFenceMsg() { |
| 2530 | if (this._sock.rQwait("ServerFence header", 8, 1)) { return false; } |
| 2531 | this._sock.rQskipBytes(3); |
| 2532 | let flags = this._sock.rQshift32(); |
| 2533 | let length = this._sock.rQshift8(); |
| 2534 | |
| 2535 | if (this._sock.rQwait("ServerFence payload", length, 9)) { return false; } |
| 2536 | |
| 2537 | if (length > 64) { |
| 2538 | Log.Warn("Bad payload length (" + length + ") in fence response"); |
| 2539 | length = 64; |
| 2540 | } |
| 2541 | |
| 2542 | const payload = this._sock.rQshiftStr(length); |
| 2543 | if ((flags & (1 << 31)) && this._cageBrowserDisplay && payload === cageWheelCapability) { |
| 2544 | this._cagePixelWheel = true; |
| 2545 | } |
| 2546 | |
| 2547 | this._supportsFence = true; |
| 2548 | |
| 2549 | |
| 2550 | |
| 2551 | |
| 2552 | |
| 2553 | |
| 2554 | |
| 2555 | |
| 2556 | |
| 2557 | |
| 2558 | if (!(flags & (1<<31))) { |
| 2559 | return this._fail("Unexpected fence response"); |
| 2560 | } |
| 2561 | |
| 2562 | |
| 2563 | FIXME: |
| 2564 | flags &= (1<<0) | (1<<1); |
| 2565 | |
| 2566 | |
| 2567 | |
| 2568 | |
| 2569 | RFB.messages.clientFence(this._sock, flags, payload); |
| 2570 | |
| 2571 | return true; |
| 2572 | } |
| 2573 | |
| 2574 | _handleXvpMsg() { |
| 2575 | if (this._sock.rQwait("XVP version and message", 3, 1)) { return false; } |
| 2576 | this._sock.rQskipBytes(1); |
| 2577 | const xvpVer = this._sock.rQshift8(); |
| 2578 | const xvpMsg = this._sock.rQshift8(); |
| 2579 | |
| 2580 | switch (xvpMsg) { |
| 2581 | case 0: |
| 2582 | Log.Error("XVP operation failed"); |
| 2583 | break; |
| 2584 | case 1: |
| 2585 | this._rfbXvpVer = xvpVer; |
| 2586 | Log.Info("XVP extensions enabled (version " + this._rfbXvpVer + ")"); |
| 2587 | this._setCapability("power", true); |
| 2588 | break; |
| 2589 | default: |
| 2590 | this._fail("Illegal server XVP message (msg: " + xvpMsg + ")"); |
| 2591 | break; |
| 2592 | } |
| 2593 | |
| 2594 | return true; |
| 2595 | } |
| 2596 | |
| 2597 | _normalMsg() { |
| 2598 | let msgType; |
| 2599 | if (this._FBU.rects > 0) { |
| 2600 | msgType = 0; |
| 2601 | } else { |
| 2602 | msgType = this._sock.rQshift8(); |
| 2603 | } |
| 2604 | |
| 2605 | let first, ret; |
| 2606 | switch (msgType) { |
| 2607 | case 0: |
| 2608 | ret = this._framebufferUpdate(); |
| 2609 | if (ret && !this._enabledContinuousUpdates) { |
| 2610 | RFB.messages.fbUpdateRequest(this._sock, true, 0, 0, |
| 2611 | this._fbWidth, this._fbHeight); |
| 2612 | } |
| 2613 | return ret; |
| 2614 | |
| 2615 | case 1: |
| 2616 | return this._handleSetColourMapMsg(); |
| 2617 | |
| 2618 | case 2: |
| 2619 | Log.Debug("Bell"); |
| 2620 | this.dispatchEvent(new CustomEvent( |
| 2621 | "bell", |
| 2622 | { detail: {} })); |
| 2623 | return true; |
| 2624 | |
| 2625 | case 3: |
| 2626 | return this._handleServerCutText(); |
| 2627 | |
| 2628 | case 150: |
| 2629 | first = !this._supportsContinuousUpdates; |
| 2630 | this._supportsContinuousUpdates = true; |
| 2631 | this._enabledContinuousUpdates = false; |
| 2632 | if (first) { |
| 2633 | this._enabledContinuousUpdates = true; |
| 2634 | this._updateContinuousUpdates(); |
| 2635 | Log.Info("Enabling continuous updates."); |
| 2636 | } else { |
| 2637 | FIXME: |
| 2638 | |
| 2639 | } |
| 2640 | return true; |
| 2641 | |
| 2642 | case 248: |
| 2643 | return this._handleServerFenceMsg(); |
| 2644 | |
| 2645 | case 250: |
| 2646 | return this._handleXvpMsg(); |
| 2647 | |
| 2648 | default: |
| 2649 | this._fail("Unexpected server message (type " + msgType + ")"); |
| 2650 | Log.Debug("sock.rQpeekBytes(30): " + this._sock.rQpeekBytes(30)); |
| 2651 | return true; |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | _framebufferUpdate() { |
| 2656 | if (this._FBU.rects === 0) { |
| 2657 | if (this._sock.rQwait("FBU header", 3, 1)) { return false; } |
| 2658 | this._sock.rQskipBytes(1); |
| 2659 | this._FBU.rects = this._sock.rQshift16(); |
| 2660 | |
| 2661 | |
| 2662 | |
| 2663 | if (this._display.pending()) { |
| 2664 | this._flushing = true; |
| 2665 | this._display.flush() |
| 2666 | .then(() => { |
| 2667 | this._flushing = false; |
| 2668 | |
| 2669 | if (!this._sock.rQwait("message", 1)) { |
| 2670 | this._handleMessage(); |
| 2671 | } |
| 2672 | }); |
| 2673 | return false; |
| 2674 | } |
| 2675 | } |
| 2676 | |
| 2677 | while (this._FBU.rects > 0) { |
| 2678 | if (this._FBU.encoding === null) { |
| 2679 | if (this._sock.rQwait("rect header", 12)) { return false; } |
| 2680 | |
| 2681 | |
| 2682 | this._FBU.x = this._sock.rQshift16(); |
| 2683 | this._FBU.y = this._sock.rQshift16(); |
| 2684 | this._FBU.width = this._sock.rQshift16(); |
| 2685 | this._FBU.height = this._sock.rQshift16(); |
| 2686 | this._FBU.encoding = this._sock.rQshift32(); |
| 2687 | |
| 2688 | this._FBU.encoding >>= 0; |
| 2689 | } |
| 2690 | |
| 2691 | if (!this._handleRect()) { |
| 2692 | return false; |
| 2693 | } |
| 2694 | |
| 2695 | this._FBU.rects--; |
| 2696 | this._FBU.encoding = null; |
| 2697 | } |
| 2698 | |
| 2699 | this._display.flip(); |
| 2700 | |
| 2701 | return true; |
| 2702 | } |
| 2703 | |
| 2704 | _handleRect() { |
| 2705 | switch (this._FBU.encoding) { |
| 2706 | case encodings.pseudoEncodingLastRect: |
| 2707 | this._FBU.rects = 1; |
| 2708 | return true; |
| 2709 | |
| 2710 | case encodings.pseudoEncodingVMwareCursor: |
| 2711 | return this._handleVMwareCursor(); |
| 2712 | |
| 2713 | case encodings.pseudoEncodingCursor: |
| 2714 | return this._handleCursor(); |
| 2715 | |
| 2716 | case encodings.pseudoEncodingQEMUExtendedKeyEvent: |
| 2717 | this._qemuExtKeyEventSupported = true; |
| 2718 | return true; |
| 2719 | |
| 2720 | case encodings.pseudoEncodingDesktopName: |
| 2721 | return this._handleDesktopName(); |
| 2722 | |
| 2723 | case encodings.pseudoEncodingDesktopSize: |
| 2724 | this._resize(this._FBU.width, this._FBU.height); |
| 2725 | return true; |
| 2726 | |
| 2727 | case encodings.pseudoEncodingExtendedDesktopSize: |
| 2728 | return this._handleExtendedDesktopSize(); |
| 2729 | |
| 2730 | case encodings.pseudoEncodingExtendedMouseButtons: |
| 2731 | this._extendedPointerEventSupported = true; |
| 2732 | return true; |
| 2733 | |
| 2734 | case encodings.pseudoEncodingQEMULedEvent: |
| 2735 | return this._handleLedEvent(); |
| 2736 | |
| 2737 | default: |
| 2738 | return this._handleDataRect(); |
| 2739 | } |
| 2740 | } |
| 2741 | |
| 2742 | _handleVMwareCursor() { |
| 2743 | const hotx = this._FBU.x; |
| 2744 | const hoty = this._FBU.y; |
| 2745 | const w = this._FBU.width; |
| 2746 | const h = this._FBU.height; |
| 2747 | if (this._sock.rQwait("VMware cursor encoding", 1)) { |
| 2748 | return false; |
| 2749 | } |
| 2750 | |
| 2751 | const cursorType = this._sock.rQshift8(); |
| 2752 | |
| 2753 | this._sock.rQshift8(); |
| 2754 | |
| 2755 | let rgba; |
| 2756 | const bytesPerPixel = 4; |
| 2757 | |
| 2758 | |
| 2759 | if (cursorType == 0) { |
| 2760 | |
| 2761 | |
| 2762 | const PIXEL_MASK = 0xffffff00 | 0; |
| 2763 | rgba = new Array(w * h * bytesPerPixel); |
| 2764 | |
| 2765 | if (this._sock.rQwait("VMware cursor classic encoding", |
| 2766 | (w * h * bytesPerPixel) * 2, 2)) { |
| 2767 | return false; |
| 2768 | } |
| 2769 | |
| 2770 | let andMask = new Array(w * h); |
| 2771 | for (let pixel = 0; pixel < (w * h); pixel++) { |
| 2772 | andMask[pixel] = this._sock.rQshift32(); |
| 2773 | } |
| 2774 | |
| 2775 | let xorMask = new Array(w * h); |
| 2776 | for (let pixel = 0; pixel < (w * h); pixel++) { |
| 2777 | xorMask[pixel] = this._sock.rQshift32(); |
| 2778 | } |
| 2779 | |
| 2780 | for (let pixel = 0; pixel < (w * h); pixel++) { |
| 2781 | if (andMask[pixel] == 0) { |
| 2782 | |
| 2783 | let bgr = xorMask[pixel]; |
| 2784 | let r = bgr >> 8 & 0xff; |
| 2785 | let g = bgr >> 16 & 0xff; |
| 2786 | let b = bgr >> 24 & 0xff; |
| 2787 | |
| 2788 | rgba[(pixel * bytesPerPixel) ] = r; |
| 2789 | rgba[(pixel * bytesPerPixel) + 1 ] = g; |
| 2790 | rgba[(pixel * bytesPerPixel) + 2 ] = b; |
| 2791 | rgba[(pixel * bytesPerPixel) + 3 ] = 0xff; |
| 2792 | |
| 2793 | } else if ((andMask[pixel] & PIXEL_MASK) == |
| 2794 | PIXEL_MASK) { |
| 2795 | |
| 2796 | if (xorMask[pixel] == 0) { |
| 2797 | |
| 2798 | rgba[(pixel * bytesPerPixel) ] = 0x00; |
| 2799 | rgba[(pixel * bytesPerPixel) + 1 ] = 0x00; |
| 2800 | rgba[(pixel * bytesPerPixel) + 2 ] = 0x00; |
| 2801 | rgba[(pixel * bytesPerPixel) + 3 ] = 0x00; |
| 2802 | |
| 2803 | } else if ((xorMask[pixel] & PIXEL_MASK) == |
| 2804 | PIXEL_MASK) { |
| 2805 | |
| 2806 | |
| 2807 | rgba[(pixel * bytesPerPixel) ] = 0x00; |
| 2808 | rgba[(pixel * bytesPerPixel) + 1 ] = 0x00; |
| 2809 | rgba[(pixel * bytesPerPixel) + 2 ] = 0x00; |
| 2810 | rgba[(pixel * bytesPerPixel) + 3 ] = 0xff; |
| 2811 | |
| 2812 | } else { |
| 2813 | |
| 2814 | rgba[(pixel * bytesPerPixel) ] = 0x00; |
| 2815 | rgba[(pixel * bytesPerPixel) + 1 ] = 0x00; |
| 2816 | rgba[(pixel * bytesPerPixel) + 2 ] = 0x00; |
| 2817 | rgba[(pixel * bytesPerPixel) + 3 ] = 0xff; |
| 2818 | } |
| 2819 | |
| 2820 | } else { |
| 2821 | |
| 2822 | rgba[(pixel * bytesPerPixel) ] = 0x00; |
| 2823 | rgba[(pixel * bytesPerPixel) + 1 ] = 0x00; |
| 2824 | rgba[(pixel * bytesPerPixel) + 2 ] = 0x00; |
| 2825 | rgba[(pixel * bytesPerPixel) + 3 ] = 0xff; |
| 2826 | } |
| 2827 | } |
| 2828 | |
| 2829 | |
| 2830 | } else if (cursorType == 1) { |
| 2831 | if (this._sock.rQwait("VMware cursor alpha encoding", |
| 2832 | (w * h * 4), 2)) { |
| 2833 | return false; |
| 2834 | } |
| 2835 | |
| 2836 | rgba = new Array(w * h * bytesPerPixel); |
| 2837 | |
| 2838 | for (let pixel = 0; pixel < (w * h); pixel++) { |
| 2839 | let data = this._sock.rQshift32(); |
| 2840 | |
| 2841 | rgba[(pixel * 4) ] = data >> 24 & 0xff; |
| 2842 | rgba[(pixel * 4) + 1 ] = data >> 16 & 0xff; |
| 2843 | rgba[(pixel * 4) + 2 ] = data >> 8 & 0xff; |
| 2844 | rgba[(pixel * 4) + 3 ] = data & 0xff; |
| 2845 | } |
| 2846 | |
| 2847 | } else { |
| 2848 | Log.Warn("The given cursor type is not supported: " |
| 2849 | + cursorType + " given."); |
| 2850 | return false; |
| 2851 | } |
| 2852 | |
| 2853 | this._updateCursor(rgba, hotx, hoty, w, h); |
| 2854 | |
| 2855 | return true; |
| 2856 | } |
| 2857 | |
| 2858 | _handleCursor() { |
| 2859 | const hotx = this._FBU.x; |
| 2860 | const hoty = this._FBU.y; |
| 2861 | const w = this._FBU.width; |
| 2862 | const h = this._FBU.height; |
| 2863 | |
| 2864 | const pixelslength = w * h * 4; |
| 2865 | const masklength = Math.ceil(w / 8) * h; |
| 2866 | |
| 2867 | let bytes = pixelslength + masklength; |
| 2868 | if (this._sock.rQwait("cursor encoding", bytes)) { |
| 2869 | return false; |
| 2870 | } |
| 2871 | |
| 2872 | |
| 2873 | const pixels = this._sock.rQshiftBytes(pixelslength); |
| 2874 | const mask = this._sock.rQshiftBytes(masklength); |
| 2875 | let rgba = new Uint8Array(w * h * 4); |
| 2876 | |
| 2877 | let pixIdx = 0; |
| 2878 | for (let y = 0; y < h; y++) { |
| 2879 | for (let x = 0; x < w; x++) { |
| 2880 | let maskIdx = y * Math.ceil(w / 8) + Math.floor(x / 8); |
| 2881 | let alpha = (mask[maskIdx] << (x % 8)) & 0x80 ? 255 : 0; |
| 2882 | rgba[pixIdx ] = pixels[pixIdx + 2]; |
| 2883 | rgba[pixIdx + 1] = pixels[pixIdx + 1]; |
| 2884 | rgba[pixIdx + 2] = pixels[pixIdx]; |
| 2885 | rgba[pixIdx + 3] = alpha; |
| 2886 | pixIdx += 4; |
| 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | this._updateCursor(rgba, hotx, hoty, w, h); |
| 2891 | |
| 2892 | return true; |
| 2893 | } |
| 2894 | |
| 2895 | _handleDesktopName() { |
| 2896 | if (this._sock.rQwait("DesktopName", 4)) { |
| 2897 | return false; |
| 2898 | } |
| 2899 | |
| 2900 | let length = this._sock.rQshift32(); |
| 2901 | |
| 2902 | if (this._sock.rQwait("DesktopName", length, 4)) { |
| 2903 | return false; |
| 2904 | } |
| 2905 | |
| 2906 | let name = this._sock.rQshiftStr(length); |
| 2907 | name = decodeUTF8(name, true); |
| 2908 | |
| 2909 | this._setDesktopName(name); |
| 2910 | |
| 2911 | return true; |
| 2912 | } |
| 2913 | |
| 2914 | _handleLedEvent() { |
| 2915 | if (this._sock.rQwait("LED status", 1)) { |
| 2916 | return false; |
| 2917 | } |
| 2918 | |
| 2919 | let data = this._sock.rQshift8(); |
| 2920 | |
| 2921 | let numLock = data & 2 ? true : false; |
| 2922 | let capsLock = data & 4 ? true : false; |
| 2923 | this._remoteCapsLock = capsLock; |
| 2924 | this._remoteNumLock = numLock; |
| 2925 | |
| 2926 | return true; |
| 2927 | } |
| 2928 | |
| 2929 | _handleExtendedDesktopSize() { |
| 2930 | if (this._sock.rQwait("ExtendedDesktopSize", 4)) { |
| 2931 | return false; |
| 2932 | } |
| 2933 | |
| 2934 | const numberOfScreens = this._sock.rQpeek8(); |
| 2935 | |
| 2936 | let bytes = 4 + (numberOfScreens * 16); |
| 2937 | if (this._sock.rQwait("ExtendedDesktopSize", bytes)) { |
| 2938 | return false; |
| 2939 | } |
| 2940 | |
| 2941 | const firstUpdate = !this._supportsSetDesktopSize; |
| 2942 | this._supportsSetDesktopSize = true; |
| 2943 | |
| 2944 | this._sock.rQskipBytes(1); |
| 2945 | this._sock.rQskipBytes(3); |
| 2946 | |
| 2947 | for (let i = 0; i < numberOfScreens; i += 1) { |
| 2948 | |
| 2949 | if (i === 0) { |
| 2950 | this._screenID = this._sock.rQshift32(); |
| 2951 | this._sock.rQskipBytes(2); |
| 2952 | this._sock.rQskipBytes(2); |
| 2953 | this._sock.rQskipBytes(2); |
| 2954 | this._sock.rQskipBytes(2); |
| 2955 | this._screenFlags = this._sock.rQshift32(); |
| 2956 | } else { |
| 2957 | this._sock.rQskipBytes(16); |
| 2958 | } |
| 2959 | } |
| 2960 | |
| 2961 | |
| 2962 | |
| 2963 | |
| 2964 | |
| 2965 | |
| 2966 | |
| 2967 | |
| 2968 | |
| 2969 | if (this._FBU.x === 1) { |
| 2970 | this._pendingRemoteResize = false; |
| 2971 | } |
| 2972 | |
| 2973 | |
| 2974 | if (this._FBU.x === 1 && this._FBU.y !== 0) { |
| 2975 | let msg = ""; |
| 2976 | |
| 2977 | switch (this._FBU.y) { |
| 2978 | case 1: |
| 2979 | msg = "Resize is administratively prohibited"; |
| 2980 | break; |
| 2981 | case 2: |
| 2982 | msg = "Out of resources"; |
| 2983 | break; |
| 2984 | case 3: |
| 2985 | msg = "Invalid screen layout"; |
| 2986 | break; |
| 2987 | default: |
| 2988 | msg = "Unknown reason"; |
| 2989 | break; |
| 2990 | } |
| 2991 | Log.Warn("Server did not accept the resize request: " |
| 2992 | + msg); |
| 2993 | } else { |
| 2994 | this._resize(this._FBU.width, this._FBU.height); |
| 2995 | } |
| 2996 | |
| 2997 | |
| 2998 | |
| 2999 | |
| 3000 | |
| 3001 | if (firstUpdate) { |
| 3002 | this._requestRemoteResize(); |
| 3003 | } |
| 3004 | |
| 3005 | if (this._FBU.x === 1 && this._FBU.y === 0) { |
| 3006 | |
| 3007 | |
| 3008 | this._requestRemoteResize(); |
| 3009 | } |
| 3010 | |
| 3011 | return true; |
| 3012 | } |
| 3013 | |
| 3014 | _handleDataRect() { |
| 3015 | let decoder = this._decoders[this._FBU.encoding]; |
| 3016 | if (!decoder) { |
| 3017 | this._fail("Unsupported encoding (encoding: " + |
| 3018 | this._FBU.encoding + ")"); |
| 3019 | return false; |
| 3020 | } |
| 3021 | |
| 3022 | try { |
| 3023 | return decoder.decodeRect(this._FBU.x, this._FBU.y, |
| 3024 | this._FBU.width, this._FBU.height, |
| 3025 | this._sock, this._display, |
| 3026 | this._fbDepth); |
| 3027 | } catch (err) { |
| 3028 | this._fail("Error decoding rect: " + err); |
| 3029 | return false; |
| 3030 | } |
| 3031 | } |
| 3032 | |
| 3033 | _updateContinuousUpdates() { |
| 3034 | if (!this._enabledContinuousUpdates) { return; } |
| 3035 | |
| 3036 | RFB.messages.enableContinuousUpdates(this._sock, true, 0, 0, |
| 3037 | this._fbWidth, this._fbHeight); |
| 3038 | } |
| 3039 | |
| 3040 | |
| 3041 | _resize(width, height) { |
| 3042 | this._fbWidth = width; |
| 3043 | this._fbHeight = height; |
| 3044 | |
| 3045 | this._display.resize(this._fbWidth, this._fbHeight); |
| 3046 | if (this._display instanceof CageBrowserDisplay && this._rfbConnectionState === 'connected') { |
| 3047 | |
| 3048 | RFB.messages.fbUpdateRequest(this._sock, false, 0, 0, width, height); |
| 3049 | } |
| 3050 | |
| 3051 | |
| 3052 | this._updateClip(); |
| 3053 | this._updateScale(); |
| 3054 | |
| 3055 | this._updateContinuousUpdates(); |
| 3056 | |
| 3057 | |
| 3058 | this._saveExpectedClientSize(); |
| 3059 | } |
| 3060 | |
| 3061 | _xvpOp(ver, op) { |
| 3062 | if (this._rfbXvpVer < ver) { return; } |
| 3063 | Log.Info("Sending XVP operation " + op + " (version " + ver + ")"); |
| 3064 | RFB.messages.xvpOp(this._sock, ver, op); |
| 3065 | } |
| 3066 | |
| 3067 | _updateCursor(rgba, hotx, hoty, w, h) { |
| 3068 | this._cursorImage = { |
| 3069 | rgbaPixels: rgba, |
| 3070 | hotx: hotx, hoty: hoty, w: w, h: h, |
| 3071 | }; |
| 3072 | this._refreshCursor(); |
| 3073 | } |
| 3074 | |
| 3075 | _shouldShowDotCursor() { |
| 3076 | |
| 3077 | if (!this._showDotCursor) { |
| 3078 | |
| 3079 | return false; |
| 3080 | } |
| 3081 | |
| 3082 | |
| 3083 | |
| 3084 | |
| 3085 | |
| 3086 | for (let i = 3; i < this._cursorImage.rgbaPixels.length; i += 4) { |
| 3087 | if (this._cursorImage.rgbaPixels[i]) { |
| 3088 | return false; |
| 3089 | } |
| 3090 | } |
| 3091 | |
| 3092 | |
| 3093 | |
| 3094 | return true; |
| 3095 | } |
| 3096 | |
| 3097 | _refreshCursor() { |
| 3098 | if (this._rfbConnectionState !== "connecting" && |
| 3099 | this._rfbConnectionState !== "connected") { |
| 3100 | return; |
| 3101 | } |
| 3102 | const image = this._shouldShowDotCursor() ? RFB.cursors.dot : this._cursorImage; |
| 3103 | this._cursor.change(image.rgbaPixels, |
| 3104 | image.hotx, image.hoty, |
| 3105 | image.w, image.h |
| 3106 | ); |
| 3107 | } |
| 3108 | |
| 3109 | static genDES(password, challenge) { |
| 3110 | const passwordChars = password.split('').map(c => c.charCodeAt(0)); |
| 3111 | const key = legacyCrypto.importKey( |
| 3112 | "raw", passwordChars, { name: "DES-ECB" }, false, ["encrypt"]); |
| 3113 | return legacyCrypto.encrypt({ name: "DES-ECB" }, key, challenge); |
| 3114 | } |
| 3115 | } |
| 3116 | |
| 3117 | |
| 3118 | RFB.messages = { |
| 3119 | keyEvent(sock, keysym, down) { |
| 3120 | sock.sQpush8(4); |
| 3121 | sock.sQpush8(down); |
| 3122 | |
| 3123 | sock.sQpush16(0); |
| 3124 | |
| 3125 | sock.sQpush32(keysym); |
| 3126 | |
| 3127 | sock.flush(); |
| 3128 | }, |
| 3129 | |
| 3130 | QEMUExtendedKeyEvent(sock, keysym, down, keycode) { |
| 3131 | function getRFBkeycode(xtScanCode) { |
| 3132 | const upperByte = (keycode >> 8); |
| 3133 | const lowerByte = (keycode & 0x00ff); |
| 3134 | if (upperByte === 0xe0 && lowerByte < 0x7f) { |
| 3135 | return lowerByte | 0x80; |
| 3136 | } |
| 3137 | return xtScanCode; |
| 3138 | } |
| 3139 | |
| 3140 | sock.sQpush8(255); |
| 3141 | sock.sQpush8(0); |
| 3142 | |
| 3143 | sock.sQpush16(down); |
| 3144 | |
| 3145 | sock.sQpush32(keysym); |
| 3146 | |
| 3147 | const RFBkeycode = getRFBkeycode(keycode); |
| 3148 | |
| 3149 | sock.sQpush32(RFBkeycode); |
| 3150 | |
| 3151 | sock.flush(); |
| 3152 | }, |
| 3153 | |
| 3154 | pointerEvent(sock, x, y, mask) { |
| 3155 | sock.sQpush8(5); |
| 3156 | |
| 3157 | |
| 3158 | |
| 3159 | |
| 3160 | mask = mask & 0x7f; |
| 3161 | sock.sQpush8(mask); |
| 3162 | |
| 3163 | sock.sQpush16(x); |
| 3164 | sock.sQpush16(y); |
| 3165 | |
| 3166 | sock.flush(); |
| 3167 | }, |
| 3168 | |
| 3169 | extendedPointerEvent(sock, x, y, mask) { |
| 3170 | sock.sQpush8(5); |
| 3171 | |
| 3172 | let higherBits = (mask >> 7) & 0xff; |
| 3173 | |
| 3174 | |
| 3175 | if (higherBits & 0xfc) { |
| 3176 | throw new Error("Invalid mouse button mask: " + mask); |
| 3177 | } |
| 3178 | |
| 3179 | let lowerBits = mask & 0x7f; |
| 3180 | lowerBits |= 0x80; |
| 3181 | |
| 3182 | sock.sQpush8(lowerBits); |
| 3183 | sock.sQpush16(x); |
| 3184 | sock.sQpush16(y); |
| 3185 | sock.sQpush8(higherBits); |
| 3186 | |
| 3187 | sock.flush(); |
| 3188 | }, |
| 3189 | |
| 3190 | |
| 3191 | _buildExtendedClipboardFlags(actions, formats) { |
| 3192 | let data = new Uint8Array(4); |
| 3193 | let formatFlag = 0x00000000; |
| 3194 | let actionFlag = 0x00000000; |
| 3195 | |
| 3196 | for (let i = 0; i < actions.length; i++) { |
| 3197 | actionFlag |= actions[i]; |
| 3198 | } |
| 3199 | |
| 3200 | for (let i = 0; i < formats.length; i++) { |
| 3201 | formatFlag |= formats[i]; |
| 3202 | } |
| 3203 | |
| 3204 | data[0] = actionFlag >> 24; |
| 3205 | data[1] = 0x00; |
| 3206 | data[2] = 0x00; |
| 3207 | data[3] = formatFlag; |
| 3208 | |
| 3209 | return data; |
| 3210 | }, |
| 3211 | |
| 3212 | extendedClipboardProvide(sock, formats, inData) { |
| 3213 | |
| 3214 | let deflator = new Deflator(); |
| 3215 | let dataToDeflate = []; |
| 3216 | |
| 3217 | for (let i = 0; i < formats.length; i++) { |
| 3218 | |
| 3219 | if (formats[i] != extendedClipboardFormatText) { |
| 3220 | throw new Error("Unsupported extended clipboard format for Provide message."); |
| 3221 | } |
| 3222 | |
| 3223 | |
| 3224 | inData[i] = inData[i].replace(/\r\n|\r|\n/gm, "\r\n"); |
| 3225 | |
| 3226 | |
| 3227 | let text = encodeUTF8(inData[i] + "\0"); |
| 3228 | |
| 3229 | dataToDeflate.push( (text.length >> 24) & 0xFF, |
| 3230 | (text.length >> 16) & 0xFF, |
| 3231 | (text.length >> 8) & 0xFF, |
| 3232 | (text.length & 0xFF)); |
| 3233 | |
| 3234 | for (let j = 0; j < text.length; j++) { |
| 3235 | dataToDeflate.push(text.charCodeAt(j)); |
| 3236 | } |
| 3237 | } |
| 3238 | |
| 3239 | let deflatedData = deflator.deflate(new Uint8Array(dataToDeflate)); |
| 3240 | |
| 3241 | |
| 3242 | let data = new Uint8Array(4 + deflatedData.length); |
| 3243 | data.set(RFB.messages._buildExtendedClipboardFlags([extendedClipboardActionProvide], |
| 3244 | formats)); |
| 3245 | data.set(deflatedData, 4); |
| 3246 | |
| 3247 | RFB.messages.clientCutText(sock, data, true); |
| 3248 | }, |
| 3249 | |
| 3250 | extendedClipboardNotify(sock, formats) { |
| 3251 | let flags = RFB.messages._buildExtendedClipboardFlags([extendedClipboardActionNotify], |
| 3252 | formats); |
| 3253 | RFB.messages.clientCutText(sock, flags, true); |
| 3254 | }, |
| 3255 | |
| 3256 | extendedClipboardRequest(sock, formats) { |
| 3257 | let flags = RFB.messages._buildExtendedClipboardFlags([extendedClipboardActionRequest], |
| 3258 | formats); |
| 3259 | RFB.messages.clientCutText(sock, flags, true); |
| 3260 | }, |
| 3261 | |
| 3262 | extendedClipboardCaps(sock, actions, formats) { |
| 3263 | let formatKeys = Object.keys(formats); |
| 3264 | let data = new Uint8Array(4 + (4 * formatKeys.length)); |
| 3265 | |
| 3266 | formatKeys.map(x => parseInt(x)); |
| 3267 | formatKeys.sort((a, b) => a - b); |
| 3268 | |
| 3269 | data.set(RFB.messages._buildExtendedClipboardFlags(actions, [])); |
| 3270 | |
| 3271 | let loopOffset = 4; |
| 3272 | for (let i = 0; i < formatKeys.length; i++) { |
| 3273 | data[loopOffset] = formats[formatKeys[i]] >> 24; |
| 3274 | data[loopOffset + 1] = formats[formatKeys[i]] >> 16; |
| 3275 | data[loopOffset + 2] = formats[formatKeys[i]] >> 8; |
| 3276 | data[loopOffset + 3] = formats[formatKeys[i]] >> 0; |
| 3277 | |
| 3278 | loopOffset += 4; |
| 3279 | data[3] |= (1 << formatKeys[i]); |
| 3280 | } |
| 3281 | |
| 3282 | RFB.messages.clientCutText(sock, data, true); |
| 3283 | }, |
| 3284 | |
| 3285 | clientCutText(sock, data, extended = false) { |
| 3286 | sock.sQpush8(6); |
| 3287 | |
| 3288 | sock.sQpush8(0); |
| 3289 | sock.sQpush8(0); |
| 3290 | sock.sQpush8(0); |
| 3291 | |
| 3292 | let length; |
| 3293 | if (extended) { |
| 3294 | length = toUnsigned32bit(-data.length); |
| 3295 | } else { |
| 3296 | length = data.length; |
| 3297 | } |
| 3298 | |
| 3299 | sock.sQpush32(length); |
| 3300 | sock.sQpushBytes(data); |
| 3301 | sock.flush(); |
| 3302 | }, |
| 3303 | |
| 3304 | setDesktopSize(sock, width, height, id, flags) { |
| 3305 | sock.sQpush8(251); |
| 3306 | |
| 3307 | sock.sQpush8(0); |
| 3308 | |
| 3309 | sock.sQpush16(width); |
| 3310 | sock.sQpush16(height); |
| 3311 | |
| 3312 | sock.sQpush8(1); |
| 3313 | |
| 3314 | sock.sQpush8(0); |
| 3315 | |
| 3316 | |
| 3317 | sock.sQpush32(id); |
| 3318 | sock.sQpush16(0); |
| 3319 | sock.sQpush16(0); |
| 3320 | sock.sQpush16(width); |
| 3321 | sock.sQpush16(height); |
| 3322 | sock.sQpush32(flags); |
| 3323 | |
| 3324 | sock.flush(); |
| 3325 | }, |
| 3326 | |
| 3327 | clientFence(sock, flags, payload) { |
| 3328 | sock.sQpush8(248); |
| 3329 | |
| 3330 | sock.sQpush8(0); |
| 3331 | sock.sQpush8(0); |
| 3332 | sock.sQpush8(0); |
| 3333 | |
| 3334 | sock.sQpush32(flags); |
| 3335 | |
| 3336 | sock.sQpush8(payload.length); |
| 3337 | sock.sQpushString(payload); |
| 3338 | |
| 3339 | sock.flush(); |
| 3340 | }, |
| 3341 | |
| 3342 | enableContinuousUpdates(sock, enable, x, y, width, height) { |
| 3343 | sock.sQpush8(150); |
| 3344 | |
| 3345 | sock.sQpush8(enable); |
| 3346 | |
| 3347 | sock.sQpush16(x); |
| 3348 | sock.sQpush16(y); |
| 3349 | sock.sQpush16(width); |
| 3350 | sock.sQpush16(height); |
| 3351 | |
| 3352 | sock.flush(); |
| 3353 | }, |
| 3354 | |
| 3355 | pixelFormat(sock, depth, trueColor) { |
| 3356 | let bpp; |
| 3357 | |
| 3358 | if (depth > 16) { |
| 3359 | bpp = 32; |
| 3360 | } else if (depth > 8) { |
| 3361 | bpp = 16; |
| 3362 | } else { |
| 3363 | bpp = 8; |
| 3364 | } |
| 3365 | |
| 3366 | const bits = Math.floor(depth/3); |
| 3367 | |
| 3368 | sock.sQpush8(0); |
| 3369 | |
| 3370 | sock.sQpush8(0); |
| 3371 | sock.sQpush8(0); |
| 3372 | sock.sQpush8(0); |
| 3373 | |
| 3374 | sock.sQpush8(bpp); |
| 3375 | sock.sQpush8(depth); |
| 3376 | sock.sQpush8(0); |
| 3377 | sock.sQpush8(trueColor ? 1 : 0); |
| 3378 | |
| 3379 | sock.sQpush16((1 << bits) - 1); |
| 3380 | sock.sQpush16((1 << bits) - 1); |
| 3381 | sock.sQpush16((1 << bits) - 1); |
| 3382 | |
| 3383 | sock.sQpush8(bits * 0); |
| 3384 | sock.sQpush8(bits * 1); |
| 3385 | sock.sQpush8(bits * 2); |
| 3386 | |
| 3387 | sock.sQpush8(0); |
| 3388 | sock.sQpush8(0); |
| 3389 | sock.sQpush8(0); |
| 3390 | |
| 3391 | sock.flush(); |
| 3392 | }, |
| 3393 | |
| 3394 | clientEncodings(sock, encodings) { |
| 3395 | sock.sQpush8(2); |
| 3396 | |
| 3397 | sock.sQpush8(0); |
| 3398 | |
| 3399 | sock.sQpush16(encodings.length); |
| 3400 | for (let i = 0; i < encodings.length; i++) { |
| 3401 | sock.sQpush32(encodings[i]); |
| 3402 | } |
| 3403 | |
| 3404 | sock.flush(); |
| 3405 | }, |
| 3406 | |
| 3407 | fbUpdateRequest(sock, incremental, x, y, w, h) { |
| 3408 | if (typeof(x) === "undefined") { x = 0; } |
| 3409 | if (typeof(y) === "undefined") { y = 0; } |
| 3410 | |
| 3411 | sock.sQpush8(3); |
| 3412 | |
| 3413 | sock.sQpush8(incremental ? 1 : 0); |
| 3414 | |
| 3415 | sock.sQpush16(x); |
| 3416 | sock.sQpush16(y); |
| 3417 | sock.sQpush16(w); |
| 3418 | sock.sQpush16(h); |
| 3419 | |
| 3420 | sock.flush(); |
| 3421 | }, |
| 3422 | |
| 3423 | xvpOp(sock, ver, op) { |
| 3424 | sock.sQpush8(250); |
| 3425 | |
| 3426 | sock.sQpush8(0); |
| 3427 | |
| 3428 | sock.sQpush8(ver); |
| 3429 | sock.sQpush8(op); |
| 3430 | |
| 3431 | sock.flush(); |
| 3432 | } |
| 3433 | }; |
| 3434 | |
| 3435 | RFB.cursors = { |
| 3436 | none: { |
| 3437 | rgbaPixels: new Uint8Array(), |
| 3438 | w: 0, h: 0, |
| 3439 | hotx: 0, hoty: 0, |
| 3440 | }, |
| 3441 | |
| 3442 | dot: { |
| 3443 | |
| 3444 | rgbaPixels: new Uint8Array([ |
| 3445 | 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255, |
| 3446 | 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 255, |
| 3447 | 255, 255, 255, 255, 0, 0, 0, 255, 255, 255, 255, 255, |
| 3448 | ]), |
| 3449 | |
| 3450 | w: 3, h: 3, |
| 3451 | hotx: 1, hoty: 1, |
| 3452 | } |
| 3453 | }; |