Respect system thisDevice

This commit is contained in:
Mygod
2020-07-07 04:54:26 +08:00
parent 82dc01ab37
commit 1ef2718d8c
5 changed files with 42 additions and 22 deletions

View File

@@ -17,10 +17,10 @@ import be.mygod.librootkotlinx.useParcel
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.MacAddressCompat import be.mygod.vpnhotspot.net.MacAddressCompat
import be.mygod.vpnhotspot.net.monitor.TetherTimeoutMonitor import be.mygod.vpnhotspot.net.monitor.TetherTimeoutMonitor
import be.mygod.vpnhotspot.net.wifi.P2pSupplicantConfiguration
import be.mygod.vpnhotspot.net.wifi.SoftApConfigurationCompat import be.mygod.vpnhotspot.net.wifi.SoftApConfigurationCompat
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.deletePersistentGroup import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.deletePersistentGroup
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.requestDeviceAddress
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.requestPersistentGroupInfo import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.requestPersistentGroupInfo
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.setWifiP2pChannels import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.setWifiP2pChannels
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.startWps import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.startWps
@@ -40,7 +40,6 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
SharedPreferences.OnSharedPreferenceChangeListener { SharedPreferences.OnSharedPreferenceChangeListener {
companion object { companion object {
const val KEY_SAFE_MODE = "service.repeater.safeMode" const val KEY_SAFE_MODE = "service.repeater.safeMode"
private const val KEY_LAST_MAC = "service.repeater.lastMac.v2"
private const val KEY_NETWORK_NAME = "service.repeater.networkName" private const val KEY_NETWORK_NAME = "service.repeater.networkName"
private const val KEY_PASSPHRASE = "service.repeater.passphrase" private const val KEY_PASSPHRASE = "service.repeater.passphrase"
@@ -64,9 +63,6 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
val safeModeConfigurable get() = Build.VERSION.SDK_INT >= 29 && hasP2pValidateName val safeModeConfigurable get() = Build.VERSION.SDK_INT >= 29 && hasP2pValidateName
val safeMode get() = Build.VERSION.SDK_INT >= 29 && val safeMode get() = Build.VERSION.SDK_INT >= 29 &&
(!hasP2pValidateName || app.pref.getBoolean(KEY_SAFE_MODE, true)) (!hasP2pValidateName || app.pref.getBoolean(KEY_SAFE_MODE, true))
var lastMac: String?
get() = app.pref.getString(KEY_LAST_MAC, null)
set(value) = app.pref.edit { putString(KEY_LAST_MAC, value) }
var networkName: String? var networkName: String?
get() = app.pref.getString(KEY_NETWORK_NAME, null) get() = app.pref.getString(KEY_NETWORK_NAME, null)
@@ -121,14 +117,18 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
} }
val groupChanged = StickyEvent1 { group } val groupChanged = StickyEvent1 { group }
@SuppressLint("NewApi") // networkId is available since Android 4.2 suspend fun obtainDeviceAddress(): MacAddressCompat? {
suspend fun fetchPersistentGroup() { return if (Build.VERSION.SDK_INT >= 29) p2pManager.requestDeviceAddress(channel ?: return null) ?: try {
val ownerAddress = lastMac?.let(MacAddressCompat.Companion::fromString) ?: try { RootManager.use { it.execute(RepeaterCommands.RequestDeviceAddress()) }
P2pSupplicantConfiguration().apply { init() }.bssid
} catch (e: Exception) { } catch (e: Exception) {
Timber.d(e) Timber.d(e)
null null
} ?: return }?.let { MacAddressCompat(it.value) } else lastMac?.let { MacAddressCompat.fromString(it) }
}
@SuppressLint("NewApi") // networkId is available since Android 4.2
suspend fun fetchPersistentGroup() {
val ownerAddress = obtainDeviceAddress() ?: return
val channel = channel ?: return val channel = channel ?: return
fun Collection<WifiP2pGroup>.filterUselessGroups(): List<WifiP2pGroup> { fun Collection<WifiP2pGroup>.filterUselessGroups(): List<WifiP2pGroup> {
if (isNotEmpty()) persistentSupported = true if (isNotEmpty()) persistentSupported = true
@@ -212,9 +212,9 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
} }
private val deviceListener = broadcastReceiver { _, intent -> private val deviceListener = broadcastReceiver { _, intent ->
val addr = intent.getParcelableExtra<WifiP2pDevice>(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE)?.deviceAddress val addr = intent.getParcelableExtra<WifiP2pDevice>(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE)?.deviceAddress
if (!addr.isNullOrEmpty() && (Build.VERSION.SDK_INT < 29 || if (!addr.isNullOrEmpty()) lastMac = addr
MacAddressCompat.fromString(addr) != MacAddressCompat.ANY_ADDRESS)) lastMac = addr
} }
private var lastMac: String? = null
/** /**
* Writes and critical reads to routingManager should be protected with this context. * Writes and critical reads to routingManager should be protected with this context.
*/ */
@@ -245,7 +245,9 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
onChannelDisconnected() onChannelDisconnected()
registerReceiver(deviceListener, intentFilter(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION)) if (Build.VERSION.SDK_INT < 29) {
registerReceiver(deviceListener, intentFilter(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION))
}
app.pref.registerOnSharedPreferenceChangeListener(this) app.pref.registerOnSharedPreferenceChangeListener(this)
} }
@@ -494,7 +496,7 @@ class RepeaterService : Service(), CoroutineScope, WifiP2pManager.ChannelListene
dispatcher.close() dispatcher.close()
} }
app.pref.unregisterOnSharedPreferenceChangeListener(this) app.pref.unregisterOnSharedPreferenceChangeListener(this)
unregisterReceiver(deviceListener) if (Build.VERSION.SDK_INT < 29) unregisterReceiver(deviceListener)
status = Status.DESTROYED status = Status.DESTROYED
if (Build.VERSION.SDK_INT >= 27) channel?.close() if (Build.VERSION.SDK_INT >= 27) channel?.close()
super.onDestroy() super.onDestroy()

View File

@@ -209,7 +209,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
shutdownTimeoutMillis = RepeaterService.shutdownTimeoutMillis).run { shutdownTimeoutMillis = RepeaterService.shutdownTimeoutMillis).run {
try { try {
val config = P2pSupplicantConfiguration(group) val config = P2pSupplicantConfiguration(group)
config.init(RepeaterService.lastMac) config.init(binder.obtainDeviceAddress()?.toString())
holder.config = config holder.config = config
passphrase = config.psk passphrase = config.psk
bssid = config.bssid bssid = config.bssid

View File

@@ -122,9 +122,7 @@ class P2pSupplicantConfiguration(private val group: WifiP2pGroup? = null) {
if (target == null) target = this if (target == null) target = this
}) })
} }
content = Content(result, target!!.apply { content = Content(result, target!!, persistentMacLine, legacy)
RepeaterService.lastMac = bssid!!
}, persistentMacLine, legacy)
} catch (e: Exception) { } catch (e: Exception) {
FirebaseCrashlytics.getInstance().apply { FirebaseCrashlytics.getInstance().apply {
setCustomKey(TAG, config) setCustomKey(TAG, config)

View File

@@ -4,7 +4,9 @@ import android.annotation.SuppressLint
import android.net.wifi.WpsInfo import android.net.wifi.WpsInfo
import android.net.wifi.p2p.WifiP2pGroup import android.net.wifi.p2p.WifiP2pGroup
import android.net.wifi.p2p.WifiP2pManager import android.net.wifi.p2p.WifiP2pManager
import androidx.annotation.RequiresApi
import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.net.MacAddressCompat
import be.mygod.vpnhotspot.util.callSuper import be.mygod.vpnhotspot.util.callSuper
import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CompletableDeferred
import timber.log.Timber import timber.log.Timber
@@ -126,4 +128,15 @@ object WifiP2pManagerHelper {
})) }))
return result.await() return result.await()
} }
@SuppressLint("MissingPermission")
@RequiresApi(29)
suspend fun WifiP2pManager.requestDeviceAddress(c: WifiP2pManager.Channel): MacAddressCompat? {
val future = CompletableDeferred<String?>()
requestDeviceInfo(c) { future.complete(it?.deviceAddress) }
return future.await()?.let {
val address = if (it.isEmpty()) null else MacAddressCompat.fromString(it)
if (address == MacAddressCompat.ANY_ADDRESS) null else address
}
}
} }

View File

@@ -6,11 +6,10 @@ import android.os.Parcelable
import android.system.Os import android.system.Os
import android.system.OsConstants import android.system.OsConstants
import android.text.TextUtils import android.text.TextUtils
import be.mygod.librootkotlinx.ParcelableInt import androidx.annotation.RequiresApi
import be.mygod.librootkotlinx.ParcelableList import be.mygod.librootkotlinx.*
import be.mygod.librootkotlinx.RootCommand
import be.mygod.librootkotlinx.RootCommandNoResult
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.deletePersistentGroup import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.deletePersistentGroup
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.requestDeviceAddress
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.requestPersistentGroupInfo import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.requestPersistentGroupInfo
import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.setWifiP2pChannels import be.mygod.vpnhotspot.net.wifi.WifiP2pManagerHelper.setWifiP2pChannels
import be.mygod.vpnhotspot.util.Services import be.mygod.vpnhotspot.util.Services
@@ -34,6 +33,14 @@ object RepeaterCommands {
} }
} }
@Parcelize
@RequiresApi(29)
class RequestDeviceAddress : RootCommand<ParcelableLong?> {
override suspend fun execute() = Services.p2p!!.run {
requestDeviceAddress(obtainChannel())?.let { ParcelableLong(it.addr) }
}
}
@Parcelize @Parcelize
class RequestPersistentGroupInfo : RootCommand<ParcelableList> { class RequestPersistentGroupInfo : RootCommand<ParcelableList> {
override suspend fun execute() = Services.p2p!!.run { override suspend fun execute() = Services.p2p!!.run {