Commit 356c9ddd by 钱炳权

Enhance code robustness

parent df2e1715
...@@ -77,3 +77,106 @@ Action: ...@@ -77,3 +77,106 @@ Action:
Identify and stop the process that's listening on port 8100 or configure this application to listen on another port. Identify and stop the process that's listening on port 8100 or configure this application to listen on another port.
2024-04-22 16:43:12.127 [main] INFO com.example.fuzzControll.FuzzControlApplication - Starting FuzzControlApplication using Java 11.0.6 on DESKTOP-GDSKRB2 with PID 33276 (D:\code\company\fuzz-backend\fuzz-backend\fuzzbackend\target\classes started by qian in D:\code\company\fuzz-backend\fuzz-backend\fuzzbackend)
2024-04-22 16:43:12.129 [main] INFO com.example.fuzzControll.FuzzControlApplication - The following 1 profile is active: "dev"
2024-04-22 16:43:12.561 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8100 (http)
2024-04-22 16:43:12.566 [main] INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]
2024-04-22 16:43:12.566 [main] INFO org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.68]
2024-04-22 16:43:12.617 [main] INFO o.a.c.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
2024-04-22 16:43:12.617 [main] INFO o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 455 ms
2024-04-22 16:43:12.832 [main] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.PortInUseException: Port 8100 is already in use
2024-04-22 16:43:12.832 [main] INFO org.apache.catalina.core.StandardService - Stopping service [Tomcat]
2024-04-22 16:43:12.840 [main] INFO o.s.b.a.l.ConditionEvaluationReportLoggingListener -
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2024-04-22 16:43:12.847 [main] ERROR o.s.b.diagnostics.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
Web server failed to start. Port 8100 was already in use.
Action:
Identify and stop the process that's listening on port 8100 or configure this application to listen on another port.
2024-04-22 16:43:43.316 [main] INFO com.example.fuzzControll.FuzzControlApplication - Starting FuzzControlApplication using Java 11.0.6 on DESKTOP-GDSKRB2 with PID 27504 (D:\code\company\fuzz-backend\fuzz-backend\fuzzbackend\target\classes started by qian in D:\code\company\fuzz-backend\fuzz-backend\fuzzbackend)
2024-04-22 16:43:43.318 [main] INFO com.example.fuzzControll.FuzzControlApplication - The following 1 profile is active: "dev"
2024-04-22 16:43:43.748 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8100 (http)
2024-04-22 16:43:43.752 [main] INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]
2024-04-22 16:43:43.752 [main] INFO org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.68]
2024-04-22 16:43:43.802 [main] INFO o.a.c.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
2024-04-22 16:43:43.802 [main] INFO o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 462 ms
2024-04-22 16:43:44.028 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8100 (http) with context path ''
2024-04-22 16:43:44.044 [main] INFO com.example.fuzzControll.FuzzControlApplication - Started FuzzControlApplication in 0.948 seconds (JVM running for 1.455)
2024-04-22 16:44:29.396 [http-nio-8100-exec-1] INFO o.a.c.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-04-22 16:44:29.396 [http-nio-8100-exec-1] INFO org.springframework.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
2024-04-22 16:44:29.397 [http-nio-8100-exec-1] INFO org.springframework.web.servlet.DispatcherServlet - Completed initialization in 1 ms
2024-04-22 17:20:02.482 [main] INFO com.example.fuzzControll.FuzzControlApplication - Starting FuzzControlApplication using Java 11.0.6 on DESKTOP-GDSKRB2 with PID 35320 (D:\code\company\fuzz-backend\fuzz-backend\fuzzbackend\target\classes started by qian in D:\code\company\fuzz-backend\fuzz-backend\fuzzbackend)
2024-04-22 17:20:02.484 [main] INFO com.example.fuzzControll.FuzzControlApplication - The following 1 profile is active: "dev"
2024-04-22 17:20:03.002 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8100 (http)
2024-04-22 17:20:03.008 [main] INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]
2024-04-22 17:20:03.008 [main] INFO org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.68]
2024-04-22 17:20:03.120 [main] INFO o.a.c.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
2024-04-22 17:20:03.121 [main] INFO o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 610 ms
2024-04-22 17:20:03.305 [main] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.PortInUseException: Port 8100 is already in use
2024-04-22 17:20:03.307 [main] INFO org.apache.catalina.core.StandardService - Stopping service [Tomcat]
2024-04-22 17:20:03.314 [main] INFO o.s.b.a.l.ConditionEvaluationReportLoggingListener -
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2024-04-22 17:20:03.320 [main] ERROR o.s.b.diagnostics.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
Web server failed to start. Port 8100 was already in use.
Action:
Identify and stop the process that's listening on port 8100 or configure this application to listen on another port.
2024-04-22 17:20:04.598 [main] INFO com.example.fuzzControll.FuzzControlApplication - Starting FuzzControlApplication using Java 11.0.6 on DESKTOP-GDSKRB2 with PID 37064 (D:\code\company\fuzz-backend\fuzz-backend\fuzzbackend\target\classes started by qian in D:\code\company\fuzz-backend\fuzz-backend\fuzzbackend)
2024-04-22 17:20:04.600 [main] INFO com.example.fuzzControll.FuzzControlApplication - The following 1 profile is active: "dev"
2024-04-22 17:20:05.053 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8100 (http)
2024-04-22 17:20:05.057 [main] INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]
2024-04-22 17:20:05.057 [main] INFO org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.68]
2024-04-22 17:20:05.109 [main] INFO o.a.c.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
2024-04-22 17:20:05.109 [main] INFO o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 485 ms
2024-04-22 17:20:05.303 [main] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.PortInUseException: Port 8100 is already in use
2024-04-22 17:20:05.304 [main] INFO org.apache.catalina.core.StandardService - Stopping service [Tomcat]
2024-04-22 17:20:05.311 [main] INFO o.s.b.a.l.ConditionEvaluationReportLoggingListener -
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2024-04-22 17:20:05.317 [main] ERROR o.s.b.diagnostics.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
Web server failed to start. Port 8100 was already in use.
Action:
Identify and stop the process that's listening on port 8100 or configure this application to listen on another port.
2024-04-22 17:20:17.525 [main] INFO com.example.fuzzControll.FuzzControlApplication - Starting FuzzControlApplication using Java 11.0.6 on DESKTOP-GDSKRB2 with PID 37260 (D:\code\company\fuzz-backend\fuzz-backend\fuzzbackend\target\classes started by qian in D:\code\company\fuzz-backend\fuzz-backend\fuzzbackend)
2024-04-22 17:20:17.527 [main] INFO com.example.fuzzControll.FuzzControlApplication - The following 1 profile is active: "dev"
2024-04-22 17:20:17.985 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8101 (http)
2024-04-22 17:20:17.989 [main] INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]
2024-04-22 17:20:17.990 [main] INFO org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.68]
2024-04-22 17:20:18.043 [main] INFO o.a.c.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
2024-04-22 17:20:18.043 [main] INFO o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 496 ms
2024-04-22 17:20:18.257 [main] INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8101 (http) with context path ''
2024-04-22 17:20:18.263 [main] INFO com.example.fuzzControll.FuzzControlApplication - Started FuzzControlApplication in 0.961 seconds (JVM running for 1.472)
2024-04-22 17:22:20.655 [http-nio-8101-exec-1] INFO o.a.c.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-04-22 17:22:20.656 [http-nio-8101-exec-1] INFO org.springframework.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
2024-04-22 17:22:20.656 [http-nio-8101-exec-1] INFO org.springframework.web.servlet.DispatcherServlet - Completed initialization in 0 ms
2024-04-22 17:49:35.091 [http-nio-8101-exec-5] INFO c.e.fuzzControll.service.impl.TestServiceImpl - Aflnet has been stopped !
package com.example.fuzzControll.controller; package com.example.fuzzControll.controller;
import com.example.fuzzControll.exception.AflnetException;
import com.example.fuzzControll.pojo.vo.AjaxResult; import com.example.fuzzControll.pojo.vo.AjaxResult;
import com.example.fuzzControll.pojo.vo.CmdStartParams; import com.example.fuzzControll.pojo.vo.CmdStartParams;
import com.example.fuzzControll.service.TestService; import com.example.fuzzControll.service.TestService;
...@@ -28,14 +29,15 @@ public class TestControler { ...@@ -28,14 +29,15 @@ public class TestControler {
@RequestMapping(value = "/testStart", method = RequestMethod.POST) @RequestMapping(value = "/testStart", method = RequestMethod.POST)
public AjaxResult list(@RequestBody final CmdStartParams cmdStartParams) { public AjaxResult list(@RequestBody final CmdStartParams cmdStartParams) {
try { try {
//todo 加个同步锁,执行到特征点就返回数据
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
service.testStart(cmdStartParams); service.testStart(cmdStartParams);
} }
}).start(); }).start();
} catch (Exception e) { } catch (AflnetException e) {
log.error(e.getMessage()); log.error(e.getDefaultMessage());
return AjaxResult.error("测试启动失败!"); return AjaxResult.error("测试启动失败!");
} }
return AjaxResult.success("测试启动成功!"); return AjaxResult.success("测试启动成功!");
...@@ -48,7 +50,8 @@ public class TestControler { ...@@ -48,7 +50,8 @@ public class TestControler {
public AjaxResult testStop() { public AjaxResult testStop() {
try { try {
service.testStop(); service.testStop();
} catch (Exception e) { } catch (AflnetException e) {
log.error(e.getDefaultMessage());
return AjaxResult.error("测试停止失败!"); return AjaxResult.error("测试停止失败!");
} }
return AjaxResult.success("测试停止成功!"); return AjaxResult.success("测试停止成功!");
......
...@@ -9,9 +9,11 @@ import com.example.fuzzControll.pojo.vo.CmdStartParams; ...@@ -9,9 +9,11 @@ import com.example.fuzzControll.pojo.vo.CmdStartParams;
import com.example.fuzzControll.service.TestService; import com.example.fuzzControll.service.TestService;
import com.example.fuzzControll.tools.CmdTools; import com.example.fuzzControll.tools.CmdTools;
import com.example.fuzzControll.tools.TestControlTools; import com.example.fuzzControll.tools.TestControlTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service("testService") @Service("testService")
@Slf4j
public class TestServiceImpl implements TestService { public class TestServiceImpl implements TestService {
KittyProperties kittyProperties = (KittyProperties) SpringContextUtil.getBean("kittyProperties"); KittyProperties kittyProperties = (KittyProperties) SpringContextUtil.getBean("kittyProperties");
...@@ -30,5 +32,6 @@ public class TestServiceImpl implements TestService { ...@@ -30,5 +32,6 @@ public class TestServiceImpl implements TestService {
@Override @Override
public void testStop() { public void testStop() {
TestControlTools.setIsRunning(false); TestControlTools.setIsRunning(false);
log.info("Aflnet has been stopped ! ");
} }
} }
...@@ -20,11 +20,12 @@ import java.util.*; ...@@ -20,11 +20,12 @@ import java.util.*;
public class CmdTools { public class CmdTools {
Boolean send = false; Boolean send = false;
WebSocket socket = (WebSocket) SpringContextUtil.getBean("WebSocket"); WebSocket socket = (WebSocket) SpringContextUtil.getBean("WebSocket");
Boolean show = true;
/** /**
* 运行不需要后台运行cmd * 运行不需要后台运行cmd
*/ */
public List<String> runCmd(String cmd,String caller) throws CmdException { public List<String> runCmd(String cmd, String caller) throws CmdException {
List<String> result = new ArrayList<String>(); List<String> result = new ArrayList<String>();
try { try {
Process process = Runtime.getRuntime().exec(cmd); Process process = Runtime.getRuntime().exec(cmd);
...@@ -33,7 +34,7 @@ public class CmdTools { ...@@ -33,7 +34,7 @@ public class CmdTools {
process.waitFor(); process.waitFor();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw new CmdException(caller+" run cmd failed!"); throw new CmdException(caller + " run cmd failed!");
} }
return result; return result;
...@@ -45,14 +46,16 @@ public class CmdTools { ...@@ -45,14 +46,16 @@ public class CmdTools {
* 运行需要后台运行cmd * 运行需要后台运行cmd
* 通过websocket返回数据 * 通过websocket返回数据
*/ */
public void runProgramCmd(String cmd) { public void runProgramCmd(String cmd) throws AflnetException {
try { try {
Process process = Runtime.getRuntime().exec(cmd); Process process = Runtime.getRuntime().exec(cmd);
printMessageToWeb(process.getInputStream(), process); printMessageToWeb(process.getInputStream());
printMessage(process.getErrorStream(), new ArrayList<String>()); printMessage(process.getErrorStream(), new ArrayList<String>());
process.waitFor(); process.waitFor();
log.info("Aflnet cmd have been run.");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw new AflnetException("Aflnet run error");
} }
} }
...@@ -78,7 +81,7 @@ public class CmdTools { ...@@ -78,7 +81,7 @@ public class CmdTools {
return result; return result;
} }
private List<String> printMessageByProgramCmd(final InputStream input, final List<String> result) { private List<String> printMessageByProgramCmd(final InputStream input, final List<String> result) {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
...@@ -118,7 +121,8 @@ public class CmdTools { ...@@ -118,7 +121,8 @@ public class CmdTools {
return result; return result;
} }
private void printMessageToWeb(final InputStream input, Process process) throws IOException { private void printMessageToWeb(final InputStream input) throws IOException, AflnetException {
show = true;
Reader reader = new InputStreamReader(input); Reader reader = new InputStreamReader(input);
BufferedReader bf = new BufferedReader(reader); BufferedReader bf = new BufferedReader(reader);
String line = null; String line = null;
...@@ -127,12 +131,23 @@ public class CmdTools { ...@@ -127,12 +131,23 @@ public class CmdTools {
makeReturnEntity(line, returnEntity); makeReturnEntity(line, returnEntity);
if (send) { if (send) {
String data = JSONObject.toJSONString(returnEntity); String data = JSONObject.toJSONString(returnEntity);
socket.appointSending("backend", "web", data); try {
socket.appointSending("backend", "web", data);
} catch (Exception ignored) {
}
} }
} }
} }
private TestReturnEntity makeReturnEntity(String line, TestReturnEntity returnEntity) { private TestReturnEntity makeReturnEntity(String line, TestReturnEntity returnEntity) {
if (line.contains("All set and ready to roll!") || line.contains("american fuzzy") || line.contains("process timing overall results")) {
show = false;
} else if (line.contains("PROGRAM ABORT")) {
throw new AflnetException("Aflnet run failed !");
}
if (show) {
log.info(line);
}
if (line.contains("run time")) { if (line.contains("run time")) {
send = false; send = false;
int run_time = line.indexOf(":"); int run_time = line.indexOf(":");
...@@ -222,8 +237,8 @@ public class CmdTools { ...@@ -222,8 +237,8 @@ public class CmdTools {
return returnEntity; return returnEntity;
} }
public String parse(CmdStartParams cmdStartParams) throws AflnetException{ public String parse(CmdStartParams cmdStartParams) throws AflnetException {
if(cmdStartParams==null){ if (cmdStartParams == null) {
throw new AflnetException("CmdStartParams is null!"); throw new AflnetException("CmdStartParams is null!");
} }
StringBuilder cmd = new StringBuilder(); StringBuilder cmd = new StringBuilder();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment