EduNetworkBuilder/Web/network.js

248 lines
7.3 KiB
JavaScript
Raw Permalink 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
//We define this here, even though it is used primarily in the UI. We define its values here
var ui_helplevel = 0;
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);
2024-05-10 23:03:52 +02:00
return networkFromIndex(index);
}
index++;
}
}
function networkFromIndex(what)
{
if(typeof(what)==="number" && what >= 0 && what < allpuzzles.length){
2024-05-10 23:03:52 +02:00
var newitem = structuredClone(allpuzzles[what].EduNetworkBuilder.Network);
console.log("It is an array:" + Array.isArray(newitem.nettest));
if (!Array.isArray(newitem.nettest)) {
2024-05-10 23:03:52 +02:00
var oneitem = newitem.nettest; //this is an object
newitem.nettest = [];
newitem.nettest.push(oneitem); //make it an one-item array.
}
//Set the help level to be whatever we need
if (newitem.startinghelplevel == "full") ui_helplevel = 3;
if (newitem.startinghelplevel == "hints") ui_helplevel = 2;
if (newitem.startinghelplevel == "basic") ui_helplevel = 1;
if (newitem.startinghelplevel == "none") ui_helplevel = 0;
console.log("Starting help level = " + ui_helplevel + " from " + newitem.startinghelplevel);
2024-05-10 23:03:52 +02:00
return newitem;
}
return null;
}
2024-05-10 23:03:52 +02:00
function deviceHasProblem(Device) {
var hostname = Device.hostname;
//console.log("Looking for tests on " + hostname + " and have: " + puzzle.nettest.length)
2024-05-10 23:03:52 +02:00
for (var index = 0; index < puzzle.nettest.length; index++) {
//console.log("Found test: " + JSON.stringify(puzzle.nettest));
2024-05-10 23:03:52 +02:00
if (puzzle.nettest[index].shost == hostname) {
if (puzzle.nettest[index].solved == null) {
//It has not yet been solved.
//console.log("Found problem on device: " + hostname);
2024-05-10 23:03:52 +02:00
return true;
}
}
2024-05-10 23:03:52 +02:00
}
return false;
}
function problemString(nettest) {
if (nettest == null) return "";
//The structure of a nettest
//"shost": "pc0",
//"dhost": "laptop0",
//"thetest": "SuccessfullyPings"
//if (ui_helplevel == 0) setStatus("There is a disturbance in the force. Guessing...");
//if (ui_helplevel == 1) setStatus("Service check detected a problem.");
//if (ui_helplevel == 2) setStatus("Someone submitted a trouble ticket.");
//if (ui_helplevel == 3) setStatus("Interviewed users for detailed info.");
switch (nettest.thetest) {
case "DHCPServerEnabled":
case "DeviceBlowsUpWithPower":
case "DeviceIsFrozen":
case "DeviceNeedsUPS":
case "FailedPing":
case "HelpRequest":
case "LockAll":
case "LockLocation":
case "LockNic":
case "LockVLANNames":
case "LockVLANsOnHost":
case "NeedsDefaultGW":
case "NeedsLinkToDevice":
case "NeedsLocalIPTo":
case "NeedsRouteToNet":
case "NeedsTaggedVLAN":
case "ReadContextHelp":
break;
case "SuccessfullyPings":
//level 0 is handled elsewhere
if (ui_helplevel == 2) return translator.getStr("NT_TstDiscriptPing");
if (ui_helplevel == 3) return translator.getStr("NT_TstDiscriptPing2") + nettest.dhost;
case "SuccessfullyPingsWithoutLoop":
case "SuccessfullyTraceroutes":
break;
}
return "";
}
function returnProblemStrings(Device) {
var hostname = Device.hostname;
var errors = [];
//console.log("Looking for tests on " + hostname + " and have: " + puzzle.nettest.length)
for (var index = 0; index < puzzle.nettest.length; index++) {
//console.log("Found test: " + JSON.stringify(puzzle.nettest));
if (puzzle.nettest[index].shost == hostname) {
if (puzzle.nettest[index].solved == null) {
//It has not yet been solved.
var error = problemString(puzzle.nettest[index]);
//console.log("We got a problem string:" + JSON.stringify(error));
if (error != "");
errors.push(error);
}
}
}
return errors;
}
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;
}