EduNetworkBuilder/Web/network.js

150 lines
4.2 KiB
JavaScript
Raw Normal View History

//This file contains the information about the puzzle (network)
//Each puzzle is stored in the allpuzzles variable. We need to select one and use it
var puzzle=networkFromName("Level0-Ping");
//var puzzle=networkFromName("Level0-NeedsLink");
//var puzzle=networkFromName("Level0_NetworkLoop");
//var puzzle=networkFromName("Level0_NetworkLoop2");
//var puzzle=networkFromName("Level0-SimpleDHCP");
//console.log(puzzle.name);
var device=deviceFromName("laptop0");
if(device !== null) {console.log("We found laptop0");}
else { console.log("Seems to be null"); }
function switchPuzzle(target)
{
var newpuzzle=null;
if(typeof(target)==="string") newpuzzle = networkFromName(target);
if(typeof(target)==="number") newpuzzle = networkFromIndex(target);
if(newpuzzle!= null)
{
puzzle = newpuzzle;
PrintScreen();
}
}
function networkFromName(what)
{
let index=0;
while (index < allpuzzles.length) {
//console.log(allpuzzles[index].EduNetworkBuilder.Network.name);
if(allpuzzles[index].EduNetworkBuilder.Network.name == what)
{
console.log("Found " + what + " at index " + index);
return structuredClone(allpuzzles[index].EduNetworkBuilder.Network);
}
index++;
}
}
function networkFromIndex(what)
{
if(typeof(what)==="number" && what >= 0 && what < allpuzzles.length){
return structuredClone(allpuzzles[what].EduNetworkBuilder.Network);
}
return null;
}
2024-04-19 18:02:46 +02:00
function networkNamesMatchingText(textToMatch)
{
var list = [];
var re = new RegExp(textToMatch);
let index=0;
while (index < allpuzzles.length) {
2024-04-19 18:02:46 +02:00
//console.log(allpuzzles[index].EduNetworkBuilder.Network.name);
if(re.test(allpuzzles[index].EduNetworkBuilder.Network.name))
{
//console.log("Found " + textToMatch + " at index " + index + " " + allpuzzles[index].EduNetworkBuilder.Network.name);
list.push(allpuzzles[index].EduNetworkBuilder.Network.name);
}
index++;
}
2024-04-19 18:02:46 +02:00
return list;
}
function deviceFromName(what)
{
if (puzzle == null) return null; //If the puzzle has not been set, return a null
let index=0;
while (index < puzzle.device.length) {
if (puzzle.device[index].hostname == what) return puzzle.device[index]; //return the device that matches
index++;
}
return null; //No match. return a null value
}
function deviceFromID(what)
{
if (puzzle == null) return null; //If the puzzle has not been set, return a null
let index=0;
while (index < puzzle.device.length) {
if (puzzle.device[index].uniqueidentifier == what) return puzzle.device[index]; //return the device that matches
index++;
}
return null; //No match. return a null value
}
2024-04-22 22:32:10 +02:00
//return a list of all the ip addresses a device has
function ipsFromDevice(what) {
var ipaddresses = [];
for (var i = 0; i < what.nic.length; i++) {
ipaddresses = ipaddresses.concat(ipsFromNic(what.nic[i]));
}
//console.log("Adding to nics " + ipaddresses.length);
return ipaddresses;
}
function ipsFromInterface(what, nictype) {
var ipaddresses = [];
if (typeof (what.myip) === "array") {
for (var x = 0; x < what.myip.length; x++) {
var one = {
nic: what.nicname,
ip: what.myip[x].ip,
mask: what.myip[x].mask,
cidrip: what.myip[x].ip + "/" + NetmaskToCIDR(what.myip[x].mask),
nictype: nictype,
};
ipaddresses.push(one);
}
}
else if (typeof (what.myip) === "object") {
var one = {
nic: what.nicname,
ip: what.myip.ip,
mask: what.myip.mask,
cidrip: what.myip.ip + "/" + NetmaskToCIDR(what.myip.mask),
nictype: nictype,
};
ipaddresses.push(one);
//console.log("found an ip object: " + JSON.stringify(one) + " " + nictype);
}
2024-04-22 22:32:10 +02:00
return ipaddresses;
}
function ipsFromNic(what) {
var ipaddresses = [];
//console.log("Looking at a nic: " + JSON.stringify(what));
if (typeof (what.interface) === "array") {
for (var i = 0; i < what.interface.length; i++) {
//console.log("Trying to add a nic." + what.interface[i].nicname);
ipaddresses=ipaddresses.concat(ipsFromInterface(what.interface[i],what.nictype[0]));
}
}
else if (typeof (what.interface) === "object")
ipaddresses=ipaddresses.concat(ipsFromInterface(what.interface, what.nictype[0]));
return ipaddresses;
}
function NetmaskToCIDR(mask) {
var cidr = 0;
var maskNodes = mask.match(/(\d+)/g);
for (var i in maskNodes) {
cidr += (((maskNodes[i] >>> 0).toString(2)).match(/1/g) || []).length;
}
return cidr;
}