Skip to content

Commit

Permalink
deprecate line_socket
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed May 3, 2024
1 parent c849c21 commit a9afeb6
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"haraka-email-message": "^1.2.3",
"haraka-message-stream": "^1.2.1",
"haraka-net-utils": "^1.7.0",
"haraka-notes": "^1.0.7",
"haraka-notes": "^1.1.0",
"haraka-plugin-redis": "^2.0.7",
"haraka-results": "^2.2.4",
"haraka-tld": "^1.2.1",
Expand Down
26 changes: 14 additions & 12 deletions plugins/auth/auth_proxy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Proxy AUTH requests selectively by domain

const sock = require('./line_socket');
const net = require('node:net')

const utils = require('haraka-utils');
const net_utils = require('haraka-net-utils')

const smtp_regexp = /^(\d{3})([ -])(.*)/;

Expand All @@ -16,7 +18,6 @@ exports.load_tls_ini = function () {
});
}


exports.hook_capabilities = (next, connection) => {
if (connection.tls.enabled) {
const methods = [ 'PLAIN', 'LOGIN' ];
Expand Down Expand Up @@ -54,35 +55,36 @@ exports.try_auth_proxy = function (connection, hosts, user, passwd, cb) {
}

const self = this;
const host = hosts.shift();
let [ host, port ] = hosts.shift().split(':'); /* eslint prefer-const: 0 */
if (!port) port = 25
let methods = [];
let auth_complete = false;
let auth_success = false;
let command = 'connect';
let response = [];
let secure = false;

const hostport = host.split(/:/);
const socket = sock.connect(((hostport[1]) ? hostport[1] : 25), hostport[0]);
connection.logdebug(self, `attempting connection to host=${hostport[0]} port=${(hostport[1]) ? hostport[1] : 25}`);
const socket = net.connect({ host, port });
net_utils.add_line_processor(socket)
connection.logdebug(this, `attempting connection to host=${host} port=${port}`);
socket.setTimeout(30 * 1000);
socket.on('connect', () => { });
socket.on('close', () => {
if (!auth_complete) {
// Try next host
return self.try_auth_proxy(connection, hosts, user, passwd, cb);
return this.try_auth_proxy(connection, hosts, user, passwd, cb);
}
connection.loginfo(self, `AUTH user="${user}" host="${host}" success=${auth_success}`);
return cb(auth_success);
connection.loginfo(this, `AUTH user="${user}" host="${host}" success=${auth_success}`);
cb(auth_success);
});
socket.on('timeout', () => {
connection.logerror(self, "connection timed out");
connection.logerror(this, "connection timed out");
socket.end();
// Try next host
return self.try_auth_proxy(connection, hosts, user, passwd, cb);
this.try_auth_proxy(connection, hosts, user, passwd, cb);
});
socket.on('error', err => {
connection.logerror(self, `connection failed to host ${host}: ${err}`);
connection.logerror(this, `connection failed to host ${host}: ${err}`);
socket.end();
});
socket.send_command = function (cmd, data) {
Expand Down
13 changes: 6 additions & 7 deletions plugins/avg.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// avg - AVG virus scanner
'use strict';

// TODO: use pooled connections
const fs = require('node:fs')
const net = require('node:net')
const path = require('node:path')

const fs = require('fs');
const path = require('path');

const sock = require('./line_socket');
const net_utils = require('haraka-net-utils')

const smtp_regexp = /^(\d{3})([ -])(.*)/;

exports.register = function () {

this.load_avg_ini();
}

Expand Down Expand Up @@ -49,7 +47,8 @@ exports.hook_data_post = function (next, connection) {

ws.once('close', () => {
const start_time = Date.now();
const socket = new sock.Socket();
const socket = new net.Socket();
net_utils.add_line_processor(socket)
socket.setTimeout((plugin.cfg.main.connect_timeout || 10) * 1000);
let connected = false;
let command = 'connect';
Expand Down
12 changes: 7 additions & 5 deletions plugins/clamd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// clamd

const sock = require('./line_socket');
const net = require('node:net')

const utils = require('haraka-utils');
const net_utils = require('haraka-net-utils')

exports.load_excludes = function () {

Expand Down Expand Up @@ -155,12 +157,11 @@ exports.hook_data = function (next, connection) {
const txn = connection.transaction;
txn.parse_body = true;
txn.attachment_hooks((ctype, filename, body) => {
connection.logdebug(this,
`found ctype=${ctype}, filename=${filename}`);
connection.logdebug(this, `found ctype=${ctype}, filename=${filename}`);
txn.notes.clamd_found_attachment = true;
});

return next();
next();
}

exports.hook_data_post = function (next, connection) {
Expand Down Expand Up @@ -197,7 +198,8 @@ exports.hook_data_post = function (next, connection) {
}
const host = hosts.shift();
connection.logdebug(plugin, `trying host: ${host}`);
const socket = new sock.Socket();
const socket = new net.Socket()
net_utils.add_line_processor(socket)

socket.on('timeout', () => {
socket.destroy();
Expand Down
9 changes: 5 additions & 4 deletions plugins/spamassassin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';
// Call spamassassin via spamd

const sock = require('./line_socket');
const net = require('node:net')

const utils = require('haraka-utils');
const net_utils = require('haraka-net-utils')

exports.register = function () {
this.load_spamassassin_ini();
Expand Down Expand Up @@ -264,10 +266,9 @@ exports.get_spamd_socket = function (next, conn, headers) {
const plugin = this;
const txn = conn.transaction;

// TODO: support multiple spamd backends

const socket = new sock.Socket();
const socket = new net.Socket();
socket.is_connected = false;
net_utils.add_line_processor(socket)
const results_timeout = parseInt(plugin.cfg.main.results_timeout) || 300;

socket.on('connect', function () {
Expand Down
13 changes: 11 additions & 2 deletions smtp_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const net_utils = require('haraka-net-utils');
const utils = require('haraka-utils');

// haraka libs
const line_socket = require('./line_socket');
const tls_socket = require('../tls_socket')
const logger = require('./logger');
const HostPool = require('./host_pool');

Expand All @@ -33,7 +33,7 @@ class SMTPClient extends events.EventEmitter {
super();
this.uuid = utils.uuid();
this.connect_timeout = parseInt(opts.connect_timeout) || 30;
this.socket = opts.socket || line_socket.connect({ host: opts.host, port: opts.port, timeout: this.connect_timeout });
this.socket = opts.socket || this.get_socket(opts)
this.socket.setTimeout(this.connect_timeout * 1000);
this.socket.setKeepAlive(true);
this.state = STATE.IDLE;
Expand Down Expand Up @@ -263,6 +263,15 @@ class SMTPClient extends events.EventEmitter {
this.release();
return true;
}

get_socket(opts) {
this.socket = tls_socket.connect({
host: opts.host,
port: opts.port,
timeout: this.connect_timeout,
})
net_utils.add_line_processor(this.socket)
}
}

exports.smtp_client = SMTPClient;
Expand Down
5 changes: 4 additions & 1 deletion tls_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const util = require('node:util');
// npm packages
exports.config = require('haraka-config'); // exported for tests
const Notes = require('haraka-notes')
const net_utils = require('haraka-net-utils')

const log = require('./logger');

Expand Down Expand Up @@ -660,8 +661,10 @@ function getCertFor (host) {
return certsByHost['*']; // the default TLS cert
}


function connect (conn_options = {}) {
// called by outbound/client_pool
// called by outbound/client_pool, smtp_client, plugins/spamassassin,avg,clamd

const cryptoSocket = net.connect(conn_options);
const socket = new pluggableStream(cryptoSocket);

Expand Down

0 comments on commit a9afeb6

Please sign in to comment.