Separate NoisySu.kt

This commit is contained in:
Mygod
2018-05-09 15:59:17 -07:00
parent 90c1951645
commit a5fa9d8801
2 changed files with 51 additions and 43 deletions

View File

@@ -0,0 +1,51 @@
package be.mygod.vpnhotspot.util
import android.util.Log
import be.mygod.vpnhotspot.App
import be.mygod.vpnhotspot.R
import java.io.IOException
import java.io.InputStream
private const val NOISYSU_TAG = "NoisySU"
private const val NOISYSU_SUFFIX = "SUCCESS\n"
fun loggerSuStream(command: String): InputStream? {
val process = try {
ProcessBuilder("su", "-c", command)
.redirectErrorStream(true)
.directory(App.app.deviceContext.cacheDir)
.start()
} catch (e: IOException) {
e.printStackTrace()
return null
}
thread("LoggerSU-error") {
val err = process.errorStream.bufferedReader().readText()
if (err.isNotBlank()) {
Log.e(NOISYSU_TAG, err)
App.app.toast(R.string.noisy_su_failure)
}
}
return process.inputStream
}
fun loggerSu(command: String): String? {
val stream = loggerSuStream(command) ?: return null
return try {
stream.bufferedReader().readText()
} catch (e: IOException) {
e.printStackTrace()
null
}
}
fun noisySu(commands: Iterable<String>): Boolean? {
var out = loggerSu("""function noisy() { "$@" || echo "$@" exited with $?; }
${commands.joinToString("\n") { if (it.startsWith("quiet ")) it.substring(6) else "noisy $it" }}
echo $NOISYSU_SUFFIX""")
val result = if (out == null) null else out == NOISYSU_SUFFIX
out = out?.removeSuffix(NOISYSU_SUFFIX)
if (!out.isNullOrBlank()) Log.i(NOISYSU_TAG, out)
return result
}
fun noisySu(vararg commands: String) = noisySu(commands.asIterable())

View File

@@ -11,8 +11,6 @@ import android.widget.ImageView
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.BuildConfig import be.mygod.vpnhotspot.BuildConfig
import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.R
import java.io.IOException
import java.io.InputStream
import java.net.NetworkInterface import java.net.NetworkInterface
import java.net.SocketException import java.net.SocketException
@@ -55,44 +53,3 @@ fun thread(name: String? = null, start: Boolean = true, isDaemon: Boolean = fals
if (start) thread.start() if (start) thread.start()
return thread return thread
} }
private const val NOISYSU_TAG = "NoisySU"
private const val NOISYSU_SUFFIX = "SUCCESS\n"
fun loggerSuStream(command: String): InputStream? {
val process = try {
ProcessBuilder("su", "-c", command)
.redirectErrorStream(true)
.directory(app.deviceContext.cacheDir)
.start()
} catch (e: IOException) {
e.printStackTrace()
return null
}
thread("LoggerSU-error") {
val err = process.errorStream.bufferedReader().readText()
if (err.isNotBlank()) {
Log.e(NOISYSU_TAG, err)
app.toast(R.string.noisy_su_failure)
}
}
return process.inputStream
}
fun loggerSu(command: String): String? {
val stream = loggerSuStream(command) ?: return null
return try {
stream.bufferedReader().readText()
} catch (e: IOException) {
e.printStackTrace()
null
}
}
fun noisySu(commands: Iterable<String>): Boolean? {
var out = loggerSu("""function noisy() { "$@" || echo "$@" exited with $?; }
${commands.joinToString("\n") { if (it.startsWith("quiet ")) it.substring(6) else "noisy $it" }}
echo $NOISYSU_SUFFIX""")
val result = if (out == null) null else out == NOISYSU_SUFFIX
out = out?.removeSuffix(NOISYSU_SUFFIX)
if (!out.isNullOrBlank()) Log.i(NOISYSU_TAG, out)
return result
}
fun noisySu(vararg commands: String) = noisySu(commands.asIterable())