Skip to content

Commit

Permalink
Revert "Disable editing repeater credentials on Android 5"
Browse files Browse the repository at this point in the history
This reverts commit df304fe.

This kind of addresses #31. Apparently we can't predict which wpa_supplicant is bundled into the system by inspecting Android version, especially now that it is put into /vendor/bin/hw (see also Project Treble: https://source.android.com/devices/architecture/vndk/).

It might be cool if someone makes a Magisk module that replaces wpa_supplicant to the latest version.
  • Loading branch information
Mygod committed Jul 28, 2018
1 parent df03665 commit 81dc43f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package be.mygod.vpnhotspot.net.wifi

import android.net.wifi.WifiConfiguration
import android.os.Build
import android.os.Parcel
import android.os.Parcelable
import android.util.Log
import androidx.annotation.RequiresApi
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.util.loggerSu
import be.mygod.vpnhotspot.util.noisySu
import com.crashlytics.android.Crashlytics
import java.io.File
import java.io.IOException

class P2pSupplicantConfiguration(private val initContent: String? = null) : Parcelable {
companion object CREATOR : Parcelable.Creator<P2pSupplicantConfiguration> {
Expand All @@ -29,6 +30,7 @@ class P2pSupplicantConfiguration(private val initContent: String? = null) : Parc
*/
private val pskParser = "^[\\r\\t ]*psk=(ext:|\"(.*)\"|\"(.*)|[0-9a-fA-F]{64}\$)".toRegex(RegexOption.MULTILINE)
}
private class InvalidConfigurationError : IOException()

override fun writeToParcel(out: Parcel, flags: Int) {
out.writeString(if (contentDelegate.isInitialized()) content else null)
Expand Down Expand Up @@ -58,8 +60,6 @@ class P2pSupplicantConfiguration(private val initContent: String? = null) : Parc
}
}

// pkill not available on Lollipop. Source: https://android.googlesource.com/platform/system/core/+/master/shell_and_utilities/README.md
@RequiresApi(23)
fun update(config: WifiConfiguration): Boolean? {
val content = content ?: return null
val tempFile = File.createTempFile("vpnhotspot-", ".conf", app.cacheDir)
Expand All @@ -82,9 +82,13 @@ class P2pSupplicantConfiguration(private val initContent: String? = null) : Parc
}
if (ssidFound != 1 || pskFound != 1) {
Crashlytics.log(Log.WARN, TAG, "Invalid conf ($ssidFound, $pskFound): $content")
Crashlytics.logException(InvalidConfigurationError())
}
if (ssidFound == 0 || pskFound == 0) return false
return noisySu("cat ${tempFile.absolutePath} > /data/misc/wifi/p2p_supplicant.conf", "pkill wpa_supplicant")
// pkill not available on Lollipop. Source: https://android.googlesource.com/platform/system/core/+/master/shell_and_utilities/README.md
return noisySu("cat ${tempFile.absolutePath} > /data/misc/wifi/p2p_supplicant.conf",
if (Build.VERSION.SDK_INT >= 23) "pkill wpa_supplicant"
else "set `ps | grep wpa_supplicant`; kill \$2")
} finally {
if (!tempFile.delete()) tempFile.deleteOnExit()
}
Expand Down
77 changes: 77 additions & 0 deletions mobile/src/main/java/be/mygod/vpnhotspot/net/wifi/WifiP2pDialog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package be.mygod.vpnhotspot.net.wifi

import android.content.Context
import android.content.DialogInterface
import android.net.wifi.WifiConfiguration
import android.net.wifi.WifiConfiguration.AuthAlgorithm
import android.os.Bundle
import com.google.android.material.textfield.TextInputLayout
import androidx.appcompat.app.AlertDialog
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.TextView
import be.mygod.vpnhotspot.R
import java.nio.charset.Charset

/**
* https://android.googlesource.com/platform/packages/apps/Settings/+/39b4674/src/com/android/settings/wifi/WifiApDialog.java
*/
class WifiP2pDialog(mContext: Context, private val mListener: DialogInterface.OnClickListener,
private val mWifiConfig: WifiConfiguration?) : AlertDialog(mContext), TextWatcher {
companion object {
private const val BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE
}

private lateinit var mView: View
private lateinit var mSsid: TextView
private lateinit var mPassword: EditText
val config: WifiConfiguration?
get() {
val config = WifiConfiguration()
config.SSID = mSsid.text.toString()
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN)
if (mPassword.length() != 0) {
val password = mPassword.text.toString()
config.preSharedKey = password
}
return config
}

override fun onCreate(savedInstanceState: Bundle?) {
mView = layoutInflater.inflate(R.layout.dialog_wifi_ap, null)
setView(mView)
val context = context
setTitle(R.string.repeater_configure)
mSsid = mView.findViewById(R.id.ssid)
mPassword = mView.findViewById(R.id.password)
setButton(BUTTON_SUBMIT, context.getString(R.string.wifi_save), mListener)
setButton(DialogInterface.BUTTON_NEGATIVE,
context.getString(R.string.wifi_cancel), mListener)
setButton(DialogInterface.BUTTON_NEUTRAL, context.getString(R.string.repeater_reset_credentials), mListener)
if (mWifiConfig != null) {
mSsid.text = mWifiConfig.SSID
mPassword.setText(mWifiConfig.preSharedKey)
}
mSsid.addTextChangedListener(this)
mPassword.addTextChangedListener(this)
super.onCreate(savedInstanceState)
validate()
}

private fun validate() {
val mSsidString = mSsid.text.toString()
val ssidValid = mSsid.length() != 0 && Charset.forName("UTF-8").encode(mSsidString).limit() <= 32
val passwordValid = mPassword.length() >= 8
mView.findViewById<TextInputLayout>(R.id.password_wrapper).error =
if (passwordValid) null else context.getString(R.string.credentials_password_too_short)
getButton(BUTTON_SUBMIT).isEnabled = ssidValid && passwordValid
}

override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { }
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { }
override fun afterTextChanged(editable: Editable) {
validate()
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package be.mygod.vpnhotspot.net.wifi

import android.annotation.TargetApi
import android.content.DialogInterface
import android.net.wifi.WifiConfiguration
import android.net.wifi.WifiConfiguration.AuthAlgorithm
import android.os.Build
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
Expand Down Expand Up @@ -53,12 +51,7 @@ class WifiP2pDialogFragment : DialogFragment(), TextWatcher, DialogInterface.OnC
setTitle(R.string.repeater_configure)
mSsid = mView.findViewById(R.id.ssid)
mPassword = mView.findViewById(R.id.password)
// Note: Reading persistent group information in p2p_supplicant.conf wasn't available until this commit:
// https://android.googlesource.com/platform/external/wpa_supplicant_8/+/216983bceec7c450951e2fbcd076b5c75d432e57%5E%21/
// which isn't merged until Android 6.0.
if (Build.VERSION.SDK_INT >= 23) {
setPositiveButton(context.getString(R.string.wifi_save), this@WifiP2pDialogFragment)
}
setPositiveButton(context.getString(R.string.wifi_save), this@WifiP2pDialogFragment)
setNegativeButton(context.getString(R.string.wifi_cancel), this@WifiP2pDialogFragment)
setNeutralButton(context.getString(R.string.repeater_reset_credentials), this@WifiP2pDialogFragment)
val arguments = arguments!!
Expand Down Expand Up @@ -92,7 +85,7 @@ class WifiP2pDialogFragment : DialogFragment(), TextWatcher, DialogInterface.OnC

override fun onClick(dialog: DialogInterface?, which: Int) {
when (which) {
DialogInterface.BUTTON_POSITIVE -> @TargetApi(23) when (configurer.update(config!!)) {
DialogInterface.BUTTON_POSITIVE -> when (configurer.update(config!!)) {
true -> {
app.handler.postDelayed((targetFragment as TetheringFragment).adapter.repeaterManager
.binder!!::requestGroupUpdate, 1000)
Expand Down

0 comments on commit 81dc43f

Please sign in to comment.