-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.js
106 lines (81 loc) · 3.01 KB
/
tools.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { networkInterfaces } from 'os';
import { promisify } from 'util';
import child_process from 'child_process';
import { readdirSync, readFileSync } from 'fs';
export const exec = promisify (child_process.exec);
export const keypress = async () => {
process.stdin.setRawMode (true);
process.stdin.resume ();
return new Promise (resolve => process.stdin.once ('data', () => {
process.stdin.setRawMode (false);
resolve ();
}));
}
/**
* Represents a collection of network interfaces and their associated details.
* Each key in the object represents a network interface name, and the value is an array containing the interface details.
* @type {Object.<string, Array<{family: string, address: string, netmask: string, cidr: string, mac: string, internal: boolean, status: string}>}
*/
export var interfaceList = {};
export function refreshInterfaces () {
interfaceList = {};
let upInterfaces = networkInterfaces ();
for (let i of Object.keys (upInterfaces)) {
interfaceList[i] = upInterfaces[i];
}
readdirSync ('/sys/class/net/').forEach (file => {
if (Object.keys (upInterfaces).indexOf (file) == -1) {
interfaceList[file] = [{status: readFileSync (`/sys/class/net/${file}/operstate`).toString ('utf-8')}];
}
});
}
/**
* Returns a list of possible IP addresses for a given IP address and subnet mask.
* @param {string} address - The IP address.
* @returns {string[]} - A list of possible IP addresses.
*/
export function determinePossibleGatewayAddress (address) {
const addresses = [];
const maskCidr = Number (address.split('/')[1]);
const zeros = 32 - maskCidr;
const ones = 32 - zeros;
const maskParts = [];
for (let i = 0; i < 4; i++) {
let maskPart = '';
for (let j = (i * 8) + 1; j < (i * 8) + 9; j++) {
if (j <= ones) {
maskPart += '1';
continue;
}
maskPart += '0';
}
maskParts.push (maskPart);
}
const addressParts = address.split('/')[0].split('.');
let newAddress = '';
for (let i = 0; i < addressParts.length; i++) {
if (i > 0) {
newAddress += '.';
}
let newAddressPart = Number (addressParts[i]) & parseInt (maskParts[i], 2);
if (i == addressParts.length - 1 && newAddressPart < 255) newAddressPart += 1;
newAddress += newAddressPart;
}
addresses.push (newAddress);
newAddress = '';
for (let i = 0; i < addressParts.length; i++) {
if (i > 0) {
newAddress += '.';
}
let newAddressPart = Number (addressParts[i]) | parseInt (stringBitNot(maskParts[i]), 2);
if (i == addressParts.length - 1 && parseInt (stringBitNot(maskParts[i]), 2) > 0) newAddressPart -= 1;
newAddress += newAddressPart;
}
addresses.push (newAddress);
return addresses;
}
function stringBitNot (bits) {
return bits.split ('').map (val => {
return val == '1'? '0' : '1';
}).join ('');
}