rfb.js 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  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(0xFFE3, 1)); // Control
  221. arr = arr.concat(RFB.messages.keyEvent(0xFFE9, 1)); // Alt
  222. arr = arr.concat(RFB.messages.keyEvent(0xFFFF, 1)); // Delete
  223. arr = arr.concat(RFB.messages.keyEvent(0xFFFF, 0)); // Delete
  224. arr = arr.concat(RFB.messages.keyEvent(0xFFE9, 0)); // Alt
  225. arr = arr.concat(RFB.messages.keyEvent(0xFFE3, 0)); // Control
  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. this._display.defaultCursor();
  355. if (Util.get_logging() !== 'debug' || state === 'loaded') {
  356. // Show noVNC logo on load and when disconnected, unless in
  357. // debug mode
  358. this._display.clear();
  359. }
  360. }
  361. this._sock.close();
  362. }
  363. if (oldstate === 'fatal') {
  364. Util.Error('Fatal error, cannot continue');
  365. }
  366. var cmsg = typeof(statusMsg) !== 'undefined' ? (" Msg: " + statusMsg) : "";
  367. var fullmsg = "New state '" + state + "', was '" + oldstate + "'." + cmsg;
  368. if (state === 'failed' || state === 'fatal') {
  369. Util.Error(cmsg);
  370. } else {
  371. Util.Warn(cmsg);
  372. }
  373. if (oldstate === 'failed' && state === 'disconnected') {
  374. // do disconnect action, but stay in failed state
  375. this._rfb_state = 'failed';
  376. } else {
  377. this._rfb_state = state;
  378. }
  379. if (this._disconnTimer && this._rfb_state !== 'disconnect') {
  380. Util.Debug("Clearing disconnect timer");
  381. clearTimeout(this._disconnTimer);
  382. this._disconnTimer = null;
  383. }
  384. switch (state) {
  385. case 'normal':
  386. if (oldstate === 'disconnected' || oldstate === 'failed') {
  387. Util.Error("Invalid transition from 'disconnected' or 'failed' to 'normal'");
  388. }
  389. break;
  390. case 'connect':
  391. this._init_vars();
  392. this._connect();
  393. // WebSocket.onopen transitions to 'ProtocolVersion'
  394. break;
  395. case 'disconnect':
  396. this._disconnTimer = setTimeout(function () {
  397. this._fail("Disconnect timeout");
  398. }.bind(this), this._disconnectTimeout * 1000);
  399. this._print_stats();
  400. // WebSocket.onclose transitions to 'disconnected'
  401. break;
  402. case 'failed':
  403. if (oldstate === 'disconnected') {
  404. Util.Error("Invalid transition from 'disconnected' to 'failed'");
  405. } else if (oldstate === 'normal') {
  406. Util.Error("Error while connected.");
  407. } else if (oldstate === 'init') {
  408. Util.Error("Error while initializing.");
  409. }
  410. // Make sure we transition to disconnected
  411. setTimeout(function () {
  412. this._updateState('disconnected');
  413. }.bind(this), 50);
  414. break;
  415. default:
  416. // No state change action to take
  417. }
  418. if (oldstate === 'failed' && state === 'disconnected') {
  419. this._onUpdateState(this, state, oldstate);
  420. } else {
  421. this._onUpdateState(this, state, oldstate, statusMsg);
  422. }
  423. },
  424. _fail: function (msg) {
  425. this._updateState('failed', msg);
  426. return false;
  427. },
  428. _handle_message: function () {
  429. if (this._sock.rQlen() === 0) {
  430. Util.Warn("handle_message called on an empty receive queue");
  431. return;
  432. }
  433. switch (this._rfb_state) {
  434. case 'disconnected':
  435. case 'failed':
  436. Util.Error("Got data while disconnected");
  437. break;
  438. case 'normal':
  439. if (this._normal_msg() && this._sock.rQlen() > 0) {
  440. // true means we can continue processing
  441. // Give other events a chance to run
  442. if (this._msgTimer === null) {
  443. Util.Debug("More data to process, creating timer");
  444. this._msgTimer = setTimeout(function () {
  445. this._msgTimer = null;
  446. this._handle_message();
  447. }.bind(this), 10);
  448. } else {
  449. Util.Debug("More data to process, existing timer");
  450. }
  451. }
  452. break;
  453. default:
  454. this._init_msg();
  455. break;
  456. }
  457. },
  458. _checkEvents: function () {
  459. if (this._rfb_state === 'normal' && !this._viewportDragging && this._mouse_arr.length > 0) {
  460. this._sock.send(this._mouse_arr);
  461. this._mouse_arr = [];
  462. }
  463. },
  464. _handleKeyPress: function (keysym, down) {
  465. if (this._view_only) { return; } // View only, skip keyboard, events
  466. this._sock.send(RFB.messages.keyEvent(keysym, down));
  467. },
  468. _handleMouseButton: function (x, y, down, bmask) {
  469. if (down) {
  470. this._mouse_buttonMask |= bmask;
  471. } else {
  472. this._mouse_buttonMask ^= bmask;
  473. }
  474. if (this._viewportDrag) {
  475. if (down && !this._viewportDragging) {
  476. this._viewportDragging = true;
  477. this._viewportDragPos = {'x': x, 'y': y};
  478. // Skip sending mouse events
  479. return;
  480. } else {
  481. this._viewportDragging = false;
  482. }
  483. }
  484. if (this._view_only) { return; } // View only, skip mouse events
  485. this._mouse_arr = this._mouse_arr.concat(
  486. RFB.messages.pointerEvent(this._display.absX(x), this._display.absY(y), this._mouse_buttonMask));
  487. this._sock.send(this._mouse_arr);
  488. this._mouse_arr = [];
  489. },
  490. _handleMouseMove: function (x, y) {
  491. if (this._viewportDragging) {
  492. var deltaX = this._viewportDragPos.x - x;
  493. var deltaY = this._viewportDragPos.y - y;
  494. this._viewportDragPos = {'x': x, 'y': y};
  495. this._display.viewportChange(deltaX, deltaY);
  496. // Skip sending mouse events
  497. return;
  498. }
  499. if (this._view_only) { return; } // View only, skip mouse events
  500. this._mouse_arr = this._mouse_arr.concat(
  501. RFB.messages.pointerEvent(this._display.absX(x), this._display.absY(y), this._mouse_buttonMask));
  502. this._checkEvents();
  503. },
  504. // Message Handlers
  505. _negotiate_protocol_version: function () {
  506. if (this._sock.rQlen() < 12) {
  507. return this._fail("Incomplete protocol version");
  508. }
  509. var sversion = this._sock.rQshiftStr(12).substr(4, 7);
  510. Util.Info("Server ProtocolVersion: " + sversion);
  511. var is_repeater = 0;
  512. switch (sversion) {
  513. case "000.000": // UltraVNC repeater
  514. is_repeater = 1;
  515. break;
  516. case "003.003":
  517. case "003.006": // UltraVNC
  518. case "003.889": // Apple Remote Desktop
  519. this._rfb_version = 3.3;
  520. break;
  521. case "003.007":
  522. this._rfb_version = 3.7;
  523. break;
  524. case "003.008":
  525. case "004.000": // Intel AMT KVM
  526. case "004.001": // RealVNC 4.6
  527. this._rfb_version = 3.8;
  528. break;
  529. default:
  530. return this._fail("Invalid server version " + sversion);
  531. }
  532. if (is_repeater) {
  533. var repeaterID = this._repeaterID;
  534. while (repeaterID.length < 250) {
  535. repeaterID += "\0";
  536. }
  537. this._sock.send_string(repeaterID);
  538. return true;
  539. }
  540. if (this._rfb_version > this._rfb_max_version) {
  541. this._rfb_version = this._rfb_max_version;
  542. }
  543. // Send updates either at a rate of 1 update per 50ms, or
  544. // whatever slower rate the network can handle
  545. this._sendTimer = setInterval(this._sock.flush.bind(this._sock), 50);
  546. var cversion = "00" + parseInt(this._rfb_version, 10) +
  547. ".00" + ((this._rfb_version * 10) % 10);
  548. this._sock.send_string("RFB " + cversion + "\n");
  549. this._updateState('Security', 'Sent ProtocolVersion: ' + cversion);
  550. },
  551. _negotiate_security: function () {
  552. if (this._rfb_version >= 3.7) {
  553. // Server sends supported list, client decides
  554. var num_types = this._sock.rQshift8();
  555. if (this._sock.rQwait("security type", num_types, 1)) { return false; }
  556. if (num_types === 0) {
  557. var strlen = this._sock.rQshift32();
  558. var reason = this._sock.rQshiftStr(strlen);
  559. return this._fail("Security failure: " + reason);
  560. }
  561. this._rfb_auth_scheme = 0;
  562. var types = this._sock.rQshiftBytes(num_types);
  563. Util.Debug("Server security types: " + types);
  564. for (var i = 0; i < types.length; i++) {
  565. if (types[i] > this._rfb_auth_scheme && (types[i] <= 16 || types[i] == 22)) {
  566. this._rfb_auth_scheme = types[i];
  567. }
  568. }
  569. if (this._rfb_auth_scheme === 0) {
  570. return this._fail("Unsupported security types: " + types);
  571. }
  572. this._sock.send([this._rfb_auth_scheme]);
  573. } else {
  574. // Server decides
  575. if (this._sock.rQwait("security scheme", 4)) { return false; }
  576. this._rfb_auth_scheme = this._sock.rQshift32();
  577. }
  578. this._updateState('Authentication', 'Authenticating using scheme: ' + this._rfb_auth_scheme);
  579. return this._init_msg(); // jump to authentication
  580. },
  581. // authentication
  582. _negotiate_xvp_auth: function () {
  583. var xvp_sep = this._xvp_password_sep;
  584. var xvp_auth = this._rfb_password.split(xvp_sep);
  585. if (xvp_auth.length < 3) {
  586. this._updateState('password', 'XVP credentials required (user' + xvp_sep +
  587. 'target' + xvp_sep + 'password) -- got only ' + this._rfb_password);
  588. this._onPasswordRequired(this);
  589. return false;
  590. }
  591. var xvp_auth_str = String.fromCharCode(xvp_auth[0].length) +
  592. String.fromCharCode(xvp_auth[1].length) +
  593. xvp_auth[0] +
  594. xvp_auth[1];
  595. this._sock.send_string(xvp_auth_str);
  596. this._rfb_password = xvp_auth.slice(2).join(xvp_sep);
  597. this._rfb_auth_scheme = 2;
  598. return this._negotiate_authentication();
  599. },
  600. _negotiate_std_vnc_auth: function () {
  601. if (this._rfb_password.length === 0) {
  602. // Notify via both callbacks since it's kind of
  603. // an RFB state change and a UI interface issue
  604. this._updateState('password', "Password Required");
  605. this._onPasswordRequired(this);
  606. }
  607. if (this._sock.rQwait("auth challenge", 16)) { return false; }
  608. var challenge = this._sock.rQshiftBytes(16);
  609. var response = RFB.genDES(this._rfb_password, challenge);
  610. this._sock.send(response);
  611. this._updateState("SecurityResult");
  612. return true;
  613. },
  614. _negotiate_tight_tunnels: function (numTunnels) {
  615. var clientSupportedTunnelTypes = {
  616. 0: { vendor: 'TGHT', signature: 'NOTUNNEL' }
  617. };
  618. var serverSupportedTunnelTypes = {};
  619. // receive tunnel capabilities
  620. for (var i = 0; i < numTunnels; i++) {
  621. var cap_code = this._sock.rQshift32();
  622. var cap_vendor = this._sock.rQshiftStr(4);
  623. var cap_signature = this._sock.rQshiftStr(8);
  624. serverSupportedTunnelTypes[cap_code] = { vendor: cap_vendor, signature: cap_signature };
  625. }
  626. // choose the notunnel type
  627. if (serverSupportedTunnelTypes[0]) {
  628. if (serverSupportedTunnelTypes[0].vendor != clientSupportedTunnelTypes[0].vendor ||
  629. serverSupportedTunnelTypes[0].signature != clientSupportedTunnelTypes[0].signature) {
  630. return this._fail("Client's tunnel type had the incorrect vendor or signature");
  631. }
  632. this._sock.send([0, 0, 0, 0]); // use NOTUNNEL
  633. return false; // wait until we receive the sub auth count to continue
  634. } else {
  635. return this._fail("Server wanted tunnels, but doesn't support the notunnel type");
  636. }
  637. },
  638. _negotiate_tight_auth: function () {
  639. if (!this._rfb_tightvnc) { // first pass, do the tunnel negotiation
  640. if (this._sock.rQwait("num tunnels", 4)) { return false; }
  641. var numTunnels = this._sock.rQshift32();
  642. if (numTunnels > 0 && this._sock.rQwait("tunnel capabilities", 16 * numTunnels, 4)) { return false; }
  643. this._rfb_tightvnc = true;
  644. if (numTunnels > 0) {
  645. this._negotiate_tight_tunnels(numTunnels);
  646. return false; // wait until we receive the sub auth to continue
  647. }
  648. }
  649. // second pass, do the sub-auth negotiation
  650. if (this._sock.rQwait("sub auth count", 4)) { return false; }
  651. var subAuthCount = this._sock.rQshift32();
  652. if (this._sock.rQwait("sub auth capabilities", 16 * subAuthCount, 4)) { return false; }
  653. var clientSupportedTypes = {
  654. 'STDVNOAUTH__': 1,
  655. 'STDVVNCAUTH_': 2
  656. };
  657. var serverSupportedTypes = [];
  658. for (var i = 0; i < subAuthCount; i++) {
  659. var capNum = this._sock.rQshift32();
  660. var capabilities = this._sock.rQshiftStr(12);
  661. serverSupportedTypes.push(capabilities);
  662. }
  663. for (var authType in clientSupportedTypes) {
  664. if (serverSupportedTypes.indexOf(authType) != -1) {
  665. this._sock.send([0, 0, 0, clientSupportedTypes[authType]]);
  666. switch (authType) {
  667. case 'STDVNOAUTH__': // no auth
  668. this._updateState('SecurityResult');
  669. return true;
  670. case 'STDVVNCAUTH_': // VNC auth
  671. this._rfb_auth_scheme = 2;
  672. return this._init_msg();
  673. default:
  674. return this._fail("Unsupported tiny auth scheme: " + authType);
  675. }
  676. }
  677. }
  678. this._fail("No supported sub-auth types!");
  679. },
  680. _negotiate_authentication: function () {
  681. switch (this._rfb_auth_scheme) {
  682. case 0: // connection failed
  683. if (this._sock.rQwait("auth reason", 4)) { return false; }
  684. var strlen = this._sock.rQshift32();
  685. var reason = this._sock.rQshiftStr(strlen);
  686. return this._fail("Auth failure: " + reason);
  687. case 1: // no auth
  688. if (this._rfb_version >= 3.8) {
  689. this._updateState('SecurityResult');
  690. return true;
  691. }
  692. this._updateState('ClientInitialisation', "No auth required");
  693. return this._init_msg();
  694. case 22: // XVP auth
  695. return this._negotiate_xvp_auth();
  696. case 2: // VNC authentication
  697. return this._negotiate_std_vnc_auth();
  698. case 16: // TightVNC Security Type
  699. return this._negotiate_tight_auth();
  700. default:
  701. return this._fail("Unsupported auth scheme: " + this._rfb_auth_scheme);
  702. }
  703. },
  704. _handle_security_result: function () {
  705. if (this._sock.rQwait('VNC auth response ', 4)) { return false; }
  706. switch (this._sock.rQshift32()) {
  707. case 0: // OK
  708. this._updateState('ClientInitialisation', 'Authentication OK');
  709. return this._init_msg();
  710. case 1: // failed
  711. if (this._rfb_version >= 3.8) {
  712. var length = this._sock.rQshift32();
  713. if (this._sock.rQwait("SecurityResult reason", length, 8)) { return false; }
  714. var reason = this._sock.rQshiftStr(length);
  715. return this._fail(reason);
  716. } else {
  717. return this._fail("Authentication failure");
  718. }
  719. return false;
  720. case 2:
  721. return this._fail("Too many auth attempts");
  722. }
  723. },
  724. _negotiate_server_init: function () {
  725. if (this._sock.rQwait("server initialization", 24)) { return false; }
  726. /* Screen size */
  727. this._fb_width = this._sock.rQshift16();
  728. this._fb_height = this._sock.rQshift16();
  729. /* PIXEL_FORMAT */
  730. var bpp = this._sock.rQshift8();
  731. var depth = this._sock.rQshift8();
  732. var big_endian = this._sock.rQshift8();
  733. var true_color = this._sock.rQshift8();
  734. var red_max = this._sock.rQshift16();
  735. var green_max = this._sock.rQshift16();
  736. var blue_max = this._sock.rQshift16();
  737. var red_shift = this._sock.rQshift8();
  738. var green_shift = this._sock.rQshift8();
  739. var blue_shift = this._sock.rQshift8();
  740. this._sock.rQskipBytes(3); // padding
  741. // NB(directxman12): we don't want to call any callbacks or print messages until
  742. // *after* we're past the point where we could backtrack
  743. /* Connection name/title */
  744. var name_length = this._sock.rQshift32();
  745. if (this._sock.rQwait('server init name', name_length, 24)) { return false; }
  746. this._fb_name = Util.decodeUTF8(this._sock.rQshiftStr(name_length));
  747. if (this._rfb_tightvnc) {
  748. if (this._sock.rQwait('TightVNC extended server init header', 8, 24 + name_length)) { return false; }
  749. // In TightVNC mode, ServerInit message is extended
  750. var numServerMessages = this._sock.rQshift16();
  751. var numClientMessages = this._sock.rQshift16();
  752. var numEncodings = this._sock.rQshift16();
  753. this._sock.rQskipBytes(2); // padding
  754. var totalMessagesLength = (numServerMessages + numClientMessages + numEncodings) * 16;
  755. if (this._sock.rQwait('TightVNC extended server init header', totalMessagesLength, 32 + name_length)) { return false; }
  756. var i;
  757. for (i = 0; i < numServerMessages; i++) {
  758. var srvMsg = this._sock.rQshiftStr(16);
  759. }
  760. for (i = 0; i < numClientMessages; i++) {
  761. var clientMsg = this._sock.rQshiftStr(16);
  762. }
  763. for (i = 0; i < numEncodings; i++) {
  764. var encoding = this._sock.rQshiftStr(16);
  765. }
  766. }
  767. // NB(directxman12): these are down here so that we don't run them multiple times
  768. // if we backtrack
  769. Util.Info("Screen: " + this._fb_width + "x" + this._fb_height +
  770. ", bpp: " + bpp + ", depth: " + depth +
  771. ", big_endian: " + big_endian +
  772. ", true_color: " + true_color +
  773. ", red_max: " + red_max +
  774. ", green_max: " + green_max +
  775. ", blue_max: " + blue_max +
  776. ", red_shift: " + red_shift +
  777. ", green_shift: " + green_shift +
  778. ", blue_shift: " + blue_shift);
  779. if (big_endian !== 0) {
  780. Util.Warn("Server native endian is not little endian");
  781. }
  782. if (red_shift !== 16) {
  783. Util.Warn("Server native red-shift is not 16");
  784. }
  785. if (blue_shift !== 0) {
  786. Util.Warn("Server native blue-shift is not 0");
  787. }
  788. // we're past the point where we could backtrack, so it's safe to call this
  789. this._onDesktopName(this, this._fb_name);
  790. if (this._true_color && this._fb_name === "Intel(r) AMT KVM") {
  791. Util.Warn("Intel AMT KVM only supports 8/16 bit depths. Disabling true color");
  792. this._true_color = false;
  793. }
  794. this._display.set_true_color(this._true_color);
  795. this._onFBResize(this, this._fb_width, this._fb_height);
  796. this._display.resize(this._fb_width, this._fb_height);
  797. this._keyboard.grab();
  798. this._mouse.grab();
  799. if (this._true_color) {
  800. this._fb_Bpp = 4;
  801. this._fb_depth = 3;
  802. } else {
  803. this._fb_Bpp = 1;
  804. this._fb_depth = 1;
  805. }
  806. var response = RFB.messages.pixelFormat(this._fb_Bpp, this._fb_depth, this._true_color);
  807. response = response.concat(
  808. RFB.messages.clientEncodings(this._encodings, this._local_cursor, this._true_color));
  809. response = response.concat(
  810. RFB.messages.fbUpdateRequests(this._display.getCleanDirtyReset(),
  811. this._fb_width, this._fb_height));
  812. this._timing.fbu_rt_start = (new Date()).getTime();
  813. this._timing.pixels = 0;
  814. this._sock.send(response);
  815. this._checkEvents();
  816. if (this._encrypt) {
  817. this._updateState('normal', 'Connected (encrypted) to: ' + this._fb_name);
  818. } else {
  819. this._updateState('normal', 'Connected (unencrypted) to: ' + this._fb_name);
  820. }
  821. },
  822. _init_msg: function () {
  823. switch (this._rfb_state) {
  824. case 'ProtocolVersion':
  825. return this._negotiate_protocol_version();
  826. case 'Security':
  827. return this._negotiate_security();
  828. case 'Authentication':
  829. return this._negotiate_authentication();
  830. case 'SecurityResult':
  831. return this._handle_security_result();
  832. case 'ClientInitialisation':
  833. this._sock.send([this._shared ? 1 : 0]); // ClientInitialisation
  834. this._updateState('ServerInitialisation', "Authentication OK");
  835. return true;
  836. case 'ServerInitialisation':
  837. return this._negotiate_server_init();
  838. }
  839. },
  840. _handle_set_colour_map_msg: function () {
  841. Util.Debug("SetColorMapEntries");
  842. this._sock.rQskip8(); // Padding
  843. var first_colour = this._sock.rQshift16();
  844. var num_colours = this._sock.rQshift16();
  845. if (this._sock.rQwait('SetColorMapEntries', num_colours * 6, 6)) { return false; }
  846. for (var c = 0; c < num_colours; c++) {
  847. var red = parseInt(this._sock.rQshift16() / 256, 10);
  848. var green = parseInt(this._sock.rQshift16() / 256, 10);
  849. var blue = parseInt(this._sock.rQshift16() / 256, 10);
  850. this._display.set_colourMap([blue, green, red], first_colour + c);
  851. }
  852. Util.Debug("colourMap: " + this._display.get_colourMap());
  853. Util.Info("Registered " + num_colours + " colourMap entries");
  854. return true;
  855. },
  856. _handle_server_cut_text: function () {
  857. Util.Debug("ServerCutText");
  858. if (this._sock.rQwait("ServerCutText header", 7, 1)) { return false; }
  859. this._sock.rQskipBytes(3); // Padding
  860. var length = this._sock.rQshift32();
  861. if (this._sock.rQwait("ServerCutText", length, 8)) { return false; }
  862. var text = this._sock.rQshiftStr(length);
  863. this._onClipboard(this, text);
  864. return true;
  865. },
  866. _handle_xvp_msg: function () {
  867. if (this._sock.rQwait("XVP version and message", 3, 1)) { return false; }
  868. this._sock.rQskip8(); // Padding
  869. var xvp_ver = this._sock.rQshift8();
  870. var xvp_msg = this._sock.rQshift8();
  871. switch (xvp_msg) {
  872. case 0: // XVP_FAIL
  873. this._updateState(this._rfb_state, "Operation Failed");
  874. break;
  875. case 1: // XVP_INIT
  876. this._rfb_xvp_ver = xvp_ver;
  877. Util.Info("XVP extensions enabled (version " + this._rfb_xvp_ver + ")");
  878. this._onXvpInit(this._rfb_xvp_ver);
  879. break;
  880. default:
  881. this._fail("Disconnected: illegal server XVP message " + xvp_msg);
  882. break;
  883. }
  884. return true;
  885. },
  886. _normal_msg: function () {
  887. var msg_type;
  888. if (this._FBU.rects > 0) {
  889. msg_type = 0;
  890. } else {
  891. msg_type = this._sock.rQshift8();
  892. }
  893. switch (msg_type) {
  894. case 0: // FramebufferUpdate
  895. var ret = this._framebufferUpdate();
  896. if (ret) {
  897. this._sock.send(RFB.messages.fbUpdateRequests(this._display.getCleanDirtyReset(),
  898. this._fb_width, this._fb_height));
  899. }
  900. return ret;
  901. case 1: // SetColorMapEntries
  902. return this._handle_set_colour_map_msg();
  903. case 2: // Bell
  904. Util.Debug("Bell");
  905. this._onBell(this);
  906. return true;
  907. case 3: // ServerCutText
  908. return this._handle_server_cut_text();
  909. case 250: // XVP
  910. return this._handle_xvp_msg();
  911. default:
  912. this._fail("Disconnected: illegal server message type " + msg_type);
  913. Util.Debug("sock.rQslice(0, 30): " + this._sock.rQslice(0, 30));
  914. return true;
  915. }
  916. },
  917. _framebufferUpdate: function () {
  918. var ret = true;
  919. var now;
  920. if (this._FBU.rects === 0) {
  921. if (this._sock.rQwait("FBU header", 3, 1)) { return false; }
  922. this._sock.rQskip8(); // Padding
  923. this._FBU.rects = this._sock.rQshift16();
  924. this._FBU.bytes = 0;
  925. this._timing.cur_fbu = 0;
  926. if (this._timing.fbu_rt_start > 0) {
  927. now = (new Date()).getTime();
  928. Util.Info("First FBU latency: " + (now - this._timing.fbu_rt_start));
  929. }
  930. }
  931. while (this._FBU.rects > 0) {
  932. if (this._rfb_state !== "normal") { return false; }
  933. if (this._sock.rQwait("FBU", this._FBU.bytes)) { return false; }
  934. if (this._FBU.bytes === 0) {
  935. if (this._sock.rQwait("rect header", 12)) { return false; }
  936. /* New FramebufferUpdate */
  937. var hdr = this._sock.rQshiftBytes(12);
  938. this._FBU.x = (hdr[0] << 8) + hdr[1];
  939. this._FBU.y = (hdr[2] << 8) + hdr[3];
  940. this._FBU.width = (hdr[4] << 8) + hdr[5];
  941. this._FBU.height = (hdr[6] << 8) + hdr[7];
  942. this._FBU.encoding = parseInt((hdr[8] << 24) + (hdr[9] << 16) +
  943. (hdr[10] << 8) + hdr[11], 10);
  944. this._onFBUReceive(this,
  945. {'x': this._FBU.x, 'y': this._FBU.y,
  946. 'width': this._FBU.width, 'height': this._FBU.height,
  947. 'encoding': this._FBU.encoding,
  948. 'encodingName': this._encNames[this._FBU.encoding]});
  949. if (!this._encNames[this._FBU.encoding]) {
  950. this._fail("Disconnected: unsupported encoding " +
  951. this._FBU.encoding);
  952. return false;
  953. }
  954. }
  955. this._timing.last_fbu = (new Date()).getTime();
  956. ret = this._encHandlers[this._FBU.encoding]();
  957. now = (new Date()).getTime();
  958. this._timing.cur_fbu += (now - this._timing.last_fbu);
  959. if (ret) {
  960. this._encStats[this._FBU.encoding][0]++;
  961. this._encStats[this._FBU.encoding][1]++;
  962. this._timing.pixels += this._FBU.width * this._FBU.height;
  963. }
  964. if (this._timing.pixels >= (this._fb_width * this._fb_height)) {
  965. if ((this._FBU.width === this._fb_width && this._FBU.height === this._fb_height) ||
  966. this._timing.fbu_rt_start > 0) {
  967. this._timing.full_fbu_total += this._timing.cur_fbu;
  968. this._timing.full_fbu_cnt++;
  969. Util.Info("Timing of full FBU, curr: " +
  970. this._timing.cur_fbu + ", total: " +
  971. this._timing.full_fbu_total + ", cnt: " +
  972. this._timing.full_fbu_cnt + ", avg: " +
  973. (this._timing.full_fbu_total / this._timing.full_fbu_cnt));
  974. }
  975. if (this._timing.fbu_rt_start > 0) {
  976. var fbu_rt_diff = now - this._timing.fbu_rt_start;
  977. this._timing.fbu_rt_total += fbu_rt_diff;
  978. this._timing.fbu_rt_cnt++;
  979. Util.Info("full FBU round-trip, cur: " +
  980. fbu_rt_diff + ", total: " +
  981. this._timing.fbu_rt_total + ", cnt: " +
  982. this._timing.fbu_rt_cnt + ", avg: " +
  983. (this._timing.fbu_rt_total / this._timing.fbu_rt_cnt));
  984. this._timing.fbu_rt_start = 0;
  985. }
  986. }
  987. if (!ret) { return ret; } // need more data
  988. }
  989. this._onFBUComplete(this,
  990. {'x': this._FBU.x, 'y': this._FBU.y,
  991. 'width': this._FBU.width, 'height': this._FBU.height,
  992. 'encoding': this._FBU.encoding,
  993. 'encodingName': this._encNames[this._FBU.encoding]});
  994. return true; // We finished this FBU
  995. },
  996. };
  997. Util.make_properties(RFB, [
  998. ['target', 'wo', 'dom'], // VNC display rendering Canvas object
  999. ['focusContainer', 'wo', 'dom'], // DOM element that captures keyboard input
  1000. ['encrypt', 'rw', 'bool'], // Use TLS/SSL/wss encryption
  1001. ['true_color', 'rw', 'bool'], // Request true color pixel data
  1002. ['local_cursor', 'rw', 'bool'], // Request locally rendered cursor
  1003. ['shared', 'rw', 'bool'], // Request shared mode
  1004. ['view_only', 'rw', 'bool'], // Disable client mouse/keyboard
  1005. ['xvp_password_sep', 'rw', 'str'], // Separator for XVP password fields
  1006. ['disconnectTimeout', 'rw', 'int'], // Time (s) to wait for disconnection
  1007. ['wsProtocols', 'rw', 'arr'], // Protocols to use in the WebSocket connection
  1008. ['repeaterID', 'rw', 'str'], // [UltraVNC] RepeaterID to connect to
  1009. ['viewportDrag', 'rw', 'bool'], // Move the viewport on mouse drags
  1010. // Callback functions
  1011. ['onUpdateState', 'rw', 'func'], // onUpdateState(rfb, state, oldstate, statusMsg): RFB state update/change
  1012. ['onPasswordRequired', 'rw', 'func'], // onPasswordRequired(rfb): VNC password is required
  1013. ['onClipboard', 'rw', 'func'], // onClipboard(rfb, text): RFB clipboard contents received
  1014. ['onBell', 'rw', 'func'], // onBell(rfb): RFB Bell message received
  1015. ['onFBUReceive', 'rw', 'func'], // onFBUReceive(rfb, fbu): RFB FBU received but not yet processed
  1016. ['onFBUComplete', 'rw', 'func'], // onFBUComplete(rfb, fbu): RFB FBU received and processed
  1017. ['onFBResize', 'rw', 'func'], // onFBResize(rfb, width, height): frame buffer resized
  1018. ['onDesktopName', 'rw', 'func'], // onDesktopName(rfb, name): desktop name received
  1019. ['onXvpInit', 'rw', 'func'], // onXvpInit(version): XVP extensions active for this connection
  1020. ]);
  1021. RFB.prototype.set_local_cursor = function (cursor) {
  1022. if (!cursor || (cursor in {'0': 1, 'no': 1, 'false': 1})) {
  1023. this._local_cursor = false;
  1024. } else {
  1025. if (this._display.get_cursor_uri()) {
  1026. this._local_cursor = true;
  1027. } else {
  1028. Util.Warn("Browser does not support local cursor");
  1029. }
  1030. }
  1031. };
  1032. RFB.prototype.get_display = function () { return this._display; };
  1033. RFB.prototype.get_keyboard = function () { return this._keyboard; };
  1034. RFB.prototype.get_mouse = function () { return this._mouse; };
  1035. // Class Methods
  1036. RFB.messages = {
  1037. keyEvent: function (keysym, down) {
  1038. var arr = [4];
  1039. arr.push8(down);
  1040. arr.push16(0);
  1041. arr.push32(keysym);
  1042. return arr;
  1043. },
  1044. pointerEvent: function (x, y, mask) {
  1045. var arr = [5]; // msg-type
  1046. arr.push8(mask);
  1047. arr.push16(x);
  1048. arr.push16(y);
  1049. return arr;
  1050. },
  1051. // TODO(directxman12): make this unicode compatible?
  1052. clientCutText: function (text) {
  1053. var arr = [6]; // msg-type
  1054. arr.push8(0); // padding
  1055. arr.push8(0); // padding
  1056. arr.push8(0); // padding
  1057. arr.push32(text.length);
  1058. var n = text.length;
  1059. for (var i = 0; i < n; i++) {
  1060. arr.push(text.charCodeAt(i));
  1061. }
  1062. return arr;
  1063. },
  1064. pixelFormat: function (bpp, depth, true_color) {
  1065. var arr = [0]; // msg-type
  1066. arr.push8(0); // padding
  1067. arr.push8(0); // padding
  1068. arr.push8(0); // padding
  1069. arr.push8(bpp * 8); // bits-per-pixel
  1070. arr.push8(depth * 8); // depth
  1071. arr.push8(0); // little-endian
  1072. arr.push8(true_color ? 1 : 0); // true-color
  1073. arr.push16(255); // red-max
  1074. arr.push16(255); // green-max
  1075. arr.push16(255); // blue-max
  1076. arr.push8(16); // red-shift
  1077. arr.push8(8); // green-shift
  1078. arr.push8(0); // blue-shift
  1079. arr.push8(0); // padding
  1080. arr.push8(0); // padding
  1081. arr.push8(0); // padding
  1082. return arr;
  1083. },
  1084. clientEncodings: function (encodings, local_cursor, true_color) {
  1085. var i, encList = [];
  1086. for (i = 0; i < encodings.length; i++) {
  1087. if (encodings[i][0] === "Cursor" && !local_cursor) {
  1088. Util.Debug("Skipping Cursor pseudo-encoding");
  1089. } else if (encodings[i][0] === "TIGHT" && !true_color) {
  1090. // TODO: remove this when we have tight+non-true-color
  1091. Util.Warn("Skipping tight as it is only supported with true color");
  1092. } else {
  1093. encList.push(encodings[i][1]);
  1094. }
  1095. }
  1096. var arr = [2]; // msg-type
  1097. arr.push8(0); // padding
  1098. arr.push16(encList.length); // encoding count
  1099. for (i = 0; i < encList.length; i++) {
  1100. arr.push32(encList[i]);
  1101. }
  1102. return arr;
  1103. },
  1104. fbUpdateRequests: function (cleanDirty, fb_width, fb_height) {
  1105. var arr = [];
  1106. var cb = cleanDirty.cleanBox;
  1107. var w, h;
  1108. if (cb.w > 0 && cb.h > 0) {
  1109. w = typeof cb.w === "undefined" ? fb_width : cb.w;
  1110. h = typeof cb.h === "undefined" ? fb_height : cb.h;
  1111. // Request incremental for clean box
  1112. arr = arr.concat(RFB.messages.fbUpdateRequest(1, cb.x, cb.y, w, h));
  1113. }
  1114. for (var i = 0; i < cleanDirty.dirtyBoxes.length; i++) {
  1115. var db = cleanDirty.dirtyBoxes[i];
  1116. // Force all (non-incremental) for dirty box
  1117. w = typeof db.w === "undefined" ? fb_width : db.w;
  1118. h = typeof db.h === "undefined" ? fb_height : db.h;
  1119. arr = arr.concat(RFB.messages.fbUpdateRequest(0, db.x, db.y, w, h));
  1120. }
  1121. return arr;
  1122. },
  1123. fbUpdateRequest: function (incremental, x, y, w, h) {
  1124. if (typeof(x) === "undefined") { x = 0; }
  1125. if (typeof(y) === "undefined") { y = 0; }
  1126. var arr = [3]; // msg-type
  1127. arr.push8(incremental);
  1128. arr.push16(x);
  1129. arr.push16(y);
  1130. arr.push16(w);
  1131. arr.push16(h);
  1132. return arr;
  1133. }
  1134. };
  1135. RFB.genDES = function (password, challenge) {
  1136. var passwd = [];
  1137. for (var i = 0; i < password.length; i++) {
  1138. passwd.push(password.charCodeAt(i));
  1139. }
  1140. return (new DES(passwd)).encrypt(challenge);
  1141. };
  1142. RFB.extract_data_uri = function (arr) {
  1143. return ";base64," + Base64.encode(arr);
  1144. };
  1145. RFB.encodingHandlers = {
  1146. RAW: function () {
  1147. if (this._FBU.lines === 0) {
  1148. this._FBU.lines = this._FBU.height;
  1149. }
  1150. this._FBU.bytes = this._FBU.width * this._fb_Bpp; // at least a line
  1151. if (this._sock.rQwait("RAW", this._FBU.bytes)) { return false; }
  1152. var cur_y = this._FBU.y + (this._FBU.height - this._FBU.lines);
  1153. var curr_height = Math.min(this._FBU.lines,
  1154. Math.floor(this._sock.rQlen() / (this._FBU.width * this._fb_Bpp)));
  1155. this._display.blitImage(this._FBU.x, cur_y, this._FBU.width,
  1156. curr_height, this._sock.get_rQ(),
  1157. this._sock.get_rQi());
  1158. this._sock.rQskipBytes(this._FBU.width * curr_height * this._fb_Bpp);
  1159. this._FBU.lines -= curr_height;
  1160. if (this._FBU.lines > 0) {
  1161. this._FBU.bytes = this._FBU.width * this._fb_Bpp; // At least another line
  1162. } else {
  1163. this._FBU.rects--;
  1164. this._FBU.bytes = 0;
  1165. }
  1166. return true;
  1167. },
  1168. COPYRECT: function () {
  1169. this._FBU.bytes = 4;
  1170. if (this._sock.rQwait("COPYRECT", 4)) { return false; }
  1171. this._display.renderQ_push({
  1172. 'type': 'copy',
  1173. 'old_x': this._sock.rQshift16(),
  1174. 'old_y': this._sock.rQshift16(),
  1175. 'x': this._FBU.x,
  1176. 'y': this._FBU.y,
  1177. 'width': this._FBU.width,
  1178. 'height': this._FBU.height
  1179. });
  1180. this._FBU.rects--;
  1181. this._FBU.bytes = 0;
  1182. return true;
  1183. },
  1184. RRE: function () {
  1185. var color;
  1186. if (this._FBU.subrects === 0) {
  1187. this._FBU.bytes = 4 + this._fb_Bpp;
  1188. if (this._sock.rQwait("RRE", 4 + this._fb_Bpp)) { return false; }
  1189. this._FBU.subrects = this._sock.rQshift32();
  1190. color = this._sock.rQshiftBytes(this._fb_Bpp); // Background
  1191. this._display.fillRect(this._FBU.x, this._FBU.y, this._FBU.width, this._FBU.height, color);
  1192. }
  1193. while (this._FBU.subrects > 0 && this._sock.rQlen() >= (this._fb_Bpp + 8)) {
  1194. color = this._sock.rQshiftBytes(this._fb_Bpp);
  1195. var x = this._sock.rQshift16();
  1196. var y = this._sock.rQshift16();
  1197. var width = this._sock.rQshift16();
  1198. var height = this._sock.rQshift16();
  1199. this._display.fillRect(this._FBU.x + x, this._FBU.y + y, width, height, color);
  1200. this._FBU.subrects--;
  1201. }
  1202. if (this._FBU.subrects > 0) {
  1203. var chunk = Math.min(this._rre_chunk_sz, this._FBU.subrects);
  1204. this._FBU.bytes = (this._fb_Bpp + 8) * chunk;
  1205. } else {
  1206. this._FBU.rects--;
  1207. this._FBU.bytes = 0;
  1208. }
  1209. return true;
  1210. },
  1211. HEXTILE: function () {
  1212. var rQ = this._sock.get_rQ();
  1213. var rQi = this._sock.get_rQi();
  1214. if (this._FBU.tiles === 0) {
  1215. this._FBU.tiles_x = Math.ceil(this._FBU.width / 16);
  1216. this._FBU.tiles_y = Math.ceil(this._FBU.height / 16);
  1217. this._FBU.total_tiles = this._FBU.tiles_x * this._FBU.tiles_y;
  1218. this._FBU.tiles = this._FBU.total_tiles;
  1219. }
  1220. while (this._FBU.tiles > 0) {
  1221. this._FBU.bytes = 1;
  1222. if (this._sock.rQwait("HEXTILE subencoding", this._FBU.bytes)) { return false; }
  1223. var subencoding = rQ[rQi]; // Peek
  1224. if (subencoding > 30) { // Raw
  1225. this._fail("Disconnected: illegal hextile subencoding " + subencoding);
  1226. return false;
  1227. }
  1228. var subrects = 0;
  1229. var curr_tile = this._FBU.total_tiles - this._FBU.tiles;
  1230. var tile_x = curr_tile % this._FBU.tiles_x;
  1231. var tile_y = Math.floor(curr_tile / this._FBU.tiles_x);
  1232. var x = this._FBU.x + tile_x * 16;
  1233. var y = this._FBU.y + tile_y * 16;
  1234. var w = Math.min(16, (this._FBU.x + this._FBU.width) - x);
  1235. var h = Math.min(16, (this._FBU.y + this._FBU.height) - y);
  1236. // Figure out how much we are expecting
  1237. if (subencoding & 0x01) { // Raw
  1238. this._FBU.bytes += w * h * this._fb_Bpp;
  1239. } else {
  1240. if (subencoding & 0x02) { // Background
  1241. this._FBU.bytes += this._fb_Bpp;
  1242. }
  1243. if (subencoding & 0x04) { // Foreground
  1244. this._FBU.bytes += this._fb_Bpp;
  1245. }
  1246. if (subencoding & 0x08) { // AnySubrects
  1247. this._FBU.bytes++; // Since we aren't shifting it off
  1248. if (this._sock.rQwait("hextile subrects header", this._FBU.bytes)) { return false; }
  1249. subrects = rQ[rQi + this._FBU.bytes - 1]; // Peek
  1250. if (subencoding & 0x10) { // SubrectsColoured
  1251. this._FBU.bytes += subrects * (this._fb_Bpp + 2);
  1252. } else {
  1253. this._FBU.bytes += subrects * 2;
  1254. }
  1255. }
  1256. }
  1257. if (this._sock.rQwait("hextile", this._FBU.bytes)) { return false; }
  1258. // We know the encoding and have a whole tile
  1259. this._FBU.subencoding = rQ[rQi];
  1260. rQi++;
  1261. if (this._FBU.subencoding === 0) {
  1262. if (this._FBU.lastsubencoding & 0x01) {
  1263. // Weird: ignore blanks are RAW
  1264. Util.Debug(" Ignoring blank after RAW");
  1265. } else {
  1266. this._display.fillRect(x, y, w, h, rQ, rQi);
  1267. rQi += this._FBU.bytes - 1;
  1268. }
  1269. } else if (this._FBU.subencoding & 0x01) { // Raw
  1270. this._display.blitImage(x, y, w, h, rQ, rQi);
  1271. rQi += this._FBU.bytes - 1;
  1272. } else {
  1273. if (this._FBU.subencoding & 0x02) { // Background
  1274. this._FBU.background = rQ.slice(rQi, rQi + this._fb_Bpp);
  1275. rQi += this._fb_Bpp;
  1276. }
  1277. if (this._FBU.subencoding & 0x04) { // Foreground
  1278. this._FBU.foreground = rQ.slice(rQi, rQi + this._fb_Bpp);
  1279. rQi += this._fb_Bpp;
  1280. }
  1281. this._display.startTile(x, y, w, h, this._FBU.background);
  1282. if (this._FBU.subencoding & 0x08) { // AnySubrects
  1283. subrects = rQ[rQi];
  1284. rQi++;
  1285. for (var s = 0; s < subrects; s++) {
  1286. var color;
  1287. if (this._FBU.subencoding & 0x10) { // SubrectsColoured
  1288. color = rQ.slice(rQi, rQi + this._fb_Bpp);
  1289. rQi += this._fb_Bpp;
  1290. } else {
  1291. color = this._FBU.foreground;
  1292. }
  1293. var xy = rQ[rQi];
  1294. rQi++;
  1295. var sx = (xy >> 4);
  1296. var sy = (xy & 0x0f);
  1297. var wh = rQ[rQi];
  1298. rQi++;
  1299. var sw = (wh >> 4) + 1;
  1300. var sh = (wh & 0x0f) + 1;
  1301. this._display.subTile(sx, sy, sw, sh, color);
  1302. }
  1303. }
  1304. this._display.finishTile();
  1305. }
  1306. this._sock.set_rQi(rQi);
  1307. this._FBU.lastsubencoding = this._FBU.subencoding;
  1308. this._FBU.bytes = 0;
  1309. this._FBU.tiles--;
  1310. }
  1311. if (this._FBU.tiles === 0) {
  1312. this._FBU.rects--;
  1313. }
  1314. return true;
  1315. },
  1316. getTightCLength: function (arr) {
  1317. var header = 1, data = 0;
  1318. data += arr[0] & 0x7f;
  1319. if (arr[0] & 0x80) {
  1320. header++;
  1321. data += (arr[1] & 0x7f) << 7;
  1322. if (arr[1] & 0x80) {
  1323. header++;
  1324. data += arr[2] << 14;
  1325. }
  1326. }
  1327. return [header, data];
  1328. },
  1329. display_tight: function (isTightPNG) {
  1330. if (this._fb_depth === 1) {
  1331. this._fail("Tight protocol handler only implements true color mode");
  1332. }
  1333. this._FBU.bytes = 1; // compression-control byte
  1334. if (this._sock.rQwait("TIGHT compression-control", this._FBU.bytes)) { return false; }
  1335. var checksum = function (data) {
  1336. var sum = 0;
  1337. for (var i = 0; i < data.length; i++) {
  1338. sum += data[i];
  1339. if (sum > 65536) sum -= 65536;
  1340. }
  1341. return sum;
  1342. };
  1343. var resetStreams = 0;
  1344. var streamId = -1;
  1345. var decompress = function (data) {
  1346. for (var i = 0; i < 4; i++) {
  1347. if ((resetStreams >> i) & 1) {
  1348. this._FBU.zlibs[i].reset();
  1349. Util.Info("Reset zlib stream " + i);
  1350. }
  1351. }
  1352. var uncompressed = this._FBU.zlibs[streamId].uncompress(data, 0);
  1353. if (uncompressed.status !== 0) {
  1354. Util.Error("Invalid data in zlib stream");
  1355. }
  1356. return uncompressed.data;
  1357. }.bind(this);
  1358. var indexedToRGB = function (data, numColors, palette, width, height) {
  1359. // Convert indexed (palette based) image data to RGB
  1360. // TODO: reduce number of calculations inside loop
  1361. var dest = [];
  1362. var x, y, dp, sp;
  1363. if (numColors === 2) {
  1364. var w = Math.floor((width + 7) / 8);
  1365. var w1 = Math.floor(width / 8);
  1366. for (y = 0; y < height; y++) {
  1367. var b;
  1368. for (x = 0; x < w1; x++) {
  1369. for (b = 7; b >= 0; b--) {
  1370. dp = (y * width + x * 8 + 7 - b) * 3;
  1371. sp = (data[y * w + x] >> b & 1) * 3;
  1372. dest[dp] = palette[sp];
  1373. dest[dp + 1] = palette[sp + 1];
  1374. dest[dp + 2] = palette[sp + 2];
  1375. }
  1376. }
  1377. for (b = 7; b >= 8 - width % 8; b--) {
  1378. dp = (y * width + x * 8 + 7 - b) * 3;
  1379. sp = (data[y * w + x] >> b & 1) * 3;
  1380. dest[dp] = palette[sp];
  1381. dest[dp + 1] = palette[sp + 1];
  1382. dest[dp + 2] = palette[sp + 2];
  1383. }
  1384. }
  1385. } else {
  1386. for (y = 0; y < height; y++) {
  1387. for (x = 0; x < width; x++) {
  1388. dp = (y * width + x) * 3;
  1389. sp = data[y * width + x] * 3;
  1390. dest[dp] = palette[sp];
  1391. dest[dp + 1] = palette[sp + 1];
  1392. dest[dp + 2] = palette[sp + 2];
  1393. }
  1394. }
  1395. }
  1396. return dest;
  1397. }.bind(this);
  1398. var rQ = this._sock.get_rQ();
  1399. var rQi = this._sock.get_rQi();
  1400. var cmode, clength, data;
  1401. var handlePalette = function () {
  1402. var numColors = rQ[rQi + 2] + 1;
  1403. var paletteSize = numColors * this._fb_depth;
  1404. this._FBU.bytes += paletteSize;
  1405. if (this._sock.rQwait("TIGHT palette " + cmode, this._FBU.bytes)) { return false; }
  1406. var bpp = (numColors <= 2) ? 1 : 8;
  1407. var rowSize = Math.floor((this._FBU.width * bpp + 7) / 8);
  1408. var raw = false;
  1409. if (rowSize * this._FBU.height < 12) {
  1410. raw = true;
  1411. clength = [0, rowSize * this._FBU.height];
  1412. } else {
  1413. clength = RFB.encodingHandlers.getTightCLength(this._sock.rQslice(3 + paletteSize,
  1414. 3 + paletteSize + 3));
  1415. }
  1416. this._FBU.bytes += clength[0] + clength[1];
  1417. if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
  1418. // Shift ctl, filter id, num colors, palette entries, and clength off
  1419. this._sock.rQskipBytes(3);
  1420. var palette = this._sock.rQshiftBytes(paletteSize);
  1421. this._sock.rQskipBytes(clength[0]);
  1422. if (raw) {
  1423. data = this._sock.rQshiftBytes(clength[1]);
  1424. } else {
  1425. data = decompress(this._sock.rQshiftBytes(clength[1]));
  1426. }
  1427. // Convert indexed (palette based) image data to RGB
  1428. var rgb = indexedToRGB(data, numColors, palette, this._FBU.width, this._FBU.height);
  1429. this._display.renderQ_push({
  1430. 'type': 'blitRgb',
  1431. 'data': rgb,
  1432. 'x': this._FBU.x,
  1433. 'y': this._FBU.y,
  1434. 'width': this._FBU.width,
  1435. 'height': this._FBU.height
  1436. });
  1437. return true;
  1438. }.bind(this);
  1439. var handleCopy = function () {
  1440. var raw = false;
  1441. var uncompressedSize = this._FBU.width * this._FBU.height * this._fb_depth;
  1442. if (uncompressedSize < 12) {
  1443. raw = true;
  1444. clength = [0, uncompressedSize];
  1445. } else {
  1446. clength = RFB.encodingHandlers.getTightCLength(this._sock.rQslice(1, 4));
  1447. }
  1448. this._FBU.bytes = 1 + clength[0] + clength[1];
  1449. if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
  1450. // Shift ctl, clength off
  1451. this._sock.rQshiftBytes(1 + clength[0]);
  1452. if (raw) {
  1453. data = this._sock.rQshiftBytes(clength[1]);
  1454. } else {
  1455. data = decompress(this._sock.rQshiftBytes(clength[1]));
  1456. }
  1457. this._display.renderQ_push({
  1458. 'type': 'blitRgb',
  1459. 'data': data,
  1460. 'x': this._FBU.x,
  1461. 'y': this._FBU.y,
  1462. 'width': this._FBU.width,
  1463. 'height': this._FBU.height
  1464. });
  1465. return true;
  1466. }.bind(this);
  1467. var ctl = this._sock.rQpeek8();
  1468. // Keep tight reset bits
  1469. resetStreams = ctl & 0xF;
  1470. // Figure out filter
  1471. ctl = ctl >> 4;
  1472. streamId = ctl & 0x3;
  1473. if (ctl === 0x08) cmode = "fill";
  1474. else if (ctl === 0x09) cmode = "jpeg";
  1475. else if (ctl === 0x0A) cmode = "png";
  1476. else if (ctl & 0x04) cmode = "filter";
  1477. else if (ctl < 0x04) cmode = "copy";
  1478. else return this._fail("Illegal tight compression received, ctl: " + ctl);
  1479. if (isTightPNG && (cmode === "filter" || cmode === "copy")) {
  1480. return this._fail("filter/copy received in tightPNG mode");
  1481. }
  1482. switch (cmode) {
  1483. // fill use fb_depth because TPIXELs drop the padding byte
  1484. case "fill": // TPIXEL
  1485. this._FBU.bytes += this._fb_depth;
  1486. break;
  1487. case "jpeg": // max clength
  1488. this._FBU.bytes += 3;
  1489. break;
  1490. case "png": // max clength
  1491. this._FBU.bytes += 3;
  1492. break;
  1493. case "filter": // filter id + num colors if palette
  1494. this._FBU.bytes += 2;
  1495. break;
  1496. case "copy":
  1497. break;
  1498. }
  1499. if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
  1500. // Determine FBU.bytes
  1501. switch (cmode) {
  1502. case "fill":
  1503. this._sock.rQskip8(); // shift off ctl
  1504. var color = this._sock.rQshiftBytes(this._fb_depth);
  1505. this._display.renderQ_push({
  1506. 'type': 'fill',
  1507. 'x': this._FBU.x,
  1508. 'y': this._FBU.y,
  1509. 'width': this._FBU.width,
  1510. 'height': this._FBU.height,
  1511. 'color': [color[2], color[1], color[0]]
  1512. });
  1513. break;
  1514. case "png":
  1515. case "jpeg":
  1516. clength = RFB.encodingHandlers.getTightCLength(this._sock.rQslice(1, 4));
  1517. this._FBU.bytes = 1 + clength[0] + clength[1]; // ctl + clength size + jpeg-data
  1518. if (this._sock.rQwait("TIGHT " + cmode, this._FBU.bytes)) { return false; }
  1519. // We have everything, render it
  1520. this._sock.rQskipBytes(1 + clength[0]); // shift off clt + compact length
  1521. var img = new Image();
  1522. img.src = "data: image/" + cmode +
  1523. RFB.extract_data_uri(this._sock.rQshiftBytes(clength[1]));
  1524. this._display.renderQ_push({
  1525. 'type': 'img',
  1526. 'img': img,
  1527. 'x': this._FBU.x,
  1528. 'y': this._FBU.y
  1529. });
  1530. img = null;
  1531. break;
  1532. case "filter":
  1533. var filterId = rQ[rQi + 1];
  1534. if (filterId === 1) {
  1535. if (!handlePalette()) { return false; }
  1536. } else {
  1537. // Filter 0, Copy could be valid here, but servers don't send it as an explicit filter
  1538. // Filter 2, Gradient is valid but not use if jpeg is enabled
  1539. // TODO(directxman12): why aren't we just calling '_fail' here
  1540. throw new Error("Unsupported tight subencoding received, filter: " + filterId);
  1541. }
  1542. break;
  1543. case "copy":
  1544. if (!handleCopy()) { return false; }
  1545. break;
  1546. }
  1547. this._FBU.bytes = 0;
  1548. this._FBU.rects--;
  1549. return true;
  1550. },
  1551. TIGHT: function () { return this._encHandlers.display_tight(false); },
  1552. TIGHT_PNG: function () { return this._encHandlers.display_tight(true); },
  1553. last_rect: function () {
  1554. this._FBU.rects = 0;
  1555. return true;
  1556. },
  1557. DesktopSize: function () {
  1558. Util.Debug(">> set_desktopsize");
  1559. this._fb_width = this._FBU.width;
  1560. this._fb_height = this._FBU.height;
  1561. this._onFBResize(this, this._fb_width, this._fb_height);
  1562. this._display.resize(this._fb_width, this._fb_height);
  1563. this._timing.fbu_rt_start = (new Date()).getTime();
  1564. this._FBU.bytes = 0;
  1565. this._FBU.rects--;
  1566. Util.Debug("<< set_desktopsize");
  1567. return true;
  1568. },
  1569. Cursor: function () {
  1570. Util.Debug(">> set_cursor");
  1571. var x = this._FBU.x; // hotspot-x
  1572. var y = this._FBU.y; // hotspot-y
  1573. var w = this._FBU.width;
  1574. var h = this._FBU.height;
  1575. var pixelslength = w * h * this._fb_Bpp;
  1576. var masklength = Math.floor((w + 7) / 8) * h;
  1577. this._FBU.bytes = pixelslength + masklength;
  1578. if (this._sock.rQwait("cursor encoding", this._FBU.bytes)) { return false; }
  1579. this._display.changeCursor(this._sock.rQshiftBytes(pixelslength),
  1580. this._sock.rQshiftBytes(masklength),
  1581. x, y, w, h);
  1582. this._FBU.bytes = 0;
  1583. this._FBU.rects--;
  1584. Util.Debug("<< set_cursor");
  1585. return true;
  1586. },
  1587. JPEG_quality_lo: function () {
  1588. Util.Error("Server sent jpeg_quality pseudo-encoding");
  1589. },
  1590. compress_lo: function () {
  1591. Util.Error("Server sent compress level pseudo-encoding");
  1592. }
  1593. };
  1594. })();