View Javadoc
1   package edu.jiangxin.apktoolbox.file.password.recovery.checker.thirdparty;
2   
3   import edu.jiangxin.apktoolbox.file.password.recovery.checker.FileChecker;
4   import edu.jiangxin.apktoolbox.utils.NoLogOutputStream;
5   import org.apache.commons.exec.CommandLine;
6   import org.apache.commons.exec.DefaultExecutor;
7   import org.apache.commons.exec.PumpStreamHandler;
8   
9   import java.io.IOException;
10  
11  public abstract class AbstractThirdPartyChecker extends FileChecker {
12      protected static final boolean DEBUG = false;
13  
14      protected String toolPath;
15  
16      @Override
17      public boolean prepareChecker() {
18          toolPath = getToolPath();
19          if (toolPath == null) {
20              logger.error("7z.exe not found");
21              return false;
22          }
23          try {
24              ProcessBuilder builder = new ProcessBuilder(toolPath.split(" "));
25              Process process = builder.start();
26          } catch (IOException e) {
27              return false;
28          }
29          return true;
30      }
31  
32      @Override
33      public boolean checkPassword(String password) {
34          if (isFiltered(password)) {
35              logger.warn("password is filtered.");
36              return false;
37          }
38  
39          if (password != null && password.contains("\"")) {
40              // It is useless to escape the password
41              logger.warn("checkPassword password contain double quote characters[Not Supported]");
42              return false;
43          }
44          String cmd = getCmd(password);
45          logger.debug("checkPassword cmd: {}", cmd);
46          boolean result = false;
47          try (NoLogOutputStream outStream = new NoLogOutputStream();
48               NoLogOutputStream errStream = new NoLogOutputStream()
49          ) {
50              CommandLine commandLine = CommandLine.parse(cmd);
51              DefaultExecutor exec = DefaultExecutor.builder().get();
52              PumpStreamHandler streamHandler = new PumpStreamHandler(outStream, errStream);
53              exec.setStreamHandler(streamHandler);
54              int exitValue = exec.execute(commandLine);
55              result = (exitValue == 0);
56          } catch (IOException e) {
57              if (DEBUG) {
58                  logger.error("checkPassword: IOException");
59              }
60          }
61          return result;
62      }
63  
64      public abstract String getToolPath();
65  
66      public abstract boolean isFiltered(String password);
67  
68      public abstract String getCmd(String password);
69  }