Separate NoisySu.kt
This commit is contained in:
51
mobile/src/main/java/be/mygod/vpnhotspot/util/NoisySu.kt
Normal file
51
mobile/src/main/java/be/mygod/vpnhotspot/util/NoisySu.kt
Normal 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())
|
||||||
@@ -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())
|
|
||||||
|
|||||||
Reference in New Issue
Block a user