-
Notifications
You must be signed in to change notification settings - Fork 0
/
lants-address.ts
60 lines (52 loc) · 2.13 KB
/
lants-address.ts
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
58
59
60
// Fixed bug -- handle non-octet mask
/* ------------------------------------------------------------------
* node-lifx-lan - lifx-lan-address.js
*
* Copyright (c) 2017-2018, Futomi Hatano, All rights reserved.
* Released under the MIT license
* Date: 2018-07-01
* ---------------------------------------------------------------- */
'use strict';
// const mOs = require('os');
import * as mOs from 'os';
type NetworkInterfaceInfoWithBroadcast = mOs.NetworkInterfaceInfo | { broadcast: mOs.NetworkInterfaceInfo };
/* ------------------------------------------------------------------
* Constructor: LifxLanAddress()
* ---------------------------------------------------------------- */
// export class LifxLanAddress {
/* ------------------------------------------------------------------
* Method: getNetworkInterfaces()
* ---------------------------------------------------------------- */
export function getNetworkInterfaces() {
const list: mOs.NetworkInterfaceInfo[] = []; // Need to fix this signature
// const list: NetworkInterfaceInfoWithBroadcast[] = []; // Need to fix this signature
const netifs = mOs.networkInterfaces();
for (const dev in netifs) {
netifs[dev]?.forEach((info) => { // The ? is here because tsc has gone fuckin' loco on null
let family = info.family;
if (typeof family == "number")
family = `IPv${family}`; // Strange change work-around
if (family !== 'IPv4' || info.internal === true) {
return;
}
let info4 = info as mOs.NetworkInterfaceInfoIPv4;
// if (/^169\.254\./.test(info.address)) {
// // return; // Why??
// console.log(`Debug ${info.address}`);
// }
(<any>info)['broadcast'] = _getBroadcastAddress(info4);
list.push(info);
})
}
return list;
};
function _getBroadcastAddress(info: mOs.NetworkInterfaceInfoIPv4) {
const addr_parts = _convIPv4ToNumList(info.address);
const mask_parts = _convIPv4ToNumList(info.netmask);
const cast_parts = addr_parts.map((a, i) => (~mask_parts[i] & 0xff) | a);
return cast_parts.join('.');
};
function _convIPv4ToNumList(address: string) {
return address.split('.').map(a => parseInt(a));
};
// export const LifxLanAddressx = new LifxLanAddress();