Skip to content

Commit

Permalink
v1.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Mar 3, 2023
1 parent 6122e95 commit 6199377
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ You can use a domain name or an email address as the target, for additional conf
- **AAAA** is an array of IPv6 addresses. Optional, resolved from exchange hostname if not set
- **connectHook** _function (options, socketOptions, callback)_ is a function handler to run before establishing a tcp connection to current target (defined in `socketOptions`). If the `socketOptions` object has a `socket` property after the callback then connection is not established. Useful if you want to divert the connection is ome cases, for example if the target domain is in the Onion network then you could create a socket against a SOCK proxy yourself.
- **mtaSts** is an object for MTA-STS configuration
- **enabled** - if not `true` then does not run MTA-STS checks
- **logger(logObj)** - method to log data
- **cache** - an object to manage MTA-STS policy caches
- **enabled** - if not `true` then does not run MTA-STS checks, disabled by default
- **logger(logObj)** - method to log MTA-STS information, logging is disabled by default
- **cache** - an object to manage MTA-STS policy cache
- **get(domain)** -> returns cached policy object
- **set(domain, policyObj)** -> caches a policy object
- **connectError** _function (err, options, socketOptions)_ is a function handler to run when a connection to a MX fails.
Expand Down
3 changes: 3 additions & 0 deletions lib/get-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ function getConnection(delivery) {
hostname: mx.hostname,
host: mx.host,
domain: delivery.domain,
mode: mx.policyMatch.mode,
testing: true
});
} else {
Expand All @@ -163,6 +164,7 @@ function getConnection(delivery) {
hostname: mx.hostname,
host: mx.host,
domain: delivery.domain,
mode: mx.policyMatch.mode,
testing: false
});
let code = 554;
Expand All @@ -183,6 +185,7 @@ function getConnection(delivery) {
hostname: mx.hostname,
host: mx.host,
domain: delivery.domain,
mode: mx.policyMatch.mode,
testing: true
});
}
Expand Down
12 changes: 10 additions & 2 deletions lib/mx-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const getConnection = require('./get-connection');
const net = require('net');
const dns = require('dns');
const { getPolicy, validateMx } = require('mailauth/lib/mta-sts');
const util = require('util');

const EMTPY_CACHE_HANDLER = {
async get(/*domain*/) {
Expand All @@ -23,7 +24,10 @@ const resolvePolicy = async delivery => {
}

const knownPolicy = await delivery.mtaSts.cache.get(delivery.decodedDomain);
const { policy, status } = await getPolicy(delivery.decodedDomain, knownPolicy);
const { policy, status } = await getPolicy(delivery.decodedDomain, knownPolicy, {
resolver: delivery.dnsOptions && delivery.dnsOptions.resolve && util.promisify(delivery.dnsOptions.resolve)
});

if (status !== 'cached') {
await delivery.mtaSts.cache.set(delivery.decodedDomain, policy);
}
Expand All @@ -34,6 +38,10 @@ const resolvePolicy = async delivery => {
};

const validateMxPolicy = async delivery => {
if (!delivery.mtaSts.enabled) {
return delivery;
}

for (let mx of delivery.mx) {
mx.policyMatch = validateMx(mx.exchange, delivery.mtaSts.policy);
}
Expand All @@ -56,7 +64,7 @@ module.exports = (options, callback) => {
}

let mtaSts = Object.assign({ enabled: false }, options.mtaSts);
mtaSts.logger = mtaSts.logger || (obj => console.error(JSON.stringify(obj)));
mtaSts.logger = mtaSts.logger || (() => false);
mtaSts.cache = mtaSts.cache || EMTPY_CACHE_HANDLER;

let delivery = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mx-connect",
"version": "1.4.1",
"version": "1.4.2",
"description": "Establish TCP connection to a MX server",
"main": "lib/mx-connect.js",
"scripts": {
Expand Down
29 changes: 29 additions & 0 deletions test/mx-connect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

'use strict';

const dns = require('dns');
const mxConnect = require('../lib/mx-connect');

module.exports.basic = test => {
Expand Down Expand Up @@ -71,3 +72,31 @@ module.exports.policyFail = test => {
}
);
};

module.exports.policySkip = test => {
mxConnect(
{
target: '[email protected]',
mtaSts: {
enabled: false
},
mx: [
{
exchange: 'aspmx.l.google.com',
priority: 10,
A: ['64.233.165.26'],
AAAA: []
}
]
},
(err, connection) => {
test.ifError(err);
test.ok(connection.socket);

test.ok(!connection.policyMatch);

connection.socket.once('end', () => test.done());
connection.socket.once('data', () => connection.socket.end());
}
);
};

0 comments on commit 6199377

Please sign in to comment.