Improve and fix proxy setup/usage

* Switch connectivitycheck URL to HTTPS as cleartext communication is disabled
* Use GlobalScope to verify and setup proxy URL
* Ignore proxy setup in HttpClient if its empty

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2024-03-05 13:07:57 +05:30
parent 80df8affe4
commit ac3065ab2c
5 changed files with 62 additions and 85 deletions

View File

@@ -47,7 +47,7 @@ object Constants {
const val URL_DISPENSER = "https://auroraoss.com/api/auth" const val URL_DISPENSER = "https://auroraoss.com/api/auth"
const val PLAY_QUERY_URL = "https://play.google.com/store/search?q=" const val PLAY_QUERY_URL = "https://play.google.com/store/search?q="
const val ANDROID_CONNECTIVITY_URL = "http://connectivitycheck.android.com/generate_204" const val ANDROID_CONNECTIVITY_URL = "https://connectivitycheck.android.com/generate_204"
//ACCOUNTS //ACCOUNTS
const val ACCOUNT_SIGNED_IN = "ACCOUNT_SIGNED_IN" const val ACCOUNT_SIGNED_IN = "ACCOUNT_SIGNED_IN"

View File

@@ -27,13 +27,10 @@ import com.google.gson.Gson
object HttpClient { object HttpClient {
fun getPreferredClient(context: Context): IProxyHttpClient { fun getPreferredClient(context: Context): IProxyHttpClient {
val proxyEnabled = Preferences.getBoolean( val proxyEnabled = Preferences.getBoolean(context, Preferences.PREFERENCE_PROXY_ENABLED)
context, val proxyInfoString = Preferences.getString(context, Preferences.PREFERENCE_PROXY_INFO)
Preferences.PREFERENCE_PROXY_ENABLED
)
return if (proxyEnabled) { return if (proxyEnabled && proxyInfoString.isNotBlank() && proxyInfoString != "{}") {
val proxyInfoString = Preferences.getString(context, Preferences.PREFERENCE_PROXY_INFO)
val proxyInfo = Gson().fromJson(proxyInfoString, ProxyInfo::class.java) val proxyInfo = Gson().fromJson(proxyInfoString, ProxyInfo::class.java)
if (proxyInfo != null) { if (proxyInfo != null) {

View File

@@ -57,16 +57,13 @@ object OkHttpClient : IProxyHttpClient {
override fun setProxy(proxyInfo: ProxyInfo): OkHttpClient { override fun setProxy(proxyInfo: ProxyInfo): OkHttpClient {
val proxy = Proxy( val proxy = Proxy(
if (proxyInfo.protocol == "SOCKS") Proxy.Type.SOCKS else Proxy.Type.HTTP, if (proxyInfo.protocol == "SOCKS") Proxy.Type.SOCKS else Proxy.Type.HTTP,
InetSocketAddress.createUnresolved( InetSocketAddress.createUnresolved(proxyInfo.host, proxyInfo.port)
proxyInfo.host,
proxyInfo.port
)
) )
val proxyUser = proxyInfo.proxyUser val proxyUser = proxyInfo.proxyUser
val proxyPassword = proxyInfo.proxyPassword val proxyPassword = proxyInfo.proxyPassword
if (proxyUser != null && proxyPassword != null) { if (!proxyUser.isNullOrBlank() && !proxyPassword.isNullOrBlank()) {
okHttpClientBuilder.proxyAuthenticator { _, response -> okHttpClientBuilder.proxyAuthenticator { _, response ->
if (response.request.header("Proxy-Authorization") != null) { if (response.request.header("Proxy-Authorization") != null) {
return@proxyAuthenticator null return@proxyAuthenticator null
@@ -82,7 +79,6 @@ object OkHttpClient : IProxyHttpClient {
okHttpClientBuilder.proxy(proxy) okHttpClientBuilder.proxy(proxy)
okHttpClient = okHttpClientBuilder.build() okHttpClient = okHttpClientBuilder.build()
return this return this
} }

View File

@@ -43,9 +43,6 @@ import com.aurora.store.util.Preferences.PREFERENCE_FOR_YOU
import com.aurora.store.util.Preferences.PREFERENCE_INSECURE_ANONYMOUS import com.aurora.store.util.Preferences.PREFERENCE_INSECURE_ANONYMOUS
import com.aurora.store.util.Preferences.PREFERENCE_INSTALLER_ID import com.aurora.store.util.Preferences.PREFERENCE_INSTALLER_ID
import com.aurora.store.util.Preferences.PREFERENCE_INTRO import com.aurora.store.util.Preferences.PREFERENCE_INTRO
import com.aurora.store.util.Preferences.PREFERENCE_PROXY_ENABLED
import com.aurora.store.util.Preferences.PREFERENCE_PROXY_INFO
import com.aurora.store.util.Preferences.PREFERENCE_PROXY_URL
import com.aurora.store.util.Preferences.PREFERENCE_SIMILAR import com.aurora.store.util.Preferences.PREFERENCE_SIMILAR
import com.aurora.store.util.Preferences.PREFERENCE_THEME_ACCENT import com.aurora.store.util.Preferences.PREFERENCE_THEME_ACCENT
import com.aurora.store.util.Preferences.PREFERENCE_THEME_TYPE import com.aurora.store.util.Preferences.PREFERENCE_THEME_TYPE
@@ -160,9 +157,6 @@ class OnboardingFragment : Fragment(R.layout.fragment_onboarding) {
/*Network*/ /*Network*/
save(PREFERENCE_INSECURE_ANONYMOUS, false) save(PREFERENCE_INSECURE_ANONYMOUS, false)
save(PREFERENCE_PROXY_ENABLED, false)
save(PREFERENCE_PROXY_URL, "")
save(PREFERENCE_PROXY_INFO, "{}")
save(PREFERENCE_VENDING_VERSION, 0) save(PREFERENCE_VENDING_VERSION, 0)
/*Customization*/ /*Customization*/

View File

@@ -20,14 +20,16 @@
package com.aurora.store.view.ui.preferences package com.aurora.store.view.ui.preferences
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.view.View import android.view.View
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import com.aurora.Constants import com.aurora.Constants
import com.aurora.extensions.runAsync
import com.aurora.extensions.runOnUiThread import com.aurora.extensions.runOnUiThread
import com.aurora.extensions.toast import com.aurora.extensions.toast
import com.aurora.store.R import com.aurora.store.R
@@ -40,6 +42,10 @@ import com.aurora.store.view.custom.preference.ListPreferenceMaterialDialogFragm
import com.google.gson.Gson import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
@AndroidEntryPoint @AndroidEntryPoint
class NetworkPreference : PreferenceFragmentCompat() { class NetworkPreference : PreferenceFragmentCompat() {
@@ -47,80 +53,64 @@ class NetworkPreference : PreferenceFragmentCompat() {
@Inject @Inject
lateinit var gson: Gson lateinit var gson: Gson
private val TAG = NetworkPreference::class.java.simpleName
@OptIn(DelicateCoroutinesApi::class)
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences_network, rootKey) setPreferencesFromResource(R.xml.preferences_network, rootKey)
val proxyUrl = Preferences.getString(requireContext(), Preferences.PREFERENCE_PROXY_URL, "") findPreference<EditTextPreference>(Preferences.PREFERENCE_PROXY_URL)?.let {
val proxyInfo = it.summary =
Preferences.getString(requireContext(), Preferences.PREFERENCE_PROXY_INFO, "{}") Preferences.getString(requireContext(), Preferences.PREFERENCE_PROXY_URL, "")
val preferenceProxyUrl: Preference? = findPreference(Preferences.PREFERENCE_PROXY_URL) it.setOnPreferenceChangeListener { _, newValue ->
preferenceProxyUrl?.summary = proxyUrl val newProxyUrl = newValue.toString()
val newProxyInfo = CommonUtil.parseProxyUrl(newProxyUrl)
preferenceProxyUrl?.let { if (newProxyInfo == null) {
it.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { preference, newValue ->
val newProxyUrl = newValue.toString()
val newProxyInfo = CommonUtil.parseProxyUrl(newProxyUrl)
if (newProxyInfo == null) {
runOnUiThread {
requireContext().toast(getString(R.string.toast_proxy_invalid))
}
false
} else {
runAsync {
kotlin.runCatching {
val result = HttpClient
.getPreferredClient(requireContext())
.setProxy(newProxyInfo)
.get(
Constants.ANDROID_CONNECTIVITY_URL,
mapOf()
)
runOnUiThread {
if (result.code == 204) {
requireContext().toast(getString(R.string.toast_proxy_success))
save(Preferences.PREFERENCE_PROXY_URL, newProxyUrl)
save(
Preferences.PREFERENCE_PROXY_INFO,
gson.toJson(newProxyInfo)
)
preference.summary = newProxyUrl
} else {
throw Exception("Failed to set proxy")
}
}
}.onFailure {
runOnUiThread {
requireContext().toast(getText(R.string.toast_proxy_failed))
save(Preferences.PREFERENCE_PROXY_URL, proxyUrl)
save(
Preferences.PREFERENCE_PROXY_INFO,
proxyInfo
)
}
}
}
true
}
}
}
val insecureAnonymous: Preference? =
findPreference(Preferences.PREFERENCE_INSECURE_ANONYMOUS)
insecureAnonymous?.let {
it.onPreferenceClickListener =
Preference.OnPreferenceClickListener {
runOnUiThread { runOnUiThread {
requireContext().toast(R.string.insecure_anonymous_apply) requireContext().toast(getString(R.string.toast_proxy_invalid))
} }
false false
} else {
GlobalScope.launch(Dispatchers.IO) {
try {
val result = HttpClient
.getPreferredClient(requireContext())
.setProxy(newProxyInfo)
.get(Constants.ANDROID_CONNECTIVITY_URL, mapOf())
if (result.code == 204) {
save(Preferences.PREFERENCE_PROXY_URL, newProxyUrl)
save(Preferences.PREFERENCE_PROXY_INFO, gson.toJson(newProxyInfo))
runOnUiThread {
requireContext().toast(getString(R.string.toast_proxy_success))
}
it.summary = newProxyUrl
} else {
throw Exception("Failed to set proxy")
}
} catch (exception: Exception) {
Log.e(TAG, "Failed to set proxy", exception)
runOnUiThread {
requireContext().toast(getText(R.string.toast_proxy_failed))
}
}
}
true
} }
}
}
findPreference<SwitchPreferenceCompat>(Preferences.PREFERENCE_INSECURE_ANONYMOUS)?.let {
it.setOnPreferenceChangeListener { _, _ ->
runOnUiThread {
requireContext().toast(R.string.insecure_anonymous_apply)
}
false
}
} }
findPreference<Preference>(Preferences.PREFERENCE_VENDING_VERSION)?.let { findPreference<Preference>(Preferences.PREFERENCE_VENDING_VERSION)?.let {