Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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,24 @@ 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: EditTextPreference, newValue: Any?): Boolean {
val rawValue = newValue as? String ?: return reloadListener.onPreferenceChange(preference, newValue)
val sanitizedValue = sanitizeDnsPreferenceValue(rawValue)
if (sanitizedValue != rawValue) {
preference.text = sanitizedValue
needReload()
return false
Comment thread
greptile-apps[bot] marked this conversation as resolved.
}
needReload()
return true
}

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

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

ipv6Mode.onPreferenceChangeListener = reloadListener
Expand Down
Loading