diff --git a/lib/client/config.js b/lib/client/config.js index 06b1a621..32f9bde8 100644 --- a/lib/client/config.js +++ b/lib/client/config.js @@ -14,6 +14,8 @@ var CloudCmd, Util, DOM, io; Dialog = DOM.Dialog, Images = DOM.Images, Events = DOM.Events, + Files = DOM.Files, + showLoad = function() { Images.show.load('top'); }, @@ -62,6 +64,8 @@ var CloudCmd, Util, DOM, io; 'reconnection limit' : FIVE_SECONDS }); + auth(socket); + socket.on('connect', function() { Config.save = save; }); @@ -88,13 +92,28 @@ var CloudCmd, Util, DOM, io; } } + function auth(socket) { + Files.get('config', function(error, config) { + if (error) + return Dialog.alert(TITLE, error); + + if (config.auth) { + socket.emit('auth', config.username, config.password); + + socket.on('auth', function() { + Dialog.alert(TITLE, 'Wrong credentials!'); + }); + } + }); + } + Config.save = saveHttp; this.show = function() { var prefix = CloudCmd.PREFIX, exec = Util.exec, funcs = [ - exec.with(DOM.Files.get, 'config-tmpl'), + exec.with(Files.get, 'config-tmpl'), exec.with(DOM.load.parallel, [ prefix + '/css/config.css', prefix + '/lib/client/input.js' @@ -111,7 +130,7 @@ var CloudCmd, Util, DOM, io; if (!Template) Template = template; - DOM.Files.get('config', function(error, config) { + Files.get('config', function(error, config) { var data, inputs, inputFirst, focus, obj; diff --git a/lib/cloudcmd.js b/lib/cloudcmd.js index db96e8a3..5c8b3698 100644 --- a/lib/cloudcmd.js +++ b/lib/cloudcmd.js @@ -110,6 +110,8 @@ var size = cloudfunc.MAX_SIZE, pref = getPrefix(prefix); + config.listen(socket, authCheck); + webconsole({ socket: socket, prefix: pref + '/console', @@ -163,9 +165,7 @@ logout, setUrl(prefix), auth(), - config.middle({ - socket: socket, - }), + config.middle, restafary({ prefix : cloudfunc.apiURL + '/fs', root : root diff --git a/lib/server/config.js b/lib/server/config.js index 777b9777..16909db7 100644 --- a/lib/server/config.js +++ b/lib/server/config.js @@ -19,6 +19,7 @@ jonny = require('jonny'), readjson = require('readjson'), tryCatch = require('try-catch'), + exec = require('execon'), HOME = require('os-homedir')(), apiURL = CloudFunc.apiURL, @@ -47,11 +48,15 @@ module.exports = manage; module.exports.save = save; - module.exports.middle = function(options) { - var o = options || {}; + module.exports.middle = middle; + module.exports.listen = function(socket, authCheck) { + if (!socket) + throw Error('socket could not be empty!'); - if (o.socket) - socket(o.socket); + if (authCheck && typeof authCheck !== 'function') + throw Error('authCheck should be function!'); + + listen(socket, authCheck); return middle; }; @@ -79,38 +84,44 @@ callback(Error('Config is empty!')); } - function socket(sock) { - check([sock], ['socket']); - + function listen(sock, authCheck) { sock.of('/config') .on('connection', function(socket) { - socket.emit('config', config); + var connect = exec.with(connection, socket); - socket.on('message', function(json) { - var data, - is = Util.type.object(json); - - if (!is) { - socket.emit('err', 'Error: Wrong data type!'); - } else { - cryptoPass(json); - - data = traverse(json); - - save(function(error) { - if (error) { - socket.emit('err', error.message); - } else { - socket.broadcast.send(json); - socket.send(json); - socket.emit('log', data); - } - }); - } + exec.if(!manage('auth'), connect, function(fn) { + authCheck(socket, fn); }); }); } + function connection(socket) { + socket.emit('config', config); + + socket.on('message', function(json) { + var data, + is = Util.type.object(json); + + if (!is) { + socket.emit('err', 'Error: Wrong data type!'); + } else { + cryptoPass(json); + + data = traverse(json); + + save(function(error) { + if (error) { + socket.emit('err', error.message); + } else { + socket.broadcast.send(json); + socket.send(json); + socket.emit('log', data); + } + }); + } + }); + } + function middle(req, res, next) { if (req.url !== apiURL + '/config') { next();