fixed targetdiscovery

This commit is contained in:
snoop 2018-04-25 20:56:07 +09:00
parent 5e16f9b686
commit 752be72906
5 changed files with 216 additions and 166 deletions

View File

@ -3,6 +3,7 @@ package com.loafle.overflow.central.module.infra.dao;
import com.loafle.overflow.model.infra.InfraOSPort; import com.loafle.overflow.model.infra.InfraOSPort;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
// import org.springframework.data.jpa.repository.Query; // import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@ -11,6 +12,6 @@ import org.springframework.stereotype.Repository;
*/ */
@Repository @Repository
public interface InfraOSPortDAO extends JpaRepository<InfraOSPort, Long> { public interface InfraOSPortDAO extends JpaRepository<InfraOSPort, Long> {
// @Query("SELECT p from InfraOSPort p WHERE p.os.id = (:osId) AND p.port = (:portNumber) AND p.portType = (:portType)") @Query("SELECT p from com.loafle.overflow.model.infra.InfraOSPort p WHERE p.os.id = (:osId) AND p.port = (:portNumber) AND p.portType = (:portType)")
InfraOSPort findByPort(@Param("osId") long osId,@Param("portNumber") int portNumber,@Param("portType") String portType); InfraOSPort findByPort(@Param("osId") long osId,@Param("portNumber") Integer portNumber,@Param("portType") String portType);
} }

View File

@ -69,10 +69,14 @@ public class CentralTargetDiscoveryService implements TargetDiscoveryService{
String portType = "UDP"; String portType = "UDP";
if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP || port.getPortType() == null) {
portType = "TCP"; portType = "TCP";
} }
if(port.getServiceList() == null) {
return;
}
// for(String key : port.getServices().keySet()) { // for(String key : port.getServices().keySet()) {
for(com.loafle.overflow.model.discovery.Service service : port.getServiceList()) { for(com.loafle.overflow.model.discovery.Service service : port.getServiceList()) {
@ -130,6 +134,11 @@ public class CentralTargetDiscoveryService implements TargetDiscoveryService{
InfraOS infraOS = infraHost.getOs(); InfraOS infraOS = infraHost.getOs();
// for( String key: host.getPorts().keySet()) { // for( String key: host.getPorts().keySet()) {
if(host.getPortList() == null) {
return;
}
for( Port port: host.getPortList()) { for( Port port: host.getPortList()) {
// Port port = host.getPorts().get(key); // Port port = host.getPorts().get(key);

View File

@ -0,0 +1,30 @@
package com.loafle.overflow.central.module.infra.dao;
import com.loafle.overflow.central.spring.AppConfigTest;
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 static org.junit.Assert.*;
/**
* Created by snoop on 18. 4. 25.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfigTest.class})
public class InfraOSPortDAOTest {
@Autowired
private InfraOSPortDAO infraOSPortDAO;
@Test
public void findByPort() throws Exception {
// this.infraOSPortDAO.findByPort(1, 22, "UDP");
}
}

View File

@ -1,37 +1,37 @@
// package com.loafle.overflow.central.module.member.service; package com.loafle.overflow.central.module.member.service;
// import com.loafle.overflow.central.spring.AppConfigTest; import com.loafle.overflow.central.spring.AppConfigTest;
// import com.loafle.overflow.model.member.Member; import com.loafle.overflow.model.member.Member;
// import com.loafle.overflow.service.central.member.MemberService; import com.loafle.overflow.service.central.member.MemberService;
// import com.loafle.overflow.service.central.member.MemberTotpService; import com.loafle.overflow.service.central.member.MemberTotpService;
// import org.junit.Assert; import org.junit.Assert;
// import org.junit.Ignore; 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.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
// 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.util.Date; import java.util.Date;
// import java.util.List; import java.util.List;
// import java.util.regex.Matcher; import java.util.regex.Matcher;
// import java.util.regex.Pattern; import java.util.regex.Pattern;
// /** /**
// * Created by insanity on 17. 6. 28. * Created by insanity on 17. 6. 28.
// */ */
// @RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
// @ActiveProfiles("test") @ActiveProfiles("test")
// @ContextConfiguration(classes = {AppConfigTest.class}) @ContextConfiguration(classes = {AppConfigTest.class})
// public class MemberServiceTest { public class MemberServiceTest {
// @Autowired @Autowired
// MemberService memberService; MemberService memberService;
// @Autowired @Autowired
// MemberTotpService totpService; MemberTotpService totpService;
// @Test // @Test
@ -44,53 +44,53 @@
// m.setEmail("geekhot@hotmail.co.kr"); // m.setEmail("geekhot@hotmail.co.kr");
// m.setName("administrator"); // m.setName("administrator");
// m.setPhone("00000000"); // m.setPhone("00000000");
//
// Member res = this.memberService.signup(m,"!@#$qwer1234"); // Member res = this.memberService.signup(m,"!@#$qwer1234");
// Assert.assertNotNull(res); // Assert.assertNotNull(res);
// } // }
// @Test @Test
// public void signin() throws Exception { public void signin() throws Exception {
// // Member m = this.memberService.signin("overflow@loafle.com", "!@#$qwer1234"); // Member m = this.memberService.signin("overflow@loafle.com", "!@#$qwer1234");
// // Assert.assertNotNull(m); // Assert.assertNotNull(m);
// } }
// @Test // @Test
// public void readAllMemberByProbeKey() { // public void readAllMemberByProbeKey() {
//
// List<Member> members = this.memberService.readAllByProbeKey("95d8bcdc739741dca74c4a0e489e0774"); // List<Member> members = this.memberService.readAllByProbeKey("95d8bcdc739741dca74c4a0e489e0774");
//
// Assert.assertNotEquals(members.size(), 0); // Assert.assertNotEquals(members.size(), 0);
//
// } // }
//
// @Test // @Test
// public void readAllMemberByApiKey() { // public void readAllMemberByApiKey() {
//
// List<Member> members = this.memberService.readAllByApiKey("52abd6fd57e511e7ac52080027658d13"); // List<Member> members = this.memberService.readAllByApiKey("52abd6fd57e511e7ac52080027658d13");
//
// Assert.assertNotEquals(members.size(), 0); // Assert.assertNotEquals(members.size(), 0);
// } // }
//
// @Test // @Test
// public void readAllMemberByDomain() { // public void readAllMemberByDomain() {
//
// Domain domain = new Domain(); // Domain domain = new Domain();
//
// domain.setId(1); // domain.setId(1);
//
// List<Member> members = this.memberService.readAllByDomain(domain); // List<Member> members = this.memberService.readAllByDomain(domain);
//
// Assert.assertNotEquals(members.size(), 0); // Assert.assertNotEquals(members.size(), 0);
//
// } // }
//
// @Test // @Test
// public void TestPasswordStrong() { // public void TestPasswordStrong() {
//
// String arrpw[] = new String[]{"!@#$Qwer1234", "Zxasqw12!!","@Cosmos@5795"}; // String arrpw[] = new String[]{"!@#$Qwer1234", "Zxasqw12!!","@Cosmos@5795"};
// // String pass = "!@#$Qwer1234"; // // String pass = "!@#$Qwer1234";
//
// boolean check = false; // boolean check = false;
// for (int i = 0; i < arrpw.length; i++) { // for (int i = 0; i < arrpw.length; i++) {
// check = this.memberService.isPasswordStrong(arrpw[i]); // check = this.memberService.isPasswordStrong(arrpw[i]);
@ -99,22 +99,22 @@
// } // }
// } // }
// // //
// // String regex = "(" + // String regex = "(" +
// // "(?=.*[a-z])" + // "(?=.*[a-z])" +
// // "(?=.*\\d)" + // "(?=.*\\d)" +
// // "(?=.*[A-Z])" + // "(?=.*[A-Z])" +
// // "(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?])" + // "(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?])" +
// // "." + // "." +
// // "{6,40}" + // "{6,40}" +
// // ")"; // ")";
// // @Test // @Test
// // public void TestPassword() { // public void TestPassword() {
// // Pattern pattern = Pattern.compile(regex); // Pattern pattern = Pattern.compile(regex);
// // Matcher matcher = pattern.matcher("qwe1231@Q\\"); // Matcher matcher = pattern.matcher("qwe1231@Q\\");
// // System.out.println("Length"+"qwe1231@Q\\".length()); // System.out.println("Length"+"qwe1231@Q\\".length());
// // //
// // System.out.println("matcher.matches() = " + matcher.matches()); // System.out.println("matcher.matches() = " + matcher.matches());
// // } // }
// } }

View File

@ -1,134 +1,144 @@
// package com.loafle.overflow.central.module.target.service; package com.loafle.overflow.central.module.target.service;
// import com.loafle.overflow.central.module.probe.model.Probe;
// import com.loafle.overflow.central.spring.AppConfigTest;
// import org.codehaus.jackson.map.DeserializationConfig;
// import org.codehaus.jackson.map.ObjectMapper;
// import org.junit.Ignore;
// import org.junit.Test;
// import org.junit.runner.RunWith;
// import org.springframework.aop.support.AopUtils;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.core.io.ResourceLoader;
// import org.springframework.test.context.ContextConfiguration;
// import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
// import java.io.BufferedReader; import com.google.gson.Gson;
// import java.io.FileReader; import com.google.gson.reflect.TypeToken;
// import java.io.IOException; import com.loafle.overflow.central.spring.AppConfigTest;
// import java.lang.reflect.Method;
// import java.lang.reflect.ParameterizedType;
// import java.lang.reflect.Type;
// import java.util.List;
// /** import com.loafle.overflow.model.discovery.Host;
// * Created by snoop on 17. 6. 28. import com.loafle.overflow.model.probe.Probe;
// */ import com.loafle.overflow.service.central.target.TargetDiscoveryService;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
// @RunWith(SpringJUnit4ClassRunner.class) import java.io.BufferedReader;
// @ContextConfiguration(classes = {AppConfigTest.class}) import java.io.FileReader;
// public class TargetDiscoveryServiceTest { import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
// @Autowired /**
// private ResourceLoader resourceLoader; * Created by snoop on 17. 6. 28.
*/
// @Autowired @RunWith(SpringJUnit4ClassRunner.class)
// private TargetDiscoveryService targetDiscoveryService; @ContextConfiguration(classes = {AppConfigTest.class})
public class TargetDiscoveryServiceTest {
@Autowired
private ResourceLoader resourceLoader;
@Autowired
private TargetDiscoveryService targetDiscoveryService;
// @Ignore // @Ignore
// @Test @Test
// public void saveAllTarget() throws Exception { public void saveAllTarget() throws Exception {
// String json = readFileAsString(resourceLoader.getResource("classpath:dh.json").getURI().getPath()); String json = readFileAsString(resourceLoader.getResource("classpath:2018-04-25-tds.json").getURI().getPath());
Gson gson = new Gson();
// ObjectMapper mapper = new ObjectMapper(); // ObjectMapper mapper = new ObjectMapper();
//
// mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); // mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// List<Host> hosts = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, Host.class)); // List<Host> hosts = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, Host.class));
// Probe probe = new Probe(); Type listType = new TypeToken<List<Host>>(){}.getType();
// probe.setId(1);
// // this.targetDiscoveryService.saveAllTarget(hosts, probe); List<Host> hosts = gson.fromJson(json, listType);
// }
// private String readFileAsString(String filePath) throws IOException { Probe probe = new Probe();
// StringBuffer fileData = new StringBuffer(); probe.setId(1);
// BufferedReader reader = new BufferedReader(
// new FileReader(filePath)); this.targetDiscoveryService.saveAllTarget(hosts, probe);
// char[] buf = new char[1024]; }
// int numRead=0;
// while((numRead=reader.read(buf)) != -1){ private String readFileAsString(String filePath) throws IOException {
// String readData = String.valueOf(buf, 0, numRead); StringBuffer fileData = new StringBuffer();
// fileData.append(readData); BufferedReader reader = new BufferedReader(
// } new FileReader(filePath));
// reader.close(); char[] buf = new char[1024];
// return fileData.toString(); int numRead=0;
// } while((numRead=reader.read(buf)) != -1){
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
}
reader.close();
return fileData.toString();
}
// @Ignore @Ignore
// @Test @Test
// public void testParam() throws NoSuchMethodException { public void testParam() throws NoSuchMethodException {
// Method m = this.targetDiscoveryService.getClass().getMethod("saveAllTarget", List.class, Probe.class); Method m = this.targetDiscoveryService.getClass().getMethod("saveAllTarget", List.class, Probe.class);
// // Method m = TargetDiscoveryService.class.getMethod("saveAllTarget", List.class, Probe.class); // Method m = TargetDiscoveryService.class.getMethod("saveAllTarget", List.class, Probe.class);
// Class<?> clazz = AopUtils.getTargetClass(this.targetDiscoveryService); Class<?> clazz = AopUtils.getTargetClass(this.targetDiscoveryService);
// Method[] ms = clazz.getMethods(); Method[] ms = clazz.getMethods();
// Method sm = null; Method sm = null;
// for(Method mm : ms){ for(Method mm : ms){
// if ("saveAllTarget".equals(mm.getName())) { if ("saveAllTarget".equals(mm.getName())) {
// sm = mm; sm = mm;
// break; break;
// } }
// } }
// if (sm != null) { if (sm != null) {
// Type[] ts = sm.getGenericParameterTypes(); Type[] ts = sm.getGenericParameterTypes();
// for(Type t : ts){ for(Type t : ts){
// if (t instanceof ParameterizedType) { if (t instanceof ParameterizedType) {
// ParameterizedType pt = (ParameterizedType)t; ParameterizedType pt = (ParameterizedType)t;
// System.out.println(pt.getActualTypeArguments()[0].getTypeName()); System.out.println(pt.getActualTypeArguments()[0].getTypeName());
// } }
// System.out.println(t.getTypeName()); System.out.println(t.getTypeName());
// } }
// } }
// System.out.println(m.getName()); System.out.println(m.getName());
// Type[] genericParameterTypes = m.getGenericParameterTypes(); Type[] genericParameterTypes = m.getGenericParameterTypes();
// for(Type genericParameterType : genericParameterTypes){ for(Type genericParameterType : genericParameterTypes){
// if(genericParameterType instanceof ParameterizedType){ if(genericParameterType instanceof ParameterizedType){
// ParameterizedType aType = (ParameterizedType) genericParameterType; ParameterizedType aType = (ParameterizedType) genericParameterType;
// Type[] parameterArgTypes = aType.getActualTypeArguments(); Type[] parameterArgTypes = aType.getActualTypeArguments();
// for(Type parameterArgType : parameterArgTypes){ for(Type parameterArgType : parameterArgTypes){
// Class parameterArgClass = (Class) parameterArgType; Class parameterArgClass = (Class) parameterArgType;
// System.out.println("parameterArgClass = " + parameterArgClass); System.out.println("parameterArgClass = " + parameterArgClass);
// } }
// } }
// } }
// } }
// @Ignore @Ignore
// @Test @Test
// public void testName() throws ClassNotFoundException { public void testName() throws ClassNotFoundException {
// Object o = this.targetDiscoveryService; Object o = this.targetDiscoveryService;
// String nnn = o.getClass().getSimpleName(); String nnn = o.getClass().getSimpleName();
// System.out.println(nnn); System.out.println(nnn);
// } }
// } }