Skip to content
Permalink
78b3ff4020
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
255 lines (215 sloc) 7.44 KB
import { Attacker } from './attacker.js';
import { Server } from './server.js';
import { Router } from './router.js';
import { DDD } from './ddd.js';
import { DDDController } from './ddd_controller.js';
import { Client } from './client.js';
import { Packet } from './packet.js';
import { Garbage } from './garbage.js';
//Define the canvas and context of canvas
var canvas = document.querySelector('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var ctx = canvas.getContext('2d');
var dddState; //global variable for DDD state
function drawEntities(allEntities) {
// Given a list of entities in the system, draw them to the screen
for (let entity of allEntities) {
entity.draw();
}
}
function runSim(packetArray, allEntities) {
let requestid = window.requestAnimationFrame(() => runSim(packetArray, allEntities));
ctx.clearRect(0, 0, innerWidth, innerHeight);
drawEntities(allEntities);
if (packetArray.length == 0) {
window.cancelAnimationFrame(requestid);
}
for (let i = 0; i < packetArray.length; i++) {
let packet = packetArray[i];
if (packet.done == false) {
packet.update();
} else {
packet.done = false;
packetArray.splice(i, 1);
}
}
}
function animatePacket(packets, allEntities) {
let sliderVal = document.getElementById('myRange').value; // global speed
let map = new Map();
map[1] = 20;
map[2] = 30;
map[3] = 40;
map[4] = 50;
map[5] = 60;
let iterations = map[sliderVal];
//console.log(sliderVal);
//console.log(iterations);
for (let packet of packets) {
packet.dxX = (packet.x - packet.nextEntity.x) / iterations;
packet.dxY = (packet.y - packet.nextEntity.y) / iterations;
}
runSim(packets, allEntities);
//Waits for 5 seconds to give requestionAnimationFrame time to run
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
;
}, 1100 / (60 / iterations)
);
});
}
function createPackets(numPackets, originEntity, nPorts) {
// Create numPackets packets at x and y of originEntity
let packetList = [];
let srcIp = "5.6.7.8";
let dstIp = "1.2.3.4";
let srcPort = 80;
for (let i = 0; i < numPackets; ++i) {
let dstPort = DDD.generatePort(nPorts);
console.log(dstPort);
let packetX = originEntity.x;
let packetY = originEntity.y;
let radius = 15;
let packet = new Packet(packetX, packetY, radius, srcIp, srcPort, dstIp, dstPort, ctx);
packetList.push(packet);
}
return packetList;
}
function allEmpty(entities) {
// Returns true if the queue of all the entities in the system are empty
for (let entity of entities) {
if (entity.hasPackets()) {
return false;
}
}
return true;
}
// dashboard functions
function dashboardOpenPorts(ddd) {
let printPorts = ddd.openPorts;
console.log(printPorts);
var printOut = new String("");
for (let i = 0; i < printPorts.length; i++) {
if (i == printPorts.length - 1) {
printOut = printOut + " and " + printPorts[i];
} else {
printOut = printOut + printPorts[i] + ", "
}
}
document.getElementById("openPorts").textContent = "DDD Open Ports: " + printOut;
}
function dashboardAttackerPorts(packets) {
let printPorts = [];
for(let packet of packets){
printPorts.push(packet.dstPort);
}
var printOut = new String("");
for (let i = 0; i < printPorts.length; i++) {
if (i == printPorts.length - 1) {
printOut = printOut + " and " + printPorts[i];
} else {
printOut = printOut + printPorts[i] + ", "
}
}
document.getElementById("attackerPorts").textContent = "Attacker Packet Ports: " + printOut;
}
function dashboardDroppedPackets(garbage){
document.getElementById("droppedPackets").textContent = "Dropped Packets: " + garbage.droppedPackets;
}
//Stop simulation
var stop = false;
window.stopSimulation = stopSimulation;
function stopSimulation() {
stop = true;
}
//Pause Simulation
var pause = false;
window.pauseSimulation = pauseSimulation;
function pauseSimulation() {
pause = true;
}
//Resume Simulation
window.resumeSimulation = resumeSimulation;
function resumeSimulation() {
pause = false;
}
window.startSimulation = startSimulation;
async function startSimulation() {
stop = false;
pause = false;
let packetsPerTimeUnit = 1;
let totalPackets = document.getElementById('npackets').value;
dddState = document.getElementById('DDDstate').checked; //global variable for DDD state
let nPorts = document.getElementById('nports').value;
// Initialize the entity objects
let y = 250;
let garbage = new Garbage(700, 320 + y);
let client = new Client(100, y, undefined, packetsPerTimeUnit);
let router = new Router(800, y, client, packetsPerTimeUnit);
if (dddState) {
var ddd = new DDD(400, y, client, packetsPerTimeUnit, nPorts);
ddd.garbage = garbage;
router.nextEntity = ddd;
}
let server = new Server(1200, y, router, packetsPerTimeUnit);
client.nextEntity = server;
garbage.nextEntity = server;
garbage.packetsPerTimeUnit = 1;
let attacker = new Attacker(1500, y, server, packetsPerTimeUnit);
let packetsList = createPackets(totalPackets, attacker, nPorts);
attacker.queue = packetsList;
dashboardAttackerPorts(packetsList);
let systemEntities = [attacker, server, router, client, garbage];
if (dddState) {
systemEntities = [attacker, server, router, ddd, client, garbage];
}
let allEntities = systemEntities.slice();
allEntities.push(garbage);
for (let entity of allEntities) {
entity.ctx = ctx;
}
while (!stop) {
while (pause || stop) {
var p = new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
;
}, 1000
);
});
await p;
}
if (stop) {
break;
}
// If all the queues of the entities are empty, then break out of the loop
if (allEmpty(systemEntities)) {
break;
}
// Metadata list that the animation uses to animate
let packets = [];
// Runs the actual simulation underneath the hood
for (let i = systemEntities.length - 1; i >= 0; i--) {
let entity = systemEntities[i];
let sentPackets = entity.sendPackets();
for (let sentPacket of sentPackets) {
if (sentPacket.dropped) {
sentPacket.nextEntity = garbage;
}
sentPacket.x = entity.x;
sentPacket.y = entity.y;
packets.push(sentPacket);
}
}
// Update dashboard
if(ddd){
dashboardOpenPorts(ddd);
}
dashboardDroppedPackets(garbage);
// Animate the packets moving through the system
await animatePacket(packets, allEntities);
// Prevents infinite loop. Take out after testing is complete
}
}