Fix unsafe repeater mode for Android 11
This commit is contained in:
@@ -209,7 +209,7 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
|
|||||||
passphrase = config.psk
|
passphrase = config.psk
|
||||||
band = SoftApConfigurationCompat.BAND_ANY
|
band = SoftApConfigurationCompat.BAND_ANY
|
||||||
channel = RepeaterService.operatingChannel
|
channel = RepeaterService.operatingChannel
|
||||||
bssid = RepeaterService.deviceAddress
|
bssid = config.bssid
|
||||||
}
|
}
|
||||||
} catch (e: RuntimeException) {
|
} catch (e: RuntimeException) {
|
||||||
Timber.w(e)
|
Timber.w(e)
|
||||||
@@ -230,8 +230,8 @@ class RepeaterManager(private val parent: TetheringFragment) : Manager(), Servic
|
|||||||
}
|
}
|
||||||
} else holder.config?.let { master ->
|
} else holder.config?.let { master ->
|
||||||
val binder = binder
|
val binder = binder
|
||||||
if (binder?.group?.networkName != config.ssid || master.psk != config.passphrase) try {
|
if (binder?.group?.networkName != config.ssid || master.psk != config.passphrase || master.bssid != config.bssid) try {
|
||||||
withContext(Dispatchers.Default) { master.update(config.ssid!!, config.passphrase!!) }
|
withContext(Dispatchers.Default) { master.update(config.ssid!!, config.passphrase!!, config.bssid!!) }
|
||||||
(this.binder ?: binder)?.group = null
|
(this.binder ?: binder)?.group = null
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.w(e)
|
Timber.w(e)
|
||||||
|
|||||||
@@ -14,11 +14,12 @@ import java.io.File
|
|||||||
* https://android.googlesource.com/platform/external/wpa_supplicant_8/+/d2986c2/wpa_supplicant/config.c#488
|
* https://android.googlesource.com/platform/external/wpa_supplicant_8/+/d2986c2/wpa_supplicant/config.c#488
|
||||||
* https://android.googlesource.com/platform/external/wpa_supplicant_8/+/6fa46df/wpa_supplicant/config_file.c#182
|
* https://android.googlesource.com/platform/external/wpa_supplicant_8/+/6fa46df/wpa_supplicant/config_file.c#182
|
||||||
*/
|
*/
|
||||||
class P2pSupplicantConfiguration(private val group: WifiP2pGroup, vararg ownerAddresses: String?) {
|
class P2pSupplicantConfiguration(private val group: WifiP2pGroup?, vararg ownerAddresses: String?) {
|
||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "P2pSupplicantConfiguration"
|
private const val TAG = "P2pSupplicantConfiguration"
|
||||||
private const val CONF_PATH_TREBLE = "/data/vendor/wifi/wpa/p2p_supplicant.conf"
|
private const val CONF_PATH_TREBLE = "/data/vendor/wifi/wpa/p2p_supplicant.conf"
|
||||||
private const val CONF_PATH_LEGACY = "/data/misc/wifi/p2p_supplicant.conf"
|
private const val CONF_PATH_LEGACY = "/data/misc/wifi/p2p_supplicant.conf"
|
||||||
|
private const val PERSISTENT_MAC = "p2p_device_persistent_mac_addr="
|
||||||
private val networkParser =
|
private val networkParser =
|
||||||
"^(bssid=(([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2})|psk=(ext:|\"(.*)\"|[0-9a-fA-F]{64}\$)?)".toRegex()
|
"^(bssid=(([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2})|psk=(ext:|\"(.*)\"|[0-9a-fA-F]{64}\$)?)".toRegex()
|
||||||
private val whitespaceMatcher = "\\s+".toRegex()
|
private val whitespaceMatcher = "\\s+".toRegex()
|
||||||
@@ -27,6 +28,7 @@ class P2pSupplicantConfiguration(private val group: WifiP2pGroup, vararg ownerAd
|
|||||||
private class NetworkBlock : ArrayList<String>() {
|
private class NetworkBlock : ArrayList<String>() {
|
||||||
var ssidLine: Int? = null
|
var ssidLine: Int? = null
|
||||||
var pskLine: Int? = null
|
var pskLine: Int? = null
|
||||||
|
var bssidLine: Int? = null
|
||||||
var psk: String? = null
|
var psk: String? = null
|
||||||
var groupOwner = false
|
var groupOwner = false
|
||||||
var bssid: String? = null
|
var bssid: String? = null
|
||||||
@@ -44,16 +46,20 @@ class P2pSupplicantConfiguration(private val group: WifiP2pGroup, vararg ownerAd
|
|||||||
} else false
|
} else false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private data class Content(val lines: ArrayList<Any>, var target: NetworkBlock, var persistentMacLine: Int?,
|
||||||
|
var legacy: Boolean)
|
||||||
|
|
||||||
private val content by lazy {
|
private val content by lazy {
|
||||||
RootSession.use {
|
RootSession.use {
|
||||||
val result = ArrayList<Any>()
|
val result = ArrayList<Any>()
|
||||||
var target: NetworkBlock? = null
|
var target: NetworkBlock? = null
|
||||||
|
var persistentMacLine: Int? = null
|
||||||
val command = "cat $CONF_PATH_TREBLE || cat $CONF_PATH_LEGACY"
|
val command = "cat $CONF_PATH_TREBLE || cat $CONF_PATH_LEGACY"
|
||||||
val shell = it.execQuiet(command)
|
val shell = it.execQuiet(command)
|
||||||
RootSession.checkOutput(command, shell, false, false)
|
RootSession.checkOutput(command, shell, false, false)
|
||||||
val parser = Parser(shell.out)
|
val parser = Parser(shell.out)
|
||||||
try {
|
try {
|
||||||
val bssids = (listOf(group.owner.deviceAddress, RepeaterService.lastMac) + ownerAddresses)
|
var bssids = (listOf(group?.owner?.deviceAddress, RepeaterService.lastMac) + ownerAddresses)
|
||||||
.filterNotNull()
|
.filterNotNull()
|
||||||
.distinct()
|
.distinct()
|
||||||
.filter {
|
.filter {
|
||||||
@@ -81,7 +87,10 @@ class P2pSupplicantConfiguration(private val group: WifiP2pGroup, vararg ownerAd
|
|||||||
block.psk = match.groupValues[5].apply { check(length in 8..63) }
|
block.psk = match.groupValues[5].apply { check(length in 8..63) }
|
||||||
}
|
}
|
||||||
block.pskLine = block.size
|
block.pskLine = block.size
|
||||||
} else if (bssids.any { matchedBssid.equals(it, true) }) block.bssid = matchedBssid
|
} else if (bssids.any { matchedBssid.equals(it, true) }) {
|
||||||
|
block.bssid = matchedBssid
|
||||||
|
block.bssidLine = block.size
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
block.add(parser.line)
|
block.add(parser.line)
|
||||||
@@ -92,7 +101,14 @@ class P2pSupplicantConfiguration(private val group: WifiP2pGroup, vararg ownerAd
|
|||||||
check(block.ssidLine != null && block.pskLine != null)
|
check(block.ssidLine != null && block.pskLine != null)
|
||||||
target = block
|
target = block
|
||||||
}
|
}
|
||||||
} else result.add(parser.line)
|
} else {
|
||||||
|
if (parser.trimmed.startsWith(PERSISTENT_MAC)) {
|
||||||
|
require(persistentMacLine == null) { "Duplicated $PERSISTENT_MAC" }
|
||||||
|
persistentMacLine = result.size
|
||||||
|
bssids = listOf(parser.trimmed.substring(PERSISTENT_MAC.length))
|
||||||
|
}
|
||||||
|
result.add(parser.line)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (target == null && !RepeaterService.persistentSupported) {
|
if (target == null && !RepeaterService.persistentSupported) {
|
||||||
result.add("")
|
result.add("")
|
||||||
@@ -114,7 +130,7 @@ class P2pSupplicantConfiguration(private val group: WifiP2pGroup, vararg ownerAd
|
|||||||
if (target == null) target = this
|
if (target == null) target = this
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Triple(result, target!!, shell.err.isNotEmpty())
|
Content(result, target!!, persistentMacLine, shell.err.isNotEmpty())
|
||||||
} catch (e: RuntimeException) {
|
} catch (e: RuntimeException) {
|
||||||
FirebaseCrashlytics.getInstance().apply {
|
FirebaseCrashlytics.getInstance().apply {
|
||||||
setCustomKey(TAG, parser.lines.joinToString("\n"))
|
setCustomKey(TAG, parser.lines.joinToString("\n"))
|
||||||
@@ -125,13 +141,16 @@ class P2pSupplicantConfiguration(private val group: WifiP2pGroup, vararg ownerAd
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val psk = group.passphrase ?: content.second.psk!!
|
val psk = group?.passphrase ?: content.target.psk!!
|
||||||
|
val bssid = MacAddressCompat.fromString(content.target.bssid!!)
|
||||||
|
|
||||||
fun update(ssid: String, psk: String) {
|
fun update(ssid: String, psk: String, bssid: MacAddressCompat) {
|
||||||
val (lines, block, legacy) = content
|
val (lines, block, persistentMacLine, legacy) = content
|
||||||
|
persistentMacLine?.let { lines[it] = PERSISTENT_MAC + bssid }
|
||||||
block[block.ssidLine!!] = "\tssid=" + ssid.toByteArray()
|
block[block.ssidLine!!] = "\tssid=" + ssid.toByteArray()
|
||||||
.joinToString("") { (it.toInt() and 255).toString(16).padStart(2, '0') }
|
.joinToString("") { (it.toInt() and 255).toString(16).padStart(2, '0') }
|
||||||
block[block.pskLine!!] = "\tpsk=\"$psk\"" // no control chars or weird stuff
|
block[block.pskLine!!] = "\tpsk=\"$psk\"" // no control chars or weird stuff
|
||||||
|
block[block.bssidLine!!] = "\tbssid=$bssid"
|
||||||
val tempFile = File.createTempFile("vpnhotspot-", ".conf", app.deviceStorage.cacheDir)
|
val tempFile = File.createTempFile("vpnhotspot-", ".conf", app.deviceStorage.cacheDir)
|
||||||
try {
|
try {
|
||||||
tempFile.printWriter().use { writer ->
|
tempFile.printWriter().use { writer ->
|
||||||
|
|||||||
Reference in New Issue
Block a user