Commit f603526c authored by David Fernandez Lobon's avatar David Fernandez Lobon

commit actual

parent 9a9cc6ad
......@@ -124,7 +124,6 @@ public class MainNodeCom {
}
public Node initialize(String token){
log.info("[--INITIALIZATION PROCESS--] Querying the Main Node for a scanning configuration...");
......
......@@ -64,6 +64,14 @@ public class Host {
return host;
}
public static Host createHost(String ip, String discoveryMethod) {
Host host = new Host();
host.setIp(ip);
host.setMacManufacturer("");
host.setDiscoveryMethod(discoveryMethod);
return host;
}
public static Host createHost(String ip, String hostname, String mac, String vlanName, Date timestampIni, ScanInfo scanInfo, String discoveryMethod) {
Host host = new Host();
host.setIp(ip);
......
......@@ -60,7 +60,4 @@ public class Port {
public String toString() {
return String.format("Port %s open on host %s with protocol %s", portNumber, host.getIp(), protocol);
}
}
package adt.newportdetection;
import adt.newportdetection.runner.ListPortDetection;
import adt.newportdetection.runner.NewPortDetection;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;
@Service
@Slf4j
public class NewPortDetectionThread extends Thread {
private ListPortDetection listPortDetection;
private NewPortDetection newPortDetection;
private static int N_THREADS = 1;
private final AtomicBoolean running = new AtomicBoolean(true);
private static Semaphore mutex;
public NewPortDetectionThread (ListPortDetection listPortDetection){
this.listPortDetection = listPortDetection;
this.newPortDetection = new NewPortDetection(this.listPortDetection, N_THREADS);
mutex = new Semaphore(1);
}
public void parar() throws InterruptedException {
mutex.wait();
}
public void activar() {
mutex.notify();
}
@Override
public void run(){
try {
newPortDetection.readLinesSnif();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package adt.newportdetection.runner;
import adt.dto.Asset;
import adt.entity.Host;
import adt.entity.Port;
import adt.entity.ScanInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ListAllPortDetection {
private boolean primer_scan;
// Mapa de key ip y value otro mapa con key y value el puerto que ya tenga esa ip;
private Map<String,Map<Integer,Integer>> mapa;
// Lista "oficial" de todos los envios al MainNode
private ArrayList<PortDetection> lista;
public ListAllPortDetection() {
this.primer_scan = true;
this.mapa = new HashMap<>();
this.lista = new ArrayList<>();
}
public void addScan(List<ScanInfo> asset){
// Cuando llega una seguna scan
if (!this.primer_scan){
// Ya ha habido un segundo
this.lista = new ArrayList<>();
this.mapa = new HashMap<>();
}
this.primer_scan = false;
addDHCP(asset);
}
public void addDHCP(List<ScanInfo> asset){
asset.forEach(e ->{
List<Host> hosts = e.getHost();
for (int i = 0; i < hosts.size(); i++){
Host host = hosts.get(i);
Map<Integer,Integer> map_port = null;
if ((map_port = mapa.get(host.getIp())) != null){
// Esta ip ya esta en el mapa
List<Port> ports = host.getPorts();
for (int j = 0; j < ports.size();j++){
Port port = ports.get(j);
if (!map_port.containsKey(port.getPortNumber())){
map_port.put(Integer.parseInt(port.getPortNumber()),Integer.parseInt(port.getPortNumber()));
lista.add(new PortDetection("",host.getIp(),Integer.parseInt(port.getPortNumber()),0));
}
}
}else {
// Esta ip no esta en el mapa
Map<Integer,Integer> mapa_aux = new HashMap<>();
List<Port> ports = host.getPorts();
for (int j = 0 ; j < ports.size(); j++){
Port port = ports.get(j);
mapa_aux.put(Integer.parseInt(port.getPortNumber()),Integer.parseInt(port.getPortNumber()));
lista.add(new PortDetection("",host.getIp(),Integer.parseInt(port.getPortNumber()), 0));
}
mapa.put(host.getIp(),mapa_aux);
}
}
});
}
public void addNewPort(ListPortDetection listPortDetection){
for(int i = 0; i< listPortDetection.getLista().size(); i++){
PortDetection portDetection = listPortDetection.getLista().get(i);
Map<Integer,Integer> mapa_ports = null;
if ((mapa_ports = mapa.get(portDetection.getIp())) != null){
// Esta ya esa ip en el mapa
if (!mapa_ports.containsKey(portDetection.getPort())){
mapa_ports.put(portDetection.getPort(), portDetection.getPort());
}
}else {
// No esta esa ip en el mapa
Map<Integer,Integer> mapa_puertos = new HashMap<>();
mapa_puertos.put(portDetection.getPort(), portDetection.getPort());
mapa.put(portDetection.getIp(),mapa_puertos);
lista.add(new PortDetection("",portDetection.getIp(), portDetection.getPort(), 0));
}
}
}
}
......@@ -11,6 +11,11 @@ import java.util.Map;
@Component
public class ListPortDetection {
private List<PortDetection> lista;
public Map<String, Map<Integer, Integer>> getIp_puertos() {
return ip_puertos;
}
private Map<String,Map<Integer, Integer>> ip_puertos;
public ListPortDetection(){
......@@ -35,6 +40,10 @@ public class ListPortDetection {
}
}
public List<PortDetection> getLista() {
return lista;
}
@Override
public String toString() {
return "ListPortDetection{" +
......
......@@ -6,18 +6,22 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
@Slf4j
public class NewPortDetection {
private ListPortDetection listPortDetection;
private final int n_threads;
private ProcessWindows [] threads;
private ThreadPoolExecutor executor_newPort;
//Constructor
public NewPortDetection (ListPortDetection listPortDetection, int n_threads){
public NewPortDetection (ListPortDetection listPortDetection, int n_threads, ThreadPoolExecutor executor_newPort){
this.n_threads = n_threads;
this.listPortDetection = listPortDetection;
this.executor_newPort = executor_newPort;
}
public void readLinesSnif() throws IOException {
......@@ -25,16 +29,17 @@ public class NewPortDetection {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
ProcessWindows [] threads = new ProcessWindows[n_threads];
CountDownLatch latch = new CountDownLatch(n_threads);
for (int i = 0; i < n_threads; i++){
threads[i] = new ProcessWindows(br,listPortDetection);
BufferedReader finalBr = br;
executor_newPort.submit(() -> {
// ProcessWindows processWindows = new ProcessWindows(finalBr,listPortDetection);
});
// threads[i] = new ProcessWindows(br,listPortDetection);
threads[i].start();
}
}catch(Exception e){
e.printStackTrace();
}
}
// Metodo para Obtener todas las ips de las diferentes interfaces de red
// @return Map con tanto la clave como el valor es la ip de la interfaz
}
package adt.newportdetection.runner;
import adt.dto.Asset;
import adt.entity.Host;
import adt.entity.Port;
import adt.entity.ScanInfo;
import com.google.common.net.InetAddresses;
import javafx.util.Pair;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.*;
import java.util.concurrent.Semaphore;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
@Slf4j
public class ProcessWindows extends Thread{
......@@ -24,18 +32,24 @@ public class ProcessWindows extends Thread{
private static int ventana_primera;
private static Pattern pattern;
private static Map<String,String> localhosts;
private static Map<String,Map<String,String>> vlans;
private static ListPortDetection listPortDetection;
private static Semaphore mutex_windows;
private static Semaphore mutex_buffer;
private static Map<String, String> name_vlans;
private final Semaphore mutex_windows = new Semaphore(1);
private final Semaphore mutex_buffer = new Semaphore(1);
private final SendInfoToMain sendInfoToMain;
private static boolean terminar = false;
@Value("${token}")
private String token;
/* ====================================
* Constructor
* ====================================
* */
public ProcessWindows(BufferedReader buffer, ListPortDetection listPortDetection) {
ProcessWindows.mutex_windows = new Semaphore(1);
ProcessWindows.mutex_buffer = new Semaphore(1);
public ProcessWindows(BufferedReader buffer, ListPortDetection listPortDetection, SendInfoToMain sendInfoToMain, List<String[]> vlanIps) {
this.sendInfoToMain = sendInfoToMain;
ProcessWindows.tiempos_mayores = new double[N];
ProcessWindows.windows = new ArrayList<>(N);
for(int i = 0; i < N; i++){
......@@ -44,10 +58,26 @@ public class ProcessWindows extends Thread{
ProcessWindows.buffer = buffer;
ProcessWindows.listPortDetection = listPortDetection;
ProcessWindows.pattern = Pattern.compile("([0-9]+:[0-9]+:[0-9]+.[0-9]+)\\sIP\\s([0-9]+.[0-9]+.[0-9]+.[0-9]+).([0-9]+) > ([0-9]+.[0-9]+.[0-9]+.[0-9]+).([0-9]+): .* length ([0-9]+)");
ProcessWindows.localhosts= getIpHost();
ProcessWindows.tiempo_limite_superior = -1;
ProcessWindows.ventana_actual = -1;
ProcessWindows.ventana_primera = 0;
for (String[] vlanIp : vlanIps) {
name_vlans.put(vlanIp[0],vlanIp[1]);
Map<String, String> aux;
if (ProcessWindows.localhosts == null) {
aux = new HashMap<>();
} else {
aux = ProcessWindows.localhosts;
}
List<String> aux_list = getIpHost(vlanIp[0], Integer.parseInt(vlanIp[2]));
Map<String,String> map_ip_ip = new HashMap<>();
for (String s : aux_list) {
aux.put(s, s);
map_ip_ip.put(s, s);
}
ProcessWindows.vlans.put(vlanIp[0],map_ip_ip);
ProcessWindows.localhosts = aux;
}
}
/* ====================================
......@@ -55,26 +85,20 @@ public class ProcessWindows extends Thread{
* ====================================
* */
private Map<String,String> getIpHost() {
Map<String, String> resultado = new HashMap<>();
try {
Process process = new ProcessBuilder("ip", "a").start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
Pattern pattern = Pattern.compile("[0-9]+: [a-z0-9-]+: [<>A-Z,_a-z-\\s0-9\\n\\/:]* inet ([0-9.\\/]+)");
String line;
StringBuilder texto = new StringBuilder();
while ((line = br.readLine()) != null) {
texto.append(line+"\n");
}
Matcher matcher = pattern.matcher(texto.toString());
while (matcher.find()){
for (int i=0;i < matcher.groupCount(); i++){
resultado.put(matcher.group(i+1).split("/")[0], matcher.group(i+1));
private List<String> getIpHost(String subnetIPAddress, int subnetMask) {
List<String> resultado = new ArrayList<>();
long limit = (long) (Math.pow(2, 32 - subnetMask) - 1);
InetAddress firstAddress = InetAddresses.forString(subnetIPAddress);
Stream<InetAddress> stream = Stream.iterate(firstAddress, InetAddresses::increment).limit(limit);
stream.forEach(addr -> {
try {
if (addr.isReachable(5000)){
resultado.add(addr.getHostAddress());
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
});
return resultado;
}
......@@ -83,11 +107,15 @@ public class ProcessWindows extends Thread{
* ====================================
* */
private String readLine() throws IOException, InterruptedException {
String line;
mutex_buffer.acquire();
line = buffer.readLine();
mutex_buffer.release();
private String readLine(){
String line ="";
try {
mutex_buffer.acquire();
line = buffer.readLine();
mutex_buffer.release();
} catch (IOException | InterruptedException e) {
// e.printStackTrace();
}
return line;
}
......@@ -173,6 +201,8 @@ public class ProcessWindows extends Thread{
// log.info(listPortDetection.toString());
// log.info("=====================================================");
}
// Enviar la informacion al nodo Main
sendAsset();
// Vaciar la ventana_primera
windows.remove(ventana_primera);
windows.add(ventana_primera,new HashMap<>());
......@@ -235,23 +265,56 @@ public class ProcessWindows extends Thread{
* Metodo que se llama cuando se inicia el Thread
* ====================================
* */
@Override
public void run() {
public void start() {
try {
String line = null;
while ( (line = readLine()) != null){
while ( (line = readLine()) != null && !terminar){
ArrayList<PortDetection> meter = match(line);
PortDetection aux = null;
for (PortDetection portDetection : meter) {
addWindows(portDetection);
}
}
}catch (IOException | InterruptedException e){
}catch (InterruptedException e){
e.printStackTrace();
}
}
private void sendAsset(){
Asset asset = new Asset();
Map<String, ScanInfo> list_scan_info = new HashMap<>();
for (Map.Entry<String, Map<Integer, Integer>> entry2 : listPortDetection.getIp_puertos().entrySet()) {
String vlan = "";
for (Map.Entry<String, Map<String, String>> entry : vlans.entrySet()) {
if (entry.getValue().containsValue(entry2.getKey())) {
vlan = entry.getKey();
break;
}
}
Host host = Host.createHost(entry2.getKey(), "sniffing");
List<Port> ports = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry1 : entry2.getValue().entrySet()) {
ports.add(Port.createPort("" + entry1.getKey(), host, "tcp", "sniffing"));
}
host.setPorts(ports);
if (list_scan_info.containsKey(vlan)){
// Ya esta esa vlan
list_scan_info.get(vlan).getHost().add(host);
}else {
// No esta esa vlan
ScanInfo aux = ScanInfo.createScanInfo(vlan,entry2.getKey(),null,"sniffing");
List<Host> meter_host = new ArrayList<>();
meter_host.add(host);
aux.setHost(meter_host);
list_scan_info.put(vlan,aux);
}
}
asset.setScanInfo(new ArrayList<>(list_scan_info.values()));
asset.setDiscoveryMethod("sniffing");
sendInfoToMain.sendInfo(asset, token);
listPortDetection = new ListPortDetection();
}
/* ====================================
* GETTERS AND SETTERS
......
package adt.newportdetection.runner;
import adt.dto.Asset;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
@Slf4j
@Service
public class SendInfoToMain {
@Value("${main-node.address}")
private String mainNodeAddress;
@Value("${main-node.port}")
private String mainNodePort;
public Long sendInfo(Asset asset, String token){
String urlQuery = "http://" + mainNodeAddress + ":" + mainNodePort + "/sniffing?token=" + token;
log.info("SENDING ASSETS TO: {}", urlQuery);
log.info("SENDING THESE ASSETS: \n{}", asset);
Long newDelay = null;
try {
URL url = new URL(urlQuery);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(out, asset);
final byte[] data = out.toByteArray();
try (OutputStream os = con.getOutputStream()) {
//byte[] input = jsonInputString.getBytes("utf-8");
os.write(data, 0, data.length);
}
try (BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
newDelay = Long.parseLong(response.toString());
} catch (Exception ex) {
// handled
log.info("##############{}",ex);
}
} catch (Exception ex) {
// handled
log.info("##############{}",ex);
}
return newDelay;
}
}
package adt.service;
import adt.newportdetection.NewPortDetectionThread;
import adt.entity.ScanInfo;
import adt.client.MainNodeClient;
import adt.client.MainNodeCom;
import adt.dto.Node;
import adt.entity.Vlan;
import adt.newportdetection.runner.ListPortDetection;
import adt.newportdetection.runner.NewPortDetection;
import adt.newportdetection.runner.ListAllPortDetection;
import adt.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -18,7 +17,6 @@ import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import adt.tcpdump.TCPDumpThread;
......@@ -54,8 +52,9 @@ public class OnStartupService implements ApplicationListener<ApplicationReadyEve
private final MainNodeCom mainNodeCom;
//// New Port Scan
private final NewPortDetectionThread newPortDetectionThread;
private ListPortDetection listPortDetection;
// private NewPortDetectionThread newPortDetectionThread;
// private ListPortDetection listPortDetection;
private ListAllPortDetection listAllPortDetection;
@Autowired
......@@ -64,11 +63,12 @@ public class OnStartupService implements ApplicationListener<ApplicationReadyEve
SendAssetService sendAssetService, ScheduleService scheduleService,
OsDetectorService osDetectorService, WrittingService writtingService,
VlanRepository vlanRepository, MainNodeClient mainNodeClient,
MainNodeCom mainNodeCom, NewPortDetectionThread newPortDetectionThread) {
MainNodeCom mainNodeCom) {
this.scanInfoRepository = scanInfoRepository;
this.portRepository = portRepository;
this.sendAssetService = sendAssetService;
// this.sendAssetService.setOnStartupService(this);
this.hostRepository = hostRepository;
this.scheduleService = scheduleService;
this.osDetectorService = osDetectorService;
......@@ -81,8 +81,7 @@ public class OnStartupService implements ApplicationListener<ApplicationReadyEve
this.mainNodeCom = mainNodeCom;
//New port detection
this.newPortDetectionThread = newPortDetectionThread;
this.listPortDetection = new ListPortDetection();
// this.newPortDetectionThread = newPortDetectionThread;
}
......@@ -91,26 +90,21 @@ public class OnStartupService implements ApplicationListener<ApplicationReadyEve
Node node = initialize();
// List<String[]> vlanIps = new ArrayList<String[]>();
//
// for(Vlan vlan: node.getVlan()){
//
// String [] toAdd = {vlan.getNetAddress(), vlan.getVlanName()};
// vlanIps.add(toAdd);
// }
try {
launchNewPortsDetection();
} catch (InterruptedException e) {
e.printStackTrace();
List<String[]> vlanIps = new ArrayList<String[]>();
for(Vlan vlan: node.getVlan()){
String [] toAdd = {vlan.getNetAddress(), vlan.getVlanName(), vlan.getMask()};
vlanIps.add(toAdd);
}
// launchDHCPDiscovery(vlanIps, node.getIp());
//
// vlanRepository.saveAll(node.getVlan());
//
// sendAssetService.sendAssets(true, null);//, "scan");
//
// scheduleService.scheduleSetUp(node);
launchDHCPDiscovery(vlanIps, node.getIp());
vlanRepository.saveAll(node.getVlan());
sendAssetService.sendAssets(true, null);//, "scan");
scheduleService.scheduleSetUp(node,vlanIps);
}
......@@ -134,22 +128,29 @@ public class OnStartupService implements ApplicationListener<ApplicationReadyEve
private void launchDHCPDiscovery(List<String[]> info, String nodeIp){
for(int i=0; i < info.size(); i++) {
String[] infoDetail = info.get(i);
TCPDumpThread tcpDumpThread = new TCPDumpThread(token, mainNodeCom, sendAssetService, portScannerService, nodeIp, infoDetail[0], infoDetail[1], hostRepository, portRepository, scanInfoRepository);
tcpDumpThread.start();
}
}
// Lanza el Thread para descubrir nuevos puertos entre Scan
private void launchNewPortsDetection() throws InterruptedException {
NewPortDetectionThread newPortDetectionThread = new NewPortDetectionThread(this.listPortDetection);
newPortDetectionThread.start();
newPortDetectionThread.parar();
// Thread.sleep(10000);
// newPortDetectionThread.terminate();
// newPortDetectionThread.join();
// log.info(this.listPortDetection.toString());
}
// Metodo para añadir a la lista de elementos enviados del servicio de DHCP
public void addDHCP(List<ScanInfo> asset) {
log.info(asset.toString());
listAllPortDetection.addDHCP(asset);
}
// Metodo para añadir a la lista de elementos enviados del servicio de Scan
public void addScan(List<ScanInfo> asset){
log.info(asset.toString());
// listAllPortDetection.addScan(asset);
}
public void addNewPort(){
}
}
package adt.service;
import adt.client.MainNodeClient;
import adt.entity.Port;
import adt.entity.ScanInfo;
import adt.entity.Vlan;
import adt.dto.Node;
import adt.newportdetection.runner.ListPortDetection;
import adt.newportdetection.runner.ProcessWindows;
import adt.newportdetection.runner.SendInfoToMain;
import adt.repository.PortRepository;
import adt.repository.ScanInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.CountDownLatch;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Date;
import java.io.InputStreamReader;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ExecutionException;
import java.lang.InterruptedException;
import java.lang.Thread;
import lombok.extern.slf4j.Slf4j;
@Service
......@@ -42,6 +39,8 @@ public class ScheduleService {
private final ScanInfoRepository scanInfoRepository;
private final PortRepository portRepository;
private final SendInfoToMain sendInfoToMain;
private List<Vlan> vlan;
private Node node;
......@@ -52,95 +51,134 @@ public class ScheduleService {
private Long prevSystemDelay;
private Long iniExecutionTime;
private boolean newPortChecker = false;
private ListPortDetection listPortDetection;
private List<String[]> vlanIps;
private class ScheduledTask implements Runnable {
private Process process;
private ProcessWindows processWindows;
ThreadPoolExecutor executor_newPort;
@Override
public void run() {
log.info("NEW EXECUTION SCHEDULED. Number of Vlans to scan: {}", vlan.size());
iniExecutionTime = System.currentTimeMillis();
CountDownLatch latch = new CountDownLatch(vlan.size());
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(vlan.size());
// Timestamp ini
Date iniTimestamp_scan = new Date();
vlan.forEach(element -> executor.submit(() -> {
log.info("Vlan address ready to scan: " + element.getNetAddress());
hostScannerService.scanNetwork(element.getVlanName(), element.getNetAddress(), element.getMask());
log.info("SCANNING OF NETWORK {} SUCCESSFULLY COMPLETED", element.getNetAddress());
Date endTimestamp_scan = new Date();
ScanInfo scanInfo = ScanInfo.createScanInfo(element.getVlanName(), node.getIp(), iniTimestamp_scan, endTimestamp_scan, "scan");
scanInfoRepository.save(scanInfo);
latch.countDown();
}));
log.info("Se ha terminado de ejecutar Scan");
if (newPortChecker){
process.destroy();
executor_newPort.shutdownNow();
log.info("Apagado del newPort");
}
// iniExecutionTime = System.currentTimeMillis();
//
// CountDownLatch latch = new CountDownLatch(vlan.size());
// ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(vlan.size());
//
// // Timestamp ini
// Date iniTimestamp_scan = new Date();
//
// vlan.forEach(element -> executor.submit(() -> {
//
// log.info("Vlan address ready to scan: " + element.getNetAddress());
//
// hostScannerService.scanNetwork(element.getVlanName(), element.getNetAddress(), element.getMask());
// log.info("SCANNING OF NETWORK {} SUCCESSFULLY COMPLETED", element.getNetAddress());
//
// Date endTimestamp_scan = new Date();
//
// ScanInfo scanInfo = ScanInfo.createScanInfo(element.getVlanName(), node.getIp(), iniTimestamp_scan, endTimestamp_scan, "scan");
//
// scanInfoRepository.save(scanInfo);
//
// latch.countDown();
//
// }));
//
// try {
// latch.await();
// } catch (InterruptedException E) {
// // handle
// }
// executor.shutdown();
//
// List<Port> ports = portScannerService.getPortList();
//
// portRepository.saveAll(ports);
//
// //boolean iniFlag = false;
//
// log.info("All networks have been successfully scanned and its assets detected. Now managing this information...\n");
// Long systemDelay = sendAssetService.sendAssets(false, vlan);//, "scan");
//
// boolean flag = true;
// if(systemDelay == null){
// flag = false;
// }else {
// flag = !Long.toString(systemDelay).equals(Long.toString(vlan.get(0).getDelay()));
// }
// if( flag ){
//
// futureTask.cancel(false);
// prevSystemDelay = vlan.get(0).getDelay();
//
//
// log.info("ATTENTION, a new execution delay has been set for this scanning process.\n--Previous: {} seconds.\n--New: {} seconds.\nNext iterations will use this new delay.\n", vlan.get(0).getDelay(), systemDelay);
//
// vlan.get(0).setDelay(systemDelay); // PONERSELO A TODOS
//
// /* At this point in the execution, it is guaranteed that the futureTask's thread has finished, so it is safely cancelled and we can schedule a new futureTask. */
// scheduleSetUp(node);
// }
// Arracamos NewPortDetection
log.info("Resuming service NewPortDetection");
try {
latch.await();
} catch (InterruptedException E) {
// handle
process = new ProcessBuilder("tcpdump", "-l","-nn","-i", "any", "ip").start();
} catch (IOException e) {
e.printStackTrace();
}
executor.shutdown();
List<Port> ports = portScannerService.getPortList();
portRepository.saveAll(ports);
//boolean iniFlag = false;
log.info("All networks have been successfully scanned and its assets detected. Now managing this information...\n");
Long systemDelay = sendAssetService.sendAssets(false, vlan);//, "scan");
boolean flag = true;
if(systemDelay == null){
flag = false;
}else {
flag = !Long.toString(systemDelay).equals(Long.toString(vlan.get(0).getDelay()));
int n_threads = 1;
executor_newPort = (ThreadPoolExecutor) Executors.newFixedThreadPool(n_threads);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
listPortDetection = new ListPortDetection();
for (int i = 0; i< n_threads;i++){
executor_newPort.submit(()-> {
processWindows = new ProcessWindows(br,listPortDetection,sendInfoToMain, vlanIps);
processWindows.start();
}
);
}
if( flag ){
futureTask.cancel(false);
prevSystemDelay = vlan.get(0).getDelay();
log.info("ATTENTION, a new execution delay has been set for this scanning process.\n--Previous: {} seconds.\n--New: {} seconds.\nNext iterations will use this new delay.\n", vlan.get(0).getDelay(), systemDelay);
vlan.get(0).setDelay(systemDelay); // PONERSELO A TODOS
/* At this point in the execution, it is guaranteed that the futureTask's thread has finished, so it is safely cancelled and we can schedule a new futureTask. */
scheduleSetUp(node);
newPortChecker = true;
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Autowired
public ScheduleService(HostScannerService hostScannerService, PortRepository portRepository,PortScannerService portScannerService, SendAssetService sendAssetService, MainNodeClient mainNodeClient, ScanInfoRepository scanInfoRepository) {
public ScheduleService(HostScannerService hostScannerService, PortRepository portRepository, PortScannerService portScannerService, SendAssetService sendAssetService, MainNodeClient mainNodeClient, ScanInfoRepository scanInfoRepository, SendInfoToMain sendInfoToMain) {
this.mainNodeClient = mainNodeClient;
this.hostScannerService = hostScannerService;
this.sendAssetService = sendAssetService;
this.portRepository = portRepository;
this.portScannerService = portScannerService;
this.scanInfoRepository = scanInfoRepository;
this.sendInfoToMain = sendInfoToMain;
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
task = new ScheduledTask();
}
void scheduleSetUp(Node node) {
void scheduleSetUp(Node node, List<String[]> vlanIps) {
if (this.vlan == null){
this.node = node;
this.vlan = node.getVlan();
this.vlanIps = vlanIps;
}
if(this.prevSystemDelay != null){
......@@ -164,8 +202,6 @@ public class ScheduleService {
}
log.info("VLAN DELAY INFO: {} seconds.", vlan.get(0).getDelay());
futureTask = scheduledExecutorService.scheduleAtFixedRate(task, 0, vlan.get(0).getDelay(), TimeUnit.SECONDS);
}
......
......@@ -3,8 +3,6 @@ package adt.service;
import adt.client.MainNodeClient;
import adt.client.MainNodeCom;
import adt.dto.Asset;
import adt.dto.Node;
import adt.dto.ScanInfoDto;
import adt.entity.ScanInfo;
import adt.entity.Host;
import adt.entity.Vlan;
......@@ -13,19 +11,13 @@ import adt.repository.HostRepository;
import adt.repository.PortRepository;
import adt.repository.ScanInfoRepository;
import adt.repository.VlanRepository;
import com.google.common.net.InetAddresses;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.mapping.Array;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.net.InetAddress;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.Date;
@Service
......@@ -41,6 +33,12 @@ public class SendAssetService {
private final VlanService vlanService;
private final WrittingService writtingService;
// public void setOnStartupService(OnStartupService onStartupService) {
// this.onStartupService = onStartupService;
// }
// private OnStartupService onStartupService;
//@Value("${vlan.name}")
//private String vlanName;
......@@ -84,7 +82,6 @@ public class SendAssetService {
List<String> vlan_names = new ArrayList<>();
/* May re-implement later. Seems to be quite inefficient. */
for (int i = 0; i < vlan.size(); i++) {
vlan_names.add(vlan.get(i).getVlanName());
}
......@@ -121,7 +118,7 @@ public class SendAssetService {
element.setHost(hosts);
});
// onStartupService.addScan(scansToSend);
Asset asset = Asset.createAsset(scansToSend, discoveryMethod);
if (executionMode.equals("manual")) {
......@@ -133,6 +130,7 @@ public class SendAssetService {
log.info("[Automatic mode execution] Assets will be sent to Main Node. Communicating with its endpoints...");
try {
//newDelay = mainNodeClient.sendAssets(/*vlanName,*/ asset, token);
newDelay = mainNodeCom.sendAssets(asset, token);
} catch (Exception e){//HystrixRuntimeException ex) {
// handle hystrix exception here
......@@ -214,6 +212,7 @@ public class SendAssetService {
List<ScanInfo> scansToSend = new ArrayList<>();
scansToSend.add(check.get(0));
// onStartupService.addDHCP(scansToSend);
Asset asset = Asset.createAsset(scansToSend, discoveryMethod);
Long newDelay = null;
......@@ -227,7 +226,6 @@ public class SendAssetService {
writtingService.writeJSONOutput("./dhcp_assets_info"+ time.toString() +".json", asset);
log.info("[Automatic mode execution] Assets will be sent to Main Node. Communicating with its endpoints...");
newDelay = mainNodeCom.sendAssets(asset, token);
/*try {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment