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

View File

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

View File

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