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
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ android {
applicationId = "com.ericlowry.dnstoggle"
minSdk = 28
targetSdk = 37
versionCode = 38
versionName = "3.2.0"
versionCode = 39
versionName = "3.2.1"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ object Constants {
const val VPN_DUMP_IN_PROCESS_TIMEOUT_MS = 1500L
const val VPN_DUMP_SERVICE_TIMEOUT_MS = 3000L
const val VPN_DUMP_ROOT_TIMEOUT_MS = 2500L
const val VPN_PACKAGE_RESOLVE_RETRY_DELAY_MS = 350L
const val ROOT_VERIFY_TIMEOUT_MS = 15000L

// Thresholds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ object DnsPolicyEvaluator {
if (network.vpnNetworkHandle != null && network.vpnNetworkHandle == cachedVpnNetworkHandle && cachedVpnPackage != null) {
cachedVpnPackage
} else {
val pkg = vpnPackageResolver(context)
var pkg = vpnPackageResolver(context)
if (pkg == null) {
delay(Constants.VPN_PACKAGE_RESOLVE_RETRY_DELAY_MS.milliseconds)
pkg = vpnPackageResolver(context)
}
if (pkg != null) {
cachedVpnNetworkHandle = network.vpnNetworkHandle
cachedVpnPackage = pkg
Expand Down Expand Up @@ -142,8 +146,10 @@ object DnsPolicyEvaluator {
if (matchedVpnProfile != null && matchedVpnProfile.isBypassMode()) {
isCurrentVpnBypassed = true
dnsSettleJob?.cancel()
isTransitioning = false
val targetKey = matchedVpnProfile.toOverrideKey()
if (isInVpnOverride || activeVpnProfileKey != targetKey) {
lastBssid = null
prefs.edit {
putBoolean(Constants.PREF_IS_IN_VPN_OVERRIDE, false)
putString(Constants.PREF_ACTIVE_VPN_PROFILE_OVERRIDE, targetKey)
Expand Down Expand Up @@ -280,9 +286,16 @@ object DnsPolicyEvaluator {

val profiles = NetworkProfileRepository.networkProfiles.value ?: emptyList()
val profile = profiles.find { it.ssid == validSsid }
val preferredMode = prefs.getString(
Constants.PREF_PREFERRED_DNS_MODE,
Constants.DNS_MODE_HOSTNAME
) ?: Constants.DNS_MODE_HOSTNAME
val targetHostname = profile?.targetHostname ?: getGlobalPreferredHostname(context)
val isApplyingHostname =
(profile == null) || (profile.isEnabled && targetHostname != null)
val isApplyingHostname = if (profile == null) {
preferredMode == Constants.DNS_MODE_HOSTNAME && targetHostname != null
} else {
profile.isEnabled && targetHostname != null
}
val isRoam = currentBssid != null && lastBssid != null && currentBssid != lastBssid

if (isApplyingHostname && (!isValidated || !hasInternet)) {
Expand Down
18 changes: 3 additions & 15 deletions app/src/main/java/com/ericlowry/dnstoggle/util/ShizukuUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ object ShizukuUtils {

val userId = android.os.Process.myUid() / 100000

var dumpSucceeded = false
val fastPathResult = try {
val vpnDump: String? =
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
Expand All @@ -199,24 +198,16 @@ object ShizukuUtils {
}

if (pkg != null) {
dumpSucceeded = true
pkg
} else {
var uidResolutionFailed = false
val connDump: String? = executeInProcessDump("connectivity")
val connPkg = if (connDump != null) {
if (connDump != null) {
VpnDumpParser.parseActiveVpnPackage(vpnDump, connDump, userId) { uid ->
val resolved = context.packageManager.getPackagesForUid(uid)?.firstOrNull()
if (resolved == null) {
uidResolutionFailed = true
}
resolved
context.packageManager.getPackagesForUid(uid)?.firstOrNull()
}
} else {
null
}
dumpSucceeded = (connDump != null) && !uidResolutionFailed
connPkg
}
} catch (e: CancellationException) {
throw e
Expand All @@ -229,10 +220,7 @@ object ShizukuUtils {
return fastPathResult
}

if (!dumpSucceeded) {
return fetchViaShizukuUserService(context, userId)
}
return null
return fetchViaShizukuUserService(context, userId)
}

private fun userServiceArgs(context: Context): Shizuku.UserServiceArgs =
Expand Down
47 changes: 32 additions & 15 deletions app/src/main/java/com/ericlowry/dnstoggle/util/VpnDumpParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ package com.ericlowry.dnstoggle.util
*/
object VpnDumpParser {
private val userHeaderRegex =
Regex("""(?:VPN for user|User)\s+(\d+)""", RegexOption.IGNORE_CASE)
Regex("""(?:VPN for user|User|mUserId)\s*[=:]?\s*(\d+)""", RegexOption.IGNORE_CASE)
private val userColonPackageRegex =
Regex("""^\s*(\d+)\s*:\s*(\S+)""")
private val activePackageRegex =
Regex(
"""\b(?:Active package name|mPackage|Package)\s*[=:]\s*(\S+)""",
"""\b(?:Active package name|mPackage|Package|ownerPackage)\s*[=:]\s*(\S+)""",
RegexOption.IGNORE_CASE
)
private val extraPackageRegex =
Regex("""extra:\s*(?:VPN:)?([a-zA-Z][a-zA-Z0-9_]*(?:\.[a-zA-Z0-9_]+)+)""")
private val ownerUidRegex =
Regex("""(?:ownerUid|OwnerUid)\s*[=:]\s*(\d+)""")
Regex("""(?:ownerUid|OwnerUid|mOwnerUid)\s*[=:]\s*(\d+)""")
private val adminUidsRegex =
Regex("""(?:adminUids|AdminUids|administratorUids)\s*[=:]\s*\[\s*(\d+)""")
private val packageNamePattern =
Expand Down Expand Up @@ -76,6 +76,10 @@ object VpnDumpParser {
var inVpnBlock = false
var currentBlockExtraPkg: String? = null
var currentBlockUid: Int? = null
var inCandidateAgentBlock = false
var candidateIsConnected = false
var candidateHasVpn = false
var candidateHasNotVpn = false

fun processCurrentBlock(): String? {
if (!inVpnBlock) {
Expand All @@ -102,28 +106,38 @@ object VpnDumpParser {
val isNetworkAgentLine =
trimmed.startsWith("NetworkAgentInfo[") || trimmed.startsWith("NetworkAgentInfo")
if (isNetworkAgentLine || trimmed.isEmpty()) {
if (inCandidateAgentBlock && candidateHasVpn && candidateIsConnected && !candidateHasNotVpn) {
inVpnBlock = true
}
val result = processCurrentBlock()
if (result != null) {
return result
}
inVpnBlock = false
inCandidateAgentBlock = isNetworkAgentLine
candidateIsConnected = false
candidateHasVpn = false
candidateHasNotVpn = false
currentBlockExtraPkg = null
currentBlockUid = null
}

if (isNetworkAgentLine) {
val hasVpnType = line.contains("type: VPN[") ||
line.contains("TRANSPORT_VPN") ||
line.contains("ni{VPN ") ||
line.contains("ni{VPN[") ||
line.contains("Transports: VPN")
val isConnected = connectedStateRegex.containsMatchIn(line)
val hasNotVpn = line.contains("NOT_VPN")
if (hasVpnType && isConnected && (!hasNotVpn || line.contains("TRANSPORT_VPN"))) {
inVpnBlock = true
if (inCandidateAgentBlock) {
if (line.contains("type: VPN[") ||
line.contains("TRANSPORT_VPN") ||
line.contains("ni{VPN ") ||
line.contains("ni{VPN[") ||
line.contains("Transports: VPN")
) {
candidateHasVpn = true
}
}
if (inVpnBlock) {
if (connectedStateRegex.containsMatchIn(line)) {
candidateIsConnected = true
}
if (line.contains("NOT_VPN") && !line.contains("TRANSPORT_VPN")) {
candidateHasNotVpn = true
}

val extraMatch = extraPackageRegex.find(line)
if (extraMatch != null) {
currentBlockExtraPkg = extraMatch.groupValues[1]
Expand All @@ -139,6 +153,9 @@ object VpnDumpParser {
}
}
}
if (inCandidateAgentBlock && candidateHasVpn && candidateIsConnected && !candidateHasNotVpn) {
inVpnBlock = true
}
val result = processCurrentBlock()
if (result != null) {
return result
Expand Down
118 changes: 59 additions & 59 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_version_format">Versione app: %s</string>
<string name="whats_new">Novità</string>
<string name="whats_new_version_format">Novità nel v%s</string>
<string name="github">GitHub (codice sorgente)</string>
<string name="ok">OK</string>
<string name="cancel">Annulla</string>
<string name="retry">Riprova</string>
<string name="close">Chiudi</string>
<string name="settings">Impostazioni</string>
<string name="on_label">Attivo</string>
<string name="off_label">Disattivato</string>
<string name="menu_content_description">Altre opzioni</string>
<string name="permission_required">Autorizzazione richiesta</string>
<string name="main_permission_message">L\'app richiede un\'autorizzazione speciale per modificare le impostazioni del DNS privato.</string>
<string name="btn_fix_permission">Aggiusta</string>
<string name="open_settings">Apri impostazioni</string>
<string name="private_dns">DNS privato</string>
<string name="disable_dns_test">Salta test di connessione</string>
<string name="disable_dns_test_summary">Attiva questa opzione solo se il DNS funziona ma l\'app lo mostra come non raggiungibile.</string>
<string name="custom_dns_provider">Provider DNS personalizzati</string>
<string name="add_hostname">Aggiungi hostname</string>
<string name="add_hostname_desc">Salva questo hostname attivo</string>
<string name="edit_hostname">Modifica hostname</string>
<string name="dns_hostname_hint">Server DNS</string>
<string name="dns_hostname_placeholder">dns.nextdns.io</string>
<string name="dns_label_hint">Nome (facoltativo)</string>
<string name="delete_hostname_confirm">Rimuovere questo hostname?</string>
<string name="error_empty_dns_host">Impossibile attivare il DNS privato se l\'indirizzo del l\'host è vuoto</string>
<string name="support">Ricevi supporto</string>
<string name="permission_message">Per usare questa opzione, devi concedere le autorizzazioni delle impostazioni tramite root, Shizuku oppure eseguire questo comando ADB sul computer con il debug USB attivato:</string>
<string name="adb_command_label">Comando ADB</string>
<string name="adb_command_content_description">Comando ADB: %s</string>
<string name="copy_adb_command">Copia comando ADB</string>
<string name="command_copied">Comando copiato negli appunti</string>
<string name="grant_via_shizuku">Concedi tramite Shizuku</string>
<string name="grant_via_root">Concedi tramite root</string>
<string name="grant_auto_fallback">Richiedi autorizzazione</string>
<string name="toast_attempting_shizuku">Richiesta autorizzazione tramite Shizuku…</string>
<string name="toast_attempting_root">Richiesta autorizzazione tramite root…</string>
<string name="toast_attempting_fallback">Richiesta autorizzazione automatica…</string>
<string name="grant_failed">Autorizzazione Shizuku/Root non riuscita o negata</string>
<string name="permission_granted">Autorizzazione concessa</string>
<string name="main_permission_title">Autorizzazione impostazioni di sistema richiesta</string>
<string name="error_invalid_dns_host">Formato dell\'indirizzo host DNS non valido</string>
<string name="status_testing_dns">Verifica connessione…</string>
<string name="unsaved_active_label">Attuale (non salvato)</string>
<string name="unsaved_inactive_label">Non attivo (non salvato)</string>
<string name="status_active_reachable">Attivo e raggiungibile</string>
<string name="default_dns_label">Predefinita</string>
<string name="default_badge">PREDEFINITO</string>
<string name="auto_blocked_prefix">Bloccato automaticamente:</string>
<string name="warning_unreachable_dns">Server DNS non raggiungibile nella porta 853 (DoT). Android potrebbe essere comunque connesso correttamente tramite altri metodi.</string>
<string name="status_override_vpn">Sovrascrittura VPN attiva</string>
<string name="status_override_ssid">Profilo Wi-Fi attivo (%s)</string>
<string name="status_override_ssid_profile">Sovrascrittura SSID attiva: %1$s è impostata su %2$s</string>
<string name="off_profile_format">Disattivato (%s)</string>
<string name="enable_private_dns">Attiva DNS privato</string>
<string name="wifi_profiles_title">Profili Wi-Fi</string>
<string name="wifi_profiles_summary">Imposta regole o eccezioni specifiche del DNS privato per le singole reti Wi-Fi.</string>
<string name="app_version_format">Versione app: %s</string>
<string name="whats_new">Novità</string>
<string name="whats_new_version_format">Novità nel v%s</string>
<string name="github">GitHub (codice sorgente)</string>
<string name="ok">OK</string>
<string name="cancel">Annulla</string>
<string name="retry">Riprova</string>
<string name="close">Chiudi</string>
<string name="settings">Impostazioni</string>
<string name="on_label">Attivo</string>
<string name="off_label">Disattivato</string>
<string name="menu_content_description">Altre opzioni</string>
<string name="permission_required">Autorizzazione richiesta</string>
<string name="main_permission_message">L\'app richiede un\'autorizzazione speciale per modificare le impostazioni del DNS privato.</string>
<string name="btn_fix_permission">Aggiusta</string>
<string name="open_settings">Apri impostazioni</string>
<string name="private_dns">DNS privato</string>
<string name="disable_dns_test">Salta test di connessione</string>
<string name="disable_dns_test_summary">Attiva questa opzione solo se il DNS funziona ma l\'app lo mostra come non raggiungibile.</string>
<string name="custom_dns_provider">Provider DNS personalizzati</string>
<string name="add_hostname">Aggiungi hostname</string>
<string name="add_hostname_desc">Salva questo hostname attivo</string>
<string name="edit_hostname">Modifica hostname</string>
<string name="dns_hostname_hint">Server DNS</string>
<string name="dns_hostname_placeholder">dns.nextdns.io</string>
<string name="dns_label_hint">Nome (facoltativo)</string>
<string name="delete_hostname_confirm">Rimuovere questo hostname?</string>
<string name="error_empty_dns_host">Impossibile attivare il DNS privato se l\'indirizzo del l\'host è vuoto</string>
<string name="support">Ricevi supporto</string>
<string name="permission_message">Per usare questa opzione, devi concedere le autorizzazioni delle impostazioni tramite root, Shizuku oppure eseguire questo comando ADB sul computer con il debug USB attivato:</string>
<string name="adb_command_label">Comando ADB</string>
<string name="adb_command_content_description">Comando ADB: %s</string>
<string name="copy_adb_command">Copia comando ADB</string>
<string name="command_copied">Comando copiato negli appunti</string>
<string name="grant_via_shizuku">Concedi tramite Shizuku</string>
<string name="grant_via_root">Concedi tramite root</string>
<string name="grant_auto_fallback">Richiedi autorizzazione</string>
<string name="toast_attempting_shizuku">Richiesta autorizzazione tramite Shizuku…</string>
<string name="toast_attempting_root">Richiesta autorizzazione tramite root…</string>
<string name="toast_attempting_fallback">Richiesta autorizzazione automatica…</string>
<string name="grant_failed">Autorizzazione Shizuku/Root non riuscita o negata</string>
<string name="permission_granted">Autorizzazione concessa</string>
<string name="main_permission_title">Autorizzazione impostazioni di sistema richiesta</string>
<string name="error_invalid_dns_host">Formato dell\'indirizzo host DNS non valido</string>
<string name="status_testing_dns">Verifica connessione…</string>
<string name="unsaved_active_label">Attuale (non salvato)</string>
<string name="unsaved_inactive_label">Non attivo (non salvato)</string>
<string name="status_active_reachable">Attivo e raggiungibile</string>
<string name="default_dns_label">Predefinita</string>
<string name="default_badge">PREDEFINITO</string>
<string name="auto_blocked_prefix">Bloccato automaticamente:</string>
<string name="warning_unreachable_dns">Server DNS non raggiungibile nella porta 853 (DoT). Android potrebbe essere comunque connesso correttamente tramite altri metodi.</string>
<string name="status_override_vpn">Sovrascrittura VPN attiva</string>
<string name="status_override_ssid">Profilo Wi-Fi attivo (%s)</string>
<string name="status_override_ssid_profile">Sovrascrittura SSID attiva: %1$s è impostata su %2$s</string>
<string name="off_profile_format">Disattivato (%s)</string>
<string name="enable_private_dns">Attiva DNS privato</string>
<string name="wifi_profiles_title">Profili Wi-Fi</string>
<string name="wifi_profiles_summary">Imposta regole o eccezioni specifiche del DNS privato per le singole reti Wi-Fi.</string>
</resources>
1 change: 1 addition & 0 deletions fastlane/metadata/android/en-US/changelogs/39.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Made VPN app detection more robust.
1 change: 1 addition & 0 deletions fastlane/metadata/android/fr-FR/changelogs/39.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- La détéction d'applications VPN est maintenant plus robuste.
Loading