forked from Acode-Foundation/acode-plugin-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetNet.js
More file actions
30 lines (27 loc) · 906 Bytes
/
getNet.js
File metadata and controls
30 lines (27 loc) · 906 Bytes
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
const { networkInterfaces } = require('os');
module.exports = async (mode = 'dev') => {
const { WiFi, Ethernet } = getIp();
const [ip] = WiFi || Ethernet;
const port = '5500';
const src = `https://${ip || '10.0.0'}:${port}`;
console.log('Server starting at: ', src);
return { ip, port };
};
function getIp() {
const nets = networkInterfaces();
const results = {}; // Or just '{}', an empty object
Object.keys(nets).forEach((name) => {
nets[name].forEach((net) => {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
// 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4;
if (net.family === familyV4Value && !net.internal) {
if (!results[name]) {
results[name] = [];
}
results[name].push(net.address);
}
});
});
return results;
}