1 var _
= require('lodash'),
2 rehash
= require('./rehash.js'),
3 config
= require('../server/configuration.js'),
4 kiwiModules
= require('../server/modules');
8 var ControlInterface
= module
.exports = function(stream_in
, stream_out
, opts
) {
9 stream_out
= stream_out
|| stream_in
;
10 this.stream_out
= stream_out
;
11 this.stream_in
= stream_in
;
14 this.prompt
= (typeof opts
.prompt
=== 'string') ?
18 this._custom_commands
= {};
20 this._onData
= this.onData
.bind(this);
21 stream_in
.on('data', this._onData
);
28 ControlInterface
.prototype.dispose = function() {
29 this.stream_in
.removeListener('data', this._onData
);
30 this.stream_in
= null;
31 this.stream_out
= null;
36 ControlInterface
.prototype.write = function(data
, append
) {
37 if (typeof append
=== 'undefined') append
= '\n';
39 this.stream_out
.write(data
+ append
);
45 ControlInterface
.prototype.displayPrompt = function(prompt
) {
46 prompt
= prompt
|| this.prompt
;
47 this.write(prompt
, '');
52 ControlInterface
.prototype.onData = function(buffered
) {
53 var data
= buffered
.toString().trim(),
54 data_parts
= data
.split(' '),
55 cmd
= data_parts
[0] || null;
57 if (typeof this._custom_commands
[cmd
] === 'function') {
58 this._custom_commands
[cmd
].call(this, data_parts
.slice(1), data
);
60 } else if (typeof commands
[cmd
] === 'function') {
61 commands
[cmd
].call(this, data_parts
.slice(1), data
);
64 this.write('Unrecognised command: ' + cmd
);
72 ControlInterface
.prototype.addCommand = function(command
, fn
) {
73 this._custom_commands
[command
] = fn
;
79 commands
.stats = function(args
, raw
) {
80 this.write('Connected clients: ' + _
.size(global
.clients
.clients
).toString());
81 this.write('Num. remote hosts: ' + _
.size(global
.clients
.addresses
).toString());
85 commands
.reconfig = function(args
, raw
) {
86 if (config
.loadConfig()) {
87 this.write('New config file loaded');
89 this.write("No new config file was loaded");
94 commands
.rehash = function(args
, raw
) {
96 this.write('Rehashed');
100 commands
.jumpserver = function(args
, raw
) {
102 num_clients
= _
.size(global
.clients
.clients
),
103 packet
= {}, args_idx
;
105 if (num_clients
=== 0) {
106 this.write('No connected clients');
110 // For each word in the line minus the last, add it to the packet
111 for(args_idx
=0; args_idx
<args
.length
-1; args_idx
++){
112 packet
[args
[args_idx
]] = true;
115 packet
.kiwi_server
= args
[args_idx
];
117 if (!packet
.kiwi_server
) {
118 this.write('No Kiwi server specified');
122 this.write('Broadcasting jumpserver to ' + num_clients
.toString() + ' clients..');
123 global
.clients
.broadcastKiwiCommand('jumpserver', packet
, function() {
124 that
.write('Broadcast complete.');
129 commands
.module = function(args
, raw
) {
133 this.write('A module name must be specified');
137 if (!kiwiModules
.unload(args
[1])) {
138 this.write('Module ' + (args
[1] || '') + ' is not loaded');
142 if (!kiwiModules
.load(args
[1])) {
143 this.write('Error loading module ' + (args
[1] || ''));
145 this.write('Module ' + args
[1] + ' reloaded');
152 var module_names
= [];
153 kiwiModules
.getRegisteredModules().forEach(function(module
) {
154 module_names
.push(module
.module_name
);
156 this.write('Loaded modules: ' + module_names
.join(', '));
161 commands
.hello = function(args
, raw
) {
162 this.write('Hello, beautiful :)');