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
.help = function(args
, raw
) {
80 var help
= 'Available commands:\n';
82 help
+= ' This help menu\n';
84 help
+= ' Close and exit this Kiwi admin console\n';
86 help
+= ' Reload the config.js file\n';
88 help
+= ' Show connection statistics\n';
89 help
+= 'module list\n';
90 help
+= ' List the loaded server modules\n';
91 help
+= 'module reload module_name\n';
92 help
+= ' Reload the module_name module\n';
93 help
+= 'jumpserver [force] http://kiwi-server.com\n';
94 help
+= ' Tell all connected clients to reconnect to a different kiwi server. If \'force\' is given, they will be forced to reconnect in 5 minutes. \n';
100 commands
.stats = function(args
, raw
) {
101 this.write('Connected clients: ' + _
.size(global
.clients
.clients
).toString());
102 this.write('Num. remote hosts: ' + _
.size(global
.clients
.addresses
).toString());
106 commands
.reconfig = function(args
, raw
) {
107 if (config
.loadConfig()) {
108 this.write('New config file loaded');
110 this.write("No new config file was loaded");
115 commands
.rehash = function(args
, raw
) {
117 this.write('Rehashed');
121 commands
.jumpserver = function(args
, raw
) {
123 num_clients
= _
.size(global
.clients
.clients
),
124 packet
= {}, args_idx
;
126 if (num_clients
=== 0) {
127 this.write('No connected clients');
131 // For each word in the line minus the last, add it to the packet
132 for(args_idx
=0; args_idx
<args
.length
-1; args_idx
++){
133 packet
[args
[args_idx
]] = true;
136 packet
.kiwi_server
= args
[args_idx
];
138 if (!packet
.kiwi_server
) {
139 this.write('No Kiwi server specified');
143 this.write('Broadcasting jumpserver to ' + num_clients
.toString() + ' clients..');
144 global
.clients
.broadcastKiwiCommand('jumpserver', packet
, function() {
145 that
.write('Broadcast complete.');
150 commands
.module = function(args
, raw
) {
154 this.write('A module name must be specified');
158 if (!kiwiModules
.unload(args
[1])) {
159 this.write('Module ' + (args
[1] || '') + ' is not loaded');
163 if (!kiwiModules
.load(args
[1])) {
164 this.write('Error loading module ' + (args
[1] || ''));
166 this.write('Module ' + args
[1] + ' reloaded');
172 this.write('A module name must be specified');
176 if (!kiwiModules
.load(args
[1])) {
177 this.write('Error loading module ' + (args
[1] || ''));
179 this.write('Module ' + args
[1] + ' loaded');
185 this.write('A module name must be specified');
189 if (!kiwiModules
.unload(args
[1])) {
190 this.write('Module ' + (args
[1] || '') + ' is not loaded');
194 this.write('Module ' + args
[1] + ' unloaded');
201 var module_names
= [];
202 kiwiModules
.getRegisteredModules().forEach(function(module
) {
203 module_names
.push(module
.module_name
);
205 this.write('Loaded modules: ' + module_names
.join(', '));
210 commands
.hello = function(args
, raw
) {
211 this.write('Hello, beautiful :)');