This commit is contained in:
even 2025-08-01 10:21:30 +08:00
commit c36b9c5713
12 changed files with 461 additions and 6 deletions

View File

@ -49,6 +49,15 @@ public class AflManagerController {
} }
@PostMapping(path="/testSaveAflInfo")
public CommonResult<String> testSaveAflInfo(@RequestBody @Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException {
String groupIdentifier = aflInfoService.testSaveAflInfo(req);
return CommonResult.success(groupIdentifier);
}
@PostMapping(path="/findAflInfoNow") @PostMapping(path="/findAflInfoNow")
public CommonResult<AflInfoResp> findAflInfo(@RequestBody @Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException { public CommonResult<AflInfoResp> findAflInfo(@RequestBody @Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException {
@ -68,7 +77,7 @@ public class AflManagerController {
@PostMapping(path="/saveAflCrashesInfo") @PostMapping(path="/saveAflCrashesInfo")
public CommonResult<String> saveAflCrashesInfo(@RequestBody @Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException { public CommonResult<String> saveAflCrashesInfo(@RequestBody @Valid AflManagerReq req){
String groupIdentifier = aflCrashesInfoService.saveAflCrashesInfo(req); String groupIdentifier = aflCrashesInfoService.saveAflCrashesInfo(req);
@ -76,8 +85,17 @@ public class AflManagerController {
} }
@PostMapping(path="/testSaveAflCrashesInfo")
public CommonResult<String> testSaveAflCrashesInfo(@RequestBody @Valid AflManagerReq req){
String groupIdentifier = aflCrashesInfoService.testSaveAflCrashesInfo(req);
return CommonResult.success(groupIdentifier);
}
@PostMapping(path="/findCrashesCount") @PostMapping(path="/findCrashesCount")
public CommonResult<AflCrashesResp> findCrashesCount(@RequestBody @Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException { public CommonResult<AflCrashesResp> findCrashesCount(@RequestBody @Valid AflManagerReq req){
AflCrashesResp resp = aflCrashesInfoService.findCrashesCount(req); AflCrashesResp resp = aflCrashesInfoService.findCrashesCount(req);
@ -86,7 +104,7 @@ public class AflManagerController {
@PostMapping(path="/findCrashesNow") @PostMapping(path="/findCrashesNow")
public CommonResult<List<AflCrashesInfoResp>> findCrashesNow(@RequestBody @Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException { public CommonResult<List<AflCrashesInfoResp>> findCrashesNow(@RequestBody @Valid AflManagerReq req){
List<AflCrashesInfoResp> respList = aflCrashesInfoService.findCrashesNow(req); List<AflCrashesInfoResp> respList = aflCrashesInfoService.findCrashesNow(req);
@ -113,7 +131,7 @@ public class AflManagerController {
@PostMapping(path="/saveAflSeedInfo") @PostMapping(path="/saveAflSeedInfo")
public CommonResult<Void> saveAflSeedInfo(@RequestBody @Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException { public CommonResult<Void> saveAflSeedInfo(@RequestBody @Valid AflManagerReq req){
aflSeedInfoService.saveAflSeedInfo(req); aflSeedInfoService.saveAflSeedInfo(req);
@ -122,6 +140,16 @@ public class AflManagerController {
@PostMapping(path="/testSaveAflSeedInfo")
public CommonResult<Void> testSaveAflSeedInfo(@RequestBody @Valid AflManagerReq req){
aflSeedInfoService.testSaveAflSeedInfo(req);
return CommonResult.success();
}
@PostMapping(path="/saveAflPlotInfo") @PostMapping(path="/saveAflPlotInfo")
public CommonResult<String> saveAflPlotInfo(@RequestBody @Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException { public CommonResult<String> saveAflPlotInfo(@RequestBody @Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException {
@ -131,6 +159,16 @@ public class AflManagerController {
} }
@PostMapping(path="/testSaveAflPlotInfo")
public CommonResult<String> testSaveAflPlotInfo(@RequestBody @Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException {
String groupIdentifier = aflPlotInfoService.testSaveAflPlotInfo(req);
return CommonResult.success(groupIdentifier);
}
@PostMapping(path="/findAflPlotInfoNow") @PostMapping(path="/findAflPlotInfoNow")
public CommonResult<AflPlotInfoResp> findAflPlotInfoNow(@RequestBody @Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException { public CommonResult<AflPlotInfoResp> findAflPlotInfoNow(@RequestBody @Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException {

View File

@ -5,7 +5,6 @@ import cd.casic.ci.process.dto.req.localAflCrashes.LocalAflCrashesQueryReq;
import cd.casic.ci.process.dto.req.localAflCrashes.LocalAflCrashesSaveReq; import cd.casic.ci.process.dto.req.localAflCrashes.LocalAflCrashesSaveReq;
import cd.casic.ci.process.dto.resp.localAflCrashes.LocalAflCrashesResp; import cd.casic.ci.process.dto.resp.localAflCrashes.LocalAflCrashesResp;
import cd.casic.ci.process.process.service.localAflCrashes.LocalAflCrashesService; import cd.casic.ci.process.process.service.localAflCrashes.LocalAflCrashesService;
import cd.casic.ci.process.util.SftpUploadUtil;
import cd.casic.framework.commons.pojo.CommonResult; import cd.casic.framework.commons.pojo.CommonResult;
import cd.casic.framework.commons.pojo.PageResult; import cd.casic.framework.commons.pojo.PageResult;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -22,13 +21,22 @@ public class LocalAflCrashesController {
private LocalAflCrashesService localAflCrashesService; private LocalAflCrashesService localAflCrashesService;
@PostMapping(path="/saveLocalAflCrashesInfo") @PostMapping(path="/saveLocalAflCrashesInfo")
public CommonResult<Void> saveLocalAflCrashesInfo(@RequestBody @Valid LocalAflCrashesSaveReq req) throws SftpUploadUtil.SftpUploadException { public CommonResult<Void> saveLocalAflCrashesInfo(@RequestBody @Valid LocalAflCrashesSaveReq req){
localAflCrashesService.saveLocalAflCrashesInfo(req); localAflCrashesService.saveLocalAflCrashesInfo(req);
return CommonResult.success(); return CommonResult.success();
} }
@PostMapping(path="/testSaveLocalAflCrashesInfo")
public CommonResult<Void> testSaveLocalAflCrashesInfo(@RequestBody @Valid LocalAflCrashesSaveReq req){
localAflCrashesService.testSaveLocalAflCrashesInfo(req);
return CommonResult.success();
}
/** /**
*分页模糊查询漏洞信息 *分页模糊查询漏洞信息
*/ */

View File

@ -28,4 +28,6 @@ public interface AflCrashesInfoService extends IService<AflCrashesInfo> {
AflCrashesResp findCrashesCount(@Valid AflManagerReq req); AflCrashesResp findCrashesCount(@Valid AflManagerReq req);
List<AflCrashesInfoResp> findCrashesNow(@Valid AflManagerReq req); List<AflCrashesInfoResp> findCrashesNow(@Valid AflManagerReq req);
String testSaveAflCrashesInfo(@Valid AflManagerReq req);
} }

View File

@ -28,4 +28,6 @@ public interface AflInfoService extends IService<AflInfo> {
List<AflInfoResp> findAflInfoListByHistory(@Valid AflManagerReq req); List<AflInfoResp> findAflInfoListByHistory(@Valid AflManagerReq req);
AflBaseInfoResp findAflBaseInfoList(@Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException; AflBaseInfoResp findAflBaseInfoList(@Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException;
String testSaveAflInfo(@Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException;
} }

View File

@ -25,4 +25,6 @@ public interface AflPlotInfoService extends IService<AflPlotInfo> {
List<AflPlotInfoResp> findAflPlotInfoListByHistory(@Valid AflManagerReq req); List<AflPlotInfoResp> findAflPlotInfoListByHistory(@Valid AflManagerReq req);
void updateHistoryPipelineIdByAflPlotInfo(@Valid AflManagerReq req); void updateHistoryPipelineIdByAflPlotInfo(@Valid AflManagerReq req);
String testSaveAflPlotInfo(@Valid AflManagerReq req) throws SftpUploadUtil.SftpUploadException;
} }

View File

@ -24,4 +24,5 @@ public interface AflSeedInfoService extends IService<AflSeedInfo> {
void updateHistoryPipelineIdByAflSeedInfo(@Valid AflManagerReq req); void updateHistoryPipelineIdByAflSeedInfo(@Valid AflManagerReq req);
void testSaveAflSeedInfo(@Valid AflManagerReq req);
} }

View File

@ -361,4 +361,124 @@ public class AflCrashesInfoServiceImpl extends ServiceImpl<AflCrashesInfoDao, Af
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"crashes入库失败"); throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"crashes入库失败");
} }
} }
@Override
public String testSaveAflCrashesInfo(AflManagerReq req) {
List<AflCrashesInfo> aflCrashesInfos = new ArrayList<>(0);
if (StringUtils.isEmpty(req.getPipelineId()) || StringUtils.isEmpty(req.getTaskId())){
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"pipelineId或者taskId不能为空");
}
String uuid = req.getGroupIdentifier();
ResourceDetailResp resourceById = resourceManagerService.findResourceDetailById("735516490182299648");
if (resourceById == null || resourceById.getMachineInfo() == null || resourceById.getMachineInfo().getAuthenticationType()!=1) {
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"资源信息错误");
}
MachineInfoDO resourceMachine = resourceById.getMachineInfo();
String password = CryptogramUtil.doDecrypt(resourceMachine.getPassword());
resourceMachine.setPassword(password);
try {
// 步骤1使用自定义sql获取崩溃文件的创建时间
//查询创建时间
List<String> resultList = SftpUploadUtil.findCreateTimeByCommand(
resourceMachine.getHostIp(),
resourceMachine.getSshPort(),
resourceMachine.getUsername(),
resourceMachine.getPassword(), null, remoteFilePath + "PIP_" + req.getPipelineId() + crashesFilePath);
if (CollectionUtils.isEmpty(resultList)){
return null;
}
//解析文件名和创建时间
for (String o : resultList) {
String[] lineParts = o.split("\\s+创建时间:\\s+");
if (lineParts.length < 2) {
System.err.println("无效格式: " + o);
break;
}
AflCrashesInfo aflCrashesInfo = new AflCrashesInfo();
String fileName = lineParts[0].substring(2).trim(); // 文件信息部分
String createTime = lineParts[1].trim(); // 创建时间
aflCrashesInfo.setCrashesName(fileName);
aflCrashesInfo.setCrashesCreateTime(createTime);
aflCrashesInfos.add(aflCrashesInfo);
}
// 步驟2.查询文件大小
List<String> fileResultList = SftpUploadUtil.findFileByteByCommand(
resourceMachine.getHostIp(),
resourceMachine.getSshPort(),
resourceMachine.getUsername(),
resourceMachine.getPassword(), null, remoteFilePath + "PIP_" + req.getPipelineId() + crashesFilePath);
if (CollectionUtils.isEmpty(fileResultList)){
return null;
}
//解析文件名和文件大小
for (String o : fileResultList) {
// 按空白字符分割
String[] parts = o.trim().split("\\s+");
String fileName = parts[0]; // 文件名称
Integer fileLength = Integer.parseInt(parts[1]); // 文件大小
if (!CollectionUtils.isEmpty(aflCrashesInfos)){
for (AflCrashesInfo aflCrashesInfo : aflCrashesInfos) {
if (aflCrashesInfo.getCrashesName().equals(fileName)) {
aflCrashesInfo.setCrashesLength(fileLength);
}
}
}
}
// 步骤3列出源目录下的所有文件
List<String> files = SftpUploadUtil.listFilesInRemoteDirectory(
resourceMachine.getHostIp(), resourceMachine.getSshPort(), resourceMachine.getUsername(), resourceMachine.getPassword(), null, remoteFilePath + "PIP_" + req.getPipelineId() + crashesFilePath);
if (!CollectionUtils.isEmpty(files)) {
// 步骤4批量复制文件到目标目录
Map<String,String> copiedFiles = SftpUploadUtil.copyRemoteFilesToLocalMap(
resourceMachine.getHostIp(), resourceMachine.getSshPort(), resourceMachine.getUsername(), resourceMachine.getPassword(), null, files, "/home/ops/opsFile/crashes_result/" + UUID.randomUUID() + "/");
System.out.println("共复制 " + copiedFiles.size() + " 个文件");
//绑定对应的文件路径
if (!CollectionUtils.isEmpty(copiedFiles)){
for (Map.Entry<String, String> entry : copiedFiles.entrySet()) {
if (!CollectionUtils.isEmpty(aflCrashesInfos)){
for (AflCrashesInfo aflCrashesInfo : aflCrashesInfos) {
if (aflCrashesInfo.getCrashesName().equals(entry.getKey())) {
aflCrashesInfo.setFilePath(entry.getValue());
}
aflCrashesInfo.setPipelineId(req.getPipelineId());
if (!StringUtils.isEmpty(req.getTaskId())){
aflCrashesInfo.setTaskId(req.getTaskId());
}
if (!StringUtils.isEmpty(req.getPipelineHistoryId())){
aflCrashesInfo.setPipelineHistoryId(req.getPipelineHistoryId());
}
aflCrashesInfo.setGroupIdentifier(uuid);
}
}
}
//新增数据到crashes表中
baseMapper.insertBatch(aflCrashesInfos);
}
return uuid;
}else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"crashes入库失败");
}
}
} }

View File

@ -228,4 +228,39 @@ public class AflInfoServiceImpl extends ServiceImpl<AflInfoDao, AflInfo> impleme
return resp; return resp;
} }
@Override
public String testSaveAflInfo(AflManagerReq req) throws SftpUploadUtil.SftpUploadException {
if (StringUtils.isEmpty(req.getPipelineId()) || StringUtils.isEmpty(req.getTaskId())){
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"pipelineId或者taskId不能为空");
}
ResourceDetailResp resourceById = resourceManagerService.findResourceDetailById("735516490182299648");
if (resourceById == null || resourceById.getMachineInfo() == null) {
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"资源信息错误");
}
MachineInfoDO resourceMachine = resourceById.getMachineInfo();
resourceMachine.setPassword(CryptogramUtil.doDecrypt(resourceMachine.getPassword()));
AflInfo aflInfo = SftpUploadUtil.downloadFileSftpForInputStreamAndSetAflInfo(resourceMachine.getHostIp(), resourceMachine.getSshPort(), resourceMachine.getUsername(), resourceMachine.getPassword(),null, remoteFilePath + "PIP_" + req.getPipelineId() + "/ai_afl/default/fuzzer_stats");
//在本地创建一个新文件夹将远程文件复制一份过去
List<String> sourceFilePaths = new ArrayList<>();
sourceFilePaths.add(remoteFilePath + "PIP_" + req.getPipelineId() + "/ai_afl/default/fuzzer_stats");
List<String> strings = SftpUploadUtil.copyRemoteFilesToLocal(resourceMachine.getHostIp(), resourceMachine.getSshPort(), resourceMachine.getUsername(), resourceMachine.getPassword(), null, sourceFilePaths, "/home/ops/opsFile/fuzzer_result/" + UUID.randomUUID() + "/");
aflInfo.setFilePath(strings.get(0));
aflInfo.setPipelineId(req.getPipelineId());
if (!StringUtils.isEmpty(req.getTaskId())){
aflInfo.setTaskId(req.getTaskId());
}
if (!StringUtils.isEmpty(req.getPipelineHistoryId())){
aflInfo.setPipelineHistoryId(req.getPipelineHistoryId());
}
aflInfo.setGroupIdentifier(req.getGroupIdentifier());
baseMapper.insert(aflInfo);
return aflInfo.getGroupIdentifier();
}
} }

View File

@ -160,4 +160,39 @@ public class AflPlotInfoServiceImpl extends ServiceImpl<AflPlotInfoDao, AflPlotI
return AflManagerConverter.INSTANCE.toAflPlotInfoRespList(aflPlotInfos); return AflManagerConverter.INSTANCE.toAflPlotInfoRespList(aflPlotInfos);
} }
@Override
public String testSaveAflPlotInfo(AflManagerReq req) throws SftpUploadUtil.SftpUploadException {
if (StringUtils.isEmpty(req.getPipelineId()) || StringUtils.isEmpty(req.getTaskId())){
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"pipelineId或者taskId不能为空");
}
ResourceDetailResp resourceById = resourceManagerService.findResourceDetailById("735516490182299648");
if (resourceById == null || resourceById.getMachineInfo() == null) {
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"资源信息错误");
}
MachineInfoDO resourceMachine = resourceById.getMachineInfo();
resourceMachine.setPassword(CryptogramUtil.doDecrypt(resourceMachine.getPassword()));
AflPlotInfo aflPlotInfo = SftpUploadUtil.downloadFileSftpForLastLineAndSetAflPlotInfo(resourceMachine.getHostIp(), resourceMachine.getSshPort(), resourceMachine.getUsername(), resourceMachine.getPassword(),null, remoteFilePath + "PIP_" + req.getPipelineId() + "/ai_afl/default/plot_data");
//在本地创建一个新文件夹将远程文件复制一份过去
List<String> sourceFilePaths = new ArrayList<>();
sourceFilePaths.add(remoteFilePath + "PIP_" + req.getPipelineId() + "/ai_afl/default/plot_data");
List<String> strings = SftpUploadUtil.copyRemoteFilesToLocal(resourceMachine.getHostIp(), resourceMachine.getSshPort(), resourceMachine.getUsername(), resourceMachine.getPassword(), null, sourceFilePaths, "/home/ops/opsFile/plot_result/" + UUID.randomUUID() + "/");
aflPlotInfo.setFilePath(strings.get(0));
aflPlotInfo.setPipelineId(req.getPipelineId());
if (!StringUtils.isEmpty(req.getTaskId())){
aflPlotInfo.setTaskId(req.getTaskId());
}
if (!StringUtils.isEmpty(req.getPipelineHistoryId())){
aflPlotInfo.setPipelineHistoryId(req.getPipelineHistoryId());
}
aflPlotInfo.setGroupIdentifier(req.getGroupIdentifier());
baseMapper.insert(aflPlotInfo);
return aflPlotInfo.getGroupIdentifier();
}
} }

View File

@ -286,4 +286,96 @@ public class AflSeedInfoServiceImpl extends ServiceImpl<AflSeedInfoDao, AflSeedI
} }
baseMapper.updateBatch(aflSeedInfos); baseMapper.updateBatch(aflSeedInfos);
} }
@Override
public void testSaveAflSeedInfo(AflManagerReq req) {
if (StringUtils.isEmpty(req.getPipelineId()) || StringUtils.isEmpty(req.getTaskId())){
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"pipelineId或者taskId不能为空");
}
String uuid = req.getGroupIdentifier();
ResourceDetailResp resourceById = resourceManagerService.findResourceDetailById("735516490182299648");
if (resourceById == null || resourceById.getMachineInfo() == null) {
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"资源信息错误");
}
MachineInfoDO resourceMachine = resourceById.getMachineInfo();
String password = CryptogramUtil.doDecrypt(resourceMachine.getPassword());
resourceMachine.setPassword(password);
try {
// 步骤1列出源目录下的所有文件
List<String> files = SftpUploadUtil.listFilesInRemoteDirectory(
resourceMachine.getHostIp(), resourceMachine.getSshPort(), resourceMachine.getUsername(), resourceMachine.getPassword(), null, remoteFilePath + "PIP_" + req.getPipelineId() + seedFilePath);
if (!CollectionUtils.isEmpty(files)) {
// 步骤2批量复制文件到目标目录
List<String> copiedFiles = SftpUploadUtil.copyRemoteFilesToLocal(
resourceMachine.getHostIp(), resourceMachine.getSshPort(), resourceMachine.getUsername(), resourceMachine.getPassword(), null, files, "/home/ops/opsFile/seed_result/" + UUID.randomUUID() + "/");
System.out.println("共复制 " + copiedFiles.size() + " 个文件");
List<AflSeedInfo> aflSeedInfos = new ArrayList<>(0);
if (!CollectionUtils.isEmpty(copiedFiles)){
//新增数据到seed表中
copiedFiles.forEach(o->{
AflSeedInfo aflSeedInfo = new AflSeedInfo();
// 提取文件名部分seed_result/之后的内容
String fileNamePart = o.substring(o.indexOf("seed_result/") + 12);
//截取最后一个斜杠后的内容即文件信息部分
fileNamePart = fileNamePart.substring(fileNamePart.lastIndexOf("/") + 1);
// 使用正则匹配 id: 开头的内容
java.util.regex.Pattern patternId = java.util.regex.Pattern.compile("id:([^,]+)");
java.util.regex.Matcher matcherId = patternId.matcher(fileNamePart);
// 使用正则匹配 rep: 开头的内容
java.util.regex.Pattern patternRep = java.util.regex.Pattern.compile("rep:(\\d+)");
java.util.regex.Matcher matcherRep = patternRep.matcher(fileNamePart);
if (matcherRep.find()) {
String repValue = matcherRep.group(1);
aflSeedInfo.setSeedAmount(repValue);
}
if (matcherId.find()) {
String idValue = matcherId.group(1);
aflSeedInfo.setSeedId(idValue);
}
aflSeedInfo.setFilePath(o);
if (!StringUtils.isEmpty(req.getTaskId())){
aflSeedInfo.setTaskId(req.getTaskId());
}
if (!StringUtils.isEmpty(req.getPipelineHistoryId())){
aflSeedInfo.setPipelineHistoryId(req.getPipelineHistoryId());
}
aflSeedInfo.setPipelineId(req.getPipelineId());
aflSeedInfo.setGroupIdentifier(uuid);
aflSeedInfos.add(aflSeedInfo);
});
}
if (!CollectionUtils.isEmpty(aflSeedInfos)){
//查询种子运行分钟数同时入库
List<SeedsCountResp> seedsCount = this.findSeedsCount(req);
if (!CollectionUtils.isEmpty(seedsCount)){
aflSeedInfos.forEach(o->{
seedsCount.forEach(j->{
if (o.getSeedId().equals(j.getSeedId())){
o.setMinutesDifference(j.getMinutesDifference());
}
});
});
}
baseMapper.insertBatch(aflSeedInfos);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
} }

View File

@ -20,4 +20,6 @@ public interface LocalAflCrashesService extends IService<LocalAflCrashesInfo> {
void saveLocalAflCrashesInfo(@Valid LocalAflCrashesSaveReq req); void saveLocalAflCrashesInfo(@Valid LocalAflCrashesSaveReq req);
PageResult<LocalAflCrashesResp> getLocalAflCrashesSearch(LocalAflCrashesQueryReq req); PageResult<LocalAflCrashesResp> getLocalAflCrashesSearch(LocalAflCrashesQueryReq req);
void testSaveLocalAflCrashesInfo(@Valid LocalAflCrashesSaveReq req);
} }

View File

@ -211,4 +211,122 @@ public class LocalAflCrashesServiceImpl extends ServiceImpl<LocalAflCrashesDao,
} }
@Override
public void testSaveLocalAflCrashesInfo(LocalAflCrashesSaveReq req) {
List<LocalAflCrashesInfo> aflCrashesInfos = new ArrayList<>(0);
if (StringUtils.isEmpty(req.getPipelineId()) || StringUtils.isEmpty(req.getTaskId())){
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"pipelineId或者taskId不能为空");
}
String uuid = req.getGroupIdentifier();
ResourceDetailResp resourceById = resourceManagerService.findResourceDetailById("735516490182299648");
if (resourceById == null || resourceById.getMachineInfo() == null || resourceById.getMachineInfo().getAuthenticationType()!=1) {
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"资源信息错误");
}
MachineInfoDO resourceMachine = resourceById.getMachineInfo();
String password = CryptogramUtil.doDecrypt(resourceMachine.getPassword());
resourceMachine.setPassword(password);
try {
// 步骤1使用自定义sql获取崩溃文件的创建时间
//查询创建时间
List<String> resultList = SftpUploadUtil.findCreateTimeByCommand(
resourceMachine.getHostIp(),
resourceMachine.getSshPort(),
resourceMachine.getUsername(),
resourceMachine.getPassword(), null, remoteFilePath + "PIP_" + req.getPipelineId() + crashesFilePath);
if (!CollectionUtils.isEmpty(resultList)){
//解析文件名和创建时间
for (String o : resultList) {
String[] lineParts = o.split("\\s+创建时间:\\s+");
if (lineParts.length < 2) {
System.err.println("无效格式: " + o);
break;
}
LocalAflCrashesInfo aflCrashesInfo = new LocalAflCrashesInfo();
String fileName = lineParts[0].substring(2).trim(); // 文件信息部分
String createTime = lineParts[1].trim(); // 创建时间
aflCrashesInfo.setCrashesName(fileName);
aflCrashesInfo.setCrashesCreateTime(createTime);
aflCrashesInfos.add(aflCrashesInfo);
}
}
// 步驟2.查询文件大小
List<String> fileResultList = SftpUploadUtil.findFileByteByCommand(
resourceMachine.getHostIp(),
resourceMachine.getSshPort(),
resourceMachine.getUsername(),
resourceMachine.getPassword(), null, remoteFilePath + "PIP_" + req.getPipelineId() + crashesFilePath);
if (!CollectionUtils.isEmpty(fileResultList) && !CollectionUtils.isEmpty(aflCrashesInfos)){
//解析文件名和文件大小
for (String o : fileResultList) {
// 按空白字符分割
String[] parts = o.trim().split("\\s+");
String fileName = parts[0]; // 文件名称
Integer fileLength = Integer.parseInt(parts[1]); // 文件大小
for (LocalAflCrashesInfo aflCrashesInfo : aflCrashesInfos) {
if (aflCrashesInfo.getCrashesName().equals(fileName)) {
aflCrashesInfo.setCrashesLength(fileLength);
}
aflCrashesInfo.setPipelineId(req.getPipelineId());
if (!StringUtils.isEmpty(req.getTaskId())){
aflCrashesInfo.setTaskId(req.getTaskId());
}
aflCrashesInfo.setGroupIdentifier(uuid);
}
}
}
// 步骤3列出源目录下的所有文件
List<String> files = SftpUploadUtil.listFilesInRemoteDirectory(
resourceMachine.getHostIp(), resourceMachine.getSshPort(), resourceMachine.getUsername(), resourceMachine.getPassword(), null, remoteFilePath + "PIP_" + req.getPipelineId() + crashesFilePath);
if (!CollectionUtils.isEmpty(files)) {
// 步骤4批量下载文件流与文件绑定
Map<String,byte[]> copiedFiles = SftpUploadUtil.copyRemoteFilesToByteArray(
resourceMachine.getHostIp(), resourceMachine.getSshPort(), resourceMachine.getUsername(), resourceMachine.getPassword(), null, files);
System.out.println("共复制 " + copiedFiles.size() + " 个文件");
//绑定对应的文件流
if (!CollectionUtils.isEmpty(copiedFiles)){
for (Map.Entry<String, byte[]> entry : copiedFiles.entrySet()) {
if (!CollectionUtils.isEmpty(aflCrashesInfos)){
for (LocalAflCrashesInfo aflCrashesInfo : aflCrashesInfos) {
if (aflCrashesInfo.getCrashesName().equals(entry.getKey())) {
aflCrashesInfo.setFileContent(entry.getValue());
}
aflCrashesInfo.setPipelineId(req.getPipelineId());
if (!StringUtils.isEmpty(req.getTaskId())){
aflCrashesInfo.setTaskId(req.getTaskId());
}
aflCrashesInfo.setTaskType("AFL");
aflCrashesInfo.setInstanceId(req.getInstanceId());
aflCrashesInfo.setTargetType(req.getTargetType());
aflCrashesInfo.setTargetName(req.getTargetName());
aflCrashesInfo.setCity(req.getCity());
}
}
}
//新增数据到crashes表中
baseMapper.insertBatch(aflCrashesInfos);
}
}
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"crashes入库失败");
}
}
} }