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