netjs-cli
Advanced tools
Comparing version 1.0.2 to 1.0.3
13
index.js
@@ -12,2 +12,4 @@ #!/usr/bin/env node | ||
import { changeStateAction } from "./actions/changeState.js"; | ||
import { addDefaultGatewayAction } from "./actions/addDefaultGateway.js"; | ||
import { removeDefaultGatewayAction } from "./actions/removeDefaultGateway.js"; | ||
@@ -39,2 +41,4 @@ const cli = meow(` | ||
'Remove ip address from interface', | ||
'Add default gateway to interface', | ||
'Remove default gateway from interface', | ||
'Flush ip addresses from interface', | ||
@@ -44,3 +48,4 @@ 'Inherit ip addresses from dhcp', | ||
'Quit' | ||
] | ||
], | ||
pageSize: 9 | ||
} | ||
@@ -64,2 +69,8 @@ ]); | ||
break; | ||
case 'Add default gateway to interface': | ||
await addDefaultGatewayAction (); | ||
break; | ||
case 'Remove default gateway from interface': | ||
await removeDefaultGatewayAction (); | ||
break; | ||
case 'Flush ip addresses from interface': | ||
@@ -66,0 +77,0 @@ await flushAddressesAction (); |
{ | ||
"name": "netjs-cli", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Network management tool written in JS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -89,3 +89,3 @@ <!-- Improved compatibility of back to top link: See: https://github.com/othneildrew/Best-README-Template/pull/73 --> | ||
- [x] Changing state of interface | ||
- [ ] Editing default gateway | ||
- [x] Editing default gateway | ||
- [ ] Editing DNS addresses | ||
@@ -92,0 +92,0 @@ - [ ] Saving configuration without any network manager installed (make own manager) |
76
tools.js
@@ -16,2 +16,7 @@ import { networkInterfaces } from 'os'; | ||
/** | ||
* 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 = {}; | ||
@@ -31,2 +36,73 @@ | ||
}); | ||
} | ||
/** | ||
* 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 (''); | ||
} |
Sorry, the diff of this file is not supported yet
52946
15
645