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              Runtime.getRuntime().exec(toolPath);
25          } catch (IOException e) {
26              return false;
27          }
28          return true;
29      }
30  
31      @Override
32      public boolean checkPassword(String password) {
33          if (isFiltered(password)) {
34              logger.warn("password is filtered.");
35              return false;
36          }
37  
38          if (password != null && password.contains("\"")) {
39              // It is useless to escape the password
40              logger.warn("checkPassword password contain double quote characters[Not Supported]");
41              return false;
42          }
43          String cmd = getCmd(password);
44          logger.debug("checkPassword cmd: {}", cmd);
45          boolean result = false;
46          try (NoLogOutputStream outStream = new NoLogOutputStream();
47               NoLogOutputStream errStream = new NoLogOutputStream()
48          ) {
49              CommandLine commandLine = CommandLine.parse(cmd);
50              DefaultExecutor exec = DefaultExecutor.builder().get();
51              PumpStreamHandler streamHandler = new PumpStreamHandler(outStream, errStream);
52              exec.setStreamHandler(streamHandler);
53              int exitValue = exec.execute(commandLine);
54              result = (exitValue == 0);
55          } catch (IOException e) {
56              logger.error("checkPassword IOException");
57          }
58          return result;
59      }
60  
61      public abstract String getToolPath();
62  
63      public abstract boolean isFiltered(String password);
64  
65      public abstract String getCmd(String password);
66  }