Minor improvments

* Added "reconnect" and "onsessionchange" message types to use with
  embedded terminal. Usage examples were added to misc/embedded.html
  file.
* Improved code for unix domain socket functionality.
This commit is contained in:
KLuka 2015-07-09 21:49:04 +02:00
parent bdca920abc
commit 458cd7aa8f
4 changed files with 66 additions and 16 deletions

View file

@ -304,7 +304,7 @@ void initServer(struct Server *server, int localhostOnly, int portMin,
struct sockaddr_un serverAddr = { 0 }; struct sockaddr_un serverAddr = { 0 };
serverAddr.sun_family = AF_UNIX; serverAddr.sun_family = AF_UNIX;
strcpy(serverAddr.sun_path, unixDomainPath); memcpy(serverAddr.sun_path, unixDomainPath, sizeof(serverAddr.sun_path));
int servlen = sizeof(serverAddr.sun_family) int servlen = sizeof(serverAddr.sun_family)
+ strlen(unixDomainPath); + strlen(unixDomainPath);

View file

@ -40,15 +40,30 @@
Following types of message can be sent to shellinabox: Following types of message can be sent to shellinabox:
input - writes content of data field to terminal * input
output - enables passing of output to parent window (data field must be set writes content of data field to terminal
to enable, disable or toggle)
session - request sessions status * output
enables passing of output to parent window (data field must be set
to enable, disable or toggle)
* session
request sessions status
* onsessionchange
enables passing session status to parent window (data field must be
set to enable, disable or toggle)
* reconnect
reconnects the terminal
Following types of messages can be received from shellinabox: Following types of messages can be received from shellinabox:
output - data field contains terminal output * output
session - data field contains session status (active or closed) data field contains terminal output
* session
data field contains session status (alive or closed)
Example for passing command to Shell In A Box frame: Example for passing command to Shell In A Box frame:
@ -99,7 +114,9 @@
<input type="button" id="execute" value="Execute"></input> <input type="button" id="execute" value="Execute"></input>
<input type="button" id="output-enable" value="Output Enable"></input> <input type="button" id="output-enable" value="Output Enable"></input>
<input type="button" id="output-disable" value="Output Disable"></input> <input type="button" id="output-disable" value="Output Disable"></input>
<input type="button" id="reconnect" value="Reconnect"></input>
<input type="button" id="session-reload" value="Session Status"></input> <input type="button" id="session-reload" value="Session Status"></input>
<input type="button" id="session-toggle" value="Session Status Toggle"></input>
</div> </div>
<p id="session">Session status: ???</p> <p id="session">Session status: ???</p>
@ -164,6 +181,23 @@
iframe.contentWindow.postMessage(message, url); iframe.contentWindow.postMessage(message, url);
}); });
document.getElementById("session-toggle").addEventListener("click", function() {
// Toggles shellinabox session status reporting
var message = JSON.stringify({
type : 'onsessionchange',
data : 'toggle'
});
iframe.contentWindow.postMessage(message, url);
});
document.getElementById("reconnect").addEventListener("click", function() {
// Request shellianbox session status
var message = JSON.stringify({
type : 'reconnect'
});
iframe.contentWindow.postMessage(message, url);
});
// Receive response from shellinabox // Receive response from shellinabox
window.addEventListener("message", function(message) { window.addEventListener("message", function(message) {

View file

@ -109,7 +109,8 @@ function ShellInABox(url, container) {
this.pendingKeys = ''; this.pendingKeys = '';
this.keysInFlight = false; this.keysInFlight = false;
this.connected = false; this.connected = false;
this.replayOutput = false; this.replayOnOutput = false;
this.replayOnSession = false;
this.superClass.constructor.call(this, container); this.superClass.constructor.call(this, container);
@ -135,6 +136,9 @@ ShellInABox.prototype.sessionClosed = function() {
this.vt100('Session closed.'); this.vt100('Session closed.');
} }
this.showReconnect(true); this.showReconnect(true);
if (this.replayOnSession) {
this.messageReplay('session', 'closed');
}
} catch (e) { } catch (e) {
} }
}; };
@ -195,7 +199,7 @@ ShellInABox.prototype.onReadyStateChange = function(request) {
this.connected = true; this.connected = true;
var response = eval('(' + request.responseText + ')'); var response = eval('(' + request.responseText + ')');
if (response.data) { if (response.data) {
if (this.replayOutput) { if (this.replayOnOutput) {
this.messageReplay('output', response.data); this.messageReplay('output', response.data);
} }
this.vt100(response.data); this.vt100(response.data);
@ -205,6 +209,9 @@ ShellInABox.prototype.onReadyStateChange = function(request) {
this.session && this.session != response.session) { this.session && this.session != response.session) {
this.sessionClosed(); this.sessionClosed();
} else { } else {
if (this.replayOnSession && !this.session && response.session) {
this.messageReplay('session', 'alive');
}
this.session = response.session; this.session = response.session;
this.sendRequest(request); this.sendRequest(request);
} }
@ -425,18 +432,27 @@ ShellInABox.prototype.messageReceive = function (message) {
break; break;
case 'output' : case 'output' :
// Enable, disable or toggle passing terminal output to parent window. // Enable, disable or toggle passing terminal output to parent window.
if (decoded.data === 'enable') { switch (decoded.data) {
this.replayOutput = true; case 'enable' : this.replayOnOutput = true; break;
} else if (decoded.data === 'disable') { case 'disable' : this.replayOnOutput = false; break;
this.replayOutput = false; case 'toggle' : this.replayOnOutput = !this.replayOnOutput; break;
} else if (decoded.data === 'toggle') {
this.replayOutput = !this.replayOutput;
} }
break; break;
case 'session': case 'session':
// Replay with session status. // Replay with session status.
this.messageReplay('session', this.session ? 'alive' : 'closed'); this.messageReplay('session', this.session ? 'alive' : 'closed');
break; break;
case 'onsessionchange':
// Enable, disable or toggle passing session status to parent window.
switch (decoded.data) {
case 'enable' : this.replayOnSession = true; break;
case 'disable' : this.replayOnSession = false; break;
case 'toggle' : this.replayOnSession = !this.replayOnSession; break;
}
break;
case 'reconnect':
this.reconnect();
break;
} }
}; };

View file

@ -1172,7 +1172,7 @@ static void parseArgs(int argc, char * const argv[]) {
if (strlen(ptr) == 1) { if (strlen(ptr) == 1) {
fatal("Syntax error in unixdomain-only chmod definition \"%s\".", optarg); fatal("Syntax error in unixdomain-only chmod definition \"%s\".", optarg);
} }
unixDomainChmod = strtol(s, NULL, 0); unixDomainChmod = strtol(s, NULL, 8);
} else if (!idx--) { } else if (!idx--) {
// User // User