From 30946a2f13b6039e77e058f3f5b62ba22e036868 Mon Sep 17 00:00:00 2001 From: liuyang Date: Fri, 28 Sep 2018 00:06:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=81=E7=A8=8B=E4=B8=B2=E8=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/duolebo/blyrobot/MainActivity.kt | 3 - .../java/com/duolebo/blyrobot/data/Task.kt | 38 +-- .../java/com/duolebo/blyrobot/util/AdbUtil.kt | 78 +++++-- .../com/duolebo/blyrobot/util/ShellUtils.java | 220 ------------------ 4 files changed, 84 insertions(+), 255 deletions(-) delete mode 100644 app/src/main/java/com/duolebo/blyrobot/util/ShellUtils.java diff --git a/app/src/main/java/com/duolebo/blyrobot/MainActivity.kt b/app/src/main/java/com/duolebo/blyrobot/MainActivity.kt index f5ef680..a80c50e 100644 --- a/app/src/main/java/com/duolebo/blyrobot/MainActivity.kt +++ b/app/src/main/java/com/duolebo/blyrobot/MainActivity.kt @@ -8,7 +8,6 @@ import android.util.Log import com.duolebo.blyrobot.service.BylRobotService import com.duolebo.blyrobot.util.AdbUtil import com.duolebo.blyrobot.util.Constants -import com.duolebo.blyrobot.util.ShellUtils import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { @@ -20,8 +19,6 @@ class MainActivity : AppCompatActivity() { Log.i(TAG, "onCreate....") setContentView(R.layout.activity_main) -// val result = ShellUtils.execCommand("killall", true) -// Log.i(TAG, result.errorMsg + " " + result.errorMsg) val hasRoot = AdbUtil.checkRootPermission() if (hasRoot) { Log.i(TAG, "rooted...") diff --git a/app/src/main/java/com/duolebo/blyrobot/data/Task.kt b/app/src/main/java/com/duolebo/blyrobot/data/Task.kt index f053f8b..cea510f 100644 --- a/app/src/main/java/com/duolebo/blyrobot/data/Task.kt +++ b/app/src/main/java/com/duolebo/blyrobot/data/Task.kt @@ -19,6 +19,7 @@ class Task { private lateinit var launcher: String private lateinit var imagePath: String private var launchDelay = 15 + private var captureDelay = 15 private lateinit var keyEvent: String private var context: Context @@ -69,14 +70,14 @@ class Task { } private fun capture() { - proc?.destroy() - Thread(Runnable { - AdbUtil.killTcpdump() - }).start() - Thread.sleep(3000) Thread(Runnable { proc = AdbUtil.tcpCapture("") }).start() + Thread.sleep(captureDelay * 1000L) + proc?.destroy() + AdbUtil.killTcpdump() + Thread.sleep(3000L) + parseCaptureFile("") } private fun report() { @@ -86,7 +87,7 @@ class Task { private fun parseCaptureFile(filterUrl: String) { val file = File("/sdcard/capture.txt") val lines = file.readLines() - val playUrlItems = JSONArray() + val playUrlItems = ArrayList() var partUrl = "" var timeStr = "" @@ -107,17 +108,23 @@ class Task { val start = it.indexOf(' ') + 1 val host = it.substring(start) val url = "http://$host$partUrl" - val item = JSONObject() - item.put(Constants.KEY_URL, url) - item.put(Constants.KEY_TIME, timeStr) - playUrlItems.put(item) + val item = PlayInfo() + item.url = url + item.time = timeStr + playUrlItems.add(item) partUrl = "" } } - for (i in 0..playUrlItems.length()) { - val jo = playUrlItems.get(i) as JSONObject - val url = jo.optString(Constants.KEY_URL) + for (i in 0..playUrlItems.size) { + val item = playUrlItems.get(i) + + if (filterUrl.isNullOrEmpty()) { + + } + else { + + } } } @@ -153,4 +160,9 @@ class Task { interface OnTaskListener { fun onComplete() } + + class PlayInfo { + var time = "" + var url = "" + } } \ No newline at end of file diff --git a/app/src/main/java/com/duolebo/blyrobot/util/AdbUtil.kt b/app/src/main/java/com/duolebo/blyrobot/util/AdbUtil.kt index 527ecb8..08f0d86 100644 --- a/app/src/main/java/com/duolebo/blyrobot/util/AdbUtil.kt +++ b/app/src/main/java/com/duolebo/blyrobot/util/AdbUtil.kt @@ -42,35 +42,38 @@ object AdbUtil { return proc } - fun exeCmdEcho(cmd: String, root: Boolean = false) { + fun exeCmdEcho(cmd: String, root: Boolean = false): CmdResult { val exeCommands = ArrayList() exeCommands.add(cmd) - exeCmdEcho(exeCommands, root) + return exeCmdEcho(exeCommands, root) } - fun exeCmdEcho(commands: ArrayList, root: Boolean = false): List? { + fun exeCmdEcho(commands: ArrayList, root: Boolean = false): CmdResult { Log.i(TAG, "exe cmd echo: $commands") val exeCommands = ArrayList() exeCommands.addAll(commands) - val results = sendCommands(exeCommands, root) - for (line in results!!) { - Log.i(TAG, "echo line: $line") + val result = sendCommands(exeCommands, root) + for (line in result.successLines) { + Log.i(TAG, "echo success line: $line") } - return results + for (line in result.errorLines) { + Log.i(TAG, "echo error line: $line") + } + + return result } - fun sendCommands(commands: ArrayList, root: Boolean = false): List? { - val results = ArrayList() + fun sendCommands(commands: ArrayList, root: Boolean = false): CmdResult { + val result = CmdResult(-1, "", "") var status = -1 if (commands.isEmpty()) { - return null + return result } Log.i(TAG, "exe commands : $commands") var proc: Process? = null var successBufReader: BufferedReader? = null var errorBufReader: BufferedReader? = null - var errorMsg: StringBuilder? = null var dos: DataOutputStream? = null try { @@ -93,7 +96,6 @@ object AdbUtil { status = proc.waitFor() - errorMsg = StringBuilder() successBufReader = BufferedReader(InputStreamReader(proc.inputStream)) errorBufReader = BufferedReader(InputStreamReader(proc.errorStream)) var lineStr: String? = null @@ -103,11 +105,10 @@ object AdbUtil { break lineStr = trimLine(lineStr) if (lineStr !in commands) - results.add(lineStr) + result.successLines.add(lineStr) Log.i(TAG, " success line echo : $lineStr") } while (true) - results.add("----------") do { lineStr = errorBufReader.readLine() @@ -115,7 +116,7 @@ object AdbUtil { break lineStr = trimLine(lineStr) if (lineStr !in commands) - results.add(lineStr) + result.errorLines.add(lineStr) Log.i(TAG, " error line echo : $lineStr") } while (true) @@ -134,8 +135,13 @@ object AdbUtil { proc?.destroy() } - Log.i(TAG, "error message:$errorMsg and status $status") - return results + Log.i(TAG, "error message:${result.errorLines} and status $status") + if (result.errorLines.size > 0 && result.successLines.size <= 0) + status = -1 + + result.status = status + + return result } private fun trimLine(line: String): String { @@ -266,8 +272,24 @@ object AdbUtil { } fun killTcpdump() { - exeCmdEcho("killall", true) -// exeCmdEcho("killall tcpdump", true) + val result = exeCmdEcho("killall", true) + + if (result.status != -1) + exeCmdEcho("killall tcpdump", true) + else { + val psResult = exeCmdEcho("ps | grep tcpdump", true) + for (line in psResult.successLines) { + val arr = line.split(' ') + val items = ArrayList() + arr.forEach { + if (!it.isNullOrEmpty()) + items.add(it) + } + if (items.isNotEmpty()) { + exeCmdEcho("kill " + items[1], true) + } + } + } } fun resetApp(packageName: String) { @@ -282,4 +304,22 @@ object AdbUtil { exeCmdEcho("am force-stop $packageName", true) } + class CmdResult { + + var status: Int = 0 + var successLines = ArrayList() + var errorLines = ArrayList() + + constructor(status: Int) { + this.status = status + } + + constructor(result: Int, successMsg: String, errorMsg: String) { + this.status = result + if (!successMsg.isNullOrEmpty()) + this.successLines.add(successMsg) + if (!errorMsg.isNullOrEmpty()) + this.errorLines.add(errorMsg) + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/duolebo/blyrobot/util/ShellUtils.java b/app/src/main/java/com/duolebo/blyrobot/util/ShellUtils.java deleted file mode 100644 index fbfb64a..0000000 --- a/app/src/main/java/com/duolebo/blyrobot/util/ShellUtils.java +++ /dev/null @@ -1,220 +0,0 @@ -package com.duolebo.blyrobot.util; - -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.List; - -/** - * ShellUtils - *
    - * Check root - *
  • {@link ShellUtils#checkRootPermission()}
  • - *
- *
    - * Execte command - *
  • {@link ShellUtils#execCommand(String, boolean)}
  • - *
  • {@link ShellUtils#execCommand(String, boolean, boolean)}
  • - *
  • {@link ShellUtils#execCommand(List, boolean)}
  • - *
  • {@link ShellUtils#execCommand(List, boolean, boolean)}
  • - *
  • {@link ShellUtils#execCommand(String[], boolean)}
  • - *
  • {@link ShellUtils#execCommand(String[], boolean, boolean)}
  • - *
- */ -public class ShellUtils { - - public static final String COMMAND_SU = "su"; - public static final String COMMAND_SH = "sh"; - public static final String COMMAND_EXIT = "exit\n"; - public static final String COMMAND_LINE_END = "\n"; - - private ShellUtils() { - throw new AssertionError(); - } - - /** - * check whether has root permission - * - * @return - */ - public static boolean checkRootPermission() { - return execCommand("echo root", true, false).result == 0; - } - - /** - * execute shell command, default return result msg - * - * @param command command - * @param isRoot whether need to run with root - * @return - * @see ShellUtils#execCommand(String[], boolean, boolean) - */ - public static CommandResult execCommand(String command, boolean isRoot) { - return execCommand(new String[] {command}, isRoot, true); - } - - /** - * execute shell commands, default return result msg - * - * @param commands command list - * @param isRoot whether need to run with root - * @return - * @see ShellUtils#execCommand(String[], boolean, boolean) - */ - public static CommandResult execCommand(List commands, boolean isRoot) { - return execCommand(commands == null ? null : commands.toArray(new String[] {}), isRoot, true); - } - - /** - * execute shell commands, default return result msg - * - * @param commands command array - * @param isRoot whether need to run with root - * @return - * @see ShellUtils#execCommand(String[], boolean, boolean) - */ - public static CommandResult execCommand(String[] commands, boolean isRoot) { - return execCommand(commands, isRoot, true); - } - - /** - * execute shell command - * - * @param command command - * @param isRoot whether need to run with root - * @param isNeedResultMsg whether need result msg - * @return - * @see ShellUtils#execCommand(String[], boolean, boolean) - */ - public static CommandResult execCommand(String command, boolean isRoot, boolean isNeedResultMsg) { - return execCommand(new String[] {command}, isRoot, isNeedResultMsg); - } - - /** - * execute shell commands - * - * @param commands command list - * @param isRoot whether need to run with root - * @param isNeedResultMsg whether need result msg - * @return - * @see ShellUtils#execCommand(String[], boolean, boolean) - */ - public static CommandResult execCommand(List commands, boolean isRoot, boolean isNeedResultMsg) { - return execCommand(commands == null ? null : commands.toArray(new String[] {}), isRoot, isNeedResultMsg); - } - - /** - * execute shell commands - * - * @param commands command array - * @param isRoot whether need to run with root - * @param isNeedResultMsg whether need result msg - * @return
    - *
  • if isNeedResultMsg is false, {@link CommandResult#successMsg} is null and - * {@link CommandResult#errorMsg} is null.
  • - *
  • if {@link CommandResult#result} is -1, there maybe some excepiton.
  • - *
- */ - public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) { - int result = -1; - if (commands == null || commands.length == 0) { - return new CommandResult(result, null, null); - } - - Process process = null; - BufferedReader successResult = null; - BufferedReader errorResult = null; - StringBuilder successMsg = null; - StringBuilder errorMsg = null; - - DataOutputStream os = null; - try { - process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH); - os = new DataOutputStream(process.getOutputStream()); - for (String command : commands) { - if (command == null) { - continue; - } - - // donnot use os.writeBytes(commmand), avoid chinese charset error - os.write(command.getBytes()); - os.writeBytes(COMMAND_LINE_END); - os.flush(); - } - os.writeBytes(COMMAND_EXIT); - os.flush(); - - result = process.waitFor(); - // get command result - if (isNeedResultMsg) { - successMsg = new StringBuilder(); - errorMsg = new StringBuilder(); - successResult = new BufferedReader(new InputStreamReader(process.getInputStream())); - errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream())); - String s; - while ((s = successResult.readLine()) != null) { - successMsg.append(s); - } - while ((s = errorResult.readLine()) != null) { - errorMsg.append(s); - } - } - } catch (IOException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - if (os != null) { - os.close(); - } - if (successResult != null) { - successResult.close(); - } - if (errorResult != null) { - errorResult.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - - if (process != null) { - process.destroy(); - } - } - return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null - : errorMsg.toString()); - } - - /** - * result of command - *
    - *
  • {@link CommandResult#result} means result of command, 0 means normal, else means error, same to excute in - * linux shell
  • - *
  • {@link CommandResult#successMsg} means success message of command result
  • - *
  • {@link CommandResult#errorMsg} means error message of command result
  • - *
- * - * @author Trinea 2013-5-16 - */ - public static class CommandResult { - - /** result of command **/ - public int result; - /** success message of command result **/ - public String successMsg; - /** error message of command result **/ - public String errorMsg; - - public CommandResult(int result) { - this.result = result; - } - - public CommandResult(int result, String successMsg, String errorMsg) { - this.result = result; - this.successMsg = successMsg; - this.errorMsg = errorMsg; - } - } -} \ No newline at end of file -- 2.21.0