Skip to content

Commit

Permalink
release 2.0.2 (#44)
Browse files Browse the repository at this point in the history
- fix: depend directly on redis
- fix: update redis command names for v4 compatibility
- fix: update redis commands to be async
  • Loading branch information
msimerson authored May 28, 2022
1 parent e9cecc4 commit e387d9a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
7 changes: 7 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
54 changes: 32 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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)
})

Expand Down Expand Up @@ -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 = {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
}
Expand All @@ -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()
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ describe('tls', function () {
})
})

describe('skiping_hooks', function () {
describe('skipping hooks', function () {
beforeEach(_set_up)

it('notes.disable_karma', function (done) {
Expand Down

0 comments on commit e387d9a

Please sign in to comment.