From bc0d6de3f9e3a333bc6f865b31f66cf70ac75a71 Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 27 Dec 2018 12:55:44 +0800 Subject: [PATCH] Log p2p_supplicant if parsing failed --- .../net/wifi/P2pSupplicantConfiguration.kt | 61 +++++++++++-------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/P2pSupplicantConfiguration.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/P2pSupplicantConfiguration.kt index e2ecd73d..3a6c8479 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/P2pSupplicantConfiguration.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/P2pSupplicantConfiguration.kt @@ -4,6 +4,7 @@ import android.net.wifi.p2p.WifiP2pGroup import android.os.Build import be.mygod.vpnhotspot.App.Companion.app import be.mygod.vpnhotspot.util.RootSession +import timber.log.Timber import java.io.File /** @@ -30,7 +31,7 @@ class P2pSupplicantConfiguration(private val group: WifiP2pGroup, ownerAddress: override fun toString() = joinToString("\n") } - private class Parser(lines: List) { + private class Parser(val lines: List) { private val iterator = lines.iterator() lateinit var line: String lateinit var trimmed: String @@ -45,36 +46,42 @@ class P2pSupplicantConfiguration(private val group: WifiP2pGroup, ownerAddress: val result = ArrayList() var target: NetworkBlock? = null val parser = Parser(it.execOutUnjoined("cat $confPath")) - while (parser.next()) { - if (parser.trimmed.startsWith("network={")) { - val block = NetworkBlock() - block.add(parser.line) - while (parser.next() && !parser.trimmed.startsWith('}')) { - if (parser.trimmed.startsWith("ssid=")) { - check(block.ssidLine == null) - block.ssidLine = block.size - } else if (parser.trimmed.startsWith("mode=3")) block.groupOwner = true else { - val match = networkParser.find(parser.trimmed) - if (match != null) if (match.groups[5] != null) { - check(block.pskLine == null && block.psk == null) - block.psk = match.groupValues[5].apply { check(length in 8..63) } - block.pskLine = block.size - } else if (match.groups[2] != null && - match.groupValues[2].equals(group.owner.deviceAddress ?: ownerAddress, true)) { - block.bssidMatches = true + try { + while (parser.next()) { + if (parser.trimmed.startsWith("network={")) { + val block = NetworkBlock() + block.add(parser.line) + while (parser.next() && !parser.trimmed.startsWith('}')) { + if (parser.trimmed.startsWith("ssid=")) { + check(block.ssidLine == null) + block.ssidLine = block.size + } else if (parser.trimmed.startsWith("mode=3")) block.groupOwner = true else { + val match = networkParser.find(parser.trimmed) + if (match != null) if (match.groups[5] != null) { + check(block.pskLine == null && block.psk == null) + block.psk = match.groupValues[5].apply { check(length in 8..63) } + block.pskLine = block.size + } else if (match.groups[2] != null && + match.groupValues[2].equals(group.owner.deviceAddress ?: ownerAddress, true)) { + block.bssidMatches = true + } } + block.add(parser.line) } block.add(parser.line) - } - block.add(parser.line) - result.add(block) - if (block.bssidMatches && block.groupOwner && target == null) { // keep first only - check(block.ssidLine != null && block.pskLine != null) - target = block - } - } else result.add(parser.line) + result.add(block) + if (block.bssidMatches && block.groupOwner && target == null) { // keep first only + check(block.ssidLine != null && block.pskLine != null) + target = block + } + } else result.add(parser.line) + } + Pair(result, target!!) + } catch (e: RuntimeException) { + Timber.w("Failed to parse p2p_supplicant.conf, ownerAddress: $ownerAddress, P2P group: $group") + Timber.w(parser.lines.joinToString("\n")) + throw e } - Pair(result, target!!) } } val ssid = group.networkName