Commit 68607be4 authored by liuyang's avatar liuyang

tcpdump 抓包http协议连接及退出测试处理

parent b4b71f45
...@@ -27,12 +27,12 @@ class BylRobotService: Service() { ...@@ -27,12 +27,12 @@ class BylRobotService: Service() {
testCapture() testCapture()
// screenShot() // screenShot()
count++ count++
if (count > 4) { if (count > 2) {
cancelTimer() cancelTimer()
// proc?.destroy() // proc?.destroy()
} }
} }
},2000, 5*1000) },2000, 10*1000)
} }
private fun screenShot() { private fun screenShot() {
...@@ -52,12 +52,15 @@ class BylRobotService: Service() { ...@@ -52,12 +52,15 @@ class BylRobotService: Service() {
var proc: Process ?= null var proc: Process ?= null
private fun testCapture() { private fun testCapture() {
if (proc == null) { proc?.destroy()
Thread(Runnable {
AdbUtil.killTcpdump()
}).start()
Thread.sleep(3000)
Thread(Runnable { Thread(Runnable {
proc = AdbUtil.tcpCapture("") proc = AdbUtil.tcpCapture("")
}).start() }).start()
} }
}
private fun cancelTimer() { private fun cancelTimer() {
timer?.cancel() timer?.cancel()
......
...@@ -9,7 +9,7 @@ import java.io.InputStreamReader ...@@ -9,7 +9,7 @@ import java.io.InputStreamReader
object AdbUtil { object AdbUtil {
const val TAG = "AdbUtil" const val TAG = "AdbUtil"
const val SHELL = "sh" const val SHELL = "sh"
const val ROOT_SHELL = "sh" const val ROOT_SHELL = "su"
const val LINE_BREAK = "\n" const val LINE_BREAK = "\n"
const val EXIT = "exit" const val EXIT = "exit"
...@@ -174,7 +174,7 @@ object AdbUtil { ...@@ -174,7 +174,7 @@ object AdbUtil {
} }
fun tcpCapture(tcpdump: String): Process? { fun tcpCapture(tcpdump: String): Process? {
val captureCmd = "su\n tcpdump -p -vv -s 0 -A 'tcp dst port 80 and tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420' >> /sdcard/capture.txt\nexit\n" val captureCmd = "tcpdump -p -vv -s 0 -A 'tcp dst port 80 and tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420' >> /sdcard/capture.txt\n"
val proc = getRootShell() val proc = getRootShell()
var dos: DataOutputStream? = null var dos: DataOutputStream? = null
...@@ -191,7 +191,59 @@ object AdbUtil { ...@@ -191,7 +191,59 @@ object AdbUtil {
dos?.close() dos?.close()
} }
object : Thread() {
override fun run() {
val insBr = BufferedReader(InputStreamReader(proc?.inputStream))
try {
var lineStr: String? = null
do {
lineStr = insBr.readLine()
if (lineStr.isNullOrEmpty())
break
lineStr = trimLine(lineStr)
Log.i(TAG, " tcpdump echo line : $lineStr")
} while (true)
} catch (e: IOException) {
e.printStackTrace()
} finally {
try {
proc?.inputStream?.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
}.start()
object : Thread() {
override fun run() {
val errBr = BufferedReader(InputStreamReader(proc?.errorStream))
try {
var errLine: String? = null
do {
errLine = errBr.readLine()
if (errLine.isNullOrEmpty())
break
errLine = trimLine(errLine)
Log.i(TAG, " tcpdump error line : $errLine")
} while (true)
} catch (e: IOException) {
e.printStackTrace()
} finally {
try {
proc?.errorStream?.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
}.start()
return proc return proc
} }
fun killTcpdump() {
exeCmdEcho("killall tcpdump", true)
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment