Dump all the possible useful information

This commit is contained in:
Mygod
2018-02-22 11:41:14 -08:00
parent ef0098e45d
commit 1bc49170d0
3 changed files with 51 additions and 30 deletions

View File

@@ -13,6 +13,7 @@ import be.mygod.vpnhotspot.net.Routing
import com.takisoft.fix.support.v7.preference.PreferenceFragmentCompatDividers
import java.io.File
import java.io.IOException
import java.io.PrintWriter
class SettingsPreferenceFragment : PreferenceFragmentCompatDividers() {
private val customTabsIntent by lazy {
@@ -31,19 +32,23 @@ class SettingsPreferenceFragment : PreferenceFragmentCompatDividers() {
val logDir = File(activity.cacheDir, "log")
logDir.mkdir()
val logFile = File.createTempFile("vpnhotspot-", ".log", logDir)
logFile.printWriter().use { writer ->
writer.write("${BuildConfig.VERSION_CODE} is running on API ${Build.VERSION.SDK_INT}\n\n")
try {
writer.write(Runtime.getRuntime().exec(arrayOf("logcat", "-d"))
.inputStream.bufferedReader().readText())
} catch (e: IOException) {
e.printStackTrace(writer)
}
writer.write("\n")
try {
writer.write(Routing.dump())
} catch (e: IOException) {
e.printStackTrace(writer)
logFile.outputStream().use { out ->
PrintWriter(out.bufferedWriter()).use { writer ->
writer.write("${BuildConfig.VERSION_CODE} is running on API ${Build.VERSION.SDK_INT}\n\n")
writer.flush()
try {
Runtime.getRuntime().exec(arrayOf("logcat", "-d")).inputStream.use { it.copyTo(out) }
} catch (e: IOException) {
e.printStackTrace(writer)
}
writer.write("\n")
writer.flush()
try {
Routing.dump()?.use { it.copyTo(out) }
} catch (e: IOException) {
e.printStackTrace(writer)
writer.flush()
}
}
}
startActivity(Intent.createChooser(Intent(Intent.ACTION_SEND)

View File

@@ -8,7 +8,9 @@ import android.databinding.BindingAdapter
import android.support.annotation.DrawableRes
import android.util.Log
import android.widget.ImageView
import be.mygod.vpnhotspot.App.Companion.app
import java.io.IOException
import java.io.InputStream
import java.net.NetworkInterface
fun debugLog(tag: String?, message: String?) {
@@ -37,23 +39,27 @@ fun NetworkInterface.formatAddresses() =
private const val NOISYSU_TAG = "NoisySU"
private const val NOISYSU_SUFFIX = "SUCCESS\n"
fun loggerSu(command: String): String? {
fun loggerSuStream(command: String): InputStream? {
val process = try {
ProcessBuilder("su", "-c", command)
.redirectErrorStream(true)
.directory(app.cacheDir)
.start()
} catch (e: IOException) {
return null
}
process.waitFor()
try {
val err = process.errorStream.bufferedReader().readText()
if (err.isNotBlank()) Log.e(NOISYSU_TAG, err)
} catch (e: IOException) {
e.printStackTrace()
}
return process.inputStream
}
fun loggerSu(command: String): String? {
val stream = loggerSuStream(command) ?: return null
return try {
process.inputStream.bufferedReader().readText()
stream.bufferedReader().readText()
} catch (e: IOException) {
e.printStackTrace()
null

View File

@@ -4,9 +4,10 @@ import android.os.Build
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.debugLog
import be.mygod.vpnhotspot.loggerSu
import be.mygod.vpnhotspot.loggerSuStream
import be.mygod.vpnhotspot.noisySu
import java.io.IOException
import java.io.InputStream
import java.net.Inet4Address
import java.net.InetAddress
import java.net.NetworkInterface
@@ -30,19 +31,28 @@ class Routing(val upstream: String?, val downstream: String, ownerAddress: InetA
"$IPTABLES -X vpnhotspot_fwd",
"quiet while ip rule del priority 17900; do done")
fun dump() = loggerSu("""
|echo logcat-su
|logcat -d
|echo
|echo iptables
|sh -c 'exec -a iptables-save iptables'
|echo
|echo iptables -t nat
|sh -c 'exec -a iptables-save iptables -t nat'
|echo
|echo ip rule
|ip rule
""".trimMargin())
fun dump(): InputStream? {
val commands = StringBuilder()
// https://android.googlesource.com/platform/external/iptables/+/android-7.0.0_r1/iptables/Android.mk#34
val iptablesSave = if (Build.VERSION.SDK_INT >= 24) "iptables-save" else {
commands.appendln("ln -sf /system/bin/iptables ./iptables-save")
"./iptables-save"
}
commands.append("""
|echo logcat-su
|logcat -d
|echo
|echo iptables
|$iptablesSave
|echo
|echo iptables -t nat
|$iptablesSave -t nat
|echo
|echo ip rule
|ip rule
""".trimMargin())
return loggerSuStream(commands.toString())
}
}
class InterfaceNotFoundException : IOException() {