Skip to content

Commit

Permalink
backport: 20230414
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Apr 17, 2023
1 parent 4f035ca commit 567f64b
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ fun StandardV2RayBean.parseDuckSoft(url: HttpUrl) {
}
}

// maybe from matsuri vmess exoprt
if (this is VMessBean) {
url.queryParameter("encryption")?.let {
encryption = it
}
}

url.queryParameter("packetEncoding")?.let {
when (it) {
"packet" -> packetEncoding = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ class ConfigurationFragment @JvmOverloads constructor(
}
}
}

R.id.action_remove_duplicate -> {
runOnDefaultDispatcher {
val profiles = SagerDatabase.proxyDao.getByGroup(DataStore.currentGroupId())
Expand Down Expand Up @@ -552,6 +553,7 @@ class ConfigurationFragment @JvmOverloads constructor(
}
}
}

R.id.action_connection_icmp_ping -> {
pingTest(true)
}
Expand Down Expand Up @@ -724,37 +726,38 @@ class ConfigurationFragment @JvmOverloads constructor(

suspend fun update(profile: ProxyEntity) {
fragment?.configurationListView?.post {
val context = context ?: return@post
if (!isAdded) return@post

var profileStatusText: String? = null
var profileStatusColor = 0

when (profile.status) {
-1 -> {
profileStatusText = profile.error
profileStatusColor =
requireContext().getColorAttr(android.R.attr.textColorSecondary)
profileStatusColor = context.getColorAttr(android.R.attr.textColorSecondary)
}

0 -> {
profileStatusText = getString(R.string.connection_test_testing)
profileStatusColor =
requireContext().getColorAttr(android.R.attr.textColorSecondary)
profileStatusColor = context.getColorAttr(android.R.attr.textColorSecondary)
}

1 -> {
profileStatusText = getString(R.string.available, profile.ping)
profileStatusColor = requireContext().getColour(R.color.material_green_500)
profileStatusColor = context.getColour(R.color.material_green_500)
}

2 -> {
profileStatusText = profile.error
profileStatusColor = requireContext().getColour(R.color.material_red_500)
profileStatusColor = context.getColour(R.color.material_red_500)
}

3 -> {
val err = profile.error ?: ""
val msg = Protocols.genFriendlyMsg(err)
profileStatusText = if (msg != err) msg else getString(R.string.unavailable)
profileStatusColor = requireContext().getColour(R.color.material_red_500)
profileStatusColor = context.getColour(R.color.material_red_500)
}
}

Expand All @@ -763,7 +766,7 @@ class ConfigurationFragment @JvmOverloads constructor(
append("\n")
append(
profile.displayType(),
ForegroundColorSpan(requireContext().getProtocolColor(profile.type)),
ForegroundColorSpan(context.getProtocolColor(profile.type)),
SPAN_EXCLUSIVE_EXCLUSIVE
)
append(" ")
Expand Down Expand Up @@ -1508,16 +1511,20 @@ class ConfigurationFragment @JvmOverloads constructor(
}

override suspend fun onUpdated(profileId: Long, trafficStats: TrafficStats) {
val index = configurationIdList.indexOf(profileId)
if (index != -1) {
val holder = layoutManager.findViewByPosition(index)
?.let { configurationListView.getChildViewHolder(it) } as ConfigurationHolder?
if (holder != null) {
holder.entity.stats = trafficStats
onMainDispatcher {
holder.bind(holder.entity)
try {
val index = configurationIdList.indexOf(profileId)
if (index != -1) {
val holder = layoutManager.findViewByPosition(index)
?.let { configurationListView.getChildViewHolder(it) } as ConfigurationHolder?
if (holder != null) {
holder.entity.stats = trafficStats
onMainDispatcher {
holder.bind(holder.entity)
}
}
}
} catch (e: Exception) {
Logs.w(e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.os.Parcelable
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AlertDialog
import androidx.core.view.ViewCompat
Expand All @@ -36,9 +37,11 @@ import com.takisoft.preferencex.SimpleMenuPreference
import io.nekohasekai.sagernet.GroupType
import io.nekohasekai.sagernet.Key
import io.nekohasekai.sagernet.R
import io.nekohasekai.sagernet.SagerNet
import io.nekohasekai.sagernet.SubscriptionType
import io.nekohasekai.sagernet.database.*
import io.nekohasekai.sagernet.database.preference.OnPreferenceDataStoreChangeListener
import io.nekohasekai.sagernet.ktx.Logs
import io.nekohasekai.sagernet.ktx.applyDefaultValues
import io.nekohasekai.sagernet.ktx.onMainDispatcher
import io.nekohasekai.sagernet.ktx.runOnDefaultDispatcher
Expand Down Expand Up @@ -118,7 +121,8 @@ class GroupSettingsActivity(
val subscriptionType = findPreference<SimpleMenuPreference>(Key.SUBSCRIPTION_TYPE)!!
val subscriptionLink = findPreference<EditTextPreference>(Key.SUBSCRIPTION_LINK)!!
val subscriptionToken = findPreference<EditTextPreference>(Key.SUBSCRIPTION_TOKEN)!!
val subscriptionUserAgent = findPreference<UserAgentPreference>(Key.SUBSCRIPTION_USER_AGENT)!!
val subscriptionUserAgent =
findPreference<UserAgentPreference>(Key.SUBSCRIPTION_USER_AGENT)!!

fun updateSubscriptionType(subscriptionType: Int = DataStore.subscriptionType) {
val isRaw = subscriptionType == SubscriptionType.RAW
Expand All @@ -135,8 +139,10 @@ class GroupSettingsActivity(
true
}

val subscriptionAutoUpdate = findPreference<SwitchPreference>(Key.SUBSCRIPTION_AUTO_UPDATE)!!
val subscriptionAutoUpdateDelay = findPreference<EditTextPreference>(Key.SUBSCRIPTION_AUTO_UPDATE_DELAY)!!
val subscriptionAutoUpdate =
findPreference<SwitchPreference>(Key.SUBSCRIPTION_AUTO_UPDATE)!!
val subscriptionAutoUpdateDelay =
findPreference<EditTextPreference>(Key.SUBSCRIPTION_AUTO_UPDATE_DELAY)!!
subscriptionAutoUpdateDelay.isEnabled = subscriptionAutoUpdate.isChecked
subscriptionAutoUpdateDelay.setOnPreferenceChangeListener { _, newValue ->
val delay = (newValue as String).toIntOrNull()
Expand Down Expand Up @@ -291,8 +297,17 @@ class GroupSettingsActivity(

override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = DataStore.profileCacheStore
activity.apply {
createPreferences(savedInstanceState, rootKey)
try {
activity.apply {
createPreferences(savedInstanceState, rootKey)
}
} catch (e: Exception) {
Toast.makeText(
SagerNet.application,
"Error on createPreferences, please try again.",
Toast.LENGTH_SHORT
).show()
Logs.e(e)
}
}

Expand All @@ -318,12 +333,14 @@ class GroupSettingsActivity(
}
true
}

R.id.action_apply -> {
runOnDefaultDispatcher {
activity.saveAndExit()
}
true
}

else -> false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import android.os.Parcelable
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.activity.result.component1
import androidx.activity.result.component2
import androidx.activity.result.contract.ActivityResultContracts
Expand All @@ -43,11 +44,13 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.takisoft.preferencex.PreferenceFragmentCompat
import io.nekohasekai.sagernet.Key
import io.nekohasekai.sagernet.R
import io.nekohasekai.sagernet.SagerNet
import io.nekohasekai.sagernet.database.DataStore
import io.nekohasekai.sagernet.database.ProfileManager
import io.nekohasekai.sagernet.database.RuleEntity
import io.nekohasekai.sagernet.database.SagerDatabase
import io.nekohasekai.sagernet.database.preference.OnPreferenceDataStoreChangeListener
import io.nekohasekai.sagernet.ktx.Logs
import io.nekohasekai.sagernet.ktx.app
import io.nekohasekai.sagernet.ktx.onMainDispatcher
import io.nekohasekai.sagernet.ktx.runOnDefaultDispatcher
Expand Down Expand Up @@ -355,8 +358,17 @@ class RouteSettingsActivity(

override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = DataStore.profileCacheStore
activity.apply {
createPreferences(savedInstanceState, rootKey)
try {
activity.apply {
createPreferences(savedInstanceState, rootKey)
}
} catch (e: Exception) {
Toast.makeText(
SagerNet.application,
"Error on createPreferences, please try again.",
Toast.LENGTH_SHORT
).show()
Logs.e(e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AlertDialog
import androidx.core.content.pm.ShortcutInfoCompat
Expand All @@ -50,10 +52,7 @@ import io.nekohasekai.sagernet.database.SagerDatabase
import io.nekohasekai.sagernet.database.preference.OnPreferenceDataStoreChangeListener
import io.nekohasekai.sagernet.databinding.LayoutGroupItemBinding
import io.nekohasekai.sagernet.fmt.AbstractBean
import io.nekohasekai.sagernet.ktx.applyDefaultValues
import io.nekohasekai.sagernet.ktx.onMainDispatcher
import io.nekohasekai.sagernet.ktx.runOnDefaultDispatcher
import io.nekohasekai.sagernet.ktx.runOnMainDispatcher
import io.nekohasekai.sagernet.ktx.*
import io.nekohasekai.sagernet.ui.ThemedActivity
import io.nekohasekai.sagernet.widget.ListListener
import kotlinx.parcelize.Parcelize
Expand Down Expand Up @@ -229,12 +228,17 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(

override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = DataStore.profileCacheStore
activity.apply {
createPreferences(savedInstanceState, rootKey)

if (isSubscription) {
// findPreference<Preference>(Key.PROFILE_NAME)?.isEnabled = false
try {
activity.apply {
createPreferences(savedInstanceState, rootKey)
}
} catch (e: Exception) {
Toast.makeText(
SagerNet.application,
"Error on createPreferences, please try again.",
Toast.LENGTH_SHORT
).show()
Logs.e(e)
}
}

Expand Down Expand Up @@ -267,12 +271,14 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(
}
true
}

R.id.action_apply -> {
runOnDefaultDispatcher {
activity.saveAndExit()
}
true
}

R.id.action_create_shortcut -> {
val ctx = requireContext()
val ent = activity.proxyEntity!!
Expand All @@ -297,6 +303,7 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(
.build()
ShortcutManagerCompat.requestPinShortcut(ctx, shortcut, null)
}

R.id.action_move -> {
val view = LinearLayout(context).apply {
val ent = activity.proxyEntity!!
Expand Down Expand Up @@ -327,9 +334,13 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(
}
}
}
MaterialAlertDialogBuilder(activity).setView(view).show()
val scrollView = ScrollView(context).apply {
addView(view)
}
MaterialAlertDialogBuilder(activity).setView(scrollView).show()
true
}

else -> false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class OOCv1TokenPreference : EditTextPreference {


init {
dialogLayoutResource = R.layout.layout_link_dialog
dialogLayoutResource = R.layout.layout_urltest_preference_dialog

setOnBindEditTextListener { editText ->
editText.isSingleLine = false
Expand Down Expand Up @@ -75,14 +75,17 @@ class OOCv1TokenPreference : EditTextPreference {
linkLayout.error = "Missing field: baseUrl"
isValid = false
}

baseUrl.endsWith("/") -> {
linkLayout.error = "baseUrl must not contain a trailing slash"
isValid = false
}

!baseUrl.startsWith("https://") -> {
isValid = false
linkLayout.error = "Protocol scheme must be https"
}

else -> try {
baseUrl.toHttpUrl()
} catch (e: Exception) {
Expand All @@ -105,7 +108,8 @@ class OOCv1TokenPreference : EditTextPreference {
when {
certSha256.length != 64 -> {
isValid = false
linkLayout.error = "certSha256 must be a SHA-256 hexadecimal string"
linkLayout.error =
"certSha256 must be a SHA-256 hexadecimal string"
}
}
}
Expand Down
Loading

0 comments on commit 567f64b

Please sign in to comment.