I am implementing SMTP email validation via https://www.npmjs.com/package/deep-email-validator in my Node.js application and want to route the connection through a Tor proxy for anonymity. I updated the code to use the 'socks' module and specified the Tor proxy options correctly. However, all attempts to validate email addresses using the Tor proxy result in timeouts.
I have verified that Tor is running and listening on the specified proxy port. Additionally, the email validation code works fine without using the Tor proxy.
I expect the email validation code to successfully connect to the SMTP server through the Tor proxy and receive SMTP responses. Any insights, suggestions, or troubleshooting steps to identify the cause of the timeouts and successfully validate email addresses through the Tor proxy would be greatly appreciated. Thank you!
Here's the working code for it:
export const checkSMTP = async (sender: string, recipient: string, exchange: string): Promise<OutputFormat> => {
const timeout = 1000 * 10 // 10 seconds
return new Promise(r => {
let receivedData = false
let closed = false
const socket = net.createConnection(25, exchange)
socket.setEncoding('ascii')
socket.setTimeout(timeout)
socket.on('error', error => {
log('error', error)
socket.emit('fail', error)
})
socket.on('close', hadError => {
if (!receivedData && !hadError) {
socket.emit('fail', 'Mail server closed connection without sending any data.')
}
if (!closed) {
socket.emit('fail', 'Mail server closed connection unexpectedly.')
}
})
socket.once('fail', msg => {
closed = true
r(createOutput('smtp', msg))
if (socket.writable && !socket.destroyed) {
socket.write(`quit\r\n`)
socket.end()
socket.destroy()
}
})
socket.on('success', () => {
closed = true
if (socket.writable && !socket.destroyed) {
socket.write(`quit\r\n`)
socket.end()
socket.destroy()
}
r(createOutput())
})
const commands = [`helo ${exchange}\r\n`, `mail from: <${sender}>\r\n`, `rcpt to: <${recipient}>\r\n`]
let i = 0
socket.on('next', () => {
if (i < 3) {
if (socket.writable) {
socket.write(commands[i++])
} else {
socket.emit('fail', 'SMTP communication unexpectedly closed.')
}
} else {
socket.emit('success')
}
})
socket.on('timeout', () => {
socket.emit('fail', 'Timeout')
})
socket.on('connect', () => {
socket.on('data', msg => {
receivedData = true
log('data', msg)
if (hasCode(msg, 220) || hasCode(msg, 250)) {
socket.emit('next', msg)
} else if (hasCode(msg, 550)) {
socket.emit('fail', 'Mailbox not found.')
} else {
const [code] = Object.typedKeys(ErrorCodes).filter(x => hasCode(msg, x))
socket.emit('fail', ErrorCodes[code] || 'Unrecognized SMTP response.')
}
})
})
})
}
Here's the updated code snippet using Tor, giving timeout::
const torProxyOptions: SocksClientOptions = {
proxy: {
ipaddress: '127.0.0.1',
port: port, // tried from 9001 to 9099
type: 5,
},
destination: {
host: exchange,
port: 25,
},
command: 'connect',
};
SocksClient.createConnection(torProxyOptions, (err, info) => {
if (err) {
console.error('Error connecting via Tor proxy:', err);
return;
}
const socket = info?.socket;
if (!socket) {
console.error('Could not establish a socket connection through the Tor proxy.');
return;
}
socket.setEncoding('ascii');
socket.setTimeout(timeout);
socket.on('error', (error) => {
log('error', error);
socket.emit('fail', error);
});
// rest of the code is same
});