fixed targetdiscovery

This commit is contained in:
snoop 2018-04-24 20:34:29 +09:00
parent 55f66f0337
commit b69ba6c9d3
4 changed files with 195 additions and 369 deletions

View File

@ -12,4 +12,50 @@ public class StringConvertor {
( i & 0xFF); ( i & 0xFF);
} }
public static long ipToLong(String ipAddress) {
String[] ipAddressInArray = ipAddress.split("\\.");
long result = 0;
for (int i = 0; i < ipAddressInArray.length; i++) {
int power = 3 - i;
int ip = Integer.parseInt(ipAddressInArray[i]);
result += ip * Math.pow(256, power);
}
return result;
}
// https://github.com/Sovietaced/floodlight/blob/master/src/main/java/net/floodlightcontroller/util/MACAddress.java
public static final int MAC_ADDRESS_LENGTH = 6;
public static long macStrToLong(String address) {
String[] elements = address.split(":");
if (elements.length != MAC_ADDRESS_LENGTH) {
throw new IllegalArgumentException(
"Specified MAC Address must contain 12 hex digits" +
" separated pairwise by :'s.");
}
byte[] addressInBytes = new byte[MAC_ADDRESS_LENGTH];
for (int i = 0; i < MAC_ADDRESS_LENGTH; i++) {
String element = elements[i];
addressInBytes[i] = (byte)Integer.parseInt(element, 16);
}
long mac = 0;
for (int i = 0; i < 6; i++) {
long t = (addressInBytes[i] & 0xffL) << ((5 - i) * 8);
mac |= t;
}
return mac;
// return new MACAddress(addressInBytes);
}
} }

View File

@ -1,14 +1,18 @@
package com.loafle.overflow.central.module.target.service; package com.loafle.overflow.central.module.target.service;
import com.loafle.overflow.central.commons.utils.StringConvertor; import com.loafle.overflow.central.commons.utils.StringConvertor;
import com.loafle.overflow.central.module.discovery.model.Host;
import com.loafle.overflow.central.module.discovery.model.Port;
import com.loafle.overflow.central.module.discovery.type.PortType;
import com.loafle.overflow.central.module.infra.model.*;
import com.loafle.overflow.central.module.infra.model.InfraService;
import com.loafle.overflow.central.module.infra.service.*; import com.loafle.overflow.central.module.infra.service.*;
import com.loafle.overflow.central.module.meta.model.MetaInfraType; import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.central.module.meta.model.MetaInfraVendor; import com.loafle.overflow.core.type.PortType;
import com.loafle.overflow.model.discovery.Host;
import com.loafle.overflow.model.discovery.Port;
import com.loafle.overflow.model.infra.*;
import com.loafle.overflow.model.meta.MetaInfraType;
import com.loafle.overflow.model.meta.MetaInfraVendor;
import com.loafle.overflow.model.probe.Probe;
import com.loafle.overflow.model.target.Target;
import com.loafle.overflow.service.central.target.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -19,202 +23,205 @@ import java.util.List;
* Created by snoop on 17. 6. 28. * Created by snoop on 17. 6. 28.
*/ */
@Service("TargetDiscoveryService") @Service("TargetDiscoveryService")
public class CentralTargetDiscoveryService { public class CentralTargetDiscoveryService implements TargetDiscoveryService{
@Autowired @Autowired
private CentralTargetService targetService; private CentralTargetService targetService;
@Autowired @Autowired
private InfraMachineService infraMachineService; private CentralInfraMachineService infraMachineService;
@Autowired @Autowired
private InfraOSService infraOSService; private CentralInfraOSService infraOSService;
@Autowired @Autowired
private InfraHostService infraHostService; private CentralInfraHostService infraHostService;
@Autowired @Autowired
private com.loafle.overflow.central.module.infra.service.InfraService infraService; private CentralInfraService infraService;
@Autowired @Autowired
private InfraOSPortService infraOSPortService; private CentralInfraOSPortService infraOSPortService;
@Autowired @Autowired
private InfraServiceService infraServiceService; private CentralInfraServiceService infraServiceService;
// @Transactional @Transactional
// public boolean saveAllTarget(List<Host> hosts, Probe probe) { public boolean saveAllTarget(List<Host> hosts, Probe probe) throws OverflowException {
// InfraHost infraHost = null; InfraHost infraHost = null;
// for(Host host : hosts) { for(Host host : hosts) {
// infraHost = this.createAndReadHost(host, probe); infraHost = this.createAndReadHost(host, probe);
// this.createPort(infraHost, host, probe); this.createPort(infraHost, host, probe);
// } }
// return true; return true;
// } }
// private void createService(InfraHost infraHost, Port port, Probe probe) { private void createService(InfraHost infraHost, Port port, Probe probe) throws OverflowException {
// if(port.getServices() == null) {
// return;
// }
// MetaInfraType typeService = new MetaInfraType(); MetaInfraType typeService = new MetaInfraType();
// typeService.setId(7); typeService.setId(7);
// String portType = "UDP"; String portType = "UDP";
// if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) {
// portType = "TCP"; portType = "TCP";
// } }
// for(com.loafle.overflow.central.module.discovery.model.Service service : port.getServices()) { // for(String key : port.getServices().keySet()) {
for(com.loafle.overflow.model.discovery.Service service : port.getServiceList()) {
// InfraService dbInfraService = this.infraServiceService.readByService(infraHost.getId(), port.getPortNumber(), portType); // com.loafle.overflow.module.discovery.model.Service service = port.getServices().get(key);
// if(dbInfraService != null) { InfraService dbInfraService = this.infraServiceService.readByService(infraHost.getId(), port.getPortNumber(), portType);
// if(service.isTarget() && dbInfraService.getTarget() == null) {
// Target targetService = new Target();
// targetService.setDisplayName(service.getServiceName() + "-Service");
// this.targetService.regist(targetService);
// dbInfraService.setTarget(targetService);
// this.infraServiceService.regist(dbInfraService);
// }
// continue;
// }
// InfraService infraService = new InfraService(); if(dbInfraService != null) {
// infraService.setHost(infraHost); if(service.isTarget() && dbInfraService.getTarget() == null) {
// infraService.setPort(port.getPortNumber()); Target targetService = new Target();
// infraService.setPortType(portType); targetService.setDisplayName(service.getServiceName());
// infraService.setInfraType(typeService); this.targetService.regist(targetService);
// infraService.setProbe(probe); dbInfraService.setTarget(targetService);
this.infraServiceService.regist(dbInfraService);
}
continue;
}
// if (port.getPortType() == PortType.TLS) { InfraService infraService = new InfraService();
// infraService.setTlsType(true); infraService.setHost(infraHost);
// } infraService.setPort(port.getPortNumber());
// infraService.setVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName())); infraService.setPortType(portType);
infraService.setInfraType(typeService);
infraService.setProbe(probe);
// if(service.isTarget()) { if (port.getPortType() == PortType.TLS) {
// Target targetService = new Target(); infraService.setTlsType(true);
// targetService.setDisplayName(service.getServiceName() + "-Service"); }
// this.targetService.regist(targetService); infraService.setVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName()));
// infraService.setTarget(targetService);
// }
// this.infraServiceService.regist(infraService); if(service.isTarget()) {
Target targetService = new Target();
targetService.setDisplayName(service.getServiceName());
this.targetService.regist(targetService);
infraService.setTarget(targetService);
}
// } this.infraServiceService.regist(infraService);
// } }
// private void createPort(InfraHost infraHost, Host host, Probe probe) { }
// if(host.getPorts() == null) { private void createPort(InfraHost infraHost, Host host, Probe probe) throws OverflowException {
// return;
// }
// String portType = "UDP"; // if(host.getPorts() == null) {
// return;
// }
// MetaInfraType typePort = new MetaInfraType(); String portType = "UDP";
// typePort.setId(6);
// InfraOS infraOS = infraHost.getOs(); MetaInfraType typePort = new MetaInfraType();
typePort.setId(6);
// for(Port port : host.getPorts()) { InfraOS infraOS = infraHost.getOs();
// if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { // for( String key: host.getPorts().keySet()) {
// portType = "TCP"; for( Port port: host.getPortList()) {
// } // Port port = host.getPorts().get(key);
// InfraOSPort dbInfraOSPort = this.infraOSPortService.readByPort(infraOS.getId(), port.getPortNumber(), portType); if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) {
// if(dbInfraOSPort == null) { portType = "TCP";
// InfraOSPort infraOSPort = new InfraOSPort(); }
// infraOSPort.setOs(infraOS);
// infraOSPort.setPort(port.getPortNumber());
// infraOSPort.setPortType(portType);
// infraOSPort.setProbe(probe);
// infraOSPort.setInfraType(typePort);
// if (port.getPortType() == PortType.TLS) { InfraOSPort dbInfraOSPort = this.infraOSPortService.readByPort(infraOS.getId(), port.getPortNumber(), portType);
// infraOSPort.setTlsType(true); if(dbInfraOSPort == null) {
// } InfraOSPort infraOSPort = new InfraOSPort();
// infraOSPort.setVendor(MetaInfraVendor.CreateInfraVendorByPort(port.getPortNumber())); infraOSPort.setOs(infraOS);
// this.infraOSPortService.regist(infraOSPort); infraOSPort.setPort(port.getPortNumber());
// } infraOSPort.setPortType(portType);
infraOSPort.setProbe(probe);
infraOSPort.setInfraType(typePort);
// this.createService(infraHost, port, probe); if (port.getPortType() == PortType.TLS) {
// } infraOSPort.setTlsType(true);
// } }
infraOSPort.setVendor(MetaInfraVendor.CreateInfraVendorByPort(port.getPortNumber()));
this.infraOSPortService.regist(infraOSPort);
}
// private InfraHost createAndReadHost(Host host, Probe probe) { this.createService(infraHost, port, probe);
}
}
// InfraHost infraHost = this.infraHostService.readByIp(host.getIp()); private InfraHost createAndReadHost(Host host, Probe probe) throws OverflowException {
// if(infraHost != null) {
// if(host.isTarget() && infraHost.getTarget() == null) {
// Target target = new Target();
// target.setDisplayName(String.valueOf(host.getIp()) + "-Host");
// this.targetService.regist(target); InfraHost infraHost = this.infraHostService.readByIp(StringConvertor.ipToLong(host.getIp()));
// infraHost.setTarget(target); if(infraHost != null) {
// this.infraHostService.regist(infraHost);
// }
// return infraHost; if(host.isTarget() && infraHost.getTarget() == null) {
// } else { Target target = new Target();
// MetaInfraType typeMachine = new MetaInfraType(); target.setDisplayName(String.valueOf(host.getIp()) + "-Host");
// typeMachine.setId(1); // 1 = Machine;
// MetaInfraType typeOS = new MetaInfraType(); this.targetService.regist(target);
// typeOS.setId(3); // 3 = Os infraHost.setTarget(target);
this.infraHostService.regist(infraHost);
}
// MetaInfraType typeHost = new MetaInfraType(); return infraHost;
// typeHost.setId(2); // 2 = Host } else {
MetaInfraType typeMachine = new MetaInfraType();
typeMachine.setId(1); // 1 = Machine;
// InfraMachine infraMachine = new InfraMachine(); MetaInfraType typeOS = new MetaInfraType();
// infraMachine.setProbe(probe); typeOS.setId(3); // 3 = Os
// infraMachine.setInfraType(typeMachine);
// infraMachine.setMeta(StringConvertor.intToIp(host.getIp())+"-MACHINE");
// this.infraMachineService.regist(infraMachine);
// InfraOS infraOS = new InfraOS(); MetaInfraType typeHost = new MetaInfraType();
// infraOS.setMachine(infraMachine); typeHost.setId(2); // 2 = Host
// infraOS.setVendor(MetaInfraVendor.CreateInfraVendorByOS(host.getOs()));
// infraOS.setInfraType(typeOS);
// infraOS.setProbe(probe);
// infraOS.setMeta(StringConvertor.intToIp(host.getIp())+"-OS");
// this.infraOSService.regist(infraOS);
// InfraHost newInfraHost = new InfraHost(); InfraMachine infraMachine = new InfraMachine();
// newInfraHost.setIp(host.getIp()); infraMachine.setProbe(probe);
// newInfraHost.setMac(host.getMac()); infraMachine.setInfraType(typeMachine);
// newInfraHost.setOs(infraOS); infraMachine.setMeta(host.getIp()+"-MACHINE");
// newInfraHost.setInfraType(typeHost); this.infraMachineService.regist(infraMachine);
// newInfraHost.setProbe(probe);
// if(host.isTarget()) { InfraOS infraOS = new InfraOS();
// Target target = new Target(); infraOS.setMachine(infraMachine);
// target.setDisplayName(StringConvertor.intToIp(host.getIp()) + "-Host"); infraOS.setVendor(MetaInfraVendor.CreateInfraVendorByOS(host.getOs()));
infraOS.setInfraType(typeOS);
infraOS.setProbe(probe);
infraOS.setMeta(host.getIp()+"-OS");
this.infraOSService.regist(infraOS);
// this.targetService.regist(target); InfraHost newInfraHost = new InfraHost();
// newInfraHost.setTarget(target); newInfraHost.setIp(StringConvertor.ipToLong(host.getIp()));
// } // newInfraHost.setMac(StringConvertor.ipToLong(host.getMac()));
newInfraHost.setMac(StringConvertor.macStrToLong(host.getMac()));
newInfraHost.setOs(infraOS);
newInfraHost.setInfraType(typeHost);
newInfraHost.setProbe(probe);
// this.infraHostService.regist(newInfraHost);
// infraHost = newInfraHost; if(host.isTarget()) {
// } Target target = new Target();
target.setDisplayName(host.getIp() + "-Host");
this.targetService.regist(target);
newInfraHost.setTarget(target);
}
this.infraHostService.regist(newInfraHost);
infraHost = newInfraHost;
}
// return infraHost; return infraHost;
// } }
} }

View File

@ -1,217 +0,0 @@
package com.loafle.overflow.central.module.target.service;
import com.loafle.overflow.central.module.infra.service.*;
import com.loafle.overflow.model.infra.InfraService;
import com.loafle.overflow.service.central.infra.InfraHostService;
import com.loafle.overflow.service.central.infra.InfraMachineService;
import com.loafle.overflow.service.central.infra.InfraOSPortService;
import com.loafle.overflow.service.central.infra.InfraOSService;
import com.loafle.overflow.service.central.infra.InfraServiceService;
import com.loafle.overflow.service.central.target.TargetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by snoop on 17. 6. 28.
*/
@Service("TargetDiscoveryService")
public class TargetDiscoveryService {
@Autowired
private TargetService targetService;
@Autowired
private InfraMachineService infraMachineService;
@Autowired
private InfraOSService infraOSService;
@Autowired
private InfraHostService infraHostService;
@Autowired
private InfraService infraService;
@Autowired
private InfraOSPortService infraOSPortService;
@Autowired
private InfraServiceService infraServiceService;
// @Transactional
// public boolean saveAllTarget(List<Host> hosts, Probe probe) {
// InfraHost infraHost = null;
// for(Host host : hosts) {
// infraHost = this.createAndReadHost(host, probe);
// this.createPort(infraHost, host, probe);
// }
// return true;
// }
// private void createService(InfraHost infraHost, Port port, Probe probe) {
// if(port.getServices() == null) {
// return;
// }
// MetaInfraType typeService = new MetaInfraType();
// typeService.setId(7);
// String portType = "UDP";
// if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) {
// portType = "TCP";
// }
// for(com.loafle.overflow.central.module.discovery.model.Service service : port.getServices()) {
// InfraService dbInfraService = this.infraServiceService.readByService(infraHost.getId(), port.getPortNumber(), portType);
// if(dbInfraService != null) {
// if(service.isTarget() && dbInfraService.getTarget() == null) {
// Target targetService = new Target();
// targetService.setDisplayName(service.getServiceName() + "-Service");
// this.targetService.regist(targetService);
// dbInfraService.setTarget(targetService);
// this.infraServiceService.regist(dbInfraService);
// }
// continue;
// }
// InfraService infraService = new InfraService();
// infraService.setHost(infraHost);
// infraService.setPort(port.getPortNumber());
// infraService.setPortType(portType);
// infraService.setInfraType(typeService);
// infraService.setProbe(probe);
// if (port.getPortType() == PortType.TLS) {
// infraService.setTlsType(true);
// }
// infraService.setVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName()));
// if(service.isTarget()) {
// Target targetService = new Target();
// targetService.setDisplayName(service.getServiceName() + "-Service");
// this.targetService.regist(targetService);
// infraService.setTarget(targetService);
// }
// this.infraServiceService.regist(infraService);
// }
// }
// private void createPort(InfraHost infraHost, Host host, Probe probe) {
// if(host.getPorts() == null) {
// return;
// }
// String portType = "UDP";
// MetaInfraType typePort = new MetaInfraType();
// typePort.setId(6);
// InfraOS infraOS = infraHost.getOs();
// for(Port port : host.getPorts()) {
// if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) {
// portType = "TCP";
// }
// InfraOSPort dbInfraOSPort = this.infraOSPortService.readByPort(infraOS.getId(), port.getPortNumber(), portType);
// if(dbInfraOSPort == null) {
// InfraOSPort infraOSPort = new InfraOSPort();
// infraOSPort.setOs(infraOS);
// infraOSPort.setPort(port.getPortNumber());
// infraOSPort.setPortType(portType);
// infraOSPort.setProbe(probe);
// infraOSPort.setInfraType(typePort);
// if (port.getPortType() == PortType.TLS) {
// infraOSPort.setTlsType(true);
// }
// infraOSPort.setVendor(MetaInfraVendor.CreateInfraVendorByPort(port.getPortNumber()));
// this.infraOSPortService.regist(infraOSPort);
// }
// this.createService(infraHost, port, probe);
// }
// }
// private InfraHost createAndReadHost(Host host, Probe probe) {
// InfraHost infraHost = this.infraHostService.readByIp(host.getIp());
// if(infraHost != null) {
// if(host.isTarget() && infraHost.getTarget() == null) {
// Target target = new Target();
// target.setDisplayName(String.valueOf(host.getIp()) + "-Host");
// this.targetService.regist(target);
// infraHost.setTarget(target);
// this.infraHostService.regist(infraHost);
// }
// return infraHost;
// } else {
// MetaInfraType typeMachine = new MetaInfraType();
// typeMachine.setId(1); // 1 = Machine;
// MetaInfraType typeOS = new MetaInfraType();
// typeOS.setId(3); // 3 = Os
// MetaInfraType typeHost = new MetaInfraType();
// typeHost.setId(2); // 2 = Host
// InfraMachine infraMachine = new InfraMachine();
// infraMachine.setProbe(probe);
// infraMachine.setInfraType(typeMachine);
// infraMachine.setMeta(StringConvertor.intToIp(host.getIp())+"-MACHINE");
// this.infraMachineService.regist(infraMachine);
// InfraOS infraOS = new InfraOS();
// infraOS.setMachine(infraMachine);
// infraOS.setVendor(MetaInfraVendor.CreateInfraVendorByOS(host.getOs()));
// infraOS.setInfraType(typeOS);
// infraOS.setProbe(probe);
// infraOS.setMeta(StringConvertor.intToIp(host.getIp())+"-OS");
// this.infraOSService.regist(infraOS);
// InfraHost newInfraHost = new InfraHost();
// newInfraHost.setIp(host.getIp());
// newInfraHost.setMac(host.getMac());
// newInfraHost.setOs(infraOS);
// newInfraHost.setInfraType(typeHost);
// newInfraHost.setProbe(probe);
// if(host.isTarget()) {
// Target target = new Target();
// target.setDisplayName(StringConvertor.intToIp(host.getIp()) + "-Host");
// this.targetService.regist(target);
// newInfraHost.setTarget(target);
// }
// this.infraHostService.regist(newInfraHost);
// infraHost = newInfraHost;
// }
// return infraHost;
// }
}

View File

@ -1,22 +1,12 @@
package com.loafle.overflow.central.proxy; package com.loafle.overflow.central.proxy;
import com.google.common.primitives.Primitives;
import com.google.protobuf.ByteString;
import com.loafle.overflow.central.module.target.service.TargetDiscoveryService;
import com.loafle.overflow.central.spring.AppConfigTest; import com.loafle.overflow.central.spring.AppConfigTest;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
/** /**
* Created by crusader on 17. 6. 30. * Created by crusader on 17. 6. 30.
*/ */