Refine monitor

This commit is contained in:
Mygod
2018-01-21 01:57:21 -08:00
parent b91cb126a9
commit 9a3606a59e
3 changed files with 13 additions and 17 deletions

View File

@@ -7,7 +7,10 @@ import android.app.NotificationManager
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.res.Configuration import android.content.res.Configuration
import android.os.Build import android.os.Build
import android.os.Handler
import android.preference.PreferenceManager import android.preference.PreferenceManager
import android.support.annotation.StringRes
import android.widget.Toast
class App : Application() { class App : Application() {
companion object { companion object {
@@ -36,6 +39,9 @@ class App : Application() {
} }
} }
private val handler = Handler()
val pref: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(this) } val pref: SharedPreferences by lazy { PreferenceManager.getDefaultSharedPreferences(this) }
val dns: String get() = app.pref.getString("service.dns", "8.8.8.8:53") val dns: String get() = app.pref.getString("service.dns", "8.8.8.8:53")
fun toast(@StringRes resId: Int) = handler.post { Toast.makeText(app, resId, Toast.LENGTH_SHORT).show() }
} }

View File

@@ -97,11 +97,11 @@ class RepeaterFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClickL
fun recreate() { fun recreate() {
clients.clear() clients.clear()
val map = HashMap(p2p.associateBy { it.deviceAddress }) val map = HashMap(p2p.associateBy { it.deviceAddress })
val tethered = (tetheredInterfaces + p2pInterface).filterNotNull()
for (neighbour in neighbours) { for (neighbour in neighbours) {
val client = map.remove(neighbour.lladdr) val client = map.remove(neighbour.lladdr)
if (client != null) clients.add(Client(client, neighbour)) if (client != null) clients.add(Client(client, neighbour))
else if (tethered.contains(neighbour.dev)) clients.add(Client(neighbour = neighbour)) else if (tetheredInterfaces.contains(neighbour.dev) || neighbour.dev == p2pInterface)
clients.add(Client(neighbour = neighbour))
} }
clients.addAll(map.map { Client(it.value) }) clients.addAll(map.map { Client(it.value) })
clients.sortWith(compareBy<Client> { it.ip }.thenBy { it.mac }) clients.sortWith(compareBy<Client> { it.ip }.thenBy { it.mac })

View File

@@ -1,9 +1,7 @@
package be.mygod.vpnhotspot.net package be.mygod.vpnhotspot.net
import android.os.Build
import android.os.Handler import android.os.Handler
import android.util.Log import android.util.Log
import android.widget.Toast
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
@@ -37,9 +35,7 @@ class IpNeighbourMonitor private constructor() {
private fun thread(name: String? = null, start: Boolean = true, isDaemon: Boolean = false, private fun thread(name: String? = null, start: Boolean = true, isDaemon: Boolean = false,
contextClassLoader: ClassLoader? = null, priority: Int = -1, block: () -> Unit): Thread { contextClassLoader: ClassLoader? = null, priority: Int = -1, block: () -> Unit): Thread {
val thread = kotlin.concurrent.thread(false, isDaemon, contextClassLoader, name, priority, block) val thread = kotlin.concurrent.thread(false, isDaemon, contextClassLoader, name, priority, block)
thread.setUncaughtExceptionHandler { _, _ -> thread.setUncaughtExceptionHandler { _, _ -> app.toast(R.string.noisy_su_failure) }
Toast.makeText(app, R.string.noisy_su_failure, Toast.LENGTH_SHORT).show()
}
if (start) thread.start() if (start) thread.start()
return thread return thread
} }
@@ -53,18 +49,12 @@ class IpNeighbourMonitor private constructor() {
private val handler = Handler() private val handler = Handler()
private var updatePosted = false private var updatePosted = false
val neighbours = HashMap<String, IpNeighbour>() val neighbours = HashMap<String, IpNeighbour>()
/**
* Using monitor requires using /proc/self/ns/net which would be problematic on Android 6.0+.
*
* Source: https://source.android.com/security/enhancements/enhancements60
*/
private var monitor: Process? = null private var monitor: Process? = null
init { init {
thread(name = TAG + "-input") { thread(name = TAG + "-input") {
val monitor = (if (Build.VERSION.SDK_INT >= 23) // monitor may get rejected by SELinux
ProcessBuilder("su", "-c", "ip", "-4", "monitor", "neigh") else val monitor = ProcessBuilder("sh", "-c", "ip -4 monitor neigh || su -c ip -4 monitor neigh")
ProcessBuilder("ip", "-4", "monitor", "neigh"))
.redirectErrorStream(true) .redirectErrorStream(true)
.start() .start()
this.monitor = monitor this.monitor = monitor
@@ -84,8 +74,8 @@ class IpNeighbourMonitor private constructor() {
if (changed) postUpdateLocked() if (changed) postUpdateLocked()
} }
} }
Log.w(TAG, if (Build.VERSION.SDK_INT >= 26 && monitor.isAlive) "monitor closed stdout" else monitor.waitFor()
"monitor died unexpectedly") if (monitor.exitValue() != 0) app.toast(R.string.noisy_su_failure)
} catch (ignore: InterruptedIOException) { } } catch (ignore: InterruptedIOException) { }
} }
} }