Commit 74b7fd7f by zCaesar

can publish onGroup

parent 14c11a3b
var config = require('config'); var config = require('config');
// https://github.com/isaacs/node-lru-cache // https://github.com/isaacs/node-lru-cache
var LRU = require("lru-cache") , var LRU = require("lru-cache"),
cache = LRU({ cache = LRU({
max: 500, max: 500,
maxAge: 1000 * 60 * 5 maxAge: 1000 * 60 * 5
...@@ -11,8 +11,8 @@ function authCheck(client_id, username, topic, callback) { ...@@ -11,8 +11,8 @@ function authCheck(client_id, username, topic, callback) {
callback(true); callback(true);
} }
module.exports = function(options={}) { module.exports = function (options = {}) {
return function(req, res, next) { return function (req, res, next) {
if (req.header('vernemq-hook') == 'auth_on_publish' && req && req.body) { if (req.header('vernemq-hook') == 'auth_on_publish' && req && req.body) {
if (options.debug) { if (options.debug) {
...@@ -23,24 +23,36 @@ module.exports = function(options={}) { ...@@ -23,24 +23,36 @@ module.exports = function(options={}) {
var authstatus; var authstatus;
var topic = req.body.topic; var topic = req.body.topic;
var cachekey = 'pub:'+req.body.client_id +':'+ req.body.username+':'+topic; var cachekey = 'pub:' + req.body.client_id + ':' + req.body.username + ':' + topic;
var authstatus = cache.get(cachekey); var authstatus = cache.get(cachekey);
if (typeof(authstatus)=='undefined') { // Set Response
var getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
// console.log(_id)
var _ftopic = require('./utils/getTopic').getTopic(topic, _id) // get topic where concat with groupID
var response = {
'result': 'ok',
'modifiers': {
'topic': _ftopic,
'qos': 0,
'retain': false
}
}
console.log(response)
if (typeof (authstatus) == 'undefined') {
// cache missed // cache missed
authCheck(req.body.client_id, req.body.username, topic, function(result) { authCheck(req.body.client_id, req.body.username, topic, function (result) {
cache.set(cachekey, result); cache.set(cachekey, result);
res.send({ res.send(response);
"result": result?"ok":"no"
});
}); });
} }
else { else {
res.send({ res.send(response);
"result": authstatus?"ok":"no"
});
} }
next(); next();
})
} }
else { else {
res.send({ res.send({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment