2022-07-19 19:34:18 +08:00
|
|
|
package com.osredm.codescan.service.impl;
|
|
|
|
|
|
2022-07-20 12:06:55 +08:00
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
2022-07-19 19:34:18 +08:00
|
|
|
import com.osredm.codescan.config.StoreConfig;
|
2022-07-20 12:06:55 +08:00
|
|
|
import com.osredm.codescan.entity.Nicad.NicadClones;
|
|
|
|
|
import com.osredm.codescan.entity.Nicad.NicadInfo;
|
|
|
|
|
import com.osredm.codescan.entity.Nicad.NicadJson;
|
2022-07-21 10:58:14 +08:00
|
|
|
import com.osredm.codescan.entity.Nicad.NicadResult;
|
2022-07-19 19:34:18 +08:00
|
|
|
import com.osredm.codescan.service.NicadService;
|
|
|
|
|
import com.osredm.codescan.utils.FileUploadUtil;
|
2022-07-21 10:58:14 +08:00
|
|
|
import com.osredm.codescan.utils.ProcessUtil;
|
2022-07-19 19:34:18 +08:00
|
|
|
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":
|
2022-07-21 10:58:14 +08:00
|
|
|
return nicadScript(nicadInfo,map,"java");
|
2022-07-19 19:34:18 +08:00
|
|
|
case "C":
|
2022-07-21 10:58:14 +08:00
|
|
|
return nicadScript(nicadInfo,map,"c");
|
2022-07-19 19:34:18 +08:00
|
|
|
|
|
|
|
|
case "C#":
|
2022-07-21 17:48:14 +08:00
|
|
|
return nicadScript(nicadInfo,map,"cs");
|
2022-07-19 19:34:18 +08:00
|
|
|
|
|
|
|
|
case "Ruby":
|
2022-07-21 17:48:14 +08:00
|
|
|
return nicadScript(nicadInfo,map,"rb");
|
2022-07-19 19:34:18 +08:00
|
|
|
|
|
|
|
|
case "JavaScript":
|
2022-07-21 17:48:14 +08:00
|
|
|
return nicadScript(nicadInfo,map,"js");
|
2022-07-19 19:34:18 +08:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
map.put("status",FAIL);
|
|
|
|
|
map.put("msg","您选择的语言暂不支持检测!或遇到预期以外的错误");
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-21 10:58:14 +08:00
|
|
|
|
|
|
|
|
private HashMap<String,Object> nicadScript(NicadInfo nicadInfo,HashMap<String,Object> map,String language){
|
|
|
|
|
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};
|
|
|
|
|
|
|
|
|
|
NicadResult process = ProcessUtil.Process(arguments);
|
|
|
|
|
if (process.getRe() == 0) {
|
|
|
|
|
//成功
|
|
|
|
|
String[] arguments1 = new String[] {"nicad6" ,"functions",language, flag, "default-report"};
|
|
|
|
|
NicadResult process1 = ProcessUtil.Process(arguments1);
|
|
|
|
|
if (process1.getRe() == 0) {
|
|
|
|
|
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"};
|
|
|
|
|
NicadResult process2 = ProcessUtil.Process(arguments2);
|
|
|
|
|
if (process2.getRe() == 0) {
|
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(process2.getLine());
|
|
|
|
|
System.out.println(jsonObject);
|
|
|
|
|
NicadJson nicadJson = JSON.toJavaObject(jsonObject,NicadJson.class);
|
2022-07-21 17:48:14 +08:00
|
|
|
// String[] arguments3 = new String[] {"rm", "-rf", storeConfig.getFilePath() + time+"/"};
|
2022-07-21 10:58:14 +08:00
|
|
|
map.put("status",SUCCESS);
|
2022-07-21 17:48:14 +08:00
|
|
|
map.put("html","/api2/files/"+time+"_functions-blind-clones/"+time+"_functions-blind-clones-0.30-classes-withsource.html");
|
2022-07-21 10:58:14 +08:00
|
|
|
map.put("json",nicadJson);
|
|
|
|
|
return map;
|
|
|
|
|
}else {
|
|
|
|
|
//失败
|
|
|
|
|
map.put("status",FAIL);
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
//失败
|
|
|
|
|
map.put("status",FAIL);
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
//失败
|
|
|
|
|
map.put("status",FAIL);
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-19 19:34:18 +08:00
|
|
|
}
|