Browse Source

优化提交

zsy 4 tháng trước cách đây
mục cha
commit
81d1d6e9e9

+ 2 - 0
build.gradle

@@ -101,6 +101,8 @@ dependencies {
 
 	implementation group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2'
 
+	implementation "io.github.fanyong920:jvppeteer:1.1.5"
+
 
 
 

+ 5 - 10
src/main/java/com/rf/fileCrack/config/StaticScheduleTask.java

@@ -20,9 +20,9 @@ import java.util.Date;
 import java.util.List;
 
 /**
- * @Author:zzf
- * @Date:2022/7/1:16:30
- * @Description:测试计划相关的定时任务信息
+ * @Author:zsy
+ * @Date:2024/12/11 16:16:30
+ * @Description:文件解密相关的定时任务信息
  */
 @Configuration
 @EnableScheduling
@@ -39,12 +39,7 @@ public class StaticScheduleTask {
     public void updateFileStatus() throws IOException {
         List<FileEntity> fileEntitys = this.fileService.findByFileStatus("1");
         for (FileEntity fileEntity : fileEntitys){
-            JSONObject jsonObject = new JSONObject();
-            jsonObject.put("fileId",fileEntity.getId());
-            jsonObject.put("filePath",fileEntity.getFilePath());
-            WebSocketServer.sendInfo(jsonObject.toJSONString());
-            fileEntity.setFileStatus("2");
-            this.fileService.save(fileEntity);
+            WebSocketServer.sendInfo(fileEntity.getId());
         }
         fileEntitys = this.fileService.findByFileStatus("3");
         for (FileEntity fileEntity : fileEntitys){
@@ -55,7 +50,7 @@ public class StaticScheduleTask {
                 //破解密码
                 sendEmail.setPassword(fileEntity.getPassword());
                 //破解的文件名
-                String[] fileNames = fileEntity.getFilePath().split("-");
+                String[] fileNames = fileEntity.getFilePath().split("-=-");
                 String fileName = fileNames[fileNames.length-1];
                 sendEmail.setFileName(fileName);
                 //设置要发送的邮箱

+ 102 - 14
src/main/java/com/rf/fileCrack/file/rest/FileController.java

@@ -6,22 +6,23 @@ import com.rf.fileCrack.base.rest.BaseController;
 import com.rf.fileCrack.file.dao.model.FileEntity;
 import com.rf.fileCrack.file.service.FileService;
 import com.rf.fileCrack.socket.WebSocketServer;
+import com.rf.fileCrack.utils.FileUtil;
 import com.rf.fileCrack.utils.Result;
 import com.rf.fileCrack.utils.SendEmail;
 import io.swagger.annotations.ApiOperation;
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.MediaType;
 import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import javax.websocket.Session;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.*;
@@ -52,11 +53,11 @@ public class FileController extends BaseController {
         String fileName = "";
         String FILEDIR = "./uploadFile/";
         if (file == null) {
-            throw new Exception("文件为空");
+            return fail("文件为空");
         } else {
             FileEntity fileEntity = new FileEntity();
             if (!file.isEmpty()) {
-                fileName = System.currentTimeMillis()+"-"+file.getOriginalFilename();
+                fileName = System.currentTimeMillis()+"-=-"+file.getOriginalFilename();
                 try {
                     File temp = new File(FILEDIR);
                     if (!temp.exists()) {
@@ -69,20 +70,107 @@ public class FileController extends BaseController {
                     fileEntity.setCallWord(callWord);
                     fileEntity.setCreateTime(DateUtil.now());
                     fileEntity.setUpdateTime(DateUtil.now());
+                    fileEntity.setFileStatus("1");
                     fileEntity = this.fileService.save(fileEntity);
                 } catch (Exception e) {
                     e.printStackTrace();
-                    throw new Exception("文件上传失败");
+                    return fail("文件上传失败");
+                }
+                try {
+                    WebSocketServer.sendInfo(fileEntity.getId());
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    //return fail("消息推送失败");
                 }
-                JSONObject jsonObject = new JSONObject();
-                jsonObject.put("fileId",fileEntity.getId());
-                jsonObject.put("filePath",fileEntity.getFilePath());
-                WebSocketServer.sendInfo(jsonObject.toJSONString());
-                fileEntity.setFileStatus("2");
-                this.fileService.save(fileEntity);
             }
         }
-        return success(FILEDIR+fileName);
+        return success(null,"上传成功");
+    }
+
+    @ApiOperation(value = "文件下载")
+    @GetMapping("/fileDownload")
+    @ResponseBody
+    public Result download(HttpServletResponse response, @RequestParam String fileId ) throws Exception  {
+        FileEntity fileEntity = null;
+        String fileName  = "";
+        try {
+            fileEntity= this.fileService.findById(fileId);
+            String filePath = fileEntity.getFilePath();
+            File fileReturn = new File(filePath);
+            fileName = fileReturn.getName();
+            FileUtil.downloadFile(response,fileReturn,fileName,false);
+        }catch (Exception e) {
+            e.printStackTrace();
+            return fail("下载失败");
+        }
+        return success(null,"下载成功");
+    }
+
+    /**
+     * 修改文件状态
+     */
+    @ApiOperation(value = "修改文件状态",notes = "参数包括:fileId:唯一标识,fileStatus:文件状态码 2--开始解密;3--解密完成。示例:{\"fileId\":\"8af1788593b4d21b0193b4d2281a0000\",\"fileStatus\":\"2\"}")
+    @PostMapping("/updateFileStatus")
+    @ResponseBody
+    public Result updateFileStatus(@RequestBody String jsonParam) {
+        JSONObject jsonObject = JSONObject.parseObject(jsonParam);
+        if (!jsonObject.containsKey("fileId") || StringUtils.isEmpty(jsonObject.getString("fileId")))
+            return failBadRequest(null, "用户id不能为空!");
+        if (!jsonObject.containsKey("fileStatus") || StringUtils.isEmpty(jsonObject.getString("fileStatus")))
+            return failBadRequest(null, "文件状态码不能为空!");
+        String fileId = jsonObject.getString("fileId");
+        FileEntity fileEntity = this.fileService.findById(fileId);
+        if (fileEntity == null){
+            return fail("用户id错误");
+        }
+        String fileStatus = jsonObject.getString("fileStatus");
+        fileEntity.setFileStatus(fileStatus);
+        fileEntity.setUpdateTime(DateUtil.now());
+        this.fileService.save(fileEntity);
+        return success(null,"修改成功");
+    }
+
+    /**
+     * 修改密码
+     */
+    @ApiOperation(value = "修改密码",notes = "参数包括:fileId:唯一标识,password:密码。示例:{\"fileId\":\"8af1788593b4d21b0193b4d2281a0000\",\"password\":\"123456\"}")
+    @PostMapping("/updatePassword")
+    @ResponseBody
+    public Result updatePassword(@RequestBody String jsonParam) {
+        JSONObject jsonObject = JSONObject.parseObject(jsonParam);
+        if (!jsonObject.containsKey("fileId") || StringUtils.isEmpty(jsonObject.getString("fileId")))
+            return failBadRequest(null, "用户id不能为空!");
+        if (!jsonObject.containsKey("password") || StringUtils.isEmpty(jsonObject.getString("password")))
+            return failBadRequest(null, "新密码不能为空!");
+        String password = jsonObject.getString("password");
+        String fileId = jsonObject.getString("fileId");
+        FileEntity fileEntity = this.fileService.findById(fileId);
+        //根据邮箱查询用户信息
+        String email = fileEntity.getEmail();
+        if (email != null) {
+            SendEmail sendEmail = new SendEmail();
+            //破解密码
+            sendEmail.setPassword(password);
+            //破解的文件名
+            String[] fileNames = fileEntity.getFilePath().split("-=-");
+            String fileName = fileNames[fileNames.length - 1];
+            sendEmail.setFileName(fileName);
+
+            //设置要发送的邮箱
+            sendEmail.setReceiveMailAccount(email);
+            try {
+                sendEmail.Send();
+                fileEntity.setFileStatus("4");
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        } else {
+            fileEntity.setFileStatus("3");
+        }
+        fileEntity.setPassword(password);
+        fileEntity.setUpdateTime(DateUtil.now());
+        this.fileService.save(fileEntity);
+        return success(null,"修改成功");
     }
 
 }

+ 7 - 5
src/main/java/com/rf/fileCrack/socket/WebSocketServer.java

@@ -123,7 +123,9 @@ public class WebSocketServer {
             System.out.println(entry.getKey()+"----"+entry.getValue());
         }
         log.info("用户消息:" + userId + ",报文:" + message);
-        if (!StringUtils.isEmpty(message)) {
+        //sendMessage("发送成功");
+        //保存解密后密码并发邮箱通知
+        /*if (!StringUtils.isEmpty(message)) {
             JSONObject jsonObject = JSONObject.parseObject(message);
             String fileId = null;
             String password = null;
@@ -142,7 +144,7 @@ public class WebSocketServer {
                 respJson.put("msg","发送失败,文件ID或密码为空!");
             }
             sendMessage(respJson.toJSONString());
-        }
+        }*/
     }
 
     /**
@@ -166,7 +168,7 @@ public class WebSocketServer {
 
     public static void sendInfo(String message) throws IOException {
         log.info("消息内容:" + message);
-        webSocketMap.get("10001").sendMessage(message);
+        webSocketMap.get("10002").sendMessage(message);
     }
 
     public static synchronized AtomicInteger getOnlineCount() {
@@ -182,7 +184,7 @@ public class WebSocketServer {
     }
 
     //保存解密后密码并发邮箱通知
-    public static void savePassword(String fileId, String password) {
+    /*public static void savePassword(String fileId, String password) {
         FileEntity fileEntity = webSocketServer.fileService.findById(fileId);
         //根据邮箱查询用户信息
         String email = fileEntity.getEmail();
@@ -208,6 +210,6 @@ public class WebSocketServer {
         fileEntity.setPassword(password);
         fileEntity.setUpdateTime(DateUtil.now());
         webSocketServer.fileService.save(fileEntity);
-    }
+    }*/
 
 }

+ 149 - 0
src/main/java/com/rf/fileCrack/utils/FileUtil.java

@@ -0,0 +1,149 @@
+package com.rf.fileCrack.utils;
+
+import com.ruiyun.jvppeteer.core.Puppeteer;
+import com.ruiyun.jvppeteer.core.browser.Browser;
+import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
+import com.ruiyun.jvppeteer.core.page.Page;
+import com.ruiyun.jvppeteer.options.LaunchOptions;
+import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
+import com.ruiyun.jvppeteer.options.PDFOptions;
+import com.ruiyun.jvppeteer.options.PageNavigateOptions;
+import com.ruiyun.jvppeteer.protocol.DOM.Margin;
+import lombok.extern.slf4j.Slf4j;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+
+/**
+ * @Author:zzf
+ * @Date:2022/6/18:11:40
+ * @Description: 文件工具类
+ */
+@Slf4j
+public class FileUtil {
+
+
+    /**
+     * html转pdf
+     * @param htmlFilePathList html文件路径
+     * @throws IOException
+     * @throws ExecutionException
+     * @throws InterruptedException
+     */
+    public static void html2pdf(List<String> htmlFilePathList) throws IOException, ExecutionException, InterruptedException {
+
+        //自动下载,第一次下载后不会再下载
+        BrowserFetcher.downloadIfNotExist(null);
+        ArrayList<String> arrayList = new ArrayList<>();
+        //生成pdf必须在无厘头模式下才能生效
+        LaunchOptions options = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(true).build();
+        if(OSUtil.isLinux()){
+            options.setExecutablePath(".local-browser/linux-722234/chrome-linux/chrome");
+        }else{
+            options.setExecutablePath(".local-browser/win64-722234/chrome-win/chrome.exe");
+        }
+
+        PageNavigateOptions pageNavigateOptions = new PageNavigateOptions();
+        pageNavigateOptions.setWaitUntil(Collections.singletonList("networkidle2"));
+        PDFOptions pdfOptions = new PDFOptions();
+        arrayList.add("--no-sandbox");
+        arrayList.add("--disable-setuid-sandbox");
+        Browser browser = Puppeteer.launch(options);
+        Page page = browser.newPage();
+        htmlFilePathList.forEach(item -> {
+            File h = new File(item);
+            if(!h.exists()){
+                return;
+            }else{
+                try{
+                    System.out.println("item------------"+item);
+                    System.out.println("h.getName()--"+h.getName());
+                    page.goTo("file:///"+h.getAbsolutePath(),pageNavigateOptions);
+                    pdfOptions.setPath("./h2p/export_pdf/PDFReport/"+h.getName().replace("html","pdf"));
+                    pdfOptions.setFormat("A4");
+                    pdfOptions.setPrintBackground(true);
+                    Margin margin = new Margin();
+                    margin.setTop("70");
+                    margin.setBottom("70");
+                    margin.setLeft("0");
+                    margin.setRight("0");
+                    pdfOptions.setMargin(margin);
+                    page.pdf(pdfOptions);
+                }catch (Exception e){
+                    log.error("转换出错:"+e.getMessage());
+                    e.printStackTrace();
+                }finally {
+                    //删除html文件
+                    FileUtil.deleteFile(h);
+                }
+            }
+        });
+        page.close();
+        browser.close();
+    }
+
+    /**
+     * 删除文件
+     * @param file
+     */
+    private static void deleteFile(File file)   {
+        if(file.exists())  file.delete();
+    }
+
+    /**
+     * 文件下载
+     * @param file 下载文件信息
+     * @param responseFileName 下载文件名称
+     * @param deleteFile 是否删除源文件
+     */
+    public static void downloadFile(HttpServletResponse response,File file,String responseFileName,boolean deleteFile) throws UnsupportedEncodingException {
+        response.reset();
+        response.setContentType("application/octet-stream");
+        response.setCharacterEncoding("utf-8");
+        response.setContentLength((int) file.length());
+        String fileName = URLEncoder.encode(responseFileName, "utf-8");
+        response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
+        byte[] buffer = new byte[1024];
+        FileInputStream fis = null;
+        BufferedInputStream bis = null;
+        try {
+            fis = new FileInputStream(file);
+            bis = new BufferedInputStream(fis);
+            OutputStream os = response.getOutputStream();
+            int i = bis.read(buffer);
+            while (i != -1) {
+                os.write(buffer, 0, i);
+                i = bis.read(buffer);
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        } finally {
+
+            if (bis != null) {
+                try {
+                    bis.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                    log.debug(e.getMessage());
+                }
+            }
+            if (fis != null) {
+                try {
+                    fis.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                    log.debug(e.getMessage());
+                }
+            }
+            if(deleteFile) deleteFile(file);
+        }
+    }
+
+
+}