haraka / haraka-net-utils

haraka network utilities
https://www.npmjs.com/package/haraka-net-utils
MIT License
2 stars 13 forks source link

Get local ips list #15

Closed acharkizakaria closed 7 years ago

acharkizakaria commented 7 years ago

Hi guys, I want to use the list of the local ips in some conditions inside a plugin that using the 'send_email' hook, is there any defined constant contain this info?

I could get the addresses using several ways, e.g :

var localAddresses = function(){
        var os = require('os');

        var interfaces = os.networkInterfaces();
        var addresses = [];
        for (var k in interfaces) {
            for (var k2 in interfaces[k]) {
                var address = interfaces[k][k2];
                if (address.family === 'IPv4' && !address.internal) {
                    addresses.push(address.address);
                }
            }
        }

        return addresses;
}

But I think it will be better do define the list elsewhere so we don't have to retrieve the ips in every call of the plugin. (Define a global variable when the sever starting up for example).

If there's no variable contain this information, I'm not sure if the 'haraka-net-utils' is the right place to add this function.

smfreegard commented 7 years ago

Just do this in your pluign by using

exports.hook_init_master = exports.hook_init_child = function (next)  {
     server.notes.interfaces = localAddresses();
     return next();
}

That way it will only run once at start-up and available to all plugins via server.notes.interfaces

acharkizakaria commented 7 years ago

Thanks you @smfreegard for the quick answer, I will try it..

acharkizakaria commented 7 years ago

Works like a charm, thanks again @smfreegard.