From ada55a1bf65008754396e1a3f8fe4ab54400f5a4 Mon Sep 17 00:00:00 2001 From: Mygod Date: Sat, 29 May 2021 20:49:51 -0400 Subject: [PATCH] Support obtaining p2p randomization setting --- .../mygod/vpnhotspot/manage/RepeaterManager.kt | 5 ++++- .../net/monitor/TetherTimeoutMonitor.kt | 2 +- .../net/wifi/SoftApConfigurationCompat.kt | 4 ++++ .../mygod/vpnhotspot/net/wifi/WifiApManager.kt | 18 ++++++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt index 0e833300..b3aabd03 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/manage/RepeaterManager.kt @@ -30,6 +30,7 @@ import be.mygod.vpnhotspot.net.MacAddressCompat import be.mygod.vpnhotspot.net.wifi.P2pSupplicantConfiguration import be.mygod.vpnhotspot.net.wifi.SoftApConfigurationCompat import be.mygod.vpnhotspot.net.wifi.WifiApDialogFragment +import be.mygod.vpnhotspot.net.wifi.WifiApManager import be.mygod.vpnhotspot.util.ServiceForegroundConnector import be.mygod.vpnhotspot.util.formatAddresses import be.mygod.vpnhotspot.util.showAllowingStateLoss @@ -197,6 +198,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic shutdownTimeoutMillis = RepeaterService.shutdownTimeoutMillis).apply { bssid = RepeaterService.deviceAddress setChannel(RepeaterService.operatingChannel, RepeaterService.operatingBand) + setMacRandomizationEnabled(WifiApManager.p2pMacRandomizationSupported) } to false } } else binder?.let { binder -> @@ -206,13 +208,14 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic securityType = SoftApConfiguration.SECURITY_TYPE_WPA2_PSK, // is not actually used isAutoShutdownEnabled = RepeaterService.isAutoShutdownEnabled, shutdownTimeoutMillis = RepeaterService.shutdownTimeoutMillis).run { + setChannel(RepeaterService.operatingChannel) + setMacRandomizationEnabled(WifiApManager.p2pMacRandomizationSupported) try { val config = P2pSupplicantConfiguration(group) config.init(binder.obtainDeviceAddress()?.toString()) holder.config = config passphrase = config.psk bssid = config.bssid - setChannel(RepeaterService.operatingChannel) this to false } catch (e: Exception) { if (e !is CancellationException) Timber.w(e) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TetherTimeoutMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TetherTimeoutMonitor.kt index 75629031..abba1855 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TetherTimeoutMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/monitor/TetherTimeoutMonitor.kt @@ -46,7 +46,7 @@ class TetherTimeoutMonitor(private val timeout: Long = 0, val info = WifiApManager.resolvedActivity.activityInfo val resources = app.packageManager.getResourcesForApplication(info.applicationInfo) resources.getInteger(resources.findIdentifier("config_wifiFrameworkSoftApShutDownTimeoutMilliseconds", - "integer", "com.android.wifi.resources", info.packageName)) + "integer", WifiApManager.RESOURCES_PACKAGE, info.packageName)) } } catch (e: RuntimeException) { Timber.w(e) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/SoftApConfigurationCompat.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/SoftApConfigurationCompat.kt index 8096c6dd..6a596c14 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/SoftApConfigurationCompat.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/SoftApConfigurationCompat.kt @@ -343,6 +343,10 @@ data class SoftApConfigurationCompat( channels = SparseIntArray(1).apply { put(band, channel) } } + fun setMacRandomizationEnabled(enabled: Boolean) { + macRandomizationSetting = if (enabled) RANDOMIZATION_PERSISTENT else RANDOMIZATION_NONE + } + /** * Only single band/channel can be supplied on API 23-30 */ diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt index b7fc93b5..81217656 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiApManager.kt @@ -3,6 +3,7 @@ package be.mygod.vpnhotspot.net.wifi import android.annotation.TargetApi import android.content.Intent import android.content.pm.PackageManager +import android.content.res.Resources import android.net.wifi.SoftApConfiguration import android.net.wifi.WifiManager import android.os.Build @@ -15,6 +16,7 @@ import be.mygod.vpnhotspot.net.wifi.SoftApConfigurationCompat.Companion.toCompat import be.mygod.vpnhotspot.util.ConstantLookup import be.mygod.vpnhotspot.util.Services import be.mygod.vpnhotspot.util.callSuper +import be.mygod.vpnhotspot.util.findIdentifier import timber.log.Timber import java.lang.reflect.InvocationHandler import java.lang.reflect.Method @@ -27,6 +29,8 @@ object WifiApManager { */ @RequiresApi(30) private const val ACTION_RESOURCES_APK = "com.android.server.wifi.intent.action.SERVICE_WIFI_RESOURCES_APK" + @RequiresApi(30) + const val RESOURCES_PACKAGE = "com.android.wifi.resources" /** * Based on: https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/000ad45/service/java/com/android/server/wifi/WifiContext.java#66 */ @@ -34,6 +38,20 @@ object WifiApManager { val resolvedActivity get() = app.packageManager.queryIntentActivities(Intent(ACTION_RESOURCES_APK), PackageManager.MATCH_SYSTEM_ONLY).single() + private const val CONFIG_P2P_MAC_RANDOMIZATION_SUPPORTED = "config_wifi_p2p_mac_randomization_supported" + val p2pMacRandomizationSupported get() = when (Build.VERSION.SDK_INT) { + 29 -> Resources.getSystem().run { + getBoolean(getIdentifier(CONFIG_P2P_MAC_RANDOMIZATION_SUPPORTED, "bool", "android")) + } + in 30..Int.MAX_VALUE -> @TargetApi(30) { + val info = resolvedActivity.activityInfo + val resources = app.packageManager.getResourcesForApplication(info.applicationInfo) + resources.getBoolean(resources.findIdentifier(CONFIG_P2P_MAC_RANDOMIZATION_SUPPORTED, "bool", + RESOURCES_PACKAGE, info.packageName)) + } + else -> false + } + private val getWifiApConfiguration by lazy { WifiManager::class.java.getDeclaredMethod("getWifiApConfiguration") } @Suppress("DEPRECATION") private val setWifiApConfiguration by lazy {