-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added async calls to the backend to run nmap in parallel.
- Loading branch information
Showing
5 changed files
with
154 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,68 @@ | ||
import { IPListToRange } from "../utils/IPUtils"; | ||
|
||
export const BASE_URI = import.meta.env.VITE_API_URL; | ||
|
||
export const scanListType = async (ip_arg: string) => { | ||
const response = await fetch(`${BASE_URI}/scan/list/${ip_arg}`); | ||
export const fetchPingData = async (ip_arg: string) => { | ||
const response = await fetch(`${BASE_URI}/scan/ping/${ip_arg}`); | ||
const data = await response.json(); | ||
return data; | ||
}; | ||
|
||
export const scanPingType = async (ip_arg: string) => { | ||
const response = await fetch(`${BASE_URI}/scan/ping/${ip_arg}`); | ||
export const fetchIPList = async (ip_arg: string) => { | ||
const response = await fetch(`${BASE_URI}/scan/list/${ip_arg}`); | ||
const data = await response.json(); | ||
return data; | ||
}; | ||
|
||
const doFullScan = async (ip_arg: string) => { | ||
let ipList = fetchIPList(ip_arg); | ||
ipList.then((data) => { | ||
console.log("IP List: ", data); | ||
if (data.length === 0) { | ||
console.log("No IP Address found in the given range"); | ||
return; | ||
} | ||
|
||
// get host IP object | ||
const hosts = data.result.nmaprun.host; | ||
|
||
console.log("Hosts: ", hosts); | ||
|
||
// divide hosts into chunks of 10 for parallel scanning | ||
const chunkSize = 10; | ||
const chunkedHosts = []; | ||
for (let i = 0; i < hosts.length; i += chunkSize) { | ||
chunkedHosts.push(hosts.slice(i, i + chunkSize)); | ||
} | ||
|
||
console.log("Chunked Hosts: ", chunkedHosts); | ||
|
||
// for each chunk, convert the IP list to IP range and scan in parallel | ||
chunkedHosts.forEach((chunk: any) => { | ||
const ipRange = IPListToRange( | ||
chunk.map((host: any) => host.address["@addr"]) | ||
); | ||
console.log("IP Range: ", ipRange); | ||
|
||
fetchPingData(ipRange).then((data) => { | ||
console.log("Ping Data: ", data); | ||
|
||
// Sometimes host is missing, if all IPs are down. | ||
if ("host" in data.result.nmaprun) { | ||
const hosts = data.result.nmaprun.host; | ||
|
||
// hosts can be an array, when multiple hosts are up. Otherwise, it's an object. | ||
if (hosts.isArray) { | ||
hosts.forEach((host: any) => { | ||
console.log("Host: ", host); | ||
}); | ||
} else { | ||
console.log("Host: ", hosts); | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
export default doFullScan; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Function to convert a range of IP addresses to a list of IP addresses | ||
// Abandon all hope, ye who enter here | ||
export const IPListToRange = (ipList: string[]): string => { | ||
let ipRange: string; | ||
|
||
if (ipList.length === 0) { | ||
return ""; | ||
} | ||
|
||
const sortedIPList = ipList | ||
.map((ip) => { | ||
return ip.split(".").map((octet) => { | ||
return parseInt(octet); | ||
}); | ||
}) | ||
.sort((a, b) => { | ||
return a[0] - b[0] || a[1] - b[1] || a[2] - b[2] || a[3] - b[3]; | ||
}); | ||
|
||
const baseIP = sortedIPList[0].slice(0, 3).join("."); // get the base IP - first three octets | ||
let startIP = sortedIPList[0][3]; // get the start IP - last octet | ||
let endIP = sortedIPList[sortedIPList.length - 1][3]; // get the end IP - last octet | ||
|
||
ipRange = `${baseIP}.${startIP}-${endIP}`; | ||
|
||
return ipRange; | ||
}; |