fixed discovery result - save infra

but
 fixme
   mac address
   infra vendor
This commit is contained in:
snoop 2018-04-03 19:03:50 +09:00
parent 396ab8c3ff
commit e8701e2695
7 changed files with 222 additions and 128 deletions

View File

@ -12,4 +12,20 @@ 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;
}
}

View File

@ -1,6 +1,7 @@
package com.loafle.overflow.module.discovery.model;
import java.util.Date;
import java.util.Map;
/**
* Created by root on 17. 6. 4.
@ -12,9 +13,12 @@ public class Host {
private String mac;
private String os;
private Date discoveredDate;
private boolean target;
private Zone zone;
private Map<String, Port> ports;
public Host(){}
public long getId() {
@ -64,4 +68,20 @@ public class Host {
public void setZone(Zone zone) {
this.zone = zone;
}
public boolean isTarget() {
return target;
}
public void setTarget(boolean target) {
this.target = target;
}
public Map<String, Port> getPorts() {
return ports;
}
public void setPorts(Map<String, Port> ports) {
this.ports = ports;
}
}

View File

@ -8,6 +8,7 @@ package com.loafle.overflow.module.discovery.model;
import com.loafle.overflow.module.discovery.type.PortType;
import java.util.Date;
import java.util.Map;
/**
* Created by root on 17. 6. 4.
@ -23,6 +24,8 @@ public class Port {
private Host host;
private Map<String, Service> services;
public Port() {}
public long getId() {
@ -65,4 +68,11 @@ public class Port {
this.host = host;
}
public Map<String, Service> getServices() {
return services;
}
public void setServices(Map<String, Service> services) {
this.services = services;
}
}

View File

@ -14,6 +14,8 @@ public class Service {
private Port port;
private boolean target;
public Service() {}
public long getId() {
@ -56,5 +58,11 @@ public class Service {
this.port = port;
}
public boolean isTarget() {
return target;
}
public void setTarget(boolean target) {
this.target = target;
}
}

View File

@ -60,7 +60,7 @@ public class MetaInfraVendor {
MetaInfraVendor vendor = new MetaInfraVendor();
if(osName == null || osName.length() <= 0) {
vendor.setId(28); // FIXME: Unknown
vendor.setId(24); // FIXME: Unknown
return vendor;
}
@ -70,7 +70,7 @@ public class MetaInfraVendor {
else if(osName.equals("Linux")) {
vendor.setId(28); // ubuntu
} else {
vendor.setId(28); // FIXME: Unknown
vendor.setId(24); // FIXME: Unknown
}
return vendor;

View File

@ -44,177 +44,184 @@ public class TargetDiscoveryService {
@Autowired
private InfraServiceService infraServiceService;
// @Transactional
// public boolean saveAllTarget(List<Host> hosts, Probe probe) {
@Transactional
public boolean saveAllTarget(List<Host> hosts, Probe probe) {
// 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) {
// if(port.getServices() == null) {
// return;
// }
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.module.discovery.model.Service service : port.getServices()) {
for(String key : port.getServices().keySet()) {
// 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() + "-Service");
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() + "-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;
// }
private void createPort(InfraHost infraHost, Host host, Probe probe) {
// 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()) {
// 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);
Port port = host.getPorts().get(key);
// if (port.getPortType() == PortType.TLS) {
// infraOSPort.setTlsType(true);
// }
// infraOSPort.setVendor(MetaInfraVendor.CreateInfraVendorByPort(port.getPortNumber()));
// this.infraOSPortService.regist(infraOSPort);
// }
if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) {
portType = "TCP";
}
// this.createService(infraHost, port, probe);
// }
// }
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);
// private InfraHost createAndReadHost(Host host, Probe probe) {
if (port.getPortType() == PortType.TLS) {
infraOSPort.setTlsType(true);
}
infraOSPort.setVendor(MetaInfraVendor.CreateInfraVendorByPort(port.getPortNumber()));
this.infraOSPortService.regist(infraOSPort);
}
// InfraHost infraHost = this.infraHostService.readByIp(host.getIp());
// if(infraHost != null) {
this.createService(infraHost, port, probe);
}
}
// if(host.isTarget() && infraHost.getTarget() == null) {
// Target target = new Target();
// target.setDisplayName(String.valueOf(host.getIp()) + "-Host");
private InfraHost createAndReadHost(Host host, Probe probe) {
// this.targetService.regist(target);
// infraHost.setTarget(target);
// this.infraHostService.regist(infraHost);
// }
// return infraHost;
// } else {
// MetaInfraType typeMachine = new MetaInfraType();
// typeMachine.setId(1); // 1 = Machine;
InfraHost infraHost = this.infraHostService.readByIp(StringConvertor.ipToLong(host.getIp()));
if(infraHost != null) {
// MetaInfraType typeOS = new MetaInfraType();
// typeOS.setId(3); // 3 = Os
if(host.isTarget() && infraHost.getTarget() == null) {
Target target = new Target();
target.setDisplayName(String.valueOf(host.getIp()) + "-Host");
// MetaInfraType typeHost = new MetaInfraType();
// typeHost.setId(2); // 2 = Host
this.targetService.regist(target);
infraHost.setTarget(target);
this.infraHostService.regist(infraHost);
}
// InfraMachine infraMachine = new InfraMachine();
// infraMachine.setProbe(probe);
// infraMachine.setInfraType(typeMachine);
// infraMachine.setMeta(StringConvertor.intToIp(host.getIp())+"-MACHINE");
// this.infraMachineService.regist(infraMachine);
return infraHost;
} else {
MetaInfraType typeMachine = new MetaInfraType();
typeMachine.setId(1); // 1 = Machine;
// 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 typeOS = new MetaInfraType();
typeOS.setId(3); // 3 = Os
// InfraHost newInfraHost = new InfraHost();
// newInfraHost.setIp(host.getIp());
// newInfraHost.setMac(host.getMac());
// newInfraHost.setOs(infraOS);
// newInfraHost.setInfraType(typeHost);
// newInfraHost.setProbe(probe);
MetaInfraType typeHost = new MetaInfraType();
typeHost.setId(2); // 2 = Host
// if(host.isTarget()) {
// Target target = new Target();
// target.setDisplayName(StringConvertor.intToIp(host.getIp()) + "-Host");
InfraMachine infraMachine = new InfraMachine();
infraMachine.setProbe(probe);
infraMachine.setInfraType(typeMachine);
infraMachine.setMeta(host.getIp()+"-MACHINE");
this.infraMachineService.regist(infraMachine);
// this.targetService.regist(target);
// newInfraHost.setTarget(target);
// }
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.infraHostService.regist(newInfraHost);
// infraHost = newInfraHost;
// }
InfraHost newInfraHost = new InfraHost();
newInfraHost.setIp(StringConvertor.ipToLong(host.getIp()));
// newInfraHost.setMac(StringConvertor.ipToLong(host.getMac()));
newInfraHost.setMac(123123123);
newInfraHost.setOs(infraOS);
newInfraHost.setInfraType(typeHost);
newInfraHost.setProbe(probe);
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,5 +1,6 @@
package com.loafle.overflow.module.target.service;
import com.loafle.overflow.commons.utils.StringConvertor;
import com.loafle.overflow.module.discovery.model.Host;
import com.loafle.overflow.module.probe.model.Probe;
import com.loafle.overflow.spring.AppConfigTest;
@ -54,6 +55,38 @@ public class TargetDiscoveryServiceTest {
// this.targetDiscoveryService.saveAllTarget(hosts, probe);
}
@Test
public void saveAllTartget2() throws Exception {
String json = readFileAsString(resourceLoader.getResource("classpath:180403-td.json").getURI().getPath());
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
List<Host> hosts = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, Host.class));
Probe probe = new Probe();
probe.setId(1);
// System.out.println(hosts.size());
this.targetDiscoveryService.saveAllTarget(hosts, probe);
}
@Test
public void testConvert() {
String ip = "192.168.1.106";
long aaa = StringConvertor.ipToLong(ip);
System.out.println(aaa);
}
private String readFileAsString(String filePath) throws IOException {
StringBuffer fileData = new StringBuffer();
BufferedReader reader = new BufferedReader(