How to pass multiple host url's in the transport object for createTransport function in nodemailer - node.js

Is it possible to configure multiple host value with their respective ports in transport object of createTransport function of nodemailer library.
Right now my code is:
var transport = {
host: "devint.test.com",
port: 22,
auth: {
user: "test",
pass: "test"
}
}
let transporter = nodemailer.createTransport(transport);
In above transport object, I have passed single host i.e. devint.test.com in host property of transport object. In my application, there are multiple email server, So I want to pass multiple host name and their port in the host and port property of transport object. Is there any way to do?
PS: For now forget about that reason for having multiple email server, its long story.

Related

Node.js HTTP agent pre establish tcp connection

So I'm working on an application where there are a lot of network requests to be sent and so I have set up an http agent to reuse tcp connections like so, which would be used all over my application :
const https = require("https");
global.httpsAgent = new https.Agent({
keepAlive: true,
})
This will allow me to reuse tcp sockets that have already been created so I don't have to create them again and save time when dealing with lots of http requests. But before I send any http requests there will not be any tcp connections. So the first time I send an http request I have to wait till the tcp connection is established. Is there a way to tell the https agent to maintain a minimum number of free tcp sockets to a domain at all times ? Right now when my applications starts up I'm just sending meaningless http requests so that the tcp connections are established and kept-alive like so :
function establishTcpConns(numConns) {
let arr = [];
for(let i=0; i<numConns; i++) {
arr.push(
// fetch is imported like so : const fetch = require("node-fetch");
fetch("https://example.com", {
agent: url => global.httpsAgent,
headers: {
"connection": "keep-alive"
}
})
);
}
return Promise.allSettled(arr);
}
I see that there a function called agent.createConnection(options[, callback]) defined in the nodejs docs https://nodejs.org/api/http.html#agentcreateconnectionoptions-callback
Is this what I'm looking for? If so how would I use it?

Is there a workaround in the nodemailer package for other services?

I noticed by looking at nodemailer docs that they didn't support unoeruo mails as a service. Is there a workaround or do i need to use another package. And if i need to use another is there anyone you recommend?
The "well know services" are just for your convenience. You can always create the SMTP Transport on your own, while using the settings from unoeuro
Assuming you are using typescript, it would look like this. Just set user and pass to your credentials:
import * as Smtp from 'nodemailer';
const options = {
host: "websmtp.unoeuro.com",
port: 587,
secure: true,
auth: {
user: "YOUR-LOGIN",
pass: "YOUR-PASSWORD"
};
};
const transport = Smtp.createTransport(options);
//do something with transport:
transport.sendMail({...});

net module "socket.remoteAddress" value is wrong

I create a TCP IVP4 socket server in Node.js using the 'net' module, and it works great for almost all of my clients, but one client has an IP similar to (I won't disclose the real one):
180.190.154.97
but when they connect to my socket server and I console.log(socket.remoteAddress), the value is this:
180.190.193.2
Why would this be happening?
// Import dependencies
let net = require('net')
// Create socket server.
const SERVER = net.createServer(socketConnection)
function socketConnection(client) {
console.log(client.remoteAddress)
}
// Server code snippet
let SERVER_LISTEN_ADDRESS = '0.0.0.0'
if (Game.local) {
SERVER_LISTEN_ADDRESS = '127.0.0.1'
Game.port = 42480
}
SERVER.listen(Game.port, SERVER_LISTEN_ADDRESS, () => {
console.log('Listening on port ' + Game.port)
if (!Game.local)
postServer()
else
console.log('Running server locally.')
})
Expected result is that it should print their IPV4 value from a site like:
https://whatismyipaddress.com/
But instead it says something completely different.
The result shown by whatipmyaddress it is not always the public IP of the single host. It could be rather also the IP of the subnet or if there's a proxy the IP of the proxy server. The fact that the address differs by a few units suggests that those particular hosts are in a subnet where a NAT service is active, or protected by a proxy or where the router acts as a VPN to the outside, encapsulating and sending the message to the server. In this case probably the entire subnet is seen as a single host.
In case you're using Express, you may want to use request-ip package in order to retrieve the IP in a more robust way. Since it seems you're not using express you should implement something similar to the first example shown in the link. For your specific case according the code you give:
const requestIp = require('request-ip');
function socketConnection(client) {
const clientIp = requestIp.getClientIp(client);
console.log(client);
}

How to get the address of the freshly looked up hostname in Node.js?

I am to write out the freshly looked up IP address of a hostname in Node.js:
var net = require('net');
var sock = net.Socket();
sock.on('lookup', function(e)
{
console.log('DNS lookup');
console.log(address);
} )
...
sock.connect(80, 'google.com');
https://nodejs.org/api/net.html#net_event_lookup
says that the lookup event is
Emitted after resolving the hostname but before connecting. Not applicable to UNIX sockets.
err <Error> | <null> The error object. See dns.lookup().
address <string> The IP address.
family <string> | <null> The address type. See dns.lookup().
host <string> The hostname.
But which object has these fields? I tried them as simple variable names -- did not work, and as fields of the e object possibly passed to the anonymous function that I register for the lookup event -- that also did not work.
How can I access these fields upon lookup?
You get all the params in the callback, see the code below.
var net = require('net');
var sock = net.Socket();
const options = {
host: "google.com",
port: 80
};
sock.connect(options);
sock.on('lookup', function(err, address, family, host) {
console.log(address);
console.log(family);
console.log(host);
console.log(err);
});

Node : how to set static port to udp client in node js

I am very new to Udp Socket programming, here i implemented echo UDP Client which connects to UDP server
var buffer = require('buffer');
var udp = require('dgram');
// creating a client socket
var client = udp.createSocket('udp4');
//buffer msg
var data = Buffer.from('Pradip Shinde');
client.on('message',function(msg,info){
console.log('Data received from server : ' + msg.toString());
console.log('Received %d bytes from %s:%d\n',msg.length, info.address, info.port);
});
//sending msg
client.send(data,9300,'192.168.1.187',function(error){
if(error){
client.close();
}else{
console.log('Data sent from client!!!');
}
});
when this client send msg to server, operating system assign the random port to this client but in my scenario i want static port which will never change, is it possible to assign static port to udp client?
As mentioned in the documentation, you can use bind method to do this,
For UDP sockets, causes the dgram.Socket to listen for datagram messages on a named port and optional address that are passed as properties of an options object passed as the first argument. If port is not specified or is 0, the operating system will attempt to bind to a random port. If address is not specified, the operating system will attempt to listen on all addresses. Once binding is complete, a 'listening' event is emitted and the optional callback function is called.
Try using
// Creating a client socket
var client = udp.createSocket('udp4');
// Bind your port here
client.bind({
address: 'localhost',
port: 8000,
exclusive: true
});
For more information follow this documentation.

Resources