Merge pull request #651 from kanaka/separatestates

Separate visual state from protocol state
This commit is contained in:
Samuel Mannehed 2016-10-01 10:20:01 +02:00 committed by GitHub
commit 00df30999b
5 changed files with 606 additions and 434 deletions

158
app/ui.js
View File

@ -36,7 +36,8 @@ var UI;
UI = {
rfb_state: 'loaded',
connected: false,
desktopName: "",
resizeTimeout: null,
statusTimeout: null,
@ -338,16 +339,19 @@ var UI;
initRFB: function() {
try {
UI.rfb = new RFB({'target': document.getElementById('noVNC_canvas'),
'onNotification': UI.notification,
'onUpdateState': UI.updateState,
'onDisconnected': UI.disconnectFinished,
'onPasswordRequired': UI.passwordRequired,
'onXvpInit': UI.updateXvpButton,
'onClipboard': UI.clipboardReceive,
'onBell': UI.bell,
'onFBUComplete': UI.initialResize,
'onFBResize': UI.updateViewDrag,
'onDesktopName': UI.updateDocumentTitle});
'onDesktopName': UI.updateDesktopName});
return true;
} catch (exc) {
UI.updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc);
UI.showStatus('Unable to create RFB client -- ' + exc, 'error');
return false;
}
},
@ -358,35 +362,31 @@ var UI;
* VISUAL
* ------v------*/
updateState: function(rfb, state, oldstate, msg) {
UI.rfb_state = state;
if (typeof(msg) !== 'undefined') {
switch (state) {
case 'failed':
case 'fatal':
// zero means no timeout
UI.showStatus(msg, 'error', 0);
break;
case 'normal':
/* falls through */
case 'disconnected':
case 'loaded':
UI.showStatus(msg, 'normal');
break;
case 'password':
document.getElementById('noVNC_password_dlg')
.classList.add('noVNC_open');
setTimeout(function () {
document.getElementById(('noVNC_password_input').focus());
}, 100);
UI.showStatus(msg, 'warn');
break;
default:
UI.showStatus(msg, 'warn');
break;
}
updateState: function(rfb, state, oldstate) {
switch (state) {
case 'connecting':
UI.showStatus("Connecting");
break;
case 'connected':
UI.connected = true;
if (rfb && rfb.get_encrypt()) {
UI.showStatus("Connected (encrypted) to " +
UI.desktopName);
} else {
UI.showStatus("Connected (unencrypted) to " +
UI.desktopName);
}
break;
case 'disconnecting':
UI.showStatus("Disconnecting");
break;
case 'disconnected':
UI.connected = false;
UI.showStatus("Disconnected");
break;
default:
UI.showStatus("Invalid state", 'error');
break;
}
UI.updateVisualState();
@ -394,26 +394,24 @@ var UI;
// Disable/enable controls depending on connection state
updateVisualState: function() {
var connected = UI.rfb && UI.rfb_state === 'normal';
//Util.Debug(">> updateVisualState");
document.getElementById('noVNC_setting_encrypt').disabled = connected;
document.getElementById('noVNC_setting_true_color').disabled = connected;
document.getElementById('noVNC_setting_encrypt').disabled = UI.connected;
document.getElementById('noVNC_setting_true_color').disabled = UI.connected;
if (Util.browserSupportsCursorURIs()) {
document.getElementById('noVNC_setting_cursor').disabled = connected;
document.getElementById('noVNC_setting_cursor').disabled = UI.connected;
} else {
UI.updateSetting('cursor', !UI.isTouchDevice);
document.getElementById('noVNC_setting_cursor').disabled = true;
}
UI.enableDisableViewClip();
document.getElementById('noVNC_setting_resize').disabled = connected;
document.getElementById('noVNC_setting_shared').disabled = connected;
document.getElementById('noVNC_setting_view_only').disabled = connected;
document.getElementById('noVNC_setting_path').disabled = connected;
document.getElementById('noVNC_setting_repeaterID').disabled = connected;
document.getElementById('noVNC_setting_resize').disabled = UI.connected;
document.getElementById('noVNC_setting_shared').disabled = UI.connected;
document.getElementById('noVNC_setting_view_only').disabled = UI.connected;
document.getElementById('noVNC_setting_path').disabled = UI.connected;
document.getElementById('noVNC_setting_repeaterID').disabled = UI.connected;
if (connected) {
if (UI.connected) {
document.documentElement.classList.add("noVNC_connected");
UI.updateViewClip();
UI.setMouseButton(1);
@ -423,6 +421,7 @@ var UI;
} else {
document.documentElement.classList.remove("noVNC_connected");
UI.updateXvpButton(0);
UI.keepControlbar();
}
// State change disables viewport dragging.
@ -433,18 +432,6 @@ var UI;
document.getElementById('noVNC_password_dlg')
.classList.remove('noVNC_open');
switch (UI.rfb_state) {
case 'fatal':
case 'failed':
case 'disconnected':
UI.openConnectPanel();
break;
case 'loaded':
break;
default:
break;
}
//Util.Debug("<< updateVisualState");
},
@ -484,8 +471,8 @@ var UI;
time = 1500;
}
// A specified time of zero means no timeout
if (time != 0) {
// Error messages do not timeout
if (status_type !== 'error') {
UI.statusTimeout = window.setTimeout(UI.hideStatus, time);
}
},
@ -495,6 +482,10 @@ var UI;
document.getElementById('noVNC_status').classList.remove("noVNC_open");
},
notification: function (rfb, msg, level, options) {
UI.showStatus(msg, level);
},
activateControlbar: function(event) {
clearTimeout(UI.idleControlbarTimeout);
// We manipulate the anchor instead of the actual control
@ -939,8 +930,6 @@ var UI;
},
connect: function() {
UI.closeAllPanels();
var host = document.getElementById('noVNC_setting_host').value;
var port = document.getElementById('noVNC_setting_port').value;
var password = document.getElementById('noVNC_setting_password').value;
@ -953,11 +942,14 @@ var UI;
}
if ((!host) || (!port)) {
throw new Error("Must set host and port");
UI.showStatus("Must set host and port", 'error');
return;
}
if (!UI.initRFB()) return;
UI.closeAllPanels();
UI.rfb.set_encrypt(UI.getSetting('encrypt'));
UI.rfb.set_true_color(UI.getSetting('true_color'));
UI.rfb.set_local_cursor(UI.getSetting('cursor'));
@ -978,6 +970,34 @@ var UI;
// Don't display the connection settings until we're actually disconnected
},
disconnectFinished: function (rfb, reason) {
if (typeof reason !== 'undefined') {
UI.showStatus(reason, 'error');
}
UI.openConnectPanel();
},
/* ------^-------
* /CONNECTION
* ==============
* PASSWORD
* ------v------*/
passwordRequired: function(rfb, msg) {
document.getElementById('noVNC_password_dlg')
.classList.add('noVNC_open');
setTimeout(function () {
document.getElementById('noVNC_password_input').focus();
}, 100);
if (typeof msg === 'undefined') {
msg = "Password is required";
}
UI.showStatus(msg, "warning");
},
setPassword: function() {
UI.rfb.sendPassword(document.getElementById('noVNC_password_input').value);
document.getElementById('noVNC_password_dlg')
@ -986,7 +1006,7 @@ var UI;
},
/* ------^-------
* /CONNECTION
* /PASSWORD
* ==============
* FULLSCREEN
* ------v------*/
@ -1045,7 +1065,7 @@ var UI;
var screen = UI.screenSize();
if (screen && UI.rfb_state === 'normal' && UI.rfb.get_display()) {
if (screen && UI.connected && UI.rfb.get_display()) {
var display = UI.rfb.get_display();
var resizeMode = UI.getSetting('resize');
@ -1170,7 +1190,6 @@ var UI;
// Handle special cases where clipping is forced on/off or locked
enableDisableViewClip: function() {
var resizeSetting = document.getElementById('noVNC_setting_resize');
var connected = UI.rfb && UI.rfb_state === 'normal';
if (UI.isSafari) {
// Safari auto-hides the scrollbars which makes them
@ -1192,9 +1211,11 @@ var UI;
} else if (document.body.msRequestFullscreen && UI.rememberedClip !== null) {
// Restore view clip to what it was before fullscreen on IE
UI.setViewClip(UI.rememberedClipSetting);
document.getElementById('noVNC_setting_clip').disabled = connected || UI.isTouchDevice;
document.getElementById('noVNC_setting_clip').disabled =
UI.connected || UI.isTouchDevice;
} else {
document.getElementById('noVNC_setting_clip').disabled = connected || UI.isTouchDevice;
document.getElementById('noVNC_setting_clip').disabled =
UI.connected || UI.isTouchDevice;
if (UI.isTouchDevice) {
UI.setViewClip(true);
}
@ -1226,7 +1247,7 @@ var UI;
updateViewDrag: function() {
var clipping = false;
if (UI.rfb_state !== 'normal') return;
if (!UI.connected) return;
// Check if viewport drag is possible. It is only possible
// if the remote display is clipping the client display.
@ -1520,8 +1541,9 @@ var UI;
UI.rfb.get_mouse().set_focused(true);
},
// Display the desktop name in the document title
updateDocumentTitle: function(rfb, name) {
updateDesktopName: function(rfb, name) {
UI.desktopName = name;
// Display the desktop name in the document title
document.title = name + " - noVNC";
},

View File

@ -35,10 +35,12 @@
this._rfb_password = '';
this._rfb_path = '';
this._rfb_state = 'disconnected';
this._rfb_connection_state = '';
this._rfb_init_state = '';
this._rfb_version = 0;
this._rfb_max_version = 3.8;
this._rfb_auth_scheme = '';
this._rfb_disconnect_reason = "";
this._rfb_tightvnc = false;
this._rfb_xvp_ver = 0;
@ -157,8 +159,10 @@
'viewportDrag': false, // Move the viewport on mouse drags
// Callback functions
'onUpdateState': function () { }, // onUpdateState(rfb, state, oldstate, statusMsg): state update/change
'onPasswordRequired': function () { }, // onPasswordRequired(rfb): VNC password is required
'onUpdateState': function () { }, // onUpdateState(rfb, state, oldstate): connection state change
'onNotification': function () { }, // onNotification(rfb, msg, level, options): notification for UI
'onDisconnected': function () { }, // onDisconnected(rfb, reason): disconnection finished
'onPasswordRequired': function () { }, // onPasswordRequired(rfb, msg): VNC password is required
'onClipboard': function () { }, // onClipboard(rfb, text): RFB clipboard contents received
'onBell': function () { }, // onBell(rfb): RFB Bell message received
'onFBUReceive': function () { }, // onFBUReceive(rfb, fbu): RFB FBU received but not yet processed
@ -203,8 +207,10 @@
this._sock = new Websock();
this._sock.on('message', this._handle_message.bind(this));
this._sock.on('open', function () {
if (this._rfb_state === 'connect') {
this._updateState('ProtocolVersion', "Starting VNC handshake");
if ((this._rfb_connection_state === 'connecting') &&
(this._rfb_init_state === '')) {
this._rfb_init_state = 'ProtocolVersion';
Util.Debug("Starting VNC handshake");
} else {
this._fail("Got unexpected WebSocket connection");
}
@ -219,14 +225,19 @@
}
msg += ")";
}
if (this._rfb_state === 'disconnect') {
this._updateState('disconnected', 'VNC disconnected' + msg);
} else if (this._rfb_state === 'ProtocolVersion') {
this._fail('Failed to connect to server' + msg);
} else if (this._rfb_state in {'failed': 1, 'disconnected': 1}) {
Util.Error("Received onclose while disconnected" + msg);
} else {
this._fail("Server disconnected" + msg);
switch (this._rfb_connection_state) {
case 'disconnecting':
this._updateConnectionState('disconnected');
break;
case 'connecting':
this._fail('Failed to connect to server' + msg);
break;
case 'disconnected':
Util.Error("Received onclose while disconnected" + msg);
break;
default:
this._fail("Server disconnected" + msg);
break;
}
this._sock.off('close');
}.bind(this));
@ -235,10 +246,10 @@
});
this._init_vars();
this._cleanup();
var rmode = this._display.get_render_mode();
Util.Info("Using native WebSockets");
this._updateState('loaded', 'noVNC ready: native WebSockets, ' + rmode);
Util.Info("Using native WebSockets, render mode: " + rmode);
Util.Debug("<< RFB.constructor");
};
@ -256,12 +267,13 @@
return this._fail("Must set host and port");
}
this._updateState('connect');
this._rfb_init_state = '';
this._updateConnectionState('connecting');
return true;
},
disconnect: function () {
this._updateState('disconnect', 'Disconnecting');
this._updateConnectionState('disconnecting');
this._sock.off('error');
this._sock.off('message');
this._sock.off('open');
@ -269,12 +281,11 @@
sendPassword: function (passwd) {
this._rfb_password = passwd;
this._rfb_state = 'Authentication';
setTimeout(this._init_msg.bind(this), 0);
},
sendCtrlAltDel: function () {
if (this._rfb_state !== 'normal' || this._view_only) { return false; }
if (this._rfb_connection_state !== 'connected' || this._view_only) { return false; }
Util.Info("Sending Ctrl-Alt-Del");
RFB.messages.keyEvent(this._sock, KeyTable.XK_Control_L, 1);
@ -308,7 +319,7 @@
// Send a key press. If 'down' is not specified then send a down key
// followed by an up key.
sendKey: function (code, down) {
if (this._rfb_state !== "normal" || this._view_only) { return false; }
if (this._rfb_connection_state !== 'connected' || this._view_only) { return false; }
if (typeof down !== 'undefined') {
Util.Info("Sending key code (" + (down ? "down" : "up") + "): " + code);
RFB.messages.keyEvent(this._sock, code, down ? 1 : 0);
@ -321,14 +332,14 @@
},
clipboardPasteFrom: function (text) {
if (this._rfb_state !== 'normal') { return; }
if (this._rfb_connection_state !== 'connected') { return; }
RFB.messages.clientCutText(this._sock, text);
},
// Requests a change of remote desktop size. This message is an extension
// and may only be sent if we have received an ExtendedDesktopSize message
requestDesktopSize: function (width, height) {
if (this._rfb_state !== "normal") { return; }
if (this._rfb_connection_state !== 'connected') { return; }
if (this._supportsSetDesktopSize) {
RFB.messages.setDesktopSize(this._sock, width, height,
@ -397,7 +408,7 @@
}
},
_cleanupSocket: function (state) {
_cleanup: function () {
if (this._msgTimer) {
clearInterval(this._msgTimer);
this._msgTimer = null;
@ -406,148 +417,156 @@
if (this._display && this._display.get_context()) {
this._keyboard.ungrab();
this._mouse.ungrab();
if (state !== 'connect' && state !== 'loaded') {
this._display.defaultCursor();
}
if (Util.get_logging() !== 'debug' || state === 'loaded') {
this._display.defaultCursor();
if (Util.get_logging() !== 'debug') {
// Show noVNC logo on load and when disconnected, unless in
// debug mode
this._display.clear();
}
}
this._sock.close();
},
/*
* Page states:
* loaded - page load, equivalent to disconnected
* disconnected - idle state
* connect - starting to connect (to ProtocolVersion)
* normal - connected
* disconnect - starting to disconnect
* failed - abnormal disconnect
* fatal - failed to load page, or fatal error
*
* RFB protocol initialization states:
* ProtocolVersion
* Security
* Authentication
* password - waiting for password, not part of RFB
* SecurityResult
* ClientInitialization - not triggered by server message
* ServerInitialization (to normal)
* Connection states:
* connecting
* connected
* disconnecting
* disconnected - permanent state
*/
_updateState: function (state, statusMsg) {
var oldstate = this._rfb_state;
_updateConnectionState: function (state) {
var oldstate = this._rfb_connection_state;
if (state === oldstate) {
// Already here, ignore
Util.Debug("Already in state '" + state + "', ignoring");
return;
}
/*
* These are disconnected states. A previous connect may
* asynchronously cause a connection so make sure we are closed.
*/
if (state in {'disconnected': 1, 'loaded': 1, 'connect': 1,
'disconnect': 1, 'failed': 1, 'fatal': 1}) {
this._cleanupSocket(state);
// The 'disconnected' state is permanent for each RFB object
if (oldstate === 'disconnected') {
Util.Error("Tried changing state of a disconnected RFB object");
return;
}
if (oldstate === 'fatal') {
Util.Error('Fatal error, cannot continue');
}
this._rfb_connection_state = state;
var cmsg = typeof(statusMsg) !== 'undefined' ? (" Msg: " + statusMsg) : "";
var fullmsg = "New state '" + state + "', was '" + oldstate + "'." + cmsg;
if (state === 'failed' || state === 'fatal') {
Util.Error(cmsg);
} else {
Util.Warn(cmsg);
}
var smsg = "New state '" + state + "', was '" + oldstate + "'.";
Util.Debug(smsg);
if (oldstate === 'failed' && state === 'disconnected') {
// do disconnect action, but stay in failed state
this._rfb_state = 'failed';
} else {
this._rfb_state = state;
}
if (this._disconnTimer && this._rfb_state !== 'disconnect') {
if (this._disconnTimer && state !== 'disconnecting') {
Util.Debug("Clearing disconnect timer");
clearTimeout(this._disconnTimer);
this._disconnTimer = null;
this._sock.off('close'); // make sure we don't get a double event
}
this._onUpdateState(this, state, oldstate);
switch (state) {
case 'normal':
if (oldstate === 'disconnected' || oldstate === 'failed') {
Util.Error("Invalid transition from 'disconnected' or 'failed' to 'normal'");
case 'connected':
if (oldstate !== 'connecting') {
Util.Error("Bad transition to connected state, " +
"previous connection state: " + oldstate);
return;
}
break;
case 'connect':
this._init_vars();
this._connect();
// WebSocket.onopen transitions to 'ProtocolVersion'
case 'disconnected':
if (oldstate !== 'disconnecting') {
Util.Error("Bad transition to disconnected state, " +
"previous connection state: " + oldstate);
return;
}
if (this._rfb_disconnect_reason !== "") {
this._onDisconnected(this, this._rfb_disconnect_reason);
} else {
// No reason means clean disconnect
this._onDisconnected(this);
}
break;
case 'disconnect':
case 'connecting':
this._init_vars();
// WebSocket.onopen transitions to the RFB init states
this._connect();
break;
case 'disconnecting':
this._cleanup();
this._sock.close(); // transitions to 'disconnected'
this._disconnTimer = setTimeout(function () {
this._fail("Disconnect timeout");
this._rfb_disconnect_reason = "Disconnect timeout";
this._updateConnectionState('disconnected');
}.bind(this), this._disconnectTimeout * 1000);
this._print_stats();
// WebSocket.onclose transitions to 'disconnected'
break;
case 'failed':
if (oldstate === 'disconnected') {
Util.Error("Invalid transition from 'disconnected' to 'failed'");
} else if (oldstate === 'normal') {
Util.Error("Error while connected.");
} else if (oldstate === 'init') {
Util.Error("Error while initializing.");
}
// Make sure we transition to disconnected
setTimeout(function () {
this._updateState('disconnected');
}.bind(this), 50);
break;
default:
// No state change action to take
}
if (oldstate === 'failed' && state === 'disconnected') {
this._onUpdateState(this, state, oldstate);
} else {
this._onUpdateState(this, state, oldstate, statusMsg);
Util.Error("Unknown connection state: " + state);
return;
}
},
_fail: function (msg) {
this._updateState('failed', msg);
switch (this._rfb_connection_state) {
case 'disconnecting':
Util.Error("Error while disconnecting: " + msg);
break;
case 'connected':
Util.Error("Error while connected: " + msg);
break;
case 'connecting':
Util.Error("Error while connecting: " + msg);
break;
default:
Util.Error("RFB error: " + msg);
break;
}
this._rfb_disconnect_reason = msg;
this._updateConnectionState('disconnecting');
return false;
},
/*
* Send a notification to the UI. Valid levels are:
* 'normal'|'warn'|'error'
*
* NOTE: Options could be added in the future.
* NOTE: If this function is called multiple times, remember that the
* interface could be only showing the latest notification.
*/
_notification: function(msg, level, options) {
switch (level) {
case 'normal':
case 'warn':
case 'error':
Util.Debug("Notification[" + level + "]:" + msg);
break;
default:
Util.Error("Invalid notification level: " + level);
return;
}
if (options) {
this._onNotification(this, msg, level, options);
} else {
this._onNotification(this, msg, level);
}
},
_handle_message: function () {
if (this._sock.rQlen() === 0) {
Util.Warn("handle_message called on an empty receive queue");
return;
}
switch (this._rfb_state) {
switch (this._rfb_connection_state) {
case 'disconnected':
case 'failed':
Util.Error("Got data while disconnected");
break;
case 'normal':
case 'connected':
if (this._normal_msg() && this._sock.rQlen() > 0) {
// true means we can continue processing
// Give other events a chance to run
@ -614,7 +633,7 @@
if (this._view_only) { return; } // View only, skip mouse events
if (this._rfb_state !== "normal") { return; }
if (this._rfb_connection_state !== 'connected') { return; }
RFB.messages.pointerEvent(this._sock, this._display.absX(x), this._display.absY(y), this._mouse_buttonMask);
},
@ -641,7 +660,7 @@
if (this._view_only) { return; } // View only, skip mouse events
if (this._rfb_state !== "normal") { return; }
if (this._rfb_connection_state !== 'connected') { return; }
RFB.messages.pointerEvent(this._sock, this._display.absX(x), this._display.absY(y), this._mouse_buttonMask);
},
@ -693,7 +712,9 @@
var cversion = "00" + parseInt(this._rfb_version, 10) +
".00" + ((this._rfb_version * 10) % 10);
this._sock.send_string("RFB " + cversion + "\n");
this._updateState('Security', 'Sent ProtocolVersion: ' + cversion);
Util.Debug('Sent ProtocolVersion: ' + cversion);
this._rfb_init_state = 'Security';
},
_negotiate_security: function () {
@ -712,8 +733,17 @@
var types = this._sock.rQshiftBytes(num_types);
Util.Debug("Server security types: " + types);
for (var i = 0; i < types.length; i++) {
if (types[i] > this._rfb_auth_scheme && (types[i] <= 16 || types[i] == 22)) {
this._rfb_auth_scheme = types[i];
switch (types[i]) {
case 1: // None
case 2: // VNC Authentication
case 16: // Tight
case 22: // XVP
if (types[i] > this._rfb_auth_scheme) {
this._rfb_auth_scheme = types[i];
}
break;
default:
break;
}
}
@ -728,7 +758,9 @@
this._rfb_auth_scheme = this._sock.rQshift32();
}
this._updateState('Authentication', 'Authenticating using scheme: ' + this._rfb_auth_scheme);
this._rfb_init_state = 'Authentication';
Util.Debug('Authenticating using scheme: ' + this._rfb_auth_scheme);
return this._init_msg(); // jump to authentication
},
@ -737,9 +769,9 @@
var xvp_sep = this._xvp_password_sep;
var xvp_auth = this._rfb_password.split(xvp_sep);
if (xvp_auth.length < 3) {
this._updateState('password', 'XVP credentials required (user' + xvp_sep +
'target' + xvp_sep + 'password) -- got only ' + this._rfb_password);
this._onPasswordRequired(this);
var msg = 'XVP credentials required (user' + xvp_sep +
'target' + xvp_sep + 'password) -- got only ' + this._rfb_password;
this._onPasswordRequired(this, msg);
return false;
}
@ -755,9 +787,6 @@
_negotiate_std_vnc_auth: function () {
if (this._rfb_password.length === 0) {
// Notify via both callbacks since it's kind of
// an RFB state change and a UI interface issue
this._updateState('password', "Password Required");
this._onPasswordRequired(this);
return false;
}
@ -768,7 +797,7 @@
var challenge = Array.prototype.slice.call(this._sock.rQshiftBytes(16));
var response = RFB.genDES(this._rfb_password, challenge);
this._sock.send(response);
this._updateState("SecurityResult");
this._rfb_init_state = "SecurityResult";
return true;
},
@ -841,7 +870,7 @@
switch (authType) {
case 'STDVNOAUTH__': // no auth
this._updateState('SecurityResult');
this._rfb_init_state = 'SecurityResult';
return true;
case 'STDVVNCAUTH_': // VNC auth
this._rfb_auth_scheme = 2;
@ -865,10 +894,10 @@
case 1: // no auth
if (this._rfb_version >= 3.8) {
this._updateState('SecurityResult');
this._rfb_init_state = 'SecurityResult';
return true;
}
this._updateState('ClientInitialisation', "No auth required");
this._rfb_init_state = 'ClientInitialisation';
return this._init_msg();
case 22: // XVP auth
@ -889,7 +918,8 @@
if (this._sock.rQwait('VNC auth response ', 4)) { return false; }
switch (this._sock.rQshift32()) {
case 0: // OK
this._updateState('ClientInitialisation', 'Authentication OK');
this._rfb_init_state = 'ClientInitialisation';
Util.Debug('Authentication OK');
return this._init_msg();
case 1: // failed
if (this._rfb_version >= 3.8) {
@ -1016,16 +1046,20 @@
this._timing.fbu_rt_start = (new Date()).getTime();
this._timing.pixels = 0;
if (this._encrypt) {
this._updateState('normal', 'Connected (encrypted) to: ' + this._fb_name);
} else {
this._updateState('normal', 'Connected (unencrypted) to: ' + this._fb_name);
}
this._updateConnectionState('connected');
return true;
},
/* RFB protocol initialization states:
* ProtocolVersion
* Security
* Authentication
* SecurityResult
* ClientInitialization - not triggered by server message
* ServerInitialization
*/
_init_msg: function () {
switch (this._rfb_state) {
switch (this._rfb_init_state) {
case 'ProtocolVersion':
return this._negotiate_protocol_version();
@ -1040,14 +1074,15 @@
case 'ClientInitialisation':
this._sock.send([this._shared ? 1 : 0]); // ClientInitialisation
this._updateState('ServerInitialisation', "Authentication OK");
this._rfb_init_state = 'ServerInitialisation';
return true;
case 'ServerInitialisation':
return this._negotiate_server_init();
default:
return this._fail("Unknown state: " + this._rfb_state);
return this._fail("Unknown init state: " +
this._rfb_init_state);
}
},
@ -1134,7 +1169,8 @@
switch (xvp_msg) {
case 0: // XVP_FAIL
this._updateState(this._rfb_state, "Operation Failed");
Util.Error("Operation Failed");
this._notification("XVP Operation Failed", 'error');
break;
case 1: // XVP_INIT
this._rfb_xvp_ver = xvp_ver;
@ -1224,7 +1260,7 @@
}
while (this._FBU.rects > 0) {
if (this._rfb_state !== "normal") { return false; }
if (this._rfb_connection_state !== 'connected') { return false; }
if (this._sock.rQwait("FBU", this._FBU.bytes)) { return false; }
if (this._FBU.bytes === 0) {
@ -1325,8 +1361,10 @@
['viewportDrag', 'rw', 'bool'], // Move the viewport on mouse drags
// Callback functions
['onUpdateState', 'rw', 'func'], // onUpdateState(rfb, state, oldstate, statusMsg): RFB state update/change
['onPasswordRequired', 'rw', 'func'], // onPasswordRequired(rfb): VNC password is required
['onUpdateState', 'rw', 'func'], // onUpdateState(rfb, state, oldstate): connection state change
['onNotification', 'rw', 'func'], // onNotification(rfb, msg, level, options): notification for the UI
['onDisconnected', 'rw', 'func'], // onDisconnected(rfb, reason): disconnection finished
['onPasswordRequired', 'rw', 'func'], // onPasswordRequired(rfb, msg): VNC password is required
['onClipboard', 'rw', 'func'], // onClipboard(rfb, text): RFB clipboard contents received
['onBell', 'rw', 'func'], // onBell(rfb): RFB Bell message received
['onFBUReceive', 'rw', 'func'], // onFBUReceive(rfb, fbu): RFB FBU received but not yet processed
@ -2279,7 +2317,8 @@
msg = "Unknown reason";
break;
}
Util.Info("Server did not accept the resize request: " + msg);
this._notification("Server did not accept the resize request: "
+ msg, 'normal');
return true;
}

View File

@ -329,7 +329,7 @@ Util.Features = {xpath: !!(document.evaluate), air: !!(window.runtime), query: !
'presto': detectPresto(),
'trident': detectTrident(),
'webkit': detectInitialWebkit(),
'gecko': detectGecko(),
'gecko': detectGecko()
};
if (Util.Engine.webkit) {

View File

@ -65,42 +65,40 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
describe('#connect', function () {
beforeEach(function () { client._updateState = sinon.spy(); });
beforeEach(function () { client._updateConnectionState = sinon.spy(); });
it('should set the current state to "connect"', function () {
it('should set the current state to "connecting"', function () {
client.connect('host', 8675);
expect(client._updateState).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledWith('connect');
expect(client._updateConnectionState).to.have.been.calledOnce;
expect(client._updateConnectionState).to.have.been.calledWith('connecting');
});
it('should fail if we are missing a host', function () {
sinon.spy(client, '_fail');
it('should not try to connect if we are missing a host', function () {
client._fail = sinon.spy();
client._rfb_connection_state = '';
client.connect(undefined, 8675);
expect(client._fail).to.have.been.calledOnce;
expect(client._updateConnectionState).to.not.have.been.called;
expect(client._rfb_connection_state).to.equal('');
});
it('should fail if we are missing a port', function () {
sinon.spy(client, '_fail');
it('should not try to connect if we are missing a port', function () {
client._fail = sinon.spy();
client._rfb_connection_state = '';
client.connect('abc');
expect(client._fail).to.have.been.calledOnce;
});
it('should not update the state if we are missing a host or port', function () {
sinon.spy(client, '_fail');
client.connect('abc');
expect(client._fail).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledWith('failed');
expect(client._updateConnectionState).to.not.have.been.called;
expect(client._rfb_connection_state).to.equal('');
});
});
describe('#disconnect', function () {
beforeEach(function () { client._updateState = sinon.spy(); });
beforeEach(function () { client._updateConnectionState = sinon.spy(); });
it('should set the current state to "disconnect"', function () {
it('should set the current state to "disconnecting"', function () {
client.disconnect();
expect(client._updateState).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledWith('disconnect');
expect(client._updateConnectionState).to.have.been.calledOnce;
expect(client._updateConnectionState).to.have.been.calledWith('disconnecting');
});
it('should unregister error event handler', function () {
@ -126,10 +124,9 @@ describe('Remote Frame Buffer Protocol Client', function() {
beforeEach(function () { this.clock = sinon.useFakeTimers(); });
afterEach(function () { this.clock.restore(); });
it('should set the state to "Authentication"', function () {
client._rfb_state = "blah";
it('should set the rfb password properly"', function () {
client.sendPassword('pass');
expect(client._rfb_state).to.equal('Authentication');
expect(client._rfb_password).to.equal('pass');
});
it('should call init_msg "soon"', function () {
@ -146,7 +143,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_state = "normal";
client._rfb_connection_state = 'connected';
client._view_only = false;
});
@ -164,7 +161,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should not send the keys if we are not in a normal state', function () {
client._rfb_state = "broken";
client._rfb_connection_state = "broken";
client.sendCtrlAltDel();
expect(client._sock.flush).to.not.have.been.called;
});
@ -182,7 +179,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_state = "normal";
client._rfb_connection_state = 'connected';
client._view_only = false;
});
@ -202,7 +199,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should not send the key if we are not in a normal state', function () {
client._rfb_state = "broken";
client._rfb_connection_state = "broken";
client.sendKey(123);
expect(client._sock.flush).to.not.have.been.called;
});
@ -220,7 +217,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_state = "normal";
client._rfb_connection_state = 'connected';
client._view_only = false;
});
@ -232,7 +229,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should not send the text if we are not in a normal state', function () {
client._rfb_state = "broken";
client._rfb_connection_state = "broken";
client.clipboardPasteFrom('abc');
expect(client._sock.flush).to.not.have.been.called;
});
@ -244,7 +241,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_state = "normal";
client._rfb_connection_state = 'connected';
client._view_only = false;
client._supportsSetDesktopSize = true;
});
@ -274,7 +271,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should not send the request if we are not in a normal state', function () {
client._rfb_state = "broken";
client._rfb_connection_state = "broken";
client.requestDesktopSize(1,2);
expect(client._sock.flush).to.not.have.been.called;
});
@ -286,7 +283,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_state = "normal";
client._rfb_connection_state = 'connected';
client._view_only = false;
client._rfb_xvp_ver = 1;
});
@ -319,7 +316,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
describe('Misc Internals', function () {
describe('#_updateState', function () {
describe('#_updateConnectionState', function () {
var client;
beforeEach(function () {
this.clock = sinon.useFakeTimers();
@ -330,67 +327,136 @@ describe('Remote Frame Buffer Protocol Client', function() {
this.clock.restore();
});
it('should clear the disconnect timer if the state is not disconnect', function () {
it('should clear the disconnect timer if the state is not "disconnecting"', function () {
var spy = sinon.spy();
client._disconnTimer = setTimeout(spy, 50);
client._updateState('normal');
client._updateConnectionState('connected');
this.clock.tick(51);
expect(spy).to.not.have.been.called;
expect(client._disconnTimer).to.be.null;
});
it('should call the updateState callback', function () {
client.set_onUpdateState(sinon.spy());
client._updateConnectionState('a specific state');
var spy = client.get_onUpdateState();
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][1]).to.equal('a specific state');
});
it('should set the rfb_connection_state', function () {
client._updateConnectionState('a specific state');
expect(client._rfb_connection_state).to.equal('a specific state');
});
it('should not change the state when we are disconnected', function () {
client._rfb_connection_state = 'disconnected';
client._updateConnectionState('a specific state');
expect(client._rfb_connection_state).to.not.equal('a specific state');
});
it('should ignore state changes to the same state', function () {
client.set_onUpdateState(sinon.spy());
client._rfb_connection_state = 'a specific state';
client._updateConnectionState('a specific state');
var spy = client.get_onUpdateState();
expect(spy).to.not.have.been.called;
});
});
describe('#_fail', function () {
var client;
beforeEach(function () {
this.clock = sinon.useFakeTimers();
client = make_rfb();
client.connect('host', 8675);
});
afterEach(function () {
this.clock.restore();
});
it('should close the WebSocket connection', function () {
sinon.spy(client._sock, 'close');
client._fail();
expect(client._sock.close).to.have.been.calledOnce;
});
it('should transition to disconnected', function () {
sinon.spy(client, '_updateConnectionState');
client._fail();
this.clock.tick(2000);
expect(client._updateConnectionState).to.have.been.called;
expect(client._rfb_connection_state).to.equal('disconnected');
});
it('should set disconnect_reason', function () {
client._fail('a reason');
expect(client._rfb_disconnect_reason).to.equal('a reason');
});
it('should result in disconnect callback with message when reason given', function () {
client.set_onDisconnected(sinon.spy());
client._fail('a reason');
var spy = client.get_onDisconnected();
this.clock.tick(2000);
expect(spy).to.have.been.calledOnce;
expect(spy.args[0].length).to.equal(2);
expect(spy.args[0][1]).to.equal('a reason');
});
});
describe('#_notification', function () {
var client;
beforeEach(function () { client = make_rfb(); });
it('should call the notification callback', function () {
client.set_onNotification(sinon.spy());
client._notification('notify!', 'warn');
var spy = client.get_onNotification();
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][1]).to.equal('notify!');
expect(spy.args[0][2]).to.equal('warn');
});
it('should not call the notification callback when level is invalid', function () {
client.set_onNotification(sinon.spy());
client._notification('notify!', 'invalid');
var spy = client.get_onNotification();
expect(spy).to.not.have.been.called;
});
});
});
describe('Page States', function () {
describe('loaded', function () {
var client;
beforeEach(function () { client = make_rfb(); });
it('should close any open WebSocket connection', function () {
sinon.spy(client._sock, 'close');
client._updateState('loaded');
expect(client._sock.close).to.have.been.calledOnce;
});
});
describe('disconnected', function () {
var client;
beforeEach(function () { client = make_rfb(); });
it('should close any open WebSocket connection', function () {
sinon.spy(client._sock, 'close');
client._updateState('disconnected');
expect(client._sock.close).to.have.been.calledOnce;
});
});
describe('connect', function () {
describe('Connection States', function () {
describe('connecting', function () {
var client;
beforeEach(function () { client = make_rfb(); });
it('should reset the variable states', function () {
sinon.spy(client, '_init_vars');
client._updateState('connect');
client._updateConnectionState('connecting');
expect(client._init_vars).to.have.been.calledOnce;
});
it('should actually connect to the websocket', function () {
sinon.spy(client._sock, 'open');
client._updateState('connect');
client._updateConnectionState('connecting');
expect(client._sock.open).to.have.been.calledOnce;
});
it('should use wss:// to connect if encryption is enabled', function () {
sinon.spy(client._sock, 'open');
client.set_encrypt(true);
client._updateState('connect');
client._updateConnectionState('connecting');
expect(client._sock.open.args[0][0]).to.contain('wss://');
});
it('should use ws:// to connect if encryption is not enabled', function () {
sinon.spy(client._sock, 'open');
client.set_encrypt(true);
client._updateState('connect');
client._updateConnectionState('connecting');
expect(client._sock.open.args[0][0]).to.contain('wss://');
});
@ -400,18 +466,12 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._rfb_host = 'HOST';
client._rfb_port = 8675;
client._rfb_path = 'PATH';
client._updateState('connect');
client._updateConnectionState('connecting');
expect(client._sock.open).to.have.been.calledWith('ws://HOST:8675/PATH');
});
it('should attempt to close the websocket before we open an new one', function () {
sinon.spy(client._sock, 'close');
client._updateState('connect');
expect(client._sock.close).to.have.been.calledOnce;
});
});
describe('disconnect', function () {
describe('disconnecting', function () {
var client;
beforeEach(function () {
this.clock = sinon.useFakeTimers();
@ -423,73 +483,74 @@ describe('Remote Frame Buffer Protocol Client', function() {
this.clock.restore();
});
it('should fail if we do not call Websock.onclose within the disconnection timeout', function () {
it('should force disconnect if we do not call Websock.onclose within the disconnection timeout', function () {
sinon.spy(client, '_updateConnectionState');
client._sock._websocket.close = function () {}; // explicitly don't call onclose
client._updateState('disconnect');
client._updateConnectionState('disconnecting');
this.clock.tick(client.get_disconnectTimeout() * 1000);
expect(client._rfb_state).to.equal('failed');
expect(client._updateConnectionState).to.have.been.calledTwice;
expect(client._rfb_disconnect_reason).to.not.equal("");
expect(client._rfb_connection_state).to.equal("disconnected");
});
it('should not fail if Websock.onclose gets called within the disconnection timeout', function () {
client._updateState('disconnect');
client._updateConnectionState('disconnecting');
this.clock.tick(client.get_disconnectTimeout() * 500);
client._sock._websocket.close();
this.clock.tick(client.get_disconnectTimeout() * 500 + 1);
expect(client._rfb_state).to.equal('disconnected');
expect(client._rfb_connection_state).to.equal('disconnected');
});
it('should close the WebSocket connection', function () {
sinon.spy(client._sock, 'close');
client._updateState('disconnect');
expect(client._sock.close).to.have.been.calledTwice; // once on loaded, once on disconnect
});
});
describe('failed', function () {
var client;
beforeEach(function () {
this.clock = sinon.useFakeTimers();
client = make_rfb();
client.connect('host', 8675);
});
afterEach(function () {
this.clock.restore();
});
it('should close the WebSocket connection', function () {
sinon.spy(client._sock, 'close');
client._updateState('failed');
expect(client._sock.close).to.have.been.called;
});
it('should transition to disconnected but stay in failed state', function () {
client.set_onUpdateState(sinon.spy());
client._updateState('failed');
this.clock.tick(50);
expect(client._rfb_state).to.equal('failed');
var onUpdateState = client.get_onUpdateState();
expect(onUpdateState).to.have.been.called;
// it should be specifically the last call
expect(onUpdateState.args[onUpdateState.args.length - 1][1]).to.equal('disconnected');
expect(onUpdateState.args[onUpdateState.args.length - 1][2]).to.equal('failed');
});
});
describe('fatal', function () {
var client;
beforeEach(function () { client = make_rfb(); });
it('should close any open WebSocket connection', function () {
sinon.spy(client._sock, 'close');
client._updateState('fatal');
client._updateConnectionState('disconnecting');
expect(client._sock.close).to.have.been.calledOnce;
});
});
// NB(directxman12): Normal does *nothing* in updateState
describe('disconnected', function () {
var client;
beforeEach(function () { client = make_rfb(); });
it('should call the disconnect callback if the state is "disconnected"', function () {
client.set_onDisconnected(sinon.spy());
client._rfb_connection_state = 'disconnecting';
client._rfb_disconnect_reason = "error";
client._updateConnectionState('disconnected');
var spy = client.get_onDisconnected();
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][1]).to.equal("error");
});
it('should not call the disconnect callback if the state is not "disconnected"', function () {
client.set_onDisconnected(sinon.spy());
client._updateConnectionState('disconnecting');
var spy = client.get_onDisconnected();
expect(spy).to.not.have.been.called;
});
it('should call the disconnect callback without msg when no reason given', function () {
client.set_onDisconnected(sinon.spy());
client._rfb_connection_state = 'disconnecting';
client._rfb_disconnect_reason = "";
client._updateConnectionState('disconnected');
var spy = client.get_onDisconnected();
expect(spy).to.have.been.calledOnce;
expect(spy.args[0].length).to.equal(1);
});
it('should call the updateState callback before the disconnect callback', function () {
client.set_onDisconnected(sinon.spy());
client.set_onUpdateState(sinon.spy());
client._rfb_connection_state = 'other state';
client._updateConnectionState('disconnected');
var updateStateSpy = client.get_onUpdateState();
var disconnectSpy = client.get_onDisconnected();
expect(updateStateSpy.calledBefore(disconnectSpy)).to.be.true;
});
});
// NB(directxman12): Connected does *nothing* in updateConnectionState
});
describe('Protocol Initialization States', function () {
@ -570,8 +631,9 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should fail on an invalid version', function () {
sinon.spy(client, "_fail");
send_ver('002.000', client);
expect(client._rfb_state).to.equal('failed');
expect(client._fail).to.have.been.calledOnce;
});
});
@ -609,7 +671,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should transition to the Security state on successful negotiation', function () {
send_ver('003.008', client);
expect(client._rfb_state).to.equal('Security');
expect(client._rfb_init_state).to.equal('Security');
});
});
@ -620,7 +682,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'Security';
client._rfb_init_state = 'Security';
});
it('should simply receive the auth scheme when for versions < 3.7', function () {
@ -641,10 +703,11 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should fail if there are no supported schemes for versions >= 3.7', function () {
sinon.spy(client, "_fail");
client._rfb_version = 3.7;
var auth_schemes = [1, 32];
client._sock._websocket._receive_data(auth_schemes);
expect(client._rfb_state).to.equal('failed');
expect(client._fail).to.have.been.calledOnce;
});
it('should fail with the appropriate message if no types are sent for versions >= 3.7', function () {
@ -653,7 +716,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
sinon.spy(client, '_fail');
client._sock._websocket._receive_data(failure_data);
expect(client._fail).to.have.been.calledTwice;
expect(client._fail).to.have.been.calledOnce;
expect(client._fail).to.have.been.calledWith('Security failure: whoops');
});
@ -662,7 +725,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
var auth_schemes = [1, 1];
client._negotiate_authentication = sinon.spy();
client._sock._websocket._receive_data(auth_schemes);
expect(client._rfb_state).to.equal('Authentication');
expect(client._rfb_init_state).to.equal('Authentication');
expect(client._negotiate_authentication).to.have.been.calledOnce;
});
});
@ -674,7 +737,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'Security';
client._rfb_init_state = 'Security';
});
function send_security(type, cl) {
@ -693,28 +756,26 @@ describe('Remote Frame Buffer Protocol Client', function() {
sinon.spy(client, '_fail');
client._sock._websocket._receive_data(new Uint8Array(data));
expect(client._rfb_state).to.equal('failed');
expect(client._fail).to.have.been.calledWith('Auth failure: Whoopsies');
});
it('should transition straight to SecurityResult on "no auth" (1) for versions >= 3.8', function () {
client._rfb_version = 3.8;
send_security(1, client);
expect(client._rfb_state).to.equal('SecurityResult');
expect(client._rfb_init_state).to.equal('SecurityResult');
});
it('should transition straight to ClientInitialisation on "no auth" for versions < 3.8', function () {
it('should transition straight to ServerInitialisation on "no auth" for versions < 3.8', function () {
client._rfb_version = 3.7;
sinon.spy(client, '_updateState');
send_security(1, client);
expect(client._updateState).to.have.been.calledWith('ClientInitialisation');
expect(client._rfb_state).to.equal('ServerInitialisation');
expect(client._rfb_init_state).to.equal('ServerInitialisation');
});
it('should fail on an unknown auth scheme', function () {
sinon.spy(client, "_fail");
client._rfb_version = 3.8;
send_security(57, client);
expect(client._rfb_state).to.equal('failed');
expect(client._fail).to.have.been.calledOnce;
});
describe('VNC Authentication (type 2) Handler', function () {
@ -724,13 +785,17 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'Security';
client._rfb_init_state = 'Security';
client._rfb_version = 3.8;
});
it('should transition to the "password" state if missing a password', function () {
it('should call the passwordRequired callback if missing a password', function () {
client.set_onPasswordRequired(sinon.spy());
send_security(2, client);
expect(client._rfb_state).to.equal('password');
var spy = client.get_onPasswordRequired();
expect(client._rfb_password.length).to.equal(0);
expect(spy).to.have.been.calledOnce;
});
it('should encrypt the password with DES and then send it back', function () {
@ -754,7 +819,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
for (var i = 0; i < 16; i++) { challenge[i] = i; }
client._sock._websocket._receive_data(new Uint8Array(challenge));
expect(client._rfb_state).to.equal('SecurityResult');
expect(client._rfb_init_state).to.equal('SecurityResult');
});
});
@ -765,7 +830,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'Security';
client._rfb_init_state = 'Security';
client._rfb_version = 3.8;
});
@ -777,15 +842,23 @@ describe('Remote Frame Buffer Protocol Client', function() {
expect(client._negotiate_std_vnc_auth).to.have.been.calledOnce;
});
it('should transition to the "password" state if the passwords is missing', function() {
it('should call the passwordRequired callback if the password is missing', function() {
client.set_onPasswordRequired(sinon.spy());
client._rfb_password = '';
send_security(22, client);
expect(client._rfb_state).to.equal('password');
var spy = client.get_onPasswordRequired();
expect(client._rfb_password.length).to.equal(0);
expect(spy).to.have.been.calledOnce;
});
it('should transition to the "password" state if the passwords is improperly formatted', function() {
it('should call the passwordRequired callback if the password is improperly formatted', function() {
client.set_onPasswordRequired(sinon.spy());
client._rfb_password = 'user@target';
send_security(22, client);
expect(client._rfb_state).to.equal('password');
var spy = client.get_onPasswordRequired();
expect(spy).to.have.been.calledOnce;
});
it('should split the password, send the first two parts, and pass on the last part', function () {
@ -811,7 +884,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'Security';
client._rfb_init_state = 'Security';
client._rfb_version = 3.8;
send_security(16, client);
client._sock._websocket._get_sent_data(); // skip the security reply
@ -842,8 +915,9 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should fail if no supported tunnels are listed', function () {
sinon.spy(client, "_fail");
send_num_str_pairs([[123, 'OTHR', 'SOMETHNG']], client);
expect(client._rfb_state).to.equal('failed');
expect(client._fail).to.have.been.calledOnce;
});
it('should choose the notunnel tunnel type', function () {
@ -856,7 +930,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._sock._websocket._get_sent_data(); // skip the tunnel choice here
send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client);
expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 1]));
expect(client._rfb_state).to.equal('SecurityResult');
expect(client._rfb_init_state).to.equal('SecurityResult');
});
/*it('should attempt to use VNC auth over no auth when possible', function () {
@ -872,7 +946,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._rfb_tightvnc = true;
send_num_str_pairs([[1, 'STDV', 'NOAUTH__']], client);
expect(client._sock).to.have.sent(new Uint8Array([0, 0, 0, 1]));
expect(client._rfb_state).to.equal('SecurityResult');
expect(client._rfb_init_state).to.equal('SecurityResult');
});
it('should accept VNC authentication and transition to that', function () {
@ -885,9 +959,10 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should fail if there are no supported auth types', function () {
sinon.spy(client, "_fail");
client._rfb_tightvnc = true;
send_num_str_pairs([[23, 'stdv', 'badval__']], client);
expect(client._rfb_state).to.equal('failed');
expect(client._fail).to.have.been.calledOnce;
});
});
});
@ -899,14 +974,13 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'SecurityResult';
client._rfb_init_state = 'SecurityResult';
});
it('should fall through to ClientInitialisation on a response code of 0', function () {
client._updateState = sinon.spy();
it('should fall through to ServerInitialisation on a response code of 0', function () {
client._updateConnectionState = sinon.spy();
client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
expect(client._updateState).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledWith('ClientInitialisation');
expect(client._rfb_init_state).to.equal('ServerInitialisation');
});
it('should fail on an error code of 1 with the given message for versions >= 3.8', function () {
@ -914,14 +988,14 @@ describe('Remote Frame Buffer Protocol Client', function() {
sinon.spy(client, '_fail');
var failure_data = [0, 0, 0, 1, 0, 0, 0, 6, 119, 104, 111, 111, 112, 115];
client._sock._websocket._receive_data(new Uint8Array(failure_data));
expect(client._rfb_state).to.equal('failed');
expect(client._fail).to.have.been.calledWith('whoops');
});
it('should fail on an error code of 1 with a standard message for version < 3.8', function () {
sinon.spy(client, '_fail');
client._rfb_version = 3.7;
client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 1]));
expect(client._rfb_state).to.equal('failed');
expect(client._fail).to.have.been.calledWith('Authentication failure');
});
});
@ -932,12 +1006,12 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'SecurityResult';
client._rfb_init_state = 'SecurityResult';
});
it('should transition to the ServerInitialisation state', function () {
client._sock._websocket._receive_data(new Uint8Array([0, 0, 0, 0]));
expect(client._rfb_state).to.equal('ServerInitialisation');
expect(client._rfb_init_state).to.equal('ServerInitialisation');
});
it('should send 1 if we are in shared mode', function () {
@ -960,7 +1034,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'ServerInitialisation';
client._rfb_init_state = 'ServerInitialisation';
});
function send_server_init(opts, client) {
@ -1036,7 +1110,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
}
client._sock._websocket._receive_data(tight_data);
expect(client._rfb_state).to.equal('normal');
expect(client._rfb_connection_state).to.equal('connected');
});
it('should set the true color mode on the display to the configuration variable', function () {
@ -1098,9 +1172,9 @@ describe('Remote Frame Buffer Protocol Client', function() {
expect(client._sock).to.have.sent(expected._sQ);
});
it('should transition to the "normal" state', function () {
it('should transition to the "connected" state', function () {
send_server_init({}, client);
expect(client._rfb_state).to.equal('normal');
expect(client._rfb_connection_state).to.equal('connected');
});
});
});
@ -1112,7 +1186,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'normal';
client._rfb_connection_state = 'connected';
client._fb_name = 'some device';
client._fb_width = 640;
client._fb_height = 20;
@ -1125,7 +1199,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'normal';
client._rfb_connection_state = 'connected';
client._fb_name = 'some device';
client._fb_width = 640;
client._fb_height = 20;
@ -1283,10 +1357,10 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should fail on an unsupported encoding', function () {
client.set_onFBUReceive(sinon.spy());
sinon.spy(client, "_fail");
var rect_info = { x: 8, y: 11, width: 27, height: 32, encoding: 234 };
send_fbu_msg([rect_info], [[]], client);
expect(client._rfb_state).to.equal('failed');
expect(client._fail).to.have.been.calledOnce;
});
it('should be able to pause and resume receiving rects if not enought data', function () {
@ -1315,7 +1389,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'normal';
client._rfb_connection_state = 'connected';
client._fb_name = 'some device';
// a really small frame
client._fb_width = 4;
@ -1392,7 +1466,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'normal';
client._rfb_connection_state = 'connected';
client._fb_name = 'some device';
// a really small frame
client._fb_width = 4;
@ -1530,10 +1604,11 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should fail on an invalid subencoding', function () {
sinon.spy(client,"_fail");
var info = [{ x: 0, y: 0, width: 4, height: 4, encoding: 0x05 }];
var rects = [[45]]; // an invalid subencoding
send_fbu_msg(info, rects, client);
expect(client._rfb_state).to.equal('failed');
expect(client._fail).to.have.been.calledOnce;
});
});
@ -1568,7 +1643,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'normal';
client._rfb_connection_state = 'connected';
client._fb_name = 'some device';
client._supportsSetDesktopSize = false;
// a really small frame
@ -1710,17 +1785,18 @@ describe('Remote Frame Buffer Protocol Client', function() {
client = make_rfb();
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'normal';
client._rfb_connection_state = 'connected';
client._fb_name = 'some device';
client._fb_width = 27;
client._fb_height = 32;
});
it('should call updateState with a message on XVP_FAIL, but keep the same state', function () {
client._updateState = sinon.spy();
it('should send a notification on XVP_FAIL', function () {
client.set_onNotification(sinon.spy());
client._sock._websocket._receive_data(new Uint8Array([250, 0, 10, 0]));
expect(client._updateState).to.have.been.calledOnce;
expect(client._updateState).to.have.been.calledWith('normal', 'Operation Failed');
var spy = client.get_onNotification();
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][1]).to.equal('XVP Operation Failed');
});
it('should set the XVP version and fire the callback with the version on XVP_INIT', function () {
@ -1732,8 +1808,9 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should fail on unknown XVP message types', function () {
sinon.spy(client, "_fail");
client._sock._websocket._receive_data(new Uint8Array([250, 0, 10, 237]));
expect(client._rfb_state).to.equal('failed');
expect(client._fail).to.have.been.calledOnce;
});
});
@ -1825,8 +1902,9 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
it('should fail on an unknown message type', function () {
sinon.spy(client, "_fail");
client._sock._websocket._receive_data(new Uint8Array([87]));
expect(client._rfb_state).to.equal('failed');
expect(client._fail).to.have.been.calledOnce;
});
});
@ -1839,7 +1917,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
client._sock.open('ws://', 'binary');
client._sock._websocket._open();
sinon.spy(client._sock, 'flush');
client._rfb_state = 'normal';
client._rfb_connection_state = 'connected';
});
it('should not send button messages in view-only mode', function () {
@ -1980,15 +2058,15 @@ describe('Remote Frame Buffer Protocol Client', function() {
// message events
it ('should do nothing if we receive an empty message and have nothing in the queue', function () {
client.connect('host', 8675);
client._rfb_state = 'normal';
client._rfb_connection_state = 'connected';
client._normal_msg = sinon.spy();
client._sock._websocket._receive_data(new Uint8Array([]));
expect(client._normal_msg).to.not.have.been.called;
});
it('should handle a message in the normal state as a normal message', function () {
it('should handle a message in the connected state as a normal message', function () {
client.connect('host', 8675);
client._rfb_state = 'normal';
client._rfb_connection_state = 'connected';
client._normal_msg = sinon.spy();
client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
expect(client._normal_msg).to.have.been.calledOnce;
@ -1996,7 +2074,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should handle a message in any non-disconnected/failed state like an init message', function () {
client.connect('host', 8675);
client._rfb_state = 'ProtocolVersion';
client._rfb_init_state = 'ProtocolVersion';
client._init_msg = sinon.spy();
client._sock._websocket._receive_data(new Uint8Array([1, 2, 3]));
expect(client._init_msg).to.have.been.calledOnce;
@ -2005,7 +2083,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
it('should split up the handling of muplitle normal messages across 10ms intervals', function () {
client.connect('host', 8675);
client._sock._websocket._open();
client._rfb_state = 'normal';
client._rfb_connection_state = 'connected';
client.set_onBell(sinon.spy());
client._sock._websocket._receive_data(new Uint8Array([0x02, 0x02]));
expect(client.get_onBell()).to.have.been.calledOnce;
@ -2014,38 +2092,40 @@ describe('Remote Frame Buffer Protocol Client', function() {
});
// open events
it('should update the state to ProtocolVersion on open (if the state is "connect")', function () {
it('should update the state to ProtocolVersion on open (if the state is "connecting")', function () {
client.connect('host', 8675);
client._sock._websocket._open();
expect(client._rfb_state).to.equal('ProtocolVersion');
expect(client._rfb_init_state).to.equal('ProtocolVersion');
});
it('should fail if we are not currently ready to connect and we get an "open" event', function () {
sinon.spy(client, "_fail");
client.connect('host', 8675);
client._rfb_state = 'some_other_state';
client._rfb_connection_state = 'some_other_state';
client._sock._websocket._open();
expect(client._rfb_state).to.equal('failed');
expect(client._fail).to.have.been.calledOnce;
});
// close events
it('should transition to "disconnected" from "disconnect" on a close event', function () {
it('should transition to "disconnected" from "disconnecting" on a close event', function () {
client.connect('host', 8675);
client._rfb_state = 'disconnect';
client._rfb_connection_state = 'disconnecting';
client._sock._websocket.close();
expect(client._rfb_state).to.equal('disconnected');
expect(client._rfb_connection_state).to.equal('disconnected');
});
it('should transition to failed if we get a close event from any non-"disconnection" state', function () {
sinon.spy(client, "_fail");
client.connect('host', 8675);
client._rfb_state = 'normal';
client._rfb_connection_state = 'connected';
client._sock._websocket.close();
expect(client._rfb_state).to.equal('failed');
expect(client._fail).to.have.been.calledOnce;
});
it('should unregister close event handler', function () {
sinon.spy(client._sock, 'off');
client.connect('host', 8675);
client._rfb_state = 'disconnect';
client._rfb_connection_state = 'disconnecting';
client._sock._websocket.close();
expect(client._sock.off).to.have.been.calledWith('close');
});

View File

@ -86,6 +86,7 @@
var rfb;
var resizeTimeout;
var desktopName;
function UIresize() {
@ -93,24 +94,28 @@
var innerW = window.innerWidth;
var innerH = window.innerHeight;
var controlbarH = document.getElementById('noVNC_status_bar').offsetHeight;
var padding = 5;
if (innerW !== undefined && innerH !== undefined)
rfb.requestDesktopSize(innerW, innerH - controlbarH - padding);
rfb.requestDesktopSize(innerW, innerH - controlbarH);
}
}
function FBUComplete(rfb, fbu) {
UIresize();
rfb.set_onFBUComplete(function() { });
}
function passwordRequired(rfb) {
var msg;
msg = '<form onsubmit="return setPassword();"';
msg += ' style="margin-bottom: 0px">';
msg += 'Password Required: ';
msg += '<input type=password size=10 id="password_input" class="noVNC_status">';
msg += '<\/form>';
document.getElementById('noVNC_status_bar').setAttribute("class", "noVNC_status_warn");
document.getElementById('noVNC_status').innerHTML = msg;
function updateDesktopName(rfb, name) {
desktopName = name;
}
function passwordRequired(rfb, msg) {
if (typeof msg === 'undefined') {
msg = 'Password Required: ';
}
var html;
html = '<form onsubmit="return setPassword();"';
html += ' style="margin-bottom: 0px">';
html += msg;
html += '<input type=password size=10 id="password_input" class="noVNC_status">';
html += '<\/form>';
status(html, "warn");
}
function setPassword() {
rfb.sendPassword(document.getElementById('password_input').value);
@ -132,32 +137,55 @@
rfb.xvpReset();
return false;
}
function updateState(rfb, state, oldstate, msg) {
var s, sb, cad, level;
s = document.getElementById('noVNC_status');
sb = document.getElementById('noVNC_status_bar');
cad = document.getElementById('sendCtrlAltDelButton');
function status(text, level) {
switch (level) {
case 'normal':
case 'warn':
case 'error':
break;
default:
level = "warn";
}
document.getElementById('noVNC_status_bar').setAttribute("class", "noVNC_status_" + level);
document.getElementById('noVNC_status').innerHTML = text;
}
function updateState(rfb, state, oldstate) {
var cad = document.getElementById('sendCtrlAltDelButton');
var encrypt = (rfb && rfb.get_encrypt()) ? 'encrypted' : 'unencrypted';
switch (state) {
case 'failed': level = "error"; break;
case 'fatal': level = "error"; break;
case 'normal': level = "normal"; break;
case 'disconnected': level = "normal"; break;
case 'loaded': level = "normal"; break;
default: level = "warn"; break;
case 'connecting':
status("Connecting", "normal");
break;
case 'connected':
status("Connected (" + encrypt + ") to: " + desktopName, "normal");
break;
case 'disconnecting':
status("Disconnecting", "normal");
break;
case 'disconnected':
status("Disconnected", "normal");
break;
default:
status(state, "warn");
break;
}
if (state === "normal") {
if (state === 'connected') {
cad.disabled = false;
} else {
cad.disabled = true;
xvpInit(0);
}
if (typeof(msg) !== 'undefined') {
sb.setAttribute("class", "noVNC_status_" + level);
s.innerHTML = msg;
}
function disconnected(rfb, reason) {
if (typeof(reason) !== 'undefined') {
status(reason, "error");
}
}
function notification(rfb, msg, level, options) {
status(msg, level);
}
window.onresize = function () {
// When the window has been resized, wait until the size remains
@ -220,7 +248,7 @@
}
if ((!host) || (!port)) {
updateState(null, 'fatal', null, 'Must specify host and port in URL');
status('Must specify host and port in URL', 'error');
return;
}
@ -233,12 +261,15 @@
'local_cursor': WebUtil.getConfigVar('cursor', true),
'shared': WebUtil.getConfigVar('shared', true),
'view_only': WebUtil.getConfigVar('view_only', false),
'onNotification': notification,
'onUpdateState': updateState,
'onDisconnected': disconnected,
'onXvpInit': xvpInit,
'onPasswordRequired': passwordRequired,
'onFBUComplete': FBUComplete});
'onFBUComplete': FBUComplete,
'onDesktopName': updateDesktopName});
} catch (exc) {
updateState(null, 'fatal', null, 'Unable to create RFB client -- ' + exc);
status('Unable to create RFB client -- ' + exc, 'error');
return; // don't continue trying to connect
}