Permalink
Browse files

Updated/fixed remote plugins (terminal, desktop)

  • Loading branch information...
1 parent 8c3f67b commit 2a0ac970360896aa8bcbca565649e1efd408ef41 @tomas tomas committed Mar 20, 2012
@@ -18,55 +18,84 @@ var RemoteDesktop = function(options){
// ActionModule.call(this);
var self = this;
- this.options = {
- vnc_port: options.vnc_port || 5900,
- vnc_pass: options.vnc_pass || 'secret',
+ this.vnc_opts = {
+ port: options.vnc_port || 5900,
+ pass: options.vnc_pass || 'secret',
desktop_scale: options.desktop_scale || 2,
- view_only: options.view_only || true,
- tunnel_host: options.tunnel_host || 'kiwi',
- tunnel_port: options.tunnel_port || 9998
+ view_only: options.view_only || true
}
+ this.tunnel_host = options.tunnel_host || 'localhost';
+ this.tunnel_port = options.tunnel_port || 9999;
+
// open: first we open the tunnel, then we run the command
// close: first we close the tunnel, then we kill the command
- this.start = function(){
+ this.start = function(callback){
- this.tunnel = new Tunnel(this.options.vnc_port, this.options.tunnel_host, this.options.tunnel_port);
+ this.tunnel = new Tunnel(this.vnc_opts.port, this.tunnel_host, this.tunnel_port);
this.tunnel.on('opened', function(){
logger.info("Tunnel for remote desktop is open!");
+
+ os_functions.vnc_server_running(function(running){
+ if(!running) self.start_vnc_server();
+ })
- var vnc_cmd = os_functions.vnc_command(self.options);
- self.child = spawn(vnc_cmd);
-
- self.child.on('exit', function(code){
- logger.info("VNC server terminated.");
- if(self.tunnel.is_open()) self.tunnel.close();
- // self.done();
- });
+ callback();
});
this.tunnel.on('closed', function(err){
-
logger.info("Tunnel closed!");
- if(self.child)
- self.child.kill();
+ self.stop(err);
+ });
- this.emit('end', err);
+ };
+
+ this.start_vnc_server = function(){
+
+ this.vnc_server_started = true;
+
+ console.log("Starting VNC server...");
+ var vnc_cmd = os_functions.vnc_command(self.vnc_opts);
+ var split = vnc_cmd.split(' ');
+ self.child = spawn(split.shift(), split);
+
+ self.child.stderr.on('data', function(data){
+ console.log(data.toString());
+ })
+ self.child.on('exit', function(code){
+ logger.info("VNC server terminated.");
+ self.stop();
});
+ };
+
+ this.stop_vnc_server = function(){
+
+ if(!this.vnc_server_started) return;
+
+ console.log("Stopping VNC server...")
+
+ if(this.child)
+ this.child.kill();
+
+ this.vnc_server_started = false;
+
}
- this.stop = function(){
+ this.stop = function(err){
if(this.tunnel.is_open())
this.tunnel.close(); // will trigger remote desktop command to stop
- else if(this.remote_desktop_command)
- this.remote_desktop_command.kill();
+
+ if(this.vnc_server_started)
+ this.stop_vnc_server();
+
+ this.emit('end', err);
}
@@ -77,17 +106,7 @@ util.inherits(RemoteDesktop, Emitter);
exports.start = function(options, callback){
var desktop = this.desktop = new RemoteDesktop(options);
- desktop.start();
-
- setTimeout(function(){
-
- if(desktop.child && desktop.child.is_running())
- callback();
- else
- callback(new Error("Child command is not running."));
-
- }, 500); // wait a bit before checking if the command is running or not
-
+ desktop.start(callback);
return desktop;
}
@@ -1,14 +1,26 @@
-var kickstart = "/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart";
var exec = require('child_process').exec;
+var kickstart = "/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart";
+
+exports.vnc_server_running = function(callback){
+
+ exec('echo "test" | /usr/bin/nc localhost 5900 &> /dev/null && echo 1', function(err, stdout, stderr){
+ callback(stdout && stdout.toString().trim() == '1')
+ });
-exports.stop_vnc = function(callback){
- exec(kickstart + ' -stop -deactivate', callback);
}
exports.vnc_command = function(options){
- var str = kickstart + ' -activate -restart -agent -configure -access';
- str += ' -on -clientopts -setvnclegacy -vnclegacy yes -setreqperm -reqperm yes';
- if(options.pass) str += '-setvncpw -vncpw $desktop__vnc_pass';
- // str += "-privs -all -allowAccessFor -allUsers";
+ var str = kickstart + ' -activate -restart -agent';
+ str += ' -console -menu';
+ str += ' -configure -access -on';
+ str += ' -privs -all -allowAccessFor -allUsers';
+ str += ' -clientopts -setvnclegacy -vnclegacy yes';
+ str += ' -setreqperm -reqperm yes';
+ if(options.pass) str += ' -setvncpw -vncpw ' + options.pass;
+ console.log(str);
return str;
-}
+}
+
+exports.stop_vnc = function(callback){
+ exec(kickstart + ' -stop -deactivate', callback);
+}
@@ -16,70 +16,72 @@ var common = require('./../../../common'),
var RemoteTerminal = function(options){
var self = this;
- this.options = {
- ssh_port: options.ssh_port || 22,
- tunnel_host: options.tunnel_host || 'kiwi',
- tunnel_port: options.tunnel_port || '9998'
- }
- this.start = function(){
+ this.ssh_server_stated = false;
+
+ this.ssh_port = options.ssh_port || 22;
+ this.tunnel_host = options.tunnel_host || 'localhost',
+ this.tunnel_port = options.tunnel_port || '9998';
+
+ this.start = function(callback){
- this.tunnel = new Tunnel(this.options.ssh_port, this.options.tunnel_host, this.options.tunnel_port);
+ this.tunnel = new Tunnel(this.ssh_port, this.tunnel_host, this.tunnel_port);
this.tunnel.on('opened', function(){
logger.info("Tunnel for remote terminal is open!");
os_functions.ssh_server_running(function(running){
-
if(!running) self.start_ssh_server();
-
});
});
this.tunnel.on('closed', function(){
logger.info("Tunnel closed!");
-
- if(self.child) // means we launched the ssh server
- self.shop_ssh_server();
-
- this.emit('end');
+ self.stop();
});
}
this.start_ssh_server = function(){
+
+ this.ssh_server_started = true;
logger.info("Starting SSH server!");
-
- var ssh_cmd = os_functions.ssh_server_command;
-
- this.child = spawn(ssh_cmd);
-
- this.child.on('exit', function(code){
- logger.info("SSH server exited.");
- if(self.tunnel.is_open()) self.tunnel.close();
- // self.done();
- });
+ os_functions.start_ssh_server(function(err){
+ if(err) console.log("Could not start SSH server.");
+ else console.log("SSH server online!")
+ })
};
this.stop_ssh_server = function(){
- // TODO
+ if(!this.ssh_server_started) return;
+
+ logger.info("Stopping SSH server!");
+ os_functions.stop_ssh_server(function(err){
+ if(err) console.log("Could not stop SSH server.");
+ else console.log("SSH server offline.")
+ })
+
+ this.ssh_server_started = false;
};
this.stop = function(){
if(this.tunnel.is_open())
this.tunnel.close(); // will trigger remote desktop command to stop
- else if(this.child)
+
+ if(this.ssh_server_started)
this.stop_ssh_server();
+ this.emit('end');
+
}
};
@@ -88,18 +90,8 @@ util.inherits(RemoteTerminal, Emitter);
exports.start = function(options, callback){
- var terminal = this.terminal = new RemoteTerminal();
- terminal.start(options);
-
- setTimeout(function(){
-
- if(terminal.child && terminal.child.is_running())
- callback();
- else
- callback(new Error("Child command is not running."));
-
- }, 500); // wait a bit before checking if the command is running or not
-
+ var terminal = this.terminal = new RemoteTerminal(options);
+ terminal.start(callback);
return terminal;
}
@@ -15,4 +15,12 @@ exports.ssh_server_running = function(){
});
+};
+
+exports.start_ssh_server = function(callback){
+ exec('/etc/init.d/ssh start', callback)
+}
+
+exports.stop_ssh_server = function(callback){
+ exec('/etc/init.d/ssh stop', callback)
}
@@ -7,12 +7,26 @@
var exec = require('child_process').exec;
-exports.ssh_server_running = function(){
+exports.ssh_server_running = function(callback){
- exec('echo "test" | nc localhost 22 &> /dev/null && echo 1', function(err, stdout, stderr){
+ exec('/usr/sbin/systemsetup -getremotelogin', function(err, stdout){
+ callback(!err && stdout && !!stdout.toString().trim().match(/On$/))
+ });
- callback(output && output.toString() == '1')
+ /*
+ exec('echo "test" | /usr/bin/nc localhost 22 &> /dev/null && echo 1', function(err, stdout, stderr){
+ callback(stdout && stdout.toString() == '1')
});
+
+ */
+
+}
+exports.start_ssh_server = function(callback){
+ exec('/usr/sbin/systemsetup -setremotelogin on', callback)
}
+
+exports.stop_ssh_server = function(callback){
+ exec('/usr/sbin/systemsetup -setremotelogin off', callback)
+}
View
@@ -13,6 +13,8 @@ var common = require('./common'),
Emitter = require('events').EventEmitter,
util = require('util');
+var stop_frame = "\r\n____STOP___\r\n";
+
var Tunnel = function(local_port, remote_host, remote_port){
var self = this;
@@ -106,7 +108,7 @@ var Tunnel = function(local_port, remote_host, remote_port){
self.log("Remote sent: " + data.length + " bytes.");
// self.log(" -- Local socket state: " + local_socket.readyState);
- if(data == "stop"){
+ if(data.toString() == stop_frame){
self.log("Got STOP signal from server. Closing local socket.");
local_socket.end();

0 comments on commit 2a0ac97

Please sign in to comment.