Skip to content

Commit f078f0a

Browse files
committed
fix(android): Fix MTU export not saving results by using correct config directory and fallback filename pattern
1 parent 6bc5295 commit f078f0a

1 file changed

Lines changed: 19 additions & 23 deletions

File tree

android/app/src/main/java/com/masterdns/vpn/service/MasterDnsVpnService.kt

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ class MasterDnsVpnService : VpnService() {
6161
private var logTailJob: Job? = null
6262
private var wakeLock: PowerManager.WakeLock? = null
6363
private var mtuExportTargetUri: String? = null
64-
private var mtuTempOutputDir: File? = null
65-
private var mtuResultsFilePath: String? = null
64+
private var mtuConfigDir: File? = null
6665
@Volatile
6766
private var isStopping = false
6867
@Volatile
@@ -120,8 +119,7 @@ class MasterDnsVpnService : VpnService() {
120119
val configFile = File(configDir, "client_config.toml")
121120
val resolversFile = File(configDir, "client_resolvers.txt")
122121
mtuExportTargetUri = null
123-
mtuTempOutputDir = null
124-
mtuResultsFilePath = null
122+
mtuConfigDir = null
125123
val advanced = parseAdvanced(profile.advancedJson)
126124
val saveMtuToFile = advanced["SAVE_MTU_SERVERS_TO_FILE"].equals("true", ignoreCase = true)
127125
var runtimeProfile = profile
@@ -133,14 +131,14 @@ class MasterDnsVpnService : VpnService() {
133131
val exportUri = advanced["MTU_EXPORT_URI"]?.trim().orEmpty()
134132
if (exportUri.isNotBlank()) {
135133
val advancedMutable = advanced.toMutableMap()
136-
val tmpDir = File(filesDir, "mtu_exports").apply { mkdirs() }
137-
val tmpPath = File(tmpDir, "mtu_results.log").absolutePath
138-
advancedMutable["MTU_SERVERS_FILE_NAME"] = tmpPath
134+
configDir.mkdirs()
135+
val targetPath = "masterdnsvpn_success_test_{time}.log"
136+
advancedMutable["MTU_SERVERS_FILE_NAME"] = targetPath
139137
runtimeProfile = profile.copy(advancedJson = Gson().toJson(advancedMutable))
140138
mtuExportTargetUri = exportUri
141-
mtuTempOutputDir = tmpDir
142-
mtuResultsFilePath = tmpPath
143-
VpnManager.appendLog("MTU results temp path: $tmpPath")
139+
mtuConfigDir = configDir
140+
mtuResultsFilePath = null
141+
VpnManager.appendLog("MTU results will be saved to config directory")
144142
VpnManager.appendLog("MTU export destination selected via file manager")
145143
} else {
146144
VpnManager.appendLog("MTU results target: $configuredPath")
@@ -455,9 +453,8 @@ class MasterDnsVpnService : VpnService() {
455453

456454
private fun exportMtuResultsIfNeeded() {
457455
val target = mtuExportTargetUri?.takeIf { it.isNotBlank() } ?: return
458-
val dir = mtuTempOutputDir ?: return
459-
val preferred = mtuResultsFilePath?.let { File(it) }
460-
val sourceFile = resolveMtuResultsSourceFile(preferred, dir)
456+
val dir = mtuConfigDir ?: return
457+
val sourceFile = resolveMtuResultsSourceFile(dir)
461458
if (sourceFile == null) {
462459
VpnManager.appendLog("MTU export skipped: no results generated")
463460
return
@@ -469,25 +466,24 @@ class MasterDnsVpnService : VpnService() {
469466
} ?: error("Cannot open selected destination")
470467
}.onSuccess {
471468
VpnManager.appendLog("MTU results exported to selected destination")
469+
VpnManager.appendLog("Exported file: ${sourceFile.absolutePath}")
472470
}.onFailure {
473471
VpnManager.appendLog("MTU export failed: ${it.message}")
474472
}
475473
}
476474

477-
private fun resolveMtuResultsSourceFile(preferred: File?, dir: File): File? {
478-
// Go may flush MTU writes shortly after stop; retry briefly before exporting.
475+
private fun resolveMtuResultsSourceFile(dir: File): File? {
479476
repeat(5) {
480-
if (preferred != null && preferred.exists() && preferred.length() > 0L) {
481-
return preferred
477+
val sourceFile = dir.listFiles()
478+
?.asSequence()
479+
?.filter { it.isFile && it.name.startsWith("masterdnsvpn_success_test") && it.length() > 0L }
480+
?.maxByOrNull { it.lastModified() }
481+
if (sourceFile != null) {
482+
return sourceFile
482483
}
483484
Thread.sleep(200L)
484485
}
485-
486-
// Fallback: pick the newest non-empty file in MTU export temp directory.
487-
return dir.listFiles()
488-
?.asSequence()
489-
?.filter { it.isFile && it.length() > 0L }
490-
?.maxByOrNull { it.lastModified() }
486+
return null
491487
}
492488

493489
private suspend fun ensureSocksPortAvailable(port: Int) {

0 commit comments

Comments
 (0)