CodeScanBack/src/main/java/com/osredm/codescan/service/impl/NicadServiceImpl.java

132 lines
5.9 KiB
Java
Raw Normal View History

2022-07-19 19:34:18 +08:00
package com.osredm.codescan.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
2022-07-19 19:34:18 +08:00
import com.osredm.codescan.config.StoreConfig;
import com.osredm.codescan.entity.Nicad.NicadClones;
import com.osredm.codescan.entity.Nicad.NicadInfo;
import com.osredm.codescan.entity.Nicad.NicadJson;
2022-07-19 19:34:18 +08:00
import com.osredm.codescan.service.NicadService;
import com.osredm.codescan.utils.FileUploadUtil;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import static com.osredm.codescan.utils.Constants.FAIL;
import static com.osredm.codescan.utils.Constants.SUCCESS;
@Service
public class NicadServiceImpl implements NicadService {
private final StoreConfig storeConfig;
public NicadServiceImpl(StoreConfig storeConfig) {
this.storeConfig = storeConfig;
}
@Override
public HashMap<String, Object> check(NicadInfo nicadInfo){
HashMap<String, Object> map = new HashMap<>();
String language = nicadInfo.getLanguage();
switch (language) {
case "Java":
String filePath = storeConfig.getFilePath() + nicadInfo.getFileName();
String time = String.valueOf(System.currentTimeMillis());
String flag = storeConfig.getFilePath() + time;
// 解压文件
String[] arguments = new String[] {"unzip" , filePath, "-d", flag};
Process proc;
try {
proc = Runtime.getRuntime().exec(arguments);// 执行解压文件
//用输入输出流来截取结果
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
String line1 = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
line1 = line;
}
in.close();
int re = proc.waitFor(); //waitFor是用来显示脚本是否运行成功1表示失败0表示成功还有其他的表示其他错误
2022-07-19 19:34:18 +08:00
if (re == 0) {
//成功
Process proc1;
String[] arguments1 = new String[] {"nicad6" ,"functions","java", flag, "default-report"};
proc1 = Runtime.getRuntime().exec(arguments1);// 执行解压文件
//用输入输出流来截取结果
BufferedReader in1 = new BufferedReader(new InputStreamReader(proc1.getInputStream()));
String line2 = null;
String line22 = null;
while ((line2 = in1.readLine()) != null) {
System.out.println(line2);
line22 = line2;
}
in1.close();
int re1 = proc1.waitFor(); //waitFor是用来显示脚本是否运行成功1表示失败0表示成功还有其他的表示其他错误
if (re1 == 0) {
//成功 读取文件
Process proc2;
String[] arguments2 = new String[] {"/usr/bin/python3.6", "/root/code-scan/nicad/main.py", storeConfig.getFilePath() + time+"_functions-blind-clones/"+time+"_functions-blind-clones-0.30-classes-withsource.xml"};
proc2 = Runtime.getRuntime().exec(arguments2);// 执行解压文件
//用输入输出流来截取结果
BufferedReader in2 = new BufferedReader(new InputStreamReader(proc2.getInputStream()));
String line3 = null;
String line33 = null;
while ((line3 = in2.readLine()) != null) {
System.out.println(line3);
line33 = line3;
}
in2.close();
int re2 = proc2.waitFor();
if (re2 == 0) {
//转换为实体类
JSONObject jsonObject = JSONObject.parseObject(line33);
System.out.println(jsonObject);
NicadJson nicadJson = JSON.toJavaObject(jsonObject,NicadJson.class);
map.put("status",SUCCESS);
2022-07-20 18:49:49 +08:00
map.put("html","/api/files/"+time+"_functions-blind-clones/"+time+"_functions-blind-clones-0.30-classes-withsource.html");
map.put("json",nicadJson);
}else {
map.put("status",FAIL);
}
2022-07-19 19:34:18 +08:00
return map;
}else {
//失败
map.put("status",FAIL);
return map;
}
}else {
//失败
map.put("status",FAIL);
return map;
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
case "C":
case "C#":
case "Ruby":
case "JavaScript":
default:
map.put("status",FAIL);
map.put("msg","您选择的语言暂不支持检测!或遇到预期以外的错误");
return map;
}
}
}