Init Commit

This commit is contained in:
Joshua Shoemaker 2017-09-08 19:17:01 -05:00
commit a30bff8abb
6 changed files with 583 additions and 0 deletions

BIN
LOST_THE_GAME.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 240 KiB

289
app.js Normal file
View File

@ -0,0 +1,289 @@
//----------Constant Values and Objects---------
let targetIpAddress = '';
const lockoutMax = 4;
let lockoutHits = 0;
let ipAttempts = [];
let time = 460000;
let lose = false;
let win = false;
let timerElement = document.getElementById('timer');
let timeInterval = {};
const systemTypes = ["HIDDEN", "KALILINUX", "WINDOWSXP", "WINDOWS2000",
"WINDOWS10", "REDHAT", "ANDROID4.4", "NETHUNTER"];
const Entry = function(){
return {
value: createIP(),
machineType: systemTypes[randomInRange(0, systemTypes.length - 1, 0)],
status: "ACTIVE",
hostName: createRandomName(),
lastResponse: randomInRange(1000, 10000000, 0),
systemLocation: {
long: randomInRange(-180, 180, 3),
lat: randomInRange(-180, 180, 3)
}
}
}
//-----------Helper Functions-----------
function createIP() {
let text = "";
let possible = "0123456789";
for (var i = 0; i < 10; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function formatIP(ip){
let newIP = ip;
newIP = newIP.slice(0, 2) + '.' + newIP.slice(2);
newIP = newIP.slice(0, 6) + '.' + newIP.slice(6);
newIP = newIP.slice(0, 9) + '.' + newIP.slice(9);
return newIP;
}
function randomInRange(min, max, fixed){
return (Math.random() * (max - min) + min).toFixed(fixed) * 1;
}
function createEntryHTML(entry){
let {value, status, machineType, hostName, lastResponse, systemLocation} = entry;
let ipAddress = formatIP(value);
let htmlString = "<tbody>\
<tr class='entry' data-ip-value='"+ value +"'>\
<span style='display: hidden'></span>\
<td>"+ formatIP(value) +"</td>\
<td>"+ status +"</td>\
<td>"+ hostName +"</td>\
<td>"+ machineType +"</td>\
<td>"+ lastResponse +"MS</td>\
<td>"+ systemLocation.long + "-" + systemLocation.lat +"</td>\
</tr>\
</tbody>"
return htmlString;
}
function createEntryHTMLArray(entries){
let htmlStrings = [];
entries.forEach(function(e) {
htmlStrings.push(createEntryHTML(e));
}, this);
return htmlStrings;
}
function createRandomName(){
let text = "";
let possible = "0123456789QWERTYUIOP_-ASDFGHJKLZXCVBNM";
for (var i = 0; i < 10; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function createEntryArray(){
let entries = [];
for(i = 0; i < 27; i++){
entries.push(new Entry());
}
return entries;
}
function concatEntryHTMLArray(entries){
let htmlString = "";
entries.forEach(function(e) {
htmlString += e;
}, this);
return htmlString;
}
function renderEntries(htmlString){
document.getElementById('entry_table').innerHTML = htmlString;
}
function extractIpAddressFromElement(element){
ipAddress = element.getAttribute('data-ip-value');
return ipAddress;
}
function selectTargetIpAddress(entries){
let value = entries[randomInRange(0, entries.length - 1, 0)].value;
return value;
}
function compareIpAddress(value){
let levDis = new Levenshtein(value, targetIpAddress);
let similarCount = 10 - levDis.distance;
return similarCount;
}
//----------Business Logic--------
function init(){
let entryArray = createEntryArray();
let htmlArray = createEntryHTMLArray(entryArray);
let entryHTMLString = concatEntryHTMLArray(htmlArray);
let entryElements = document.getElementsByClassName('entry');
targetIpAddress = selectTargetIpAddress(entryArray);
renderEntries(entryHTMLString);
assignClickEvent(entryElements);
timeInterval = setInterval(countDown, 10);
console.log(targetIpAddress);
}
function assignClickEvent(elements){
for(i = 0; i < elements.length; i++){
let entry = elements[i];
entry.onclick = function(){
clickedEntry(entry);
}
}
}
function clickedEntry(entry){
if(!lose && !win){
let ipDifference = compareIpAddress(extractIpAddressFromElement(entry));
if(ipDifference === 10){
targetIpAddressFound(entry);
renderSuccessPrecentage(100)
}
else{
wrongEntrySelected(entry, ipDifference);
renderLockout();
renderSuccessPrecentage(ipDifference * 10);
checkStatus();
}
}
}
function targetIpAddressFound(entry){
gameWin();
}
function wrongEntrySelected(entry, similarity){
let value = extractIpAddressFromElement(entry);
lockoutHits = lockoutHits + 1;
saveAttempt(value);
renderAttempts();
console.log(value + " was incorrect. Tries left: " + (lockoutMax - lockoutHits));
console.log(similarity + " characters were correct. Try Again!")
}
function renderSuccessPrecentage(percentage){
let successPercentage = document.getElementById('precentage');
successPercentage.innerHTML = percentage + "%";
}
function renderLockout(){
let lockoutElement = document.getElementById('lockout');
lockoutElement.innerHTML = '';
for(i = 0; i < lockoutHits; i++){
lockoutElement.innerHTML = lockoutElement.innerHTML + "<span class'lockoutMark'> X </span>";
}
}
function saveAttempt(value){
ipAttempts.push(value);
}
function renderAttempts(){
let attemptsTable = document.getElementById('attempts_table');
attemptsTable.innerHTML = "";
ipAttempts.forEach(function(e) {
attemptsTable.innerHTML += "<td>" + formatIP(e) + "</td>\
<td>" + compareIpAddress(e) + " similar chars</td>"
}, this);
}
function checkStatus(){
if(lockoutHits >= lockoutMax){
gameLose();
}
}
function gameLose(){
let entryElements = document.getElementsByClassName('entry');
let entryArray = [];
lose = true;
timerElement.innerHTML = 0;
clearInterval(timeInterval);
Array.prototype.forEach.call(entryElements, function(e) {
e.className = "entry error"
}, this);
}
function gameWin(){
let targetElement = document.querySelector('[data-ip-value="' + targetIpAddress + '"]')
win = true;
targetElement.className = "win";
clearInterval(timeInterval);
timerElement.innerHTML = 0;
}
function countDown(){
if(time > 0 && !lose){
time -= 10;
timerElement.innerHTML = time;
}
else{
gameLose();
}
}
init();

75
index.html Normal file
View File

@ -0,0 +1,75 @@
<!DOCTYPE html>
<html>
<head>
<title>Lost</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<section class="screen">
<h3 id="title">DISPLAY REPORT</h3>
<section class="score-wrapper">
<div class="left">
<h3>TRACKING SUCCESS: <span class="green" id="precentage"></span></h3>
<h3>QUERY: <span class="green" id="query-name"></span></h3>
</div>
<div class="right">
<h3>BRUTE FORCE LOCKOUT: <span class="green" id="lockout"></span></h3>
<h3>TIME LOCKOUT: <span class="green" id="timer"></span>MS</h3>
</div>
</section>
<hr>
<section id="play-wrapper">
<table>
<thead>
<tr class="green">
<th>IP ADDRESS</th>
<th>ACTIVE</th>
<th>HOST_NAME</th>
<th>MACHINE_TYPE</th>
<th>LAST_RESPONCE</th>
<th>SYSTEM_LOCATION</th>
</tr>
</thead>
<tbody id="entry_table">
</tbody>
</table>
</section>
<hr>
<section class="response-wrapper">
<table id="attempted-ips">
<thead>
<th class="green">ATTEMPTS</th>
<th class="green">SHARED CHARS</th>
</thead>
<tbody id="attempts_table">
</tbody>
</table>
</section>
</section>
</body>
<script src="levenshtein.js"></script>
<script src="app.js"></script>
</html>

106
levenshtein.js Normal file
View File

@ -0,0 +1,106 @@
(function(root, factory){
if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
define(function(){
return factory(root);
});
} else if (typeof module == 'object' && module && module.exports) {
module.exports = factory(root);
} else {
root.Levenshtein = factory(root);
}
}(this, function(root){
function forEach( array, fn ) { var i, length
i = -1
length = array.length
while ( ++i < length )
fn( array[ i ], i, array )
}
function map( array, fn ) { var result
result = Array( array.length )
forEach( array, function ( val, i, array ) {
result[i] = fn( val, i, array )
})
return result
}
function reduce( array, fn, accumulator ) {
forEach( array, function( val, i, array ) {
accumulator = fn( val, i, array )
})
return accumulator
}
// Levenshtein distance
function Levenshtein( str_m, str_n ) { var previous, current, matrix
// Constructor
matrix = this._matrix = []
// Sanity checks
if ( str_m == str_n )
return this.distance = 0
else if ( str_m == '' )
return this.distance = str_n.length
else if ( str_n == '' )
return this.distance = str_m.length
else {
// Danger Will Robinson
previous = [ 0 ]
forEach( str_m, function( v, i ) { i++, previous[ i ] = i } )
matrix[0] = previous
forEach( str_n, function( n_val, n_idx ) {
current = [ ++n_idx ]
forEach( str_m, function( m_val, m_idx ) {
m_idx++
if ( str_m.charAt( m_idx - 1 ) == str_n.charAt( n_idx - 1 ) )
current[ m_idx ] = previous[ m_idx - 1 ]
else
current[ m_idx ] = Math.min
( previous[ m_idx ] + 1 // Deletion
, current[ m_idx - 1 ] + 1 // Insertion
, previous[ m_idx - 1 ] + 1 // Subtraction
)
})
previous = current
matrix[ matrix.length ] = previous
})
return this.distance = current[ current.length - 1 ]
}
}
Levenshtein.prototype.toString = Levenshtein.prototype.inspect = function inspect ( no_print ) { var matrix, max, buff, sep, rows
matrix = this.getMatrix()
max = reduce( matrix,function( m, o ) {
return Math.max( m, reduce( o, Math.max, 0 ) )
}, 0 )
buff = Array( ( max + '' ).length ).join( ' ' )
sep = []
while ( sep.length < (matrix[0] && matrix[0].length || 0) )
sep[ sep.length ] = Array( buff.length + 1 ).join( '-' )
sep = sep.join( '-+' ) + '-'
rows = map( matrix, function( row ) { var cells
cells = map( row, function( cell ) {
return ( buff + cell ).slice( - buff.length )
})
return cells.join( ' |' ) + ' '
})
return rows.join( "\n" + sep + "\n" )
}
Levenshtein.prototype.getMatrix = function () {
return this._matrix.slice()
}
Levenshtein.prototype.valueOf = function() {
return this.distance
}
return Levenshtein
}));

BIN
lost_the_game_UI.xd Normal file

Binary file not shown.

113
style.css Normal file
View File

@ -0,0 +1,113 @@
.screen{
width: 900px;
height: 800px;
margin: 30px auto;
border-color: #00FF00;
border-width: 1px;
border-style: solid;
border-radius: 26px;
background-color: black;
color: white;
font-family: monospace;
overflow: hidden;
}
#title{
text-align: center;
}
.green{
color: #00FF00;
}
.error{
background-color: red;
color: white;
}
.win{
background-color: white;
color: #00FF00;
}
.win :hover{
background-color: white;
color: #00FF00;
}
.score-wrapper{
padding: 5px;
}
.score-wrapper h3{
margin: 0px;
}
.score-wrapper .left{
float: left;
width: 50%;
margin: 0px;
}
.score-wrapper .right{
float: right;
width: 50%;
margin: 0px;
}
hr{
border-color: #00ff00;
margin: 6px 0px;
}
table{
width: 900px;
font-size: 14px;
}
th{
text-align: left;
padding: 0px 4px;
}
td{
padding: 0px 4px;
overflow: hidden;
}
.column-titles{
margin-bottom: 2px;
}
#play-wrapper{
padding:0px 2px;
margin: 0px;
}
.column{
padding: 0px 1px;
overflow: hidden;
margin: 0px;
}
.address-choice{
margin: 0px;
padding: 0px;
}
.address-choice span{
overflow: hidden;
display: inline-block;
}
.entry:hover{
color: black;
background-color: #00ff00;
cursor: pointer;
}
.lockoutMark{
display: inline;
}