Browse Source

印章解析更新

JustryDeng 3 năm trước cách đây
mục cha
commit
b16b3fca4c

+ 50 - 26
class-winter-core/src/main/java/winter/com/ideaaedi/classwinter/Reverses.java

@@ -141,37 +141,16 @@ public class Reverses {
                             return classfileBuffer;
                         }
                     }
-                    if (decryptProjectPathPrefixSet.size() > 0) {
+                    boolean settingDecryptProjectPath = decryptProjectPathPrefixSet.size() > 0;
+                    if (settingDecryptProjectPath) {
                         if (notPointToDecrypt(projectPath, decryptProjectPathPrefixSet)) {
                             return classfileBuffer;
                         }
                     }
                     className = className.replace("/", ".").replace("\\", ".");
-                    // 本项目的印章
-                    if (!Cache.sealCache.containsKey(projectPath)) {
-                        try {
-                            byte[] sealByte = IOUtil.readFileFromWorkbenchRoot(new File(projectPath), Constant.SEAL_FILE);
-                            if (sealByte == null) {
-                                Logger.error(Reverses.class, "Obtain project seal fail. "
-                                        + "Maybe you need to try it with decrypt params 'decryptProjectPathPrefix' or 'skipProjectPathPrefix'. "
-                                        + "@see https://gitee.com/JustryDeng/class-winter#%E8%A7%A3%E5%AF%86%E5%8F%82%E6%95%B0 ");
-                                // 结束程序
-                                exit(projectPath);
-                            }
-                            String sealContent = new String(sealByte, StandardCharsets.UTF_8);
-                            Logger.debug(Reverses.class, "seal of the project is -> " + sealContent);
-                            Cache.sealCache.put(projectPath, sealContent);
-                        } catch (Exception e) {
-                            Logger.error(Reverses.class, "Obtain project seal fail.");
-                            Logger.error(Reverses.class, ExceptionUtil.getStackTraceMessage(e));
-                            exit(projectPath);
-                            for (int i = 0; i < Constant.TEN; i++) {
-                                Logger.error(Reverses.class, "!!!!!!!!!!! class-winter Invalidation. !!!!!!!!!!!");
-                            }
-                            return null;
-                        }
-                    }
-                    
+                    // 抽取印章
+                    extractSeal(projectPath, settingDecryptProjectPath);
+    
                     /// ============================================ 1.校验启动时是否输入了加密时指定的jvm参数   2.处理non-class文件
                     final String inputPwd = javaagentCmdArgs.getPassword();
                     if (firstExec.compareAndSet(false, true)) {
@@ -303,6 +282,51 @@ public class Reverses {
         }
     }
     
+    /**
+     * 抽取项目印章
+     *
+     * @param settingDecryptProjectPath 是否指定了项目解密路径前缀
+     */
+    private static void extractSeal(String projectPath, boolean settingDecryptProjectPath) {
+        if (Cache.sealCache.containsKey(projectPath)) {
+            return;
+        }
+        try {
+            byte[] sealByte = IOUtil.readFileFromWorkbenchRoot(new File(projectPath), Constant.SEAL_FILE);
+            // 如果指定了解密路径,但是没有获取到印章的话,直接报错
+            if (settingDecryptProjectPath) {
+                if (sealByte == null) {
+                    Logger.error(Reverses.class, "Obtain project seal fail. "
+                            + "Cannot found seal at " + new File(projectPath, Constant.SEAL_FILE).getAbsolutePath());
+                    // 结束程序
+                    exit(projectPath);
+                }
+                String sealContent = new String(sealByte, StandardCharsets.UTF_8);
+                Logger.debug(Reverses.class, "seal of the project is -> " + sealContent);
+                Cache.sealCache.put(projectPath, sealContent);
+            } else if (Cache.firstSealCache != null) {
+                // 如果在没有指定解密路径的情况下,没有获取到印章的话,那么获取第一个印章作为本projectPath的印章
+                Logger.debug(Reverses.class, "direct-seal of the project obtain fail. use firstSealCache as this project seal. "
+                        + "firstSealProjectPath -> '" + Cache.firstSealProjectPath + "'. firstSealCache -> '" + Cache.firstSealCache + "'.");
+                Cache.sealCache.put(projectPath, Cache.firstSealCache);
+            } else {
+                if (sealByte == null) {
+                    Logger.error(Reverses.class, "Obtain project seal fail. "
+                            + "Cannot found seal at " + new File(projectPath, Constant.SEAL_FILE).getAbsolutePath());
+                    // 结束程序
+                    exit(projectPath);
+                }
+            }
+        } catch (Exception e) {
+            Logger.error(Reverses.class, "Obtain project seal fail.");
+            Logger.error(Reverses.class, ExceptionUtil.getStackTraceMessage(e));
+            exit(projectPath);
+            for (int i = 0; i < Constant.TEN; i++) {
+                Logger.error(Reverses.class, "!!!!!!!!!!! class-winter Invalidation. !!!!!!!!!!!");
+            }
+        }
+    }
+    
     /**
      * 是否不需要试着解密projectPath指向的代码
      */

+ 6 - 0
class-winter-core/src/main/java/winter/com/ideaaedi/classwinter/util/Cache.java

@@ -29,6 +29,12 @@ public final class Cache {
     /** 本项目加密时的印章 (key-projectPath; value-印章) */
     public static volatile Map<String, String>  sealCache =  new ConcurrentHashMap<>(8);
     
+    /** 本项目获取到的第一个印章 */
+    public static volatile String  firstSealCache;
+    
+    /** 本项目获取到的第一个印章的项目路径 */
+    public static volatile String  firstSealProjectPath;
+    
     /**
      * 以key-value的形式记录lib的印章。(key-存放采集到的类所在lib的信息的文件夹, value-印章字符串)
      *

BIN
class-winter-core/src/test/resources/class-winter-core-2.8.0.jar