haraka-plugin-karma 2.0.1 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Changes.md CHANGED
@@ -1,4 +1,11 @@
1
1
 
2
+ ### 2.0.2 - 2022-05-27
3
+
4
+ - fix: depend directly on redis
5
+ - fix: update redis command names for v4 compatibility
6
+ - fix: update redis commands to be async
7
+
8
+
2
9
  ### 2.0.1 - 2022-05-27
3
10
 
4
11
  - chore(ci): depend on shared GHA workflows
package/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
  // karma - reward good and penalize bad mail senders
3
3
 
4
4
  const constants = require('haraka-constants')
5
+ const redis = require('redis')
5
6
  const utils = require('haraka-utils')
6
7
 
7
8
  const phase_prefixes = utils.to_object([
@@ -104,8 +105,16 @@ exports.results_init = async function (next, connection) {
104
105
 
105
106
  if (!this.result_awards) return next() // not configured
106
107
 
107
- // subscribe to result_store publish messages
108
- await this.redis_subscribe(connection, (channel, message) => {
108
+ if (connection.notes.redis) {
109
+ connection.logdebug(this, `redis already subscribed`)
110
+ return // another plugin has already called this.
111
+ }
112
+
113
+ connection.notes.redis = redis.createClient(this.redisCfg.pubsub)
114
+ await connection.notes.redis.connect()
115
+
116
+ const pattern = this.get_redis_sub_channel(connection)
117
+ connection.notes.redis.pSubscribe(pattern, (message) => {
109
118
  this.check_result(connection, message)
110
119
  })
111
120
 
@@ -543,22 +552,20 @@ exports.ip_history_from_redis = function (next, connection) {
543
552
  // redis plugin is emitting errors, no need to here
544
553
  if (!plugin.db) return next()
545
554
 
546
- plugin.db.hgetall(dbkey, (err, dbr) => {
547
- if (err) {
548
- connection.results.add(plugin, { err })
549
- return next()
550
- }
551
-
555
+ plugin.db.hGetAll(dbkey).then(dbr => {
552
556
  if (dbr === null) {
553
557
  plugin.init_ip(dbkey, connection.remote.ip, expire)
554
558
  return next()
555
559
  }
556
560
 
557
561
  plugin.db.multi()
558
- .hincrby(dbkey, 'connections', 1) // increment total conn
562
+ .hIncrBy(dbkey, 'connections', 1) // increment total conn
559
563
  .expire(dbkey, expire) // extend expiration
560
- .exec((err2, replies) => {
561
- if (err2) connection.results.add(plugin, {err: err2})
564
+ .exec()
565
+ .then(replies => {
566
+ console.log(replies)
567
+ }).catch(err2 => {
568
+ connection.results.add(plugin, {err: err2})
562
569
  })
563
570
 
564
571
  const results = {
@@ -582,6 +589,11 @@ exports.ip_history_from_redis = function (next, connection) {
582
589
  plugin.check_awards(connection)
583
590
  return next()
584
591
  })
592
+ .catch(err => {
593
+ connection.results.add(plugin, { err })
594
+ next()
595
+ })
596
+
585
597
  }
586
598
 
587
599
  exports.hook_mail = function (next, connection, params) {
@@ -665,10 +677,10 @@ exports.increment = function (connection, key, val) {
665
677
  const plugin = this
666
678
  if (!plugin.db) return
667
679
 
668
- plugin.db.hincrby(`karma|${connection.remote.ip}`, key, 1)
680
+ plugin.db.hIncrBy(`karma|${connection.remote.ip}`, key, 1)
669
681
 
670
682
  const asnkey = plugin.get_asn_key(connection)
671
- if (asnkey) plugin.db.hincrby(asnkey, key, 1)
683
+ if (asnkey) plugin.db.hIncrBy(asnkey, key, 1)
672
684
  }
673
685
 
674
686
  exports.hook_disconnect = function (next, connection) {
@@ -930,19 +942,14 @@ exports.check_asn = function (connection, asnkey) {
930
942
 
931
943
  if (this.cfg.asn.report_as) report_as.name = this.cfg.asn.report_as
932
944
 
933
- this.db.hgetall(asnkey, (err, res) => {
934
- if (err) {
935
- connection.results.add(this, { err })
936
- return
937
- }
938
-
945
+ this.db.hGetAll(asnkey).then(res => {
939
946
  if (res === null) {
940
947
  const expire = (this.cfg.redis.expire_days || 60) * 86400 // days
941
948
  this.init_asn(asnkey, expire)
942
949
  return
943
950
  }
944
951
 
945
- this.db.hincrby(asnkey, 'connections', 1)
952
+ this.db.hIncrBy(asnkey, 'connections', 1)
946
953
  const asn_score = parseInt(res.good || 0) - (res.bad || 0)
947
954
  const asn_results = {
948
955
  asn_score,
@@ -970,12 +977,15 @@ exports.check_asn = function (connection, asnkey) {
970
977
 
971
978
  connection.results.add(report_as, asn_results)
972
979
  })
980
+ .catch(err => {
981
+ connection.results.add(this, { err })
982
+ })
973
983
  }
974
984
 
975
985
  exports.init_ip = function (dbkey, rip, expire) {
976
986
  if (!this.db) return
977
987
  this.db.multi()
978
- .hmset(dbkey, {'bad': 0, 'good': 0, 'connections': 1})
988
+ .hmSet(dbkey, {'bad': 0, 'good': 0, 'connections': 1})
979
989
  .expire(dbkey, expire)
980
990
  .exec()
981
991
  }
@@ -992,7 +1002,7 @@ exports.init_asn = function (asnkey, expire) {
992
1002
  const plugin = this
993
1003
  if (!plugin.db) return
994
1004
  plugin.db.multi()
995
- .hmset(asnkey, {'bad': 0, 'good': 0, 'connections': 1})
1005
+ .hmSet(asnkey, {'bad': 0, 'good': 0, 'connections': 1})
996
1006
  .expire(asnkey, expire * 2) // keep ASN longer
997
1007
  .exec()
998
1008
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "haraka-plugin-karma",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "A heuristics scoring and reputation engine for SMTP connections",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,7 +26,8 @@
26
26
  "address-rfc2821": "*",
27
27
  "haraka-constants": ">=1.0.2",
28
28
  "haraka-utils": "*",
29
- "haraka-plugin-redis": "2"
29
+ "haraka-plugin-redis": "2",
30
+ "redis": "4"
30
31
  },
31
32
  "devDependencies": {
32
33
  "eslint": "8",
package/test/karma.js CHANGED
@@ -762,7 +762,7 @@ describe('tls', function () {
762
762
  })
763
763
  })
764
764
 
765
- describe('skiping_hooks', function () {
765
+ describe('skipping hooks', function () {
766
766
  beforeEach(_set_up)
767
767
 
768
768
  it('notes.disable_karma', function (done) {