From e387d9a3556aece888092494341a202c7b9190d0 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Sat, 28 May 2022 01:05:19 -0700 Subject: [PATCH] release 2.0.2 (#44) - fix: depend directly on redis - fix: update redis command names for v4 compatibility - fix: update redis commands to be async --- Changes.md | 7 +++++++ index.js | 54 ++++++++++++++++++++++++++++++--------------------- package.json | 5 +++-- test/karma.js | 2 +- 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/Changes.md b/Changes.md index a88efa1..743963d 100644 --- a/Changes.md +++ b/Changes.md @@ -1,4 +1,11 @@ +### 2.0.2 - 2022-05-27 + +- fix: depend directly on redis +- fix: update redis command names for v4 compatibility +- fix: update redis commands to be async + + ### 2.0.1 - 2022-05-27 - chore(ci): depend on shared GHA workflows diff --git a/index.js b/index.js index 83b0aeb..0956885 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ // karma - reward good and penalize bad mail senders const constants = require('haraka-constants') +const redis = require('redis') const utils = require('haraka-utils') const phase_prefixes = utils.to_object([ @@ -104,8 +105,16 @@ exports.results_init = async function (next, connection) { if (!this.result_awards) return next() // not configured - // subscribe to result_store publish messages - await this.redis_subscribe(connection, (channel, message) => { + if (connection.notes.redis) { + connection.logdebug(this, `redis already subscribed`) + return // another plugin has already called this. + } + + connection.notes.redis = redis.createClient(this.redisCfg.pubsub) + await connection.notes.redis.connect() + + const pattern = this.get_redis_sub_channel(connection) + connection.notes.redis.pSubscribe(pattern, (message) => { this.check_result(connection, message) }) @@ -543,22 +552,20 @@ exports.ip_history_from_redis = function (next, connection) { // redis plugin is emitting errors, no need to here if (!plugin.db) return next() - plugin.db.hgetall(dbkey, (err, dbr) => { - if (err) { - connection.results.add(plugin, { err }) - return next() - } - + plugin.db.hGetAll(dbkey).then(dbr => { if (dbr === null) { plugin.init_ip(dbkey, connection.remote.ip, expire) return next() } plugin.db.multi() - .hincrby(dbkey, 'connections', 1) // increment total conn + .hIncrBy(dbkey, 'connections', 1) // increment total conn .expire(dbkey, expire) // extend expiration - .exec((err2, replies) => { - if (err2) connection.results.add(plugin, {err: err2}) + .exec() + .then(replies => { + console.log(replies) + }).catch(err2 => { + connection.results.add(plugin, {err: err2}) }) const results = { @@ -582,6 +589,11 @@ exports.ip_history_from_redis = function (next, connection) { plugin.check_awards(connection) return next() }) + .catch(err => { + connection.results.add(plugin, { err }) + next() + }) + } exports.hook_mail = function (next, connection, params) { @@ -665,10 +677,10 @@ exports.increment = function (connection, key, val) { const plugin = this if (!plugin.db) return - plugin.db.hincrby(`karma|${connection.remote.ip}`, key, 1) + plugin.db.hIncrBy(`karma|${connection.remote.ip}`, key, 1) const asnkey = plugin.get_asn_key(connection) - if (asnkey) plugin.db.hincrby(asnkey, key, 1) + if (asnkey) plugin.db.hIncrBy(asnkey, key, 1) } exports.hook_disconnect = function (next, connection) { @@ -930,19 +942,14 @@ exports.check_asn = function (connection, asnkey) { if (this.cfg.asn.report_as) report_as.name = this.cfg.asn.report_as - this.db.hgetall(asnkey, (err, res) => { - if (err) { - connection.results.add(this, { err }) - return - } - + this.db.hGetAll(asnkey).then(res => { if (res === null) { const expire = (this.cfg.redis.expire_days || 60) * 86400 // days this.init_asn(asnkey, expire) return } - this.db.hincrby(asnkey, 'connections', 1) + this.db.hIncrBy(asnkey, 'connections', 1) const asn_score = parseInt(res.good || 0) - (res.bad || 0) const asn_results = { asn_score, @@ -970,12 +977,15 @@ exports.check_asn = function (connection, asnkey) { connection.results.add(report_as, asn_results) }) + .catch(err => { + connection.results.add(this, { err }) + }) } exports.init_ip = function (dbkey, rip, expire) { if (!this.db) return this.db.multi() - .hmset(dbkey, {'bad': 0, 'good': 0, 'connections': 1}) + .hmSet(dbkey, {'bad': 0, 'good': 0, 'connections': 1}) .expire(dbkey, expire) .exec() } @@ -992,7 +1002,7 @@ exports.init_asn = function (asnkey, expire) { const plugin = this if (!plugin.db) return plugin.db.multi() - .hmset(asnkey, {'bad': 0, 'good': 0, 'connections': 1}) + .hmSet(asnkey, {'bad': 0, 'good': 0, 'connections': 1}) .expire(asnkey, expire * 2) // keep ASN longer .exec() } diff --git a/package.json b/package.json index 86a34d2..f0d27e1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "haraka-plugin-karma", - "version": "2.0.1", + "version": "2.0.2", "description": "A heuristics scoring and reputation engine for SMTP connections", "main": "index.js", "scripts": { @@ -26,7 +26,8 @@ "address-rfc2821": "*", "haraka-constants": ">=1.0.2", "haraka-utils": "*", - "haraka-plugin-redis": "2" + "haraka-plugin-redis": "2", + "redis": "4" }, "devDependencies": { "eslint": "8", diff --git a/test/karma.js b/test/karma.js index fb43fe0..ad81c0e 100644 --- a/test/karma.js +++ b/test/karma.js @@ -762,7 +762,7 @@ describe('tls', function () { }) }) -describe('skiping_hooks', function () { +describe('skipping hooks', function () { beforeEach(_set_up) it('notes.disable_karma', function (done) {