Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ import moe.matsuri.nb4a.utils.Util
import moe.matsuri.nb4a.utils.listByLineOrComma
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull

private fun sanitizeDnsEntry(value: String): String {
return value.filterNot { it.isISOControl() }.trim()
}

const val TAG_MIXED = "mixed-in"

const val TAG_PROXY = "proxy"
Expand Down Expand Up @@ -144,9 +148,9 @@ fun buildConfig(
val isVPN = DataStore.serviceMode == Key.MODE_VPN
val bind = if (!forTest && DataStore.allowAccess) "0.0.0.0" else LOCALHOST
val remoteDns = DataStore.remoteDns.split("\n")
.mapNotNull { dns -> dns.trim().takeIf { it.isNotBlank() && !it.startsWith("#") } }
.mapNotNull { dns -> sanitizeDnsEntry(dns).takeIf { it.isNotBlank() && !it.startsWith("#") } }
val directDNS = DataStore.directDns.split("\n")
.mapNotNull { dns -> dns.trim().takeIf { it.isNotBlank() && !it.startsWith("#") } }
.mapNotNull { dns -> sanitizeDnsEntry(dns).takeIf { it.isNotBlank() && !it.startsWith("#") } }
val enableDnsRouting = DataStore.enableDnsRouting
val useFakeDns = DataStore.enableFakeDns && !forTest
val needSniff = DataStore.trafficSniffing > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
true
}

private fun sanitizeDnsPreferenceValue(value: String): String {
return value.lines().joinToString("\n") { line ->
line.filterNot { it.isISOControl() }.trim()
}
}

private fun dnsReloadListener(preference: Preference, newValue: Any?): Boolean {
val rawValue = newValue as? String ?: return reloadListener.onPreferenceChange(preference, newValue)
val sanitizedValue = sanitizeDnsPreferenceValue(rawValue)
if (sanitizedValue != rawValue) {
if (preference is EditTextPreference) {
preference.text = sanitizedValue
}
needReload()
return false
Comment thread
greptile-apps[bot] marked this conversation as resolved.
}
needReload()
return true
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 needReload() is called unconditionally in both branches of the if/else, so it can be hoisted before the conditional to remove the duplication.

Suggested change
private fun dnsReloadListener(preference: Preference, newValue: Any?): Boolean {
val rawValue = newValue as? String ?: return reloadListener.onPreferenceChange(preference, newValue)
val sanitizedValue = sanitizeDnsPreferenceValue(rawValue)
if (sanitizedValue != rawValue) {
if (preference is EditTextPreference) {
preference.text = sanitizedValue
}
needReload()
return false
}
needReload()
return true
}
private fun dnsReloadListener(preference: Preference, newValue: Any?): Boolean {
val rawValue = newValue as? String ?: return reloadListener.onPreferenceChange(preference, newValue)
val sanitizedValue = sanitizeDnsPreferenceValue(rawValue)
needReload()
if (sanitizedValue != rawValue) {
if (preference is EditTextPreference) {
preference.text = sanitizedValue
}
return false
}
return true
}

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = DataStore.configurationStore
DataStore.initGlobal()
Expand Down Expand Up @@ -203,8 +223,12 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
concurrentDial.onPreferenceChangeListener = reloadListener

enableFakeDns.onPreferenceChangeListener = reloadListener
remoteDns.onPreferenceChangeListener = reloadListener
directDns.onPreferenceChangeListener = reloadListener
remoteDns.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue ->
dnsReloadListener(preference, newValue)
}
directDns.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue ->
dnsReloadListener(preference, newValue)
}
enableDnsRouting.onPreferenceChangeListener = reloadListener

ipv6Mode.onPreferenceChangeListener = reloadListener
Expand Down
Loading