This repository was archived by the owner on Feb 27, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathip.js
57 lines (51 loc) · 1.95 KB
/
ip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module.exports = function (RED) {
"use strict";
const internalIp = require('internal-ip');
const publicIp = require('public-ip');
function ip(n) {
RED.nodes.createNode(this, n);
var node = this;
node.name = n.name;
node.opt = {};
node.requests = [];
if(n.https) node.opt.onlyHttps = true;
if((parseInt(n.timeout)) !== 5000 && (parseInt(n.timeout)) >= 5000 && (parseInt(n.timeout)) <= 10000){
node.opt.timeout = parseInt(n.timeout);
}
function getIP(name, request, args){
return async () => {
try{
let ip = await request(args);
return [name, ip];
}catch(e){
return [name, null];
}
};
}
if(n.internalIPv4) node.requests.push(getIP('internalIPv4', internalIp.v4));
if(n.internalIPv6) node.requests.push(getIP('internalIPv6', internalIp.v6));
if(n.publicIPv4) node.requests.push(getIP('publicIPv4', publicIp.v4, node.opt));
if(n.publicIPv6) node.requests.push(getIP('publicIPv6', publicIp.v6, node.opt));
node.on("input", function(msg) {
msg.payload = {};
(async () =>{
let requests = node.requests;
let promises = [];
for (let i = 0; i < requests.length; i++) {
promises.push(requests[i]());
}
let results = await Promise.all(promises);
for(let i = 0; i < results.length; i++){
let result = results[i];
if(result !== null && result !== undefined){
msg.payload[result[0]] = result[1];
}
}
if(Object.keys(msg.payload).length > 0){
node.send(msg);
}
})();
});
}
RED.nodes.registerType("ip", ip);
};