Skip to content

Commit

Permalink
Add Option to Disable Redis (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
superman20 committed Mar 29, 2023
1 parent a6bd8d1 commit a6f7833
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
### Unreleased

- add redis enabled setting

### [1.1.0] - 2022-11-22

- fix: use this.redis_ping during runtime, #26
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Recipient Routes does recipient validation and MX routing.
## Recipient Validation

Recipients can be listed in the [routes] section of the config file
`config/rcpt_to.routes.ini` or in Redis. If Redis is available, it is checked
`config/rcpt_to.routes.ini` or in Redis. If Redis is enabled and available, it is checked
first. Then the config file is checked.

Entries can be email addresses or domains. If both are present, email
Expand Down Expand Up @@ -49,12 +49,13 @@ The following options can be specified in `config/rcpt_to.routes.ini`:

### Redis

The [redis] section has three optional settings (defaults shown):
The [redis] section has optional settings (defaults shown):

[redis]
host=127.0.0.1
port=6379
db=0
enabled=true

### Routes

Expand Down
1 change: 1 addition & 0 deletions config/rcpt_to.routes.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
; host=127.0.0.1
; port=6379
; db=0
; enabled=true

; [routes]
; matt@example.com=192.168.76.66
Expand Down
24 changes: 16 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@ exports.register = function () {
this.route_list={};

this.load_rcpt_to_routes_ini();
this.merge_redis_ini();
if (this.cfg.redis.opts.enabled) {
this.merge_redis_ini();

this.register_hook('init_master', 'init_redis_plugin');
this.register_hook('init_child', 'init_redis_plugin');
this.register_hook('init_master', 'init_redis_plugin');
this.register_hook('init_child', 'init_redis_plugin');
}

this.register_hook('rcpt', 'rcpt');
this.register_hook('get_mx', 'get_mx');
}

exports.load_rcpt_to_routes_ini = function () {
const plugin = this;
plugin.cfg = plugin.config.get('rcpt_to.routes.ini', function () {
plugin.cfg = plugin.config.get('rcpt_to.routes.ini', {
booleans: [

Check failure on line 29 in index.js

View workflow job for this annotation

GitHub Actions / lint / lint

Expected indentation of 4 spaces but found 6
'+redis.enabled',

Check failure on line 30 in index.js

View workflow job for this annotation

GitHub Actions / lint / lint

Expected indentation of 6 spaces but found 10
],

Check failure on line 31 in index.js

View workflow job for this annotation

GitHub Actions / lint / lint

Expected indentation of 4 spaces but found 6
},
function () {
plugin.load_rcpt_to_routes_ini();
})

Expand All @@ -32,6 +39,7 @@ exports.load_rcpt_to_routes_ini = function () {
plugin.cfg.redis.opts = {
host: plugin.cfg.redis.server_ip || plugin.cfg.redis.host || '127.0.0.1',
port: plugin.cfg.redis.server_port || plugin.cfg.redis.port || 6379,
enabled: plugin.cfg.redis.enabled ?? true,
}

const lowered = {};
Expand Down Expand Up @@ -105,7 +113,7 @@ exports.rcpt = async function (next, connection, params) {
}

// if we can't use redis, try files
if (!this.db || ! await this.redis_ping() ) {
if (!this.cfg.redis.opts.enabled || !this.db || !await this.redis_ping()) {
return next(await this.do_file_search(txn, address, domain));
}

Expand Down Expand Up @@ -163,7 +171,7 @@ exports.get_mx = async function (next, hmail, domain) {
}

// if we can't use redis, try files and return
if (! this.db || ! await this.redis_ping() ) {
if (!this.cfg.redis.opts.enabled || !this.db || !await this.redis_ping()) {
this.get_mx_file(address, domain, next);
return;
}
Expand All @@ -190,13 +198,13 @@ exports.get_mx = async function (next, hmail, domain) {

exports.insert_route = function (email, route) {
// for importing, see http://redis.io/topics/mass-insert
if (!this.db || !this.redis_pings) return false;
if (!this.cfg.redis.opts.enabled || !this.db || !this.redis_pings) return false;

this.db.set(email, route);
}

exports.delete_route = function (email) {
if (!this.redis_pings) return false;
if (!this.cfg.redis.opts.enabled || !this.redis_pings) return false;

this.db.del(email);
}

0 comments on commit a6f7833

Please sign in to comment.