AccessControl.prototype.attach = function (server)
{
    if (server.mqlobber_access_control)
    {
        throw new Error('server has access control');
    }

    server.on('pre_subscribe_requested', this._pre_subscribe_requested);
    server.on('pre_unsubscribe_requested', this._pre_unsubscribe_requested);
    server.on('pre_publish_requested', this._pre_publish_requested);

    for (var topic of this._blocked_topics)
    {
        blocked_matcher.add(topic, server.handler);
    }

    this._blocked_handlers.add(server.handler);

    if (server.fsq.filters.indexOf(filter) < 0)
    {
        server.fsq.filters.push(filter);
    }

    server.mqlobber_access_control_publish_count = 0;

    server.mqlobber_access_control = this;
};
AccessControl.prototype.detach = function (server)
{
    server.removeListener('pre_subscribe_requested',
                          this._pre_subscribe_requested);
    server.removeListener('pre_publish_requested',
                          this._pre_publish_requested);

    for (var topic of this._blocked_topics)
    {
        blocked_matcher.remove(topic, server.handler);
    }

    this._blocked_handlers.delete(server.handler);

    delete server.mqlobber_access_control;
};
function filter(info, handlers, cb)
{
    var blocked_handlers = blocked_matcher.match(info.topic);

    if (blocked_handlers.size === 0)
    {
        return cb(null, true, handlers);
    }

    cb(null, true, wu(handlers).filter(function (handler)
    {
        if (blocked_handlers.has(handler))
        {
            var server = handler.mqlobber_server,
                access_control = server.mqlobber_access_control;

            access_control.emit('message_blocked', info.topic, server);

            return false;
        }

        return true;
    }));
}
Example #4
0
function Matcher() {
  Qlobber.call(this, { separator: "/", wildcard_one: "+" });
}
AccessControl.prototype.reset = function (options)
{
    var ths = this, topic, handler;

    options = options || {};

    this._matchers = {
        publish: {},
        subscribe: {}
    };

    function make(type, access)
    {
        if (options[type] && options[type][access])
        {
            ths._matchers[type][access] = new QlobberDedup();
            for (topic of options[type][access])
            {
                ths._matchers[type][access].add(topic, true);
            }
        }
    }

    function setup(type)
    {
        make(type, 'allow');
        make(type, 'disallow');
    }

    setup('publish');
    setup('subscribe');

    for (topic of this._blocked_topics)
    {
        for (handler of this._blocked_handlers)
        {
            blocked_matcher.remove(topic, handler);
        }
    }

    this._blocked_topics = options.block || [];

    for (topic of this._blocked_topics)
    {
        for (handler of this._blocked_handlers)
        {
            blocked_matcher.add(topic, handler);
        }
    }

    this._max_topic_length = options.max_topic_length;
    this._max_subscriptions = options.subscribe ?
            options.subscribe.max_subscriptions : 0;
    this._max_publish_data_length = options.publish ?
            options.publish.max_data_length : 0;
    this._max_publications = options.publish ?
            options.publish.max_publications : 0;
    this._disallow_publish_single = options.publish ?
            options.publish.disallow_single : false;
    this._disallow_publish_multi = options.publish ?
            options.publish.disallow_multi : false;
};