rfb.js 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2012 Joel Martin
  4. * Copyright (C) 2013 Samuel Mannehed for Cendio AB
  5. * Licensed under MPL 2.0 (see LICENSE.txt)
  6. *
  7. * See README.md for usage and integration instructions.
  8. *
  9. * TIGHT decoder portion:
  10. * (c) 2012 Michael Tinglof, Joe Balaz, Les Piech (Mercuri.ca)
  11. */
  12. /*jslint white: false, browser: true */
  13. /*global window, Util, Display, Keyboard, Mouse, Websock, Websock_native, Base64, DES */
  14. var RFB;
  15. (function () {
  16. "use strict";
  17. RFB = function (defaults) {
  18. if (!defaults) {
  19. defaults = {};
  20. }
  21. this._rfb_host = '';
  22. this._rfb_port = 5900;
  23. this._rfb_password = '';
  24. this._rfb_path = '';
  25. this._rfb_state = 'disconnected';
  26. this._rfb_version = 0;
  27. this._rfb_max_version = 3.8;
  28. this._rfb_auth_scheme = '';
  29. this._rfb_tightvnc = false;
  30. this._rfb_xvp_ver = 0;
  31. // In preference order
  32. this._encodings = [
  33. ['COPYRECT', 0x01 ],
  34. ['TIGHT', 0x07 ],
  35. ['TIGHT_PNG', -260 ],
  36. ['HEXTILE', 0x05 ],
  37. ['RRE', 0x02 ],
  38. ['RAW', 0x00 ],
  39. ['DesktopSize', -223 ],
  40. ['Cursor', -239 ],
  41. // Psuedo-encoding settings
  42. //['JPEG_quality_lo', -32 ],
  43. ['JPEG_quality_med', -26 ],
  44. //['JPEG_quality_hi', -23 ],
  45. //['compress_lo', -255 ],
  46. ['compress_hi', -247 ],
  47. ['last_rect', -224 ],
  48. ['xvp', -309 ]
  49. ];
  50. this._encHandlers = {};
  51. this._encNames = {};
  52. this._encStats = {};
  53. this._sock = null; // Websock object
  54. this._display = null; // Display object
  55. this._keyboard = null; // Keyboard input handler object
  56. this._mouse = null; // Mouse input handler object
  57. this._sendTimer = null; // Send Queue check timer
  58. this._disconnTimer = null; // disconnection timer
  59. this._msgTimer = null; // queued handle_msg timer
  60. // Frame buffer update state
  61. this._FBU = {
  62. rects: 0,
  63. subrects: 0, // RRE
  64. lines: 0, // RAW
  65. tiles: 0, // HEXTILE
  66. bytes: 0,
  67. x: 0,
  68. y: 0,
  69. width: 0,
  70. height: 0,
  71. encoding: 0,
  72. subencoding: -1,
  73. background: null,
  74. zlib: [] // TIGHT zlib streams
  75. };
  76. this._fb_Bpp = 4;
  77. this._fb_depth = 3;
  78. this._fb_width = 0;
  79. this._fb_height = 0;
  80. this._fb_name = "";
  81. this._rre_chunk_sz = 100;
  82. this._timing = {
  83. last_fbu: 0,
  84. fbu_total: 0,
  85. fbu_total_cnt: 0,
  86. full_fbu_total: 0,
  87. full_fbu_cnt: 0,
  88. fbu_rt_start: 0,
  89. fbu_rt_total: 0,
  90. fbu_rt_cnt: 0,
  91. pixels: 0
  92. };
  93. // Mouse state
  94. this._mouse_buttonMask = 0;
  95. this._mouse_arr = [];
  96. this._viewportDragging = false;
  97. this._viewportDragPos = {};
  98. // set the default value on user-facing properties
  99. Util.set_defaults(this, defaults, {
  100. 'target': 'null', // VNC display rendering Canvas object
  101. 'focusContainer': document, // DOM element that captures keyboard input
  102. 'encrypt': false, // Use TLS/SSL/wss encryption
  103. 'true_color': true, // Request true color pixel data
  104. 'local_cursor': false, // Request locally rendered cursor
  105. 'shared': true, // Request shared mode
  106. 'view_only': false, // Disable client mouse/keyboard
  107. 'xvp_password_sep': '@', // Separator for XVP password fields
  108. 'disconnectTimeout': 3, // Time (s) to wait for disconnection
  109. 'wsProtocols': ['binary', 'base64'], // Protocols to use in the WebSocket connection
  110. 'repeaterID': '', // [UltraVNC] RepeaterID to connect to
  111. 'viewportDrag': false, // Move the viewport on mouse drags
  112. // Callback functions
  113. 'onUpdateState': function () { }, // onUpdateState(rfb, state, oldstate, statusMsg): state update/change
  114. 'onPasswordRequired': function () { }, // onPasswordRequired(rfb): VNC password is required
  115. 'onClipboard': function () { }, // onClipboard(rfb, text): RFB clipboard contents received
  116. 'onBell': function () { }, // onBell(rfb): RFB Bell message received
  117. 'onFBUReceive': function () { }, // onFBUReceive(rfb, fbu): RFB FBU received but not yet processed
  118. 'onFBUComplete': function () { }, // onFBUComplete(rfb, fbu): RFB FBU received and processed
  119. 'onFBResize': function () { }, // onFBResize(rfb, width, height): frame buffer resized
  120. 'onDesktopName': function () { }, // onDesktopName(rfb, name): desktop name received
  121. 'onXvpInit': function () { }, // onXvpInit(version): XVP extensions active for this connection
  122. });
  123. // main setup
  124. Util.Debug(">> RFB.constructor");
  125. // populate encHandlers with bound versions
  126. Object.keys(RFB.encodingHandlers).forEach(function (encName) {
  127. this._encHandlers[encName] = RFB.encodingHandlers[encName].bind(this);
  128. }.bind(this));
  129. // Create lookup tables based on encoding number
  130. for (var i = 0; i < this._encodings.length; i++) {
  131. this._encHandlers[this._encodings[i][1]] = this._encHandlers[this._encodings[i][0]];
  132. this._encNames[this._encodings[i][1]] = this._encodings[i][0];
  133. this._encStats[this._encodings[i][1]] = [0, 0];
  134. }
  135. try {
  136. this._display = new Display({target: this._target});
  137. } catch (exc) {
  138. Util.Error("Display exception: " + exc);
  139. this._updateState('fatal', "No working Display");
  140. }
  141. this._keyboard = new Keyboard({target: this._focusContainer,
  142. onKeyPress: this._handleKeyPress.bind(this)});
  143. this._mouse = new Mouse({target: this._target,
  144. onMouseButton: this._handleMouseButton.bind(this),
  145. onMouseMove: this._handleMouseMove.bind(this),
  146. notify: this._keyboard.sync.bind(this._keyboard)});
  147. this._sock = new Websock();
  148. this._sock.on('message', this._handle_message.bind(this));
  149. this._sock.on('open', function () {
  150. if (this._rfb_state === 'connect') {
  151. this._updateState('ProtocolVersion', "Starting VNC handshake");
  152. } else {
  153. this._fail("Got unexpected WebSocket connection");
  154. }
  155. }.bind(this));
  156. this._sock.on('close', function (e) {
  157. Util.Warn("WebSocket on-close event");
  158. var msg = "";
  159. if (e.code) {
  160. msg = " (code: " + e.code;
  161. if (e.reason) {
  162. msg += ", reason: " + e.reason;
  163. }
  164. msg += ")";
  165. }
  166. if (this._rfb_state === 'disconnect') {
  167. this._updateState('disconnected', 'VNC disconnected' + msg);
  168. } else if (this._rfb_state === 'ProtocolVersion') {
  169. this._fail('Failed to connect to server' + msg);
  170. } else if (this._rfb_state in {'failed': 1, 'disconnected': 1}) {
  171. Util.Error("Received onclose while disconnected" + msg);
  172. } else {
  173. this._fail("Server disconnected" + msg);
  174. }
  175. }.bind(this));
  176. this._sock.on('error', function (e) {
  177. Util.Warn("WebSocket on-error event");
  178. });
  179. this._init_vars();
  180. var rmode = this._display.get_render_mode();
  181. if (Websock_native) {
  182. Util.Info("Using native WebSockets");
  183. this._updateState('loaded', 'noVNC ready: native WebSockets, ' + rmode);
  184. } else {
  185. Util.Warn("Using web-socket-js bridge. Flash version: " + Util.Flash.version);
  186. if (!Util.Flash || Util.Flash.version < 9) {
  187. this._updateState('fatal', "WebSockets or <a href='http://get.adobe.com/flashplayer'>Adobe Flash</a> is required");
  188. } else if (document.location.href.substr(0, 7) === 'file://') {
  189. this._updateState('fatal', "'file://' URL is incompatible with Adobe Flash");
  190. } else {
  191. this._updateState('loaded', 'noVNC ready: WebSockets emulation, ' + rmode);
  192. }
  193. }
  194. Util.Debug("<< RFB.constructor");
  195. };
  196. RFB.prototype = {
  197. // Public methods
  198. connect: function (host, port, password, path) {
  199. this._rfb_host = host;
  200. this._rfb_port = port;
  201. this._rfb_password = (password !== undefined) ? password : "";
  202. this._rfb_path = (path !== undefined) ? path : "";
  203. if (!this._rfb_host || !this._rfb_port) {
  204. return this._fail("Must set host and port");
  205. }
  206. this._updateState('connect');
  207. },
  208. disconnect: function () {
  209. this._updateState('disconnect', 'Disconnecting');
  210. },
  211. sendPassword: function (passwd) {
  212. this._rfb_password = passwd;
  213. this._rfb_state = 'Authentication';
  214. setTimeout(this._init_msg.bind(this), 1);
  215. },
  216. sendCtrlAltDel: function () {
  217. if (this._rfb_state !== 'normal' || this._view_only) { return false; }
  218. Util.Info("Sending Ctrl-Alt-Del");
  219. var arr = [];
  220. arr = arr.concat(RFB.messages.keyEvent(XK_Control_L, 1));
  221. arr = arr.concat(RFB.messages.keyEvent(XK_Alt_L, 1));
  222. arr = arr.concat(RFB.messages.keyEvent(XK_Delete, 1));
  223. arr = arr.concat(RFB.messages.keyEvent(XK_Delete, 0));
  224. arr = arr.concat(RFB.messages.keyEvent(XK_Alt_L, 0));
  225. arr = arr.concat(RFB.messages.keyEvent(XK_Control_L, 0));
  226. this._sock.send(arr);
  227. },
  228. xvpOp: function (ver, op) {
  229. if (this._rfb_xvp_ver < ver) { return false; }
  230. Util.Info("Sending XVP operation " + op + " (version " + ver + ")");
  231. this._sock.send_string("\xFA\x00" + String.fromCharCode(ver) + String.fromCharCode(op));
  232. return true;
  233. },
  234. xvpShutdown: function () {
  235. return this.xvpOp(1, 2);
  236. },
  237. xvpReboot: function () {
  238. return this.xvpOp(1, 3);
  239. },
  240. xvpReset: function () {
  241. return this.xvpOp(1, 4);
  242. },
  243. // Send a key press. If 'down' is not specified then send a down key
  244. // followed by an up key.
  245. sendKey: function (code, down) {
  246. if (this._rfb_state !== "normal" || this._view_only) { return false; }
  247. var arr = [];
  248. if (typeof down !== 'undefined') {
  249. Util.Info("Sending key code (" + (down ? "down" : "up") + "): " + code);
  250. arr = arr.concat(RFB.messages.keyEvent(code, down ? 1 : 0));
  251. } else {
  252. Util.Info("Sending key code (down + up): " + code);
  253. arr = arr.concat(RFB.messages.keyEvent(code, 1));
  254. arr = arr.concat(RFB.messages.keyEvent(code, 0));
  255. }
  256. this._sock.send(arr);
  257. },
  258. clipboardPasteFrom: function (text) {
  259. if (this._rfb_state !== 'normal') { return; }
  260. this._sock.send(RFB.messages.clientCutText(text));
  261. },
  262. // Private methods
  263. _connect: function () {
  264. Util.Debug(">> RFB.connect");
  265. var uri;
  266. if (typeof UsingSocketIO !== 'undefined') {
  267. uri = 'http';
  268. } else {
  269. uri = this._encrypt ? 'wss' : 'ws';
  270. }
  271. uri += '://' + this._rfb_host + ':' + this._rfb_port + '/' + this._rfb_path;
  272. Util.Info("connecting to " + uri);
  273. this._sock.open(uri, this._wsProtocols);
  274. Util.Debug("<< RFB.connect");
  275. },
  276. _init_vars: function () {
  277. // reset state
  278. this._sock.init();
  279. this._FBU.rects = 0;
  280. this._FBU.subrects = 0; // RRE and HEXTILE
  281. this._FBU.lines = 0; // RAW
  282. this._FBU.tiles = 0; // HEXTILE
  283. this._FBU.zlibs = []; // TIGHT zlib encoders
  284. this._mouse_buttonMask = 0;
  285. this._mouse_arr = [];
  286. this._rfb_tightvnc = false;
  287. // Clear the per connection encoding stats
  288. var i;
  289. for (i = 0; i < this._encodings.length; i++) {
  290. this._encStats[this._encodings[i][1]][0] = 0;
  291. }
  292. for (i = 0; i < 4; i++) {
  293. this._FBU.zlibs[i] = new TINF();
  294. this._FBU.zlibs[i].init();
  295. }
  296. },
  297. _print_stats: function () {
  298. Util.Info("Encoding stats for this connection:");
  299. var i, s;
  300. for (i = 0; i < this._encodings.length; i++) {
  301. s = this._encStats[this._encodings[i][1]];
  302. if (s[0] + s[1] > 0) {
  303. Util.Info(" " + this._encodings[i][0] + ": " + s[0] + " rects");
  304. }
  305. }
  306. Util.Info("Encoding stats since page load:");
  307. for (i = 0; i < this._encodings.length; i++) {
  308. s = this._encStats[this._encodings[i][1]];
  309. Util.Info(" " + this._encodings[i][0] + ": " + s[1] + " rects");
  310. }
  311. },
  312. /*
  313. * Page states:
  314. * loaded - page load, equivalent to disconnected
  315. * disconnected - idle state
  316. * connect - starting to connect (to ProtocolVersion)
  317. * normal - connected
  318. * disconnect - starting to disconnect
  319. * failed - abnormal disconnect
  320. * fatal - failed to load page, or fatal error
  321. *
  322. * RFB protocol initialization states:
  323. * ProtocolVersion
  324. * Security
  325. * Authentication
  326. * password - waiting for password, not part of RFB
  327. * SecurityResult
  328. * ClientInitialization - not triggered by server message
  329. * ServerInitialization (to normal)
  330. */
  331. _updateState: function (state, statusMsg) {
  332. var oldstate = this._rfb_state;
  333. if (state === oldstate) {
  334. // Already here, ignore
  335. Util.Debug("Already in state '" + state + "', ignoring");
  336. }
  337. /*
  338. * These are disconnected states. A previous connect may
  339. * asynchronously cause a connection so make sure we are closed.
  340. */
  341. if (state in {'disconnected': 1, 'loaded': 1, 'connect': 1,
  342. 'disconnect': 1, 'failed': 1, 'fatal': 1}) {
  343. if (this._sendTimer) {
  344. clearInterval(this._sendTimer);
  345. this._sendTimer = null;
  346. }
  347. if (this._msgTimer) {
  348. clearInterval(this._msgTimer);
  349. this._msgTimer = null;
  350. }
  351. if (this._display && this._display.get_context()) {
  352. this._keyboard.ungrab();
  353. this._mouse.ungrab();
  354. if (state !== 'connect' && state !== 'loaded') {
  355. this._display.defaultCursor();
  356. }
  357. if (Util.get_logging() !== 'debug' || state === 'loaded') {
  358. // Show noVNC logo on load and when disconnected, unless in
  359. // debug mode
  360. this._display.clear();
  361. }
  362. }
  363. this._sock.close();
  364. }
  365. if (oldstate === 'fatal') {
  366. Util.Error('Fatal error, cannot continue');
  367. }
  368. var cmsg = typeof(statusMsg) !== 'undefined' ? (" Msg: " + statusMsg) : "";
  369. var fullmsg = "New state '" + state + "', was '" + oldstate + "'." + cmsg;
  370. if (state === 'failed' || state === 'fatal') {
  371. Util.Error(cmsg);
  372. } else {
  373. Util.Warn(cmsg);
  374. }
  375. if (oldstate === 'failed' && state === 'disconnected') {
  376. // do disconnect action, but stay in failed state
  377. this._rfb_state = 'failed';
  378. } else {
  379. this._rfb_state = state;
  380. }
  381. if (this._disconnTimer && this._rfb_state !== 'disconnect') {
  382. Util.Debug("Clearing disconnect timer");
  383. clearTimeout(this._disconnTimer);
  384. this._disconnTimer = null;
  385. }
  386. switch (state) {
  387. case 'normal':
  388. if (oldstate === 'disconnected' || oldstate === 'failed') {
  389. Util.Error("Invalid transition from 'disconnected' or 'failed' to 'normal'");
  390. }
  391. break;
  392. case 'connect':
  393. this._init_vars();
  394. this._connect();
  395. // WebSocket.onopen transitions to 'ProtocolVersion'
  396. break;
  397. case 'disconnect':
  398. this._disconnTimer = setTimeout(function () {
  399. this._fail("Disconnect timeout");
  400. }.bind(this), this._disconnectTimeout * 1000);
  401. this._print_stats();
  402. // WebSocket.onclose transitions to 'disconnected'
  403. break;
  404. case 'failed':
  405. if (oldstate === 'disconnected') {
  406. Util.Error("Invalid transition from 'disconnected' to 'failed'");
  407. } else if (oldstate === 'normal') {
  408. Util.Error("Error while connected.");
  409. } else if (oldstate === 'init') {
  410. Util.Error("Error while initializing.");
  411. }
  412. // Make sure we transition to disconnected
  413. setTimeout(function () {
  414. this._updateState('disconnected');
  415. }.bind(this), 50);
  416. break;
  417. default:
  418. // No state change action to take
  419. }
  420. if (oldstate === 'failed' && state === 'disconnected') {
  421. this._onUpdateState(this, state, oldstate);
  422. } else {
  423. this._onUpdateState(this, state, oldstate, statusMsg);
  424. }
  425. },
  426. _fail: function (msg) {
  427. this._updateState('failed', msg);
  428. return false;
  429. },
  430. _handle_message: function () {
  431. if (this._sock.rQlen() === 0) {
  432. Util.Warn("handle_message called on an empty receive queue");
  433. return;
  434. }
  435. switch (this._rfb_state) {
  436. case 'disconnected':
  437. case 'failed':
  438. Util.Error("Got data while disconnected");
  439. break;
  440. case 'normal':
  441. if (this._normal_msg() && this._sock.rQlen() > 0) {
  442. // true means we can continue processing
  443. // Give other events a chance to run
  444. if (this._msgTimer === null) {
  445. Util.Debug("More data to process, creating timer");
  446. this._msgTimer = setTimeout(function () {
  447. this._msgTimer = null;
  448. this._handle_message();
  449. }.bind(this), 10);
  450. } else {
  451. Util.Debug("More data to process, existing timer");
  452. }
  453. }
  454. break;
  455. default:
  456. this._init_msg();
  457. break;
  458. }
  459. },
  460. _checkEvents: function () {
  461. if (this._rfb_state === 'normal' && !this._viewportDragging && this._mouse_arr.length > 0) {
  462. this._sock.send(this._mouse_arr);
  463. this._mouse_arr = [];
  464. }
  465. },
  466. _handleKeyPress: function (keysym, down) {
  467. if (this._view_only) { return; } // View only, skip keyboard, events
  468. this._sock.send(RFB.messages.keyEvent(keysym, down));
  469. },
  470. _handleMouseButton: function (x, y, down, bmask) {
  471. if (down) {
  472. this._mouse_buttonMask |= bmask;
  473. } else {
  474. this._mouse_buttonMask ^= bmask;
  475. }
  476. if (this._viewportDrag) {
  477. if (down && !this._viewportDragging) {
  478. this._viewportDragging = true;
  479. this._viewportDragPos = {'x': x, 'y': y};
  480. // Skip sending mouse events
  481. return;
  482. } else {
  483. this._viewportDragging = false;
  484. }
  485. }
  486. if (this._view_only) { return; } // View only, skip mouse events
  487. this._mouse_arr = this._mouse_arr.concat(
  488. RFB.messages.pointerEvent(this._display.absX(x), this._display.absY(y), this._mouse_buttonMask));
  489. this._sock.send(this._mouse_arr);
  490. this._mouse_arr = [];
  491. },
  492. _handleMouseMove: function (x, y) {
  493. if (this._viewportDragging) {
  494. var deltaX = this._viewportDragPos.x - x;
  495. var deltaY = this._viewportDragPos.y - y;
  496. this._viewportDragPos = {'x': x, 'y': y};
  497. this._display.viewportChange(deltaX, deltaY);
  498. // Skip sending mouse events
  499. return;
  500. }
  501. if (this._view_only) { return; } // View only, skip mouse events
  502. this._mouse_arr = this._mouse_arr.concat(
  503. RFB.messages.pointerEvent(this._display.absX(x), this._display.absY(y), this._mouse_buttonMask));
  504. this._checkEvents();
  505. },
  506. // Message Handlers
  507. _negotiate_protocol_version: function () {
  508. if (this._sock.rQlen() < 12) {
  509. return this._fail("Incomplete protocol version");
  510. }
  511. var sversion = this._sock.rQshiftStr(12).substr(4, 7);
  512. Util.Info("Server ProtocolVersion: " + sversion);
  513. var is_repeater = 0;
  514. switch (sversion) {
  515. case "000.000": // UltraVNC repeater
  516. is_repeater = 1;
  517. break;
  518. case "003.003":
  519. case "003.006": // UltraVNC
  520. case "003.889": // Apple Remote Desktop
  521. this._rfb_version = 3.3;
  522. break;
  523. case "003.007":
  524. this._rfb_version = 3.7;
  525. break;
  526. case "003.008":
  527. case "004.000": // Intel AMT KVM
  528. case "004.001": // RealVNC 4.6
  529. this._rfb_version = 3.8;
  530. break;
  531. default:
  532. return this._fail("Invalid server version " + sversion);
  533. }
  534. if (is_repeater) {
  535. var repeaterID = this._repeaterID;
  536. while (repeaterID.length < 250) {
  537. repeaterID += "\0";
  538. }
  539. this._sock.send_string(repeaterID);
  540. return true;
  541. }
  542. if (this._rfb_version > this._rfb_max_version) {
  543. this._rfb_version = this._rfb_max_version;
  544. }
  545. // Send updates either at a rate of 1 update per 50ms, or
  546. // whatever slower rate the network can handle
  547. this._sendTimer = setInterval(this._sock.flush.bind(this._sock), 50);
  548. var cversion = "00" + parseInt(this._rfb_version, 10) +
  549. ".00" + ((this._rfb_version * 10) % 10);
  550. this._sock.send_string("RFB " + cversion + "\n");
  551. this._updateState('Security', 'Sent ProtocolVersion: ' + cversion);
  552. },
  553. _negotiate_security: function () {
  554. if (this._rfb_version >= 3.7) {
  555. // Server sends supported list, client decides
  556. var num_types = this._sock.rQshift8();
  557. if (this._sock.rQwait("security type", num_types, 1)) { return false; }
  558. if (num_types === 0) {
  559. var strlen = this._sock.rQshift32();
  560. var reason = this._sock.rQshiftStr(strlen);
  561. return this._fail("Security failure: " + reason);
  562. }
  563. this._rfb_auth_scheme = 0;
  564. var types = this._sock.rQshiftBytes(num_types);
  565. Util.Debug("Server security types: " + types);
  566. for (var i = 0; i < types.length; i++) {
  567. if (types[i] > this._rfb_auth_scheme && (types[i] <= 16 || types[i] == 22)) {
  568. this._rfb_auth_scheme = types[i];
  569. }
  570. }
  571. if (this._rfb_auth_scheme === 0) {
  572. return this._fail("Unsupported security types: " + types);
  573. }
  574. this._sock.send([this._rfb_auth_scheme]);
  575. } else {
  576. // Server decides
  577. if (this._sock.rQwait("security scheme", 4)) { return false; }
  578. this._rfb_auth_scheme = this._sock.rQshift32();
  579. }
  580. this._updateState('Authentication', 'Authenticating using scheme: ' + this._rfb_auth_scheme);
  581. return this._init_msg(); // jump to authentication
  582. },
  583. // authentication
  584. _negotiate_xvp_auth: function () {
  585. var xvp_sep = this._xvp_password_sep;
  586. var xvp_auth = this._rfb_password.split(xvp_sep);
  587. if (xvp_auth.length < 3) {
  588. this._updateState('password', 'XVP credentials required (user' + xvp_sep +
  589. 'target' + xvp_sep + 'password) -- got only ' + this._rfb_password);
  590. this._onPasswordRequired(this);
  591. return false;
  592. }
  593. var xvp_auth_str = String.fromCharCode(xvp_auth[0].length) +
  594. String.fromCharCode(xvp_auth[1].length) +
  595. xvp_auth[0] +
  596. xvp_auth[1];
  597. this._sock.send_string(xvp_auth_str);
  598. this._rfb_password = xvp_auth.slice(2).join(xvp_sep);
  599. this._rfb_auth_scheme = 2;
  600. return this._negotiate_authentication();
  601. },
  602. _negotiate_std_vnc_auth: function () {
  603. if (this._rfb_password.length === 0) {
  604. // Notify via both callbacks since it's kind of
  605. // an RFB state change and a UI interface issue
  606. this._updateState('password', "Password Required");
  607. this._onPasswordRequired(this);
  608. }
  609. if (this._sock.rQwait("auth challenge", 16)) { return false; }
  610. var challenge = this._sock.rQshiftBytes(16);
  611. var response = RFB.genDES(this._rfb_password, challenge);
  612. this._sock.send(response);
  613. this._updateState("SecurityResult");
  614. return true;
  615. },
  616. _negotiate_tight_tunnels: function (numTunnels) {
  617. var clientSupportedTunnelTypes = {
  618. 0: { vendor: 'TGHT', signature: 'NOTUNNEL' }
  619. };
  620. var serverSupportedTunnelTypes = {};
  621. // receive tunnel capabilities
  622. for (var i = 0; i < numTunnels; i++) {
  623. var cap_code = this._sock.rQshift32();
  624. var cap_vendor = this._sock.rQshiftStr(4);
  625. var cap_signature = this._sock.rQshiftStr(8);
  626. serverSupportedTunnelTypes[cap_code] = { vendor: cap_vendor, signature: cap_signature };
  627. }
  628. // choose the notunnel type
  629. if (serverSupportedTunnelTypes[0]) {
  630. if (serverSupportedTunnelTypes[0].vendor != clientSupportedTunnelTypes[0].vendor ||
  631. serverSupportedTunnelTypes[0].signature != clientSupportedTunnelTypes[0].signature) {
  632. return this._fail("Client's tunnel type had the incorrect vendor or signature");
  633. }
  634. this._sock.send([0, 0, 0, 0]); // use NOTUNNEL
  635. return false; // wait until we receive the sub auth count to continue
  636. } else {
  637. return this._fail("Server wanted tunnels, but doesn't support the notunnel type");
  638. }
  639. },
  640. _negotiate_tight_auth: function () {
  641. if (!this._rfb_tightvnc) { // first pass, do the tunnel negotiation
  642. if (this._sock.rQwait("num tunnels", 4)) { return false; }
  643. var numTunnels = this._sock.rQshift32();
  644. if (numTunnels > 0 && this._sock.rQwait("tunnel capabilities", 16 * numTunnels, 4)) { return false; }
  645. this._rfb_tightvnc = true;
  646. if (numTunnels > 0) {
  647. this._negotiate_tight_tunnels(numTunnels);
  648. return false; // wait until we receive the sub auth to continue
  649. }
  650. }
  651. // second pass, do the sub-auth negotiation
  652. if (this._sock.rQwait("sub auth count", 4)) { return false; }
  653. var subAuthCount = this._sock.rQshift32();
  654. if (this._sock.rQwait("sub auth capabilities", 16 * subAuthCount, 4)) { return false; }
  655. var clientSupportedTypes = {
  656. 'STDVNOAUTH__': 1,
  657. 'STDVVNCAUTH_': 2
  658. };
  659. var serverSupportedTypes = [];
  660. for (var i = 0; i < subAuthCount; i++) {
  661. var capNum = this._sock.rQshift32();
  662. var capabilities = this._sock.rQshiftStr(12);
  663. serverSupportedTypes.push(capabilities);
  664. }
  665. for (var authType in clientSupportedTypes) {
  666. if (serverSupportedTypes.indexOf(authType) != -1) {
  667. this._sock.send([0, 0, 0, clientSupportedTypes[authType]]);
  668. switch (authType) {
  669. case 'STDVNOAUTH__': // no auth
  670. this._updateState('SecurityResult');
  671. return true;
  672. case 'STDVVNCAUTH_': // VNC auth
  673. this._rfb_auth_scheme = 2;
  674. return this._init_msg();
  675. default:
  676. return this._fail("Unsupported tiny auth scheme: " + authType);
  677. }
  678. }
  679. }
  680. this._fail("No supported sub-auth types!");
  681. },
  682. _negotiate_authentication: function () {
  683. switch (this._rfb_auth_scheme) {
  684. case 0: // connection failed
  685. if (this._sock.rQwait("auth reason", 4)) { return false; }
  686. var strlen = this._sock.rQshift32();
  687. var reason = this._sock.rQshiftStr(strlen);
  688. return this._fail("Auth failure: " + reason);
  689. case 1: // no auth
  690. if (this._rfb_version >= 3.8) {
  691. this._updateState('SecurityResult');
  692. return true;
  693. }
  694. this._updateState('ClientInitialisation', "No auth required");
  695. return this._init_msg();
  696. case 22: // XVP auth
  697. return this._negotiate_xvp_auth();
  698. case 2: // VNC authentication
  699. return this._negotiate_std_vnc_auth();
  700. case 16: // TightVNC Security Type
  701. return this._negotiate_tight_auth();
  702. default:
  703. return this._fail("Unsupported auth scheme: " + this._rfb_auth_scheme);
  704. }
  705. },
  706. _handle_security_result: function () {
  707. if (this._sock.rQwait('VNC auth response ', 4)) { return false; }
  708. switch (this._sock.rQshift32()) {
  709. case 0: // OK
  710. this._updateState('ClientInitialisation', 'Authentication OK');
  711. return this._init_msg();
  712. case 1: // failed
  713. if (this._rfb_version >= 3.8) {
  714. var length = this._sock.rQshift32();
  715. if (this._sock.rQwait("SecurityResult reason", length, 8)) { return false; }
  716. var reason = this._sock.rQshiftStr(length);
  717. return this._fail(reason);
  718. } else {
  719. return this._fail("Authentication failure");
  720. }
  721. return false;
  722. case 2:
  723. return this._fail("Too many auth attempts");
  724. }
  725. },
  726. _negotiate_server_init: function () {
  727. if (this._sock.rQwait("server initialization", 24)) { return false; }
  728. /* Screen size */
  729. this._fb_width = this._sock.rQshift16();
  730. this._fb_height = this._sock.rQshift16();
  731. /* PIXEL_FORMAT */
  732. var bpp = this._sock.rQshift8();
  733. var depth = this._sock.rQshift8();
  734. var big_endian = this._sock.rQshift8();
  735. var true_color = this._sock.rQshift8();
  736. var red_max = this._sock.rQshift16();
  737. var green_max = this._sock.rQshift16();
  738. var blue_max = this._sock.rQshift16();
  739. var red_shift = this._sock.rQshift8();
  740. var green_shift = this._sock.rQshift8();
  741. var blue_shift = this._sock.rQshift8();
  742. this._sock.rQskipBytes(3); // padding
  743. // NB(directxman12): we don't want to call any callbacks or print messages until
  744. // *after* we're past the point where we could backtrack
  745. /* Connection name/title */
  746. var name_length = this._sock.rQshift32();
  747. if (this._sock.rQwait('server init name', name_length, 24)) { return false; }
  748. this._fb_name = Util.decodeUTF8(this._sock.rQshiftStr(name_length));
  749. if (this._rfb_tightvnc) {
  750. if (this._sock.rQwait('TightVNC extended server init header', 8, 24 + name_length)) { return false; }
  751. // In TightVNC mode, ServerInit message is extended
  752. var numServerMessages = this._sock.rQshift16();
  753. var numClientMessages = this._sock.rQshift16();
  754. var numEncodings = this._sock.rQshift16();
  755. this._sock.rQskipBytes(2); // padding
  756. var totalMessagesLength = (numServerMessages + numClientMessages + numEncodings) * 16;
  757. if (this._sock.rQwait('TightVNC extended server init header', totalMessagesLength, 32 + name_length)) { return false; }
  758. var i;
  759. for (i = 0; i < numServerMessages; i++) {
  760. var srvMsg = this._sock.rQshiftStr(16);
  761. }
  762. for (i = 0; i < numClientMessages; i++) {
  763. var clientMsg = this._sock.rQshiftStr(16);
  764. }
  765. for (i = 0; i < numEncodings; i++) {
  766. var encoding = this._sock.rQshiftStr(16);
  767. }
  768. }
  769. // NB(directxman12): these are down here so that we don't run them multiple times
  770. // if we backtrack
  771. Util.Info("Screen: " + this._fb_width + "x" + this._fb_height +
  772. ", bpp: " + bpp + ", depth: " + depth +
  773. ", big_endian: " + big_endian +
  774. ", true_color: " + true_color +
  775. ", red_max: " + red_max +
  776. ", green_max: " + green_max +
  777. ", blue_max: " + blue_max +
  778. ", red_shift: " + red_shift +
  779. ", green_shift: " + green_shift +
  780. ", blue_shift: " + blue_shift);
  781. if (big_endian !== 0) {
  782. Util.Warn("Server native endian is not little endian");
  783. }
  784. if (red_shift !== 16) {
  785. Util.Warn("Server native red-shift is not 16");
  786. }
  787. if (blue_shift !== 0) {
  788. Util.Warn("Server native blue-shift is not 0");
  789. }
  790. // we're past the point where we could backtrack, so it's safe to call this
  791. this._onDesktopName(this, this._fb_name);
  792. if (this._true_color && this._fb_name === "Intel(r) AMT KVM") {
  793. Util.Warn("Intel AMT KVM only supports 8/16 bit depths. Disabling true color");
  794. this._true_color = false;
  795. }
  796. this._display.set_true_color(this._true_color);
  797. this._onFBResize(this, this._fb_width, this._fb_height);
  798. this._display.resize(this._fb_width, this._fb_height);
  799. this._keyboard.grab();
  800. this._mouse.grab();
  801. if (this._true_color) {
  802. this._fb_Bpp = 4;
  803. this._fb_depth = 3;
  804. } else {
  805. this._fb_Bpp = 1;
  806. this._fb_depth = 1;
  807. }
  808. var response = RFB.messages.pixelFormat(this._fb_Bpp, this._fb_depth, this._true_color);
  809. response = response.concat(
  810. RFB.messages.clientEncodings(this._encodings, this._local_cursor, this._true_color));
  811. response = response.concat(
  812. RFB.messages.fbUpdateRequests(this._display.getCleanDirtyReset(),
  813. this._fb_width, this._fb_height));
  814. this._timing.fbu_rt_start = (new Date()).getTime();
  815. this._timing.pixels = 0;
  816. this._sock.send(response);
  817. this._checkEvents();
  818. if (this._encrypt) {
  819. this._updateState('normal', 'Connected (encrypted) to: ' + this._fb_name);
  820. } else {
  821. this._updateState('normal', 'Connected (unencrypted) to: ' + this._fb_name);
  822. }
  823. },
  824. _init_msg: function () {
  825. switch (this._rfb_state) {
  826. case 'ProtocolVersion':
  827. return this._negotiate_protocol_version();
  828. case 'Security':
  829. return this._negotiate_security();
  830. case 'Authentication':
  831. return this._negotiate_authentication();
  832. case 'SecurityResult':
  833. return this._handle_security_result();
  834. case 'ClientInitialisation':
  835. this._sock.send([this._shared ? 1 : 0]); // ClientInitialisation
  836. this._updateState('ServerInitialisation', "Authentication OK");
  837. return true;
  838. case 'ServerInitialisation':
  839. return this._negotiate_server_init();
  840. }
  841. },
  842. _handle_set_colour_map_msg: function () {
  843. Util.Debug("SetColorMapEntries");
  844. this._sock.rQskip8(); // Padding
  845. var first_colour = this._sock.rQshift16();
  846. var num_colours = this._sock.rQshift16();
  847. if (this._sock.rQwait('SetColorMapEntries', num_colours * 6, 6)) { return false; }
  848. for (var c = 0; c < num_colours; c++) {
  849. var red = parseInt(this._sock.rQshift16() / 256, 10);
  850. var green = parseInt(this._sock.rQshift16() / 256, 10);
  851. var blue = parseInt(this._sock.rQshift16() / 256, 10);
  852. this._display.set_colourMap([blue, green, red], first_colour + c);
  853. }
  854. Util.Debug("colourMap: " + this._display.get_colourMap());
  855. Util.Info("Registered " + num_colours + " colourMap entries");
  856. return true;
  857. },
  858. _handle_server_cut_text: function () {
  859. Util.Debug("ServerCutText");
  860. if (this._sock.rQwait("ServerCutText header", 7, 1)) { return false; }
  861. this._sock.rQskipBytes(3); // Padding
  862. var length = this._sock.rQshift32();
  863. if (this._sock.rQwait("ServerCutText", length, 8)) { return false; }
  864. var text = this._sock.rQshiftStr(length);
  865. this._onClipboard(this, text);
  866. return true;
  867. },
  868. _handle_xvp_msg: function () {
  869. if (this._sock.rQwait("XVP version and message", 3, 1)) { return false; }
  870. this._sock.rQskip8(); // Padding
  871. var xvp_ver = this._sock.rQshift8();
  872. var xvp_msg = this._sock.rQshift8();
  873. switch (xvp_msg) {
  874. case 0: // XVP_FAIL
  875. this._updateState(this._rfb_state, "Operation Failed");
  876. break;
  877. case 1: // XVP_INIT
  878. this._rfb_xvp_ver = xvp_ver;
  879. Util.Info("XVP extensions enabled (version " + this._rfb_xvp_ver + ")");
  880. this._onXvpInit(this._rfb_xvp_ver);
  881. break;
  882. default:
  883. this._fail("Disconnected: illegal server XVP message " + xvp_msg);
  884. break;
  885. }
  886. return true;
  887. },
  888. _normal_msg: function () {
  889. var msg_type;
  890. if (this._FBU.rects > 0) {
  891. msg_type = 0;
  892. } else {
  893. msg_type = this._sock.rQshift8();
  894. }
  895. switch (msg_type) {
  896. case 0: // FramebufferUpdate
  897. var ret = this._framebufferUpdate();
  898. if (ret) {
  899. this._sock.send(RFB.messages.fbUpdateRequests(this._display.getCleanDirtyReset(),
  900. this._fb_width, this._fb_height));
  901. }
  902. return ret;
  903. case 1: // SetColorMapEntries
  904. return this._handle_set_colour_map_msg();
  905. case 2: // Bell
  906. Util.Debug("Bell");
  907. this._onBell(this);
  908. return true;
  909. case 3: // ServerCutText
  910. return this._handle_server_cut_text();
  911. case 250: // XVP
  912. return this._handle_xvp_msg();
  913. default:
  914. this._fail("Disconnected: illegal server message type " + msg_type);
  915. Util.Debug("sock.rQslice(0, 30): " + this._sock.rQslice(0, 30));
  916. return true;
  917. }
  918. },
  919. _framebufferUpdate: function () {
  920. var ret = true;
  921. var now;
  922. if (this._FBU.rects === 0) {
  923. if (this._sock.rQwait("FBU header", 3, 1)) { return false; }
  924. this._sock.rQskip8(); // Padding
  925. this._FBU.rects = this._sock.rQshift16();
  926. this._FBU.bytes = 0;
  927. this._timing.cur_fbu = 0;
  928. if (this._timing.fbu_rt_start > 0) {
  929. now = (new Date()).getTime();
  930. Util.Info("First FBU latency: " + (now - this._timing.fbu_rt_start));
  931. }
  932. }
  933. while (this._FBU.rects > 0) {
  934. if (this._rfb_state !== "normal") { return false; }
  935. if (this._sock.rQwait("FBU", this._FBU.bytes)) { return false; }
  936. if (this._FBU.bytes === 0) {
  937. if (this._sock.rQwait("rect header", 12)) { return false; }
  938. /* New FramebufferUpdate */
  939. var hdr = this._sock.rQshiftBytes(12);
  940. this._FBU.x = (hdr[0] << 8) + hdr[1];
  941. this._FBU.y = (hdr[2] << 8) + hdr[3];
  942. this._FBU.width = (hdr[4] << 8) + hdr[5];
  943. this._FBU.height = (hdr[6] << 8) + hdr[7];
  944. this._FBU.encoding = parseInt((hdr[8] << 24) + (hdr[9] << 16) +
  945. (hdr[10] << 8) + hdr[11], 10);
  946. this._onFBUReceive(this,
  947. {'x': this._FBU.x, 'y': this._FBU.y,
  948. 'width': this._FBU.width, 'height': this._FBU.height,
  949. 'encoding': this._FBU.encoding,
  950. 'encodingName': this._encNames[this._FBU.encoding]});
  951. if (!this._encNames[this._FBU.encoding]) {
  952. this._fail("Disconnected: unsupported encoding " +
  953. this._FBU.encoding);
  954. return false;
  955. }
  956. }
  957. this._timing.last_fbu = (new Date()).getTime();
  958. ret = this._encHandlers[this._FBU.encoding]();
  959. now = (new Date()).getTime();
  960. this._timing.cur_fbu += (now - this._timing.last_fbu);
  961. if (ret) {
  962. this._encStats[this._FBU.encoding][0]++;
  963. this._encStats[this._FBU.encoding][1]++;
  964. this._timing.pixels += this._FBU.width * this._FBU.height;
  965. }
  966. if (this._timing.pixels >= (this._fb_width * this._fb_height)) {
  967. if ((this._FBU.width === this._fb_width && this._FBU.height === this._fb_height) ||
  968. this._timing.fbu_rt_start > 0) {
  969. this._timing.full_fbu_total += this._timing.cur_fbu;
  970. this._timing.full_fbu_cnt++;
  971. Util.Info("Timing of full FBU, curr: " +
  972. this._timing.cur_fbu + ", total: " +
  973. this._timing.full_fbu_total + ", cnt: " +
  974. this._timing.full_fbu_cnt + ", avg: " +
  975. (this._timing.full_fbu_total / this._timing.full_fbu_cnt));
  976. }
  977. if (this._timing.fbu_rt_start > 0) {
  978. var fbu_rt_diff = now - this._timing.fbu_rt_start;
  979. this._timing.fbu_rt_total += fbu_rt_diff;
  980. this._timing.fbu_rt_cnt++;
  981. Util.Info("full FBU round-trip, cur: " +
  982. fbu_rt_diff + ", total: " +
  983. this._timing.fbu_rt_total + ", cnt: " +
  984. this._timing.fbu_rt_cnt + ", avg: " +
  985. (this._timing.fbu_rt_total / this._timing.fbu_rt_cnt));
  986. this._timing.fbu_rt_start = 0;
  987. }
  988. }
  989. if (!ret) { return ret; } // need more data
  990. }
  991. this._onFBUComplete(this,
  992. {'x': this._FBU.x, 'y': this._FBU.y,
  993. 'width': this._FBU.width, 'height': this._FBU.height,
  994. 'encoding': this._FBU.encoding,
  995. 'encodingName': this._encNames[this._FBU.encoding]});
  996. return true; // We finished this FBU
  997. },
  998. };
  999. Util.make_properties(RFB, [
  1000. ['target', 'wo', 'dom'], // VNC display rendering Canvas object
  1001. ['focusContainer', 'wo', 'dom'], // DOM element that captures keyboard input
  1002. ['encrypt', 'rw', 'bool'], // Use TLS/SSL/wss encryption
  1003. ['true_color', 'rw', 'bool'], // Request true color pixel data
  1004. ['local_cursor', 'rw', 'bool'], // Request locally rendered cursor
  1005. ['shared', 'rw', 'bool'], // Request shared mode
  1006. ['view_only', 'rw', 'bool'], // Disable client mouse/keyboard
  1007. ['xvp_password_sep', 'rw', 'str'], // Separator for XVP password fields
  1008. ['disconnectTimeout', 'rw', 'int'], // Time (s) to wait for disconnection
  1009. ['wsProtocols', 'rw', 'arr'], // Protocols to use in the WebSocket connection
  1010. ['repeaterID', 'rw', 'str'], // [UltraVNC] RepeaterID to connect to
  1011. ['viewportDrag', 'rw', 'bool'], // Move the viewport on mouse drags
  1012. // Callback functions
  1013. ['onUpdateState', 'rw', 'func'], // onUpdateState(rfb, state, oldstate, statusMsg): RFB state update/change
  1014. ['onPasswordRequired', 'rw', 'func'], // onPasswordRequired(rfb): VNC password is required
  1015. ['onClipboard', 'rw', 'func'], // onClipboard(rfb, text): RFB clipboard contents received
  1016. ['onBell', 'rw', 'func'], // onBell(rfb): RFB Bell message received
  1017. ['onFBUReceive', 'rw', 'func'], // onFBUReceive(rfb, fbu): RFB FBU received but not yet processed
  1018. ['onFBUComplete', 'rw', 'func'], // onFBUComplete(rfb, fbu): RFB FBU received and processed
  1019. ['onFBResize', 'rw', 'func'], // onFBResize(rfb, width, height): frame buffer resized
  1020. ['onDesktopName', 'rw', 'func'], // onDesktopName(rfb, name): desktop name received
  1021. ['onXvpInit', 'rw', 'func'], // onXvpInit(version): XVP extensions active for this connection
  1022. ]);
  1023. RFB.prototype.set_local_cursor = function (cursor) {
  1024. if (!cursor || (cursor in {'0': 1, 'no': 1, 'false': 1})) {
  1025. this._local_cursor = false;
  1026. this._display.disableLocalCursor(); //Only show server-side cursor
  1027. } else {
  1028. if (this._display.get_cursor_uri()) {
  1029. this._local_cursor = true;
  1030. } else {
  1031. Util.Warn("Browser does not support local cursor");
  1032. }
  1033. }
  1034. };
  1035. RFB.prototype.get_display = function () { return this._display; };
  1036. RFB.prototype.get_keyboard = function () { return this._keyboard; };
  1037. RFB.prototype.get_mouse = function () { return this._mouse; };
  1038. // Class Methods
  1039. RFB.messages = {
  1040. keyEvent: function (keysym, down) {
  1041. var arr = [4];
  1042. arr.push8(down);
  1043. arr.push16(0);
  1044. arr.push32(keysym);
  1045. return arr;
  1046. },
  1047. pointerEvent: function (x, y, mask) {
  1048. var arr = [5]; // msg-type
  1049. arr.push8(mask);
  1050. arr.push16(x);
  1051. arr.push16(y);
  1052. return arr;
  1053. },
  1054. // TODO(directxman12): make this unicode compatible?
  1055. clientCutText: function (text) {
  1056. var arr = [6]; // msg-type
  1057. arr.push8(0); // padding
  1058. arr.push8(0); // padding
  1059. arr.push8(0); // padding
  1060. arr.push32(text.length);
  1061. var n = text.length;
  1062. for (var i = 0; i < n; i++) {
  1063. arr.push(text.charCodeAt(i));
  1064. }
  1065. return arr;
  1066. },
  1067. pixelFormat: function (bpp, depth, true_color) {
  1068. var arr = [0]; // msg-type
  1069. arr.push8(0); // padding
  1070. arr.push8(0); // padding
  1071. arr.push8(0); // padding
  1072. arr.push8(bpp * 8); // bits-per-pixel
  1073. arr.push8(depth * 8); // depth
  1074. arr.push8(0); // little-endian
  1075. arr.push8(true_color ? 1 : 0); // true-color
  1076. arr.push16(255); // red-max
  1077. arr.push16(255); // green-max
  1078. arr.push16(255); // blue-max
  1079. arr.push8(16); // red-shift
  1080. arr.push8(8); // green-shift
  1081. arr.push8(0); // blue-shift
  1082. arr.push8(0); // padding
  1083. arr.push8(0); // padding
  1084. arr.push8(0); // padding
  1085. return arr;
  1086. },
  1087. clientEncodings: function (encodings, local_cursor, true_color) {
  1088. var i, encList = [];
  1089. for (i = 0; i < encodings.length; i++) {
  1090. if (encodings[i][0] === "Cursor" && !local_cursor) {
  1091. Util.Debug("Skipping Cursor pseudo-encoding");
  1092. } else if (encodings[i][0] === "TIGHT" && !true_color) {
  1093. // TODO: remove this when we have tight+non-true-color
  1094. Util.Warn("Skipping tight as it is only supported with true color");
  1095. } else {
  1096. encList.push(encodings[i][1]);
  1097. }
  1098. }
  1099. var arr = [2]; // msg-type
  1100. arr.push8(0); // padding
  1101. arr.push16(encList.length); // encoding count
  1102. for (i = 0; i < encList.length; i++) {
  1103. arr.push32(encList[i]);
  1104. }
  1105. return arr;
  1106. },
  1107. fbUpdateRequests: function (cleanDirty, fb_width, fb_height) {
  1108. var arr = [];
  1109. var cb = cleanDirty.cleanBox;
  1110. var w, h;
  1111. if (cb.w > 0 && cb.h > 0) {
  1112. w = typeof cb.w === "undefined" ? fb_width : cb.w;
  1113. h = typeof cb.h === "undefined" ? fb_height : cb.h;
  1114. // Request incremental for clean box
  1115. arr = arr.concat(RFB.messages.fbUpdateRequest(1, cb.x, cb.y, w, h));
  1116. }
  1117. for (var i = 0; i < cleanDirty.dirtyBoxes.length; i++) {
  1118. var db = cleanDirty.dirtyBoxes[i];
  1119. // Force all (non-incremental) for dirty box
  1120. w = typeof db.w === "undefined" ? fb_width : db.w;
  1121. h = typeof db.h === "undefined" ? fb_height : db.h;
  1122. arr = arr.concat(RFB.messages.fbUpdateRequest(0, db.x, db.y, w, h));
  1123. }
  1124. return arr;
  1125. },
  1126. fbUpdateRequest: function (incremental, x, y, w, h) {
  1127. if (typeof(x) === "undefined") { x = 0; }
  1128. if (typeof(y) === "undefined") { y = 0; }
  1129. var arr = [3]; // msg-type
  1130. arr.push8(incremental);
  1131. arr.push16(x);
  1132. arr.push16(y);
  1133. arr.push16(w);
  1134. arr.push16(h);
  1135. return arr;
  1136. }
  1137. };
  1138. RFB.genDES = function (password, challenge) {
  1139. var passwd = [];
  1140. for (var i = 0; i < password.length; i++) {
  1141. passwd.push(password.charCodeAt(i));
  1142. }
  1143. return (new DES(passwd)).encrypt(challenge);
  1144. };
  1145. RFB.extract_data_uri = function (arr) {
  1146. return ";base64," + Base64.encode(arr);
  1147. };
  1148. RFB.encodingHandlers = {
  1149. RAW: function () {
  1150. if (this._FBU.lines === 0) {
  1151. this._FBU.lines = this._FBU.height;
  1152. }
  1153. this._FBU.bytes = this._FBU.width * this._fb_Bpp; // at least a line
  1154. if (this._sock.rQwait("RAW", this._FBU.bytes)) { return false; }
  1155. var cur_y = this._FBU.y + (this._FBU.height - this._FBU.lines);
  1156. var curr_height = Math.min(this._FBU.lines,
  1157. Math.floor(this._sock.rQlen() / (this._FBU.width * this._fb_Bpp)));
  1158. this._display.blitImage(this._FBU.x, cur_y, this._FBU.width,
  1159. curr_height, this._sock.get_rQ(),
  1160. this._sock.get_rQi());
  1161. this._sock.rQskipBytes(this._FBU.width * curr_height * this._fb_Bpp);
  1162. this._FBU.lines -= curr_height;
  1163. if (this._FBU.lines > 0) {
  1164. this._FBU.bytes = this._FBU.width * this._fb_Bpp; // At least another line
  1165. } else {
  1166. this._FBU.rects--;
  1167. this._FBU.bytes = 0;
  1168. }
  1169. return true;
  1170. },
  1171. COPYRECT: function () {
  1172. this._FBU.bytes = 4;
  1173. if (this._sock.rQwait("COPYRECT", 4)) { return false; }
  1174. this._display.renderQ_push({
  1175. 'type': 'copy',
  1176. 'old_x': this._sock.rQshift16(),
  1177. 'old_y': this._sock.rQshift16(),
  1178. 'x': this._FBU.x,
  1179. 'y': this._FBU.y,
  1180. 'width': this._FBU.width,
  1181. 'height': this._FBU.height
  1182. });
  1183. this._FBU.rects--;
  1184. this._FBU.bytes = 0;
  1185. return true;
  1186. },
  1187. RRE: function () {
  1188. var color;
  1189. if (this._FBU.subrects === 0) {
  1190. this._FBU.bytes = 4 + this._fb_Bpp;
  1191. if (this._sock.rQwait("RRE", 4 + this._fb_Bpp)) { return false; }
  1192. this._FBU.subrects = this._sock.rQshift32();
  1193. color = this._sock.rQshiftBytes(this._fb_Bpp); // Background
  1194. this._display.fillRect(this._FBU.x, this._FBU.y, this._FBU.width, this._FBU.height, color);
  1195. }
  1196. while (this._FBU.subrects > 0 && this._sock.rQlen() >= (this._fb_Bpp + 8)) {
  1197. color = this._sock.rQshiftBytes(this._fb_Bpp);
  1198. var x = this._sock.rQshift16();
  1199. var y = this._sock.rQshift16();
  1200. var width = this._sock.rQshift16();
  1201. var height = this._sock.rQshift16();
  1202. this._display.fillRect(this._FBU.x + x, this._FBU.y + y, width, height, color);
  1203. this._FBU.subrects--;
  1204. }
  1205. if (this._FBU.subrects > 0) {
  1206. var chunk = Math.min(this._rre_chunk_sz, this._FBU.subrects);
  1207. this._FBU.bytes = (this._fb_Bpp + 8) * chunk;
  1208. } else {
  1209. this._FBU.rects--;
  1210. this._FBU.bytes = 0;
  1211. }
  1212. return true;
  1213. },
  1214. HEXTILE: function () {
  1215. var rQ = this._sock.get_rQ();
  1216. var rQi = this._sock.get_rQi();
  1217. if (this._FBU.tiles === 0) {
  1218. this._FBU.tiles_x = Math.ceil(this._FBU.width / 16);
  1219. this._FBU.tiles_y = Math.ceil(this._FBU.height / 16);
  1220. this._FBU.total_tiles = this._FBU.tiles_x * this._FBU.tiles_y;
  1221. this._FBU.tiles = this._FBU.total_tiles;
  1222. }
  1223. while (this._FBU.tiles > 0) {
  1224. this._FBU.bytes = 1;
  1225. if (this._sock.rQwait("HEXTILE subencoding", this._FBU.bytes)) { return false; }
  1226. var subencoding = rQ[rQi]; // Peek
  1227. if (subencoding > 30) { // Raw
  1228. this._fail("Disconnected: illegal hextile subencoding " + subencoding);
  1229. return false;
  1230. }
  1231. var subrects = 0;
  1232. var curr_tile = this._FBU.total_tiles - this._FBU.tiles;
  1233. var tile_x = curr_tile % this._FBU.tiles_x;
  1234. var tile_y = Math.floor(curr_tile / this._FBU.tiles_x);
  1235. var x = this._FBU.x + tile_x * 16;
  1236. var y = this._FBU.y + tile_y * 16;
  1237. var w = Math.min(16, (this._FBU.x + this._FBU.width) - x);
  1238. var h = Math.min(16, (this._FBU.y + this._FBU.height) - y);
  1239. // Figure out how much we are expecting
  1240. if (subencoding & 0x01) { // Raw
  1241. this._FBU.bytes += w * h * this._fb_Bpp;
  1242. } else {
  1243. if (subencoding & 0x02) { // Background
  1244. this._FBU.bytes += this._fb_Bpp;
  1245. }
  1246. if (subencoding & 0x04) { // Foreground
  1247. this._FBU.bytes += this._fb_Bpp;
  1248. }
  1249. if (subencoding & 0x08) { // AnySubrects
  1250. this._FBU.bytes++; // Since we aren't shifting it off
  1251. if (this._sock.rQwait("hextile subrects header", this._FBU.bytes)) { return false; }
  1252. subrects = rQ[rQi + this._FBU.bytes - 1]; // Peek
  1253. if (subencoding & 0x10) { // SubrectsColoured
  1254. this._FBU.bytes += subrects * (this._fb_Bpp + 2);
  1255. } else {
  1256. this._FBU.bytes += subrects * 2;
  1257. }
  1258. }
  1259. }
  1260. if (this._sock.rQwait("hextile", this._FBU.bytes)) { return false; }
  1261. // We know the encoding and have a whole tile
  1262. this._FBU.subencoding = rQ[rQi];
  1263. rQi++;
  1264. if (this._FBU.subencoding === 0) {
  1265. if (this._FBU.lastsubencoding & 0x01) {
  1266. // Weird: ignore blanks are RAW
  1267. Util.Debug(" Ignoring blank after RAW");
  1268. } else {
  1269. this._display.fillRect(x, y, w, h, this._FBU.background);
  1270. }
  1271. } else if (this._FBU.subencoding & 0x01) { // Raw
  1272. this._display.blitImage(x, y, w, h, rQ, rQi);
  1273. rQi += this._FBU.bytes - 1;
  1274. } else {
  1275. if (this._FBU.subencoding & 0x02) { // Background
  1276. this._FBU.background = rQ.slice(rQi, rQi + this._fb_Bpp);
  1277. rQi += this._fb_Bpp;
  1278. }
  1279. if (this._FBU.subencoding & 0x04) { // Foreground
  1280. this._FBU.foreground = rQ.slice(rQi, rQi + this._fb_Bpp);
  1281. rQi += this._fb_Bpp;
  1282. }
  1283. this._display.startTile(x, y, w, h, this._FBU.background);
  1284. if (this._FBU.subencoding & 0x08) { // AnySubrects
  1285. subrects = rQ[rQi];
  1286. rQi++;
  1287. for (var s = 0; s < subrects; s++) {
  1288. var color;
  1289. if (this._FBU.subencoding & 0x10) { // SubrectsColoured
  1290. color = rQ.slice(rQi, rQi + this._fb_Bpp);
  1291. rQi += this._fb_Bpp;
  1292. } else {
  1293. color = this._FBU.foreground;
  1294. }
  1295. var xy = rQ[rQi];
  1296. rQi++;
  1297. var sx = (xy >> 4);
  1298. var sy = (xy & 0x0f);
  1299. var wh = rQ[rQi];
  1300. rQi++;
  1301. var sw = (wh >> 4) + 1;
  1302. var sh = (wh & 0x0f) + 1;
  1303. this._display.subTile(sx, sy, sw, sh, color);
  1304. }
  1305. }
  1306. this._display.finishTile();
  1307. }
  1308. this._sock.set_rQi(rQi);
  1309. this._FBU.lastsubencoding = this._FBU.subencoding;
  1310. this._FBU.bytes = 0;
  1311. this._FBU.tiles--;
  1312. }
  1313. if (this._FBU.tiles === 0) {
  1314. this._FBU.rects--;
  1315. }
  1316. return true;
  1317. },
  1318. getTightCLength: function (arr) {
  1319. var header = 1, data = 0;
  1320. data += arr[0] & 0x7f;
  1321. if (arr[0] & 0x80) {
  1322. header++;
  1323. data += (arr[1] & 0x7f) << 7;
  1324. if (arr[1] & 0x80) {
  1325. header++;
  1326. data += arr[2] << 14;
  1327. }
  1328. }
  1329. return [header, data];
  1330. },
  1331. display_tight: function (isTightPNG) {
  1332. if (this._fb_depth === 1) {
  1333. this._fail("Tight protocol handler only implements true color mode");
  1334. }
  1335. this._FBU.bytes = 1; // compression-control byte
  1336. if (this._sock.rQwait("TIGHT compression-control", this._FBU.bytes)) { return false; }
  1337. var checksum = function (data) {
  1338. var sum = 0;
  1339. for (var i = 0; i < data.length; i++) {
  1340. sum += data[i];
  1341. if (sum > 65536) sum -= 65536;
  1342. }
  1343. return sum;
  1344. };
  1345. var resetStreams = 0;
  1346. var streamId = -1;
  1347. var decompress = function (data) {
  1348. for (var i = 0; i < 4; i++) {
  1349. if ((resetStreams >> i) & 1) {
  1350. this._FBU.zlibs[i].reset();
  1351. Util.Info("Reset zlib stream " + i);
  1352. }
  1353. }
  1354. var uncompressed = this._FBU.zlibs[streamId].uncompress(data, 0);
  1355. if (uncompressed.status !== 0) {
  1356. Util.Error("Invalid data in zlib stream");
  1357. }
  1358. return uncompressed.data;
  1359. }.bind(this);
  1360. var indexedToRGB = function (data, numColors, palette, width, height) {
  1361. // Convert indexed (palette based) image data to RGB
  1362. // TODO: reduce number of calculations inside loop
  1363. var dest = [];
  1364. var x, y, dp, sp;
  1365. if (numColors === 2) {
  1366. var w = Math.floor((width + 7) / 8);
  1367. var w1 = Math.floor(width / 8);
  1368. for (y = 0; y < height; y++) {
  1369. var b;
  1370. for (x = 0; x < w1; x++) {
  1371. for (b = 7; b >= 0; b--) {
  1372. dp = (y * width + x * 8 + 7 - b) * 3;
  1373. sp = (data[y * w + x] >> b & 1) * 3;
  1374. dest[dp] = palette[sp];
  1375. dest[dp + 1] = palette[sp + 1];
  1376. dest[dp + 2] = palette[sp + 2];
  1377. }
  1378. }
  1379. for (b = 7; b >= 8 - width % 8; b--) {
  1380. dp = (y * width + x * 8 + 7 - b) * 3;
  1381. sp = (data[y * w + x] >> b & 1) * 3;
  1382. dest[dp] = palette[sp];
  1383. dest[dp + 1] = palette[sp + 1];
  1384. dest[dp + 2] = palette[sp + 2];
  1385. }
  1386. }
  1387. } else {
  1388. for (y = 0; y < height; y++) {
  1389. for (x = 0; x < width; x++) {
  1390. dp = (y * width + x) * 3;
  1391. sp = data[y * width + x] * 3;
  1392. dest[dp] = palette[sp];
  1393. dest[dp + 1] = palette[sp + 1];
  1394. dest[dp + 2] = palette[sp + 2];
  1395. }
  1396. }
  1397. }
  1398. return dest;
  1399. }.bind(this);
  1400. var rQ = this._sock.get_rQ();
  1401. var rQi = this._sock.get_rQi();
  1402. var cmode, clength, data;
  1403. var handlePalette = function () {
  1404. var numColors = rQ[rQi + 2] + 1;
  1405. var paletteSize = numColors * this._fb_depth;
  1406. this._FBU.bytes += paletteSize;
  1407. if (this._sock.rQwait("TIGHT palette " + cmode, this._FBU.bytes)) { return false; }
  1408. var bpp = (numColors <= 2) ? 1 : 8;
  1409. var rowSize = Math.floor((this._FBU.width * bpp + 7) / 8);
  1410. var raw = false;
  1411. if (rowSize * this._FBU.height < 12) {
  1412. raw = true;
  1413. clength = [0, rowSize * this._FBU.height];
  1414. } else {
  1415. clength = RFB.encodingHandlers.getTightCLength(this._sock.rQslice(3 + paletteSize,
  1416. 3 + paletteSize + 3));
  1417. }
  1418. this._FBU.bytes += clength[0] + clength[1];
  1419. if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
  1420. // Shift ctl, filter id, num colors, palette entries, and clength off
  1421. this._sock.rQskipBytes(3);
  1422. var palette = this._sock.rQshiftBytes(paletteSize);
  1423. this._sock.rQskipBytes(clength[0]);
  1424. if (raw) {
  1425. data = this._sock.rQshiftBytes(clength[1]);
  1426. } else {
  1427. data = decompress(this._sock.rQshiftBytes(clength[1]));
  1428. }
  1429. // Convert indexed (palette based) image data to RGB
  1430. var rgb = indexedToRGB(data, numColors, palette, this._FBU.width, this._FBU.height);
  1431. this._display.renderQ_push({
  1432. 'type': 'blitRgb',
  1433. 'data': rgb,
  1434. 'x': this._FBU.x,
  1435. 'y': this._FBU.y,
  1436. 'width': this._FBU.width,
  1437. 'height': this._FBU.height
  1438. });
  1439. return true;
  1440. }.bind(this);
  1441. var handleCopy = function () {
  1442. var raw = false;
  1443. var uncompressedSize = this._FBU.width * this._FBU.height * this._fb_depth;
  1444. if (uncompressedSize < 12) {
  1445. raw = true;
  1446. clength = [0, uncompressedSize];
  1447. } else {
  1448. clength = RFB.encodingHandlers.getTightCLength(this._sock.rQslice(1, 4));
  1449. }
  1450. this._FBU.bytes = 1 + clength[0] + clength[1];
  1451. if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
  1452. // Shift ctl, clength off
  1453. this._sock.rQshiftBytes(1 + clength[0]);
  1454. if (raw) {
  1455. data = this._sock.rQshiftBytes(clength[1]);
  1456. } else {
  1457. data = decompress(this._sock.rQshiftBytes(clength[1]));
  1458. }
  1459. this._display.renderQ_push({
  1460. 'type': 'blitRgb',
  1461. 'data': data,
  1462. 'x': this._FBU.x,
  1463. 'y': this._FBU.y,
  1464. 'width': this._FBU.width,
  1465. 'height': this._FBU.height
  1466. });
  1467. return true;
  1468. }.bind(this);
  1469. var ctl = this._sock.rQpeek8();
  1470. // Keep tight reset bits
  1471. resetStreams = ctl & 0xF;
  1472. // Figure out filter
  1473. ctl = ctl >> 4;
  1474. streamId = ctl & 0x3;
  1475. if (ctl === 0x08) cmode = "fill";
  1476. else if (ctl === 0x09) cmode = "jpeg";
  1477. else if (ctl === 0x0A) cmode = "png";
  1478. else if (ctl & 0x04) cmode = "filter";
  1479. else if (ctl < 0x04) cmode = "copy";
  1480. else return this._fail("Illegal tight compression received, ctl: " + ctl);
  1481. if (isTightPNG && (cmode === "filter" || cmode === "copy")) {
  1482. return this._fail("filter/copy received in tightPNG mode");
  1483. }
  1484. switch (cmode) {
  1485. // fill use fb_depth because TPIXELs drop the padding byte
  1486. case "fill": // TPIXEL
  1487. this._FBU.bytes += this._fb_depth;
  1488. break;
  1489. case "jpeg": // max clength
  1490. this._FBU.bytes += 3;
  1491. break;
  1492. case "png": // max clength
  1493. this._FBU.bytes += 3;
  1494. break;
  1495. case "filter": // filter id + num colors if palette
  1496. this._FBU.bytes += 2;
  1497. break;
  1498. case "copy":
  1499. break;
  1500. }
  1501. if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
  1502. // Determine FBU.bytes
  1503. switch (cmode) {
  1504. case "fill":
  1505. this._sock.rQskip8(); // shift off ctl
  1506. var color = this._sock.rQshiftBytes(this._fb_depth);
  1507. this._display.renderQ_push({
  1508. 'type': 'fill',
  1509. 'x': this._FBU.x,
  1510. 'y': this._FBU.y,
  1511. 'width': this._FBU.width,
  1512. 'height': this._FBU.height,
  1513. 'color': [color[2], color[1], color[0]]
  1514. });
  1515. break;
  1516. case "png":
  1517. case "jpeg":
  1518. clength = RFB.encodingHandlers.getTightCLength(this._sock.rQslice(1, 4));
  1519. this._FBU.bytes = 1 + clength[0] + clength[1]; // ctl + clength size + jpeg-data
  1520. if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
  1521. // We have everything, render it
  1522. this._sock.rQskipBytes(1 + clength[0]); // shift off clt + compact length
  1523. var img = new Image();
  1524. img.src = "data: image/" + cmode +
  1525. RFB.extract_data_uri(this._sock.rQshiftBytes(clength[1]));
  1526. this._display.renderQ_push({
  1527. 'type': 'img',
  1528. 'img': img,
  1529. 'x': this._FBU.x,
  1530. 'y': this._FBU.y
  1531. });
  1532. img = null;
  1533. break;
  1534. case "filter":
  1535. var filterId = rQ[rQi + 1];
  1536. if (filterId === 1) {
  1537. if (!handlePalette()) { return false; }
  1538. } else {
  1539. // Filter 0, Copy could be valid here, but servers don't send it as an explicit filter
  1540. // Filter 2, Gradient is valid but not use if jpeg is enabled
  1541. // TODO(directxman12): why aren't we just calling '_fail' here
  1542. throw new Error("Unsupported tight subencoding received, filter: " + filterId);
  1543. }
  1544. break;
  1545. case "copy":
  1546. if (!handleCopy()) { return false; }
  1547. break;
  1548. }
  1549. this._FBU.bytes = 0;
  1550. this._FBU.rects--;
  1551. return true;
  1552. },
  1553. TIGHT: function () { return this._encHandlers.display_tight(false); },
  1554. TIGHT_PNG: function () { return this._encHandlers.display_tight(true); },
  1555. last_rect: function () {
  1556. this._FBU.rects = 0;
  1557. return true;
  1558. },
  1559. DesktopSize: function () {
  1560. Util.Debug(">> set_desktopsize");
  1561. this._fb_width = this._FBU.width;
  1562. this._fb_height = this._FBU.height;
  1563. this._onFBResize(this, this._fb_width, this._fb_height);
  1564. this._display.resize(this._fb_width, this._fb_height);
  1565. this._timing.fbu_rt_start = (new Date()).getTime();
  1566. this._FBU.bytes = 0;
  1567. this._FBU.rects--;
  1568. Util.Debug("<< set_desktopsize");
  1569. return true;
  1570. },
  1571. Cursor: function () {
  1572. Util.Debug(">> set_cursor");
  1573. var x = this._FBU.x; // hotspot-x
  1574. var y = this._FBU.y; // hotspot-y
  1575. var w = this._FBU.width;
  1576. var h = this._FBU.height;
  1577. var pixelslength = w * h * this._fb_Bpp;
  1578. var masklength = Math.floor((w + 7) / 8) * h;
  1579. this._FBU.bytes = pixelslength + masklength;
  1580. if (this._sock.rQwait("cursor encoding", this._FBU.bytes)) { return false; }
  1581. this._display.changeCursor(this._sock.rQshiftBytes(pixelslength),
  1582. this._sock.rQshiftBytes(masklength),
  1583. x, y, w, h);
  1584. this._FBU.bytes = 0;
  1585. this._FBU.rects--;
  1586. Util.Debug("<< set_cursor");
  1587. return true;
  1588. },
  1589. JPEG_quality_lo: function () {
  1590. Util.Error("Server sent jpeg_quality pseudo-encoding");
  1591. },
  1592. compress_lo: function () {
  1593. Util.Error("Server sent compress level pseudo-encoding");
  1594. }
  1595. };
  1596. })();