diff --git a/src/main/java/com/loafle/overflow/central/commons/utils/StringConvertor.java b/src/main/java/com/loafle/overflow/central/commons/utils/StringConvertor.java index ab01f99..1ab7bbd 100644 --- a/src/main/java/com/loafle/overflow/central/commons/utils/StringConvertor.java +++ b/src/main/java/com/loafle/overflow/central/commons/utils/StringConvertor.java @@ -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); + } + } diff --git a/src/main/java/com/loafle/overflow/central/module/target/service/CentralTargetDiscoveryService.java b/src/main/java/com/loafle/overflow/central/module/target/service/CentralTargetDiscoveryService.java index 1819eb3..3d3e1b8 100644 --- a/src/main/java/com/loafle/overflow/central/module/target/service/CentralTargetDiscoveryService.java +++ b/src/main/java/com/loafle/overflow/central/module/target/service/CentralTargetDiscoveryService.java @@ -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 hosts, Probe probe) { + @Transactional + public boolean saveAllTarget(List 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) { + } - // if(host.getPorts() == null) { - // return; - // } + private void createPort(InfraHost infraHost, Host host, Probe probe) throws OverflowException { - // String portType = "UDP"; +// if(host.getPorts() == null) { +// return; +// } - // MetaInfraType typePort = new MetaInfraType(); - // typePort.setId(6); + String portType = "UDP"; - // 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) { - // portType = "TCP"; - // } +// for( String key: host.getPorts().keySet()) { + for( Port port: host.getPortList()) { +// Port port = host.getPorts().get(key); - // 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 || port.getPortType() == PortType.TCP) { + portType = "TCP"; + } - // if (port.getPortType() == PortType.TLS) { - // infraOSPort.setTlsType(true); - // } - // infraOSPort.setVendor(MetaInfraVendor.CreateInfraVendorByPort(port.getPortNumber())); - // this.infraOSPortService.regist(infraOSPort); - // } + 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); - // 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()); - // if(infraHost != null) { + private InfraHost createAndReadHost(Host host, Probe probe) throws OverflowException { - // 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); - // } + InfraHost infraHost = this.infraHostService.readByIp(StringConvertor.ipToLong(host.getIp())); + if(infraHost != null) { - // return infraHost; - // } else { - // MetaInfraType typeMachine = new MetaInfraType(); - // typeMachine.setId(1); // 1 = Machine; + if(host.isTarget() && infraHost.getTarget() == null) { + Target target = new Target(); + target.setDisplayName(String.valueOf(host.getIp()) + "-Host"); - // MetaInfraType typeOS = new MetaInfraType(); - // typeOS.setId(3); // 3 = Os + this.targetService.regist(target); + infraHost.setTarget(target); + this.infraHostService.regist(infraHost); + } - // MetaInfraType typeHost = new MetaInfraType(); - // typeHost.setId(2); // 2 = Host + return infraHost; + } else { + MetaInfraType typeMachine = new MetaInfraType(); + typeMachine.setId(1); // 1 = Machine; - // InfraMachine infraMachine = new InfraMachine(); - // infraMachine.setProbe(probe); - // infraMachine.setInfraType(typeMachine); - // infraMachine.setMeta(StringConvertor.intToIp(host.getIp())+"-MACHINE"); - // this.infraMachineService.regist(infraMachine); + MetaInfraType typeOS = new MetaInfraType(); + typeOS.setId(3); // 3 = Os - // 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); + MetaInfraType typeHost = new MetaInfraType(); + typeHost.setId(2); // 2 = Host - // InfraHost newInfraHost = new InfraHost(); - // newInfraHost.setIp(host.getIp()); - // newInfraHost.setMac(host.getMac()); - // newInfraHost.setOs(infraOS); - // newInfraHost.setInfraType(typeHost); - // newInfraHost.setProbe(probe); + InfraMachine infraMachine = new InfraMachine(); + infraMachine.setProbe(probe); + infraMachine.setInfraType(typeMachine); + infraMachine.setMeta(host.getIp()+"-MACHINE"); + this.infraMachineService.regist(infraMachine); - // if(host.isTarget()) { - // Target target = new Target(); - // target.setDisplayName(StringConvertor.intToIp(host.getIp()) + "-Host"); + 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); - // this.targetService.regist(target); - // newInfraHost.setTarget(target); - // } + 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.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; + } } diff --git a/src/main/java/com/loafle/overflow/central/module/target/service/TargetDiscoveryService.java b/src/main/java/com/loafle/overflow/central/module/target/service/TargetDiscoveryService.java deleted file mode 100644 index efb7195..0000000 --- a/src/main/java/com/loafle/overflow/central/module/target/service/TargetDiscoveryService.java +++ /dev/null @@ -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 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; - // } - - - - -} diff --git a/src/test/java/com/loafle/overflow/central/proxy/ServiceInvokerTest.java b/src/test/java/com/loafle/overflow/central/proxy/ServiceInvokerTest.java index b1cd715..275f13d 100644 --- a/src/test/java/com/loafle/overflow/central/proxy/ServiceInvokerTest.java +++ b/src/test/java/com/loafle/overflow/central/proxy/ServiceInvokerTest.java @@ -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. */