Switch to URL for connect()

This is more in line with how other JavaScript APIs work.
This commit is contained in:
Pierre Ossman 2017-10-14 12:02:02 +02:00
parent 3a0010a3d0
commit 5b4e5d016e
6 changed files with 68 additions and 92 deletions

View File

@ -432,7 +432,7 @@ var UI = {
UI.inhibit_reconnect = false; UI.inhibit_reconnect = false;
UI.doneInitialResize = false; UI.doneInitialResize = false;
document.documentElement.classList.add("noVNC_connected"); document.documentElement.classList.add("noVNC_connected");
if (rfb && rfb.get_encrypt()) { if (UI.getSetting('encrypt')) {
msg = _("Connected (encrypted) to ") + UI.desktopName; msg = _("Connected (encrypted) to ") + UI.desktopName;
} else { } else {
msg = _("Connected (unencrypted) to ") + UI.desktopName; msg = _("Connected (unencrypted) to ") + UI.desktopName;
@ -1062,14 +1062,23 @@ var UI = {
UI.closeAllPanels(); UI.closeAllPanels();
UI.closeConnectPanel(); UI.closeConnectPanel();
UI.rfb.set_encrypt(UI.getSetting('encrypt'));
UI.rfb.set_shared(UI.getSetting('shared')); UI.rfb.set_shared(UI.getSetting('shared'));
UI.rfb.set_repeaterID(UI.getSetting('repeaterID')); UI.rfb.set_repeaterID(UI.getSetting('repeaterID'));
UI.updateLocalCursor(); UI.updateLocalCursor();
UI.updateViewOnly(); UI.updateViewOnly();
UI.rfb.connect(host, port, { password: password }, path); var url;
url = UI.getSetting('encrypt') ? 'wss' : 'ws';
url += '://' + host;
if(port) {
url += ':' + port;
}
url += '/' + path;
UI.rfb.connect(url, { password: password });
}, },
disconnect: function() { disconnect: function() {

View File

@ -35,10 +35,8 @@ export default function RFB(defaults) {
} }
// Connection details // Connection details
this._rfb_host = ''; this._url = '';
this._rfb_port = 5900;
this._rfb_credentials = {}; this._rfb_credentials = {};
this._rfb_path = '';
// Internal state // Internal state
this._rfb_connection_state = ''; this._rfb_connection_state = '';
@ -131,7 +129,6 @@ export default function RFB(defaults) {
// set the default value on user-facing properties // set the default value on user-facing properties
set_defaults(this, defaults, { set_defaults(this, defaults, {
'target': 'null', // VNC display rendering Canvas object 'target': 'null', // VNC display rendering Canvas object
'encrypt': false, // Use TLS/SSL/wss encryption
'local_cursor': false, // Request locally rendered cursor 'local_cursor': false, // Request locally rendered cursor
'shared': true, // Request shared mode 'shared': true, // Request shared mode
'view_only': false, // Disable client mouse/keyboard 'view_only': false, // Disable client mouse/keyboard
@ -247,15 +244,13 @@ export default function RFB(defaults) {
RFB.prototype = { RFB.prototype = {
// Public methods // Public methods
connect: function (host, port, creds, path) { connect: function (url, creds) {
this._rfb_host = host; this._url = url;
this._rfb_port = port;
this._rfb_credentials = (creds !== undefined) ? creds : {}; this._rfb_credentials = (creds !== undefined) ? creds : {};
this._rfb_path = (path !== undefined) ? path : "";
if (!this._rfb_host) { if (!url) {
return this._fail( this._fail(_("Must specify URL"));
_("Must set host")); return;
} }
this._rfb_init_state = ''; this._rfb_init_state = '';
@ -376,24 +371,11 @@ RFB.prototype = {
_connect: function () { _connect: function () {
Log.Debug(">> RFB.connect"); Log.Debug(">> RFB.connect");
var uri; Log.Info("connecting to " + this._url);
if (typeof UsingSocketIO !== 'undefined') {
uri = 'http';
} else {
uri = this._encrypt ? 'wss' : 'ws';
}
uri += '://' + this._rfb_host;
if(this._rfb_port) {
uri += ':' + this._rfb_port;
}
uri += '/' + this._rfb_path;
Log.Info("connecting to " + uri);
try { try {
// WebSocket.onopen transitions to the RFB init states // WebSocket.onopen transitions to the RFB init states
this._sock.open(uri, this._wsProtocols); this._sock.open(this._url, this._wsProtocols);
} catch (e) { } catch (e) {
if (e.name === 'SyntaxError') { if (e.name === 'SyntaxError') {
this._fail("Invalid host or port value given", e); this._fail("Invalid host or port value given", e);
@ -1464,7 +1446,6 @@ RFB.prototype = {
make_properties(RFB, [ make_properties(RFB, [
['target', 'wo', 'dom'], // VNC display rendering Canvas object ['target', 'wo', 'dom'], // VNC display rendering Canvas object
['encrypt', 'rw', 'bool'], // Use TLS/SSL/wss encryption
['local_cursor', 'rw', 'bool'], // Request locally rendered cursor ['local_cursor', 'rw', 'bool'], // Request locally rendered cursor
['shared', 'rw', 'bool'], // Request shared mode ['shared', 'rw', 'bool'], // Request shared mode
['view_only', 'rw', 'bool'], // Disable client mouse/keyboard ['view_only', 'rw', 'bool'], // Disable client mouse/keyboard

View File

@ -10,14 +10,14 @@ Each configuration option has a default value, which can be overridden
by a a configuration object passed to the constructor. Configuration by a a configuration object passed to the constructor. Configuration
options can then be read and modified after initialization with "get_*" options can then be read and modified after initialization with "get_*"
and "set_*" methods respectively. For example, the following and "set_*" methods respectively. For example, the following
initializes an RFB object with the 'encrypt' configuration option initializes an RFB object with the 'view_only' configuration option
enabled, then confirms it was set, then disables it: enabled, then confirms it was set, then disables it:
var rfb = new RFB({'encrypt': true}); var rfb = new RFB({'view_only': true});
if (rfb.get_encrypt()) { if (rfb.get_view_only()) {
alert("Encryption is set"); alert("View Only is set");
} }
rfb.set_encrypt(false); rfb.set_view_only(false);
Some attributes are read-only and cannot be changed. An exception will Some attributes are read-only and cannot be changed. An exception will
be thrown if an attempt is made to set one of these attributs. The be thrown if an attempt is made to set one of these attributs. The
@ -30,7 +30,6 @@ attribute mode is one of the following:
| name | type | mode | default | description | name | type | mode | default | description
| ----------------- | ----- | ---- | ---------- | ------------ | ----------------- | ----- | ---- | ---------- | ------------
| target | DOM | WO | null | Canvas element for rendering (passed to Display, Mouse and Keyboard) | target | DOM | WO | null | Canvas element for rendering (passed to Display, Mouse and Keyboard)
| encrypt | bool | RW | false | Use TLS/SSL encryption
| local_cursor | bool | RW | false | Request locally rendered cursor | local_cursor | bool | RW | false | Request locally rendered cursor
| shared | bool | RW | true | Request shared VNC mode | shared | bool | RW | true | Request shared VNC mode
| view_only | bool | RW | false | Disable client mouse/keyboard | view_only | bool | RW | false | Disable client mouse/keyboard
@ -52,7 +51,7 @@ object instance.
| name | parameters | description | name | parameters | description
| ------------------ | ------------------------------- | ------------ | ------------------ | ------------------------------- | ------------
| connect | (host, port, credentials, path) | Connect to the given host:port/path. Optional credentials and path. | connect | (url, credentials) | Connect to the given URL. Optional credentials.
| disconnect | () | Disconnect | disconnect | () | Disconnect
| sendCredentials | (credentials) | Send credentials after onCredentialsRequired callback | sendCredentials | (credentials) | Send credentials after onCredentialsRequired callback
| sendCtrlAltDel | () | Send Ctrl-Alt-Del key sequence | sendCtrlAltDel | () | Send Ctrl-Alt-Del key sequence

View File

@ -93,7 +93,7 @@ RecordingPlayer.prototype = {
this._running = true; this._running = true;
// launch the tests // launch the tests
this._rfb.connect('test', 0, 'bogus'); this._rfb.connect('wss://test');
this._queueNextPacket(); this._queueNextPacket();
}, },
@ -104,11 +104,9 @@ RecordingPlayer.prototype = {
this._rfb._sock.close = function () {}; this._rfb._sock.close = function () {};
this._rfb._sock.flush = function () {}; this._rfb._sock.flush = function () {};
this._rfb._checkEvents = function () {}; this._rfb._checkEvents = function () {};
this._rfb.connect = function (host, port, credentials, path) { this._rfb.connect = function (url) {
this._rfb_host = host; this._url = url;
this._rfb_port = port;
this._rfb_credentials = {}; this._rfb_credentials = {};
this._rfb_path = (path !== undefined) ? path : "";
this._sock.init('binary', 'ws'); this._sock.init('binary', 'ws');
this._rfb_connection_state = 'connecting'; this._rfb_connection_state = 'connecting';
this._rfb_init_state = 'ProtocolVersion'; this._rfb_init_state = 'ProtocolVersion';

View File

@ -73,15 +73,15 @@ describe('Remote Frame Buffer Protocol Client', function() {
beforeEach(function () { client._updateConnectionState = sinon.spy(); }); beforeEach(function () { client._updateConnectionState = sinon.spy(); });
it('should set the current state to "connecting"', function () { it('should set the current state to "connecting"', function () {
client.connect('host', 8675); client.connect('wss://host:8675');
expect(client._updateConnectionState).to.have.been.calledOnce; expect(client._updateConnectionState).to.have.been.calledOnce;
expect(client._updateConnectionState).to.have.been.calledWith('connecting'); expect(client._updateConnectionState).to.have.been.calledWith('connecting');
}); });
it('should not try to connect if we are missing a host', function () { it('should not try to connect if we are missing a URL', function () {
client._fail = sinon.spy(); client._fail = sinon.spy();
client._rfb_connection_state = ''; client._rfb_connection_state = '';
client.connect(undefined, 8675); client.connect(undefined);
expect(client._fail).to.have.been.calledOnce; expect(client._fail).to.have.been.calledOnce;
expect(client._updateConnectionState).to.not.have.been.called; expect(client._updateConnectionState).to.not.have.been.called;
expect(client._rfb_connection_state).to.equal(''); expect(client._rfb_connection_state).to.equal('');
@ -347,7 +347,6 @@ describe('Remote Frame Buffer Protocol Client', function() {
client.set_onUpdateState(sinon.spy()); client.set_onUpdateState(sinon.spy());
client._updateConnectionState('connecting'); client._updateConnectionState('connecting');
var spy = client.get_onUpdateState(); var spy = client.get_onUpdateState();
expect(spy).to.have.been.calledOnce;
expect(spy.args[0][1]).to.equal('connecting'); expect(spy.args[0][1]).to.equal('connecting');
}); });
@ -386,7 +385,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
beforeEach(function () { beforeEach(function () {
this.clock = sinon.useFakeTimers(); this.clock = sinon.useFakeTimers();
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
}); });
afterEach(function () { afterEach(function () {
@ -465,36 +464,12 @@ describe('Remote Frame Buffer Protocol Client', function() {
expect(client._sock.open).to.have.been.calledOnce; expect(client._sock.open).to.have.been.calledOnce;
}); });
it('should use wss:// to connect if encryption is enabled', function () { it('should use a url specified to connect', function () {
sinon.spy(client._sock, 'open'); sinon.spy(client._sock, 'open');
client.set_encrypt(true); client._url = 'ws://HOST:8675/PATH';
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._updateConnectionState('connecting');
expect(client._sock.open.args[0][0]).to.contain('wss://');
});
it('should use a uri with the host, port, and path specified to connect', function () {
sinon.spy(client._sock, 'open');
client.set_encrypt(false);
client._rfb_host = 'HOST';
client._rfb_port = 8675;
client._rfb_path = 'PATH';
client._updateConnectionState('connecting'); client._updateConnectionState('connecting');
expect(client._sock.open).to.have.been.calledWith('ws://HOST:8675/PATH'); expect(client._sock.open).to.have.been.calledWith('ws://HOST:8675/PATH');
}); });
it('should not include a port in the uri if not specified in connect', function () {
sinon.spy(client._sock, 'open');
client.set_encrypt(true);
client.connect('HOST', undefined)
expect(client._sock.open).to.have.been.calledWith('wss://HOST/');
});
}); });
describe('disconnecting', function () { describe('disconnecting', function () {
@ -502,7 +477,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
beforeEach(function () { beforeEach(function () {
this.clock = sinon.useFakeTimers(); this.clock = sinon.useFakeTimers();
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
}); });
afterEach(function () { afterEach(function () {
@ -603,7 +578,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
var client; var client;
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
client._sock._websocket._open(); client._sock._websocket._open();
}); });
@ -666,7 +641,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
var client; var client;
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
client._sock._websocket._open(); client._sock._websocket._open();
}); });
@ -706,7 +681,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_init_state = 'Security'; client._rfb_init_state = 'Security';
}); });
@ -770,7 +745,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_init_state = 'Security'; client._rfb_init_state = 'Security';
}); });
@ -819,7 +794,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_init_state = 'Security'; client._rfb_init_state = 'Security';
client._rfb_version = 3.8; client._rfb_version = 3.8;
@ -869,7 +844,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_init_state = 'Security'; client._rfb_init_state = 'Security';
client._rfb_version = 3.8; client._rfb_version = 3.8;
@ -926,7 +901,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_init_state = 'Security'; client._rfb_init_state = 'Security';
client._rfb_version = 3.8; client._rfb_version = 3.8;
@ -1016,7 +991,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_init_state = 'SecurityResult'; client._rfb_init_state = 'SecurityResult';
}); });
@ -1049,7 +1024,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_init_state = 'SecurityResult'; client._rfb_init_state = 'SecurityResult';
}); });
@ -1077,7 +1052,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_init_state = 'ServerInitialisation'; client._rfb_init_state = 'ServerInitialisation';
}); });
@ -1233,7 +1208,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_connection_state = 'connected'; client._rfb_connection_state = 'connected';
client._fb_name = 'some device'; client._fb_name = 'some device';
@ -1353,7 +1328,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_connection_state = 'connected'; client._rfb_connection_state = 'connected';
client._fb_name = 'some device'; client._fb_name = 'some device';
@ -1439,7 +1414,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
var client; var client;
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
client._sock._websocket._open(); client._sock._websocket._open();
client._rfb_connection_state = 'connected'; client._rfb_connection_state = 'connected';
client._fb_name = 'some device'; client._fb_name = 'some device';
@ -1997,7 +1972,7 @@ describe('Remote Frame Buffer Protocol Client', function() {
var client; var client;
beforeEach(function () { beforeEach(function () {
client = make_rfb(); client = make_rfb();
client.connect('host', 8675); client.connect('wss://host:8675');
this.clock = sinon.useFakeTimers(); this.clock = sinon.useFakeTimers();
}); });

View File

@ -156,7 +156,8 @@
break; break;
case 'connected': case 'connected':
doneInitialResize = false; doneInitialResize = false;
if (rfb && rfb.get_encrypt()) { if (WebUtil.getConfigVar('encrypt',
(window.location.protocol === "https:"))) {
status("Connected (encrypted) to " + status("Connected (encrypted) to " +
desktopName, "normal"); desktopName, "normal");
} else { } else {
@ -256,8 +257,6 @@
try { try {
rfb = new RFB({'target': document.getElementById('noVNC_canvas'), rfb = new RFB({'target': document.getElementById('noVNC_canvas'),
'encrypt': WebUtil.getConfigVar('encrypt',
(window.location.protocol === "https:")),
'repeaterID': WebUtil.getConfigVar('repeaterID', ''), 'repeaterID': WebUtil.getConfigVar('repeaterID', ''),
'local_cursor': WebUtil.getConfigVar('cursor', true), 'local_cursor': WebUtil.getConfigVar('cursor', true),
'shared': WebUtil.getConfigVar('shared', true), 'shared': WebUtil.getConfigVar('shared', true),
@ -273,7 +272,22 @@
return; // don't continue trying to connect return; // don't continue trying to connect
} }
rfb.connect(host, port, { password: password }, path); var url;
if (WebUtil.getConfigVar('encrypt',
(window.location.protocol === "https:"))) {
url = 'wss';
} else {
url = 'ws';
}
url += '://' + host;
if(port) {
url += ':' + port;
}
url += '/' + path;
rfb.connect(url, { password: password });
})(); })();
</script> </script>
</head> </head>