@@ -52,47 +52,76 @@ class TorManager(
5252 _statusText .value = " Starting..."
5353 _logs .value = emptyList()
5454
55+ addLog(" [DEBUG] === TorManager.start() called ===" )
56+ addLog(" [DEBUG] label=$label socksPort=$socksPort ctrlPort=$ctrlPort " )
57+ addLog(" [DEBUG] ptDir=$ptDir " )
58+ addLog(" [DEBUG] timeoutSeconds=$timeoutSeconds " )
59+
5560 val dataDir = File (context.filesDir, " data_$socksPort " ).also { it.mkdirs() }
61+ addLog(" [DEBUG] dataDir=${dataDir.absolutePath} exists=${dataDir.exists()} " )
5662
5763 copyGeoIpFiles(dataDir)
5864
5965 val torrcFile = File (dataDir, " torrc" )
6066 torrcFile.writeText(torrcContent)
67+ addLog(" [DEBUG] torrc written to ${torrcFile.absolutePath} size=${torrcFile.length()} " )
6168
62- val torBinary = findTorBinary() ? : run {
69+ val torBinary = findTorBinary()
70+ if (torBinary == null ) {
6371 _state .value = TorState .FAILED
6472 _statusText .value = " tor binary not found"
65- addLog(" [Error] Tor binary not found in bundle" )
73+ addLog(" [ERROR] No tor binary found!" )
74+ addLog(" [ERROR] Checked paths:" )
75+ listOf (" tor/libTor.so" , " tor/tor" , " tor/pluggable_transports/lyrebird" ).forEach { p ->
76+ val f = File (context.filesDir, p)
77+ addLog(" [ERROR] ${f.absolutePath} exists=${f.exists()} canExec=${f.canExecute()} " )
78+ }
79+ addLog(" [ERROR] filesDir contents:" )
80+ context.filesDir.listFiles()?.forEach { f ->
81+ addLog(" [ERROR] ${f.name} ${if (f.isDirectory) " DIR" else " FILE ${f.length()} " } " )
82+ }
83+ File (context.filesDir, " tor" ).listFiles()?.forEach { f ->
84+ addLog(" [ERROR] tor/${f.name} ${if (f.isDirectory) " DIR" else " FILE ${f.length()} " } " )
85+ }
6686 return
6787 }
6888
69- addLog(" [Tor] Binary: $torBinary " )
70- addLog(" [Tor] DataDir: ${dataDir.absolutePath} " )
89+ addLog(" [DEBUG] torBinary=$torBinary " )
90+ addLog(" [DEBUG] torBinary exists=${File (torBinary).exists()} canExecute=${File (torBinary).canExecute()} " )
91+ addLog(" [DEBUG] torBinary size=${File (torBinary).length()} " )
7192
7293 try {
94+ addLog(" [DEBUG] Building ProcessBuilder..." )
7395 val pb = ProcessBuilder (torBinary, " -f" , torrcFile.absolutePath)
7496 .directory(dataDir)
7597 .redirectErrorStream(true )
7698
7799 val env = pb.environment()
78- env[" TOR_PT_STATE_LOCATION" ] = File (dataDir, " pt_state" ).apply { mkdirs() }.absolutePath
100+ val ptStateDir = File (dataDir, " pt_state" ).apply { mkdirs() }
101+ env[" TOR_PT_STATE_LOCATION" ] = ptStateDir.absolutePath
102+ addLog(" [DEBUG] TOR_PT_STATE_LOCATION=${ptStateDir.absolutePath} " )
79103
104+ addLog(" [DEBUG] Starting process..." )
80105 torProcess = pb.start()
106+ addLog(" [DEBUG] Process started, pid=${torProcess?.toString()} " )
81107
82108 val reader = BufferedReader (InputStreamReader (torProcess!! .inputStream))
83109
84110 withContext(Dispatchers .IO ) {
85111 var lastPercent = - 1
86112 var lastMoveTime = System .currentTimeMillis()
87113 val timeoutMs = timeoutSeconds * 1000L
114+ var lineCount = 0
88115
89116 reader.useLines { lines ->
90117 for (line in lines) {
118+ lineCount++
91119 addLog(line)
92120
93121 if (line.contains(" Reading config failed" ) || line.contains(" Failed to parse/validate config" )) {
94122 _statusText .value = " Config error"
95123 _state .value = TorState .FAILED
124+ addLog(" [ERROR] Config parse failed!" )
96125 return @useLines
97126 }
98127
@@ -116,18 +145,28 @@ class TorManager(
116145 ) {
117146 _statusText .value = " Timeout at $lastPercent %"
118147 _state .value = TorState .FAILED
148+ addLog(" [ERROR] Timeout at $lastPercent %" )
119149 stop()
120150 return @useLines
121151 }
122152
123153 if (match != null ) lastPercent = match.groupValues[1 ].toInt()
124154 }
125155 }
156+ addLog(" [DEBUG] Process exited. Total lines: $lineCount " )
157+ }
158+
159+ val exitCode = torProcess?.waitFor()
160+ addLog(" [DEBUG] Process exit code: $exitCode " )
161+ if (_state .value == TorState .CONNECTING ) {
162+ _state .value = TorState .FAILED
163+ _statusText .value = " Process exited with code $exitCode "
126164 }
127165 } catch (e: Exception ) {
128- addLog(" Error: ${e.message} " )
166+ addLog(" [ERROR] Exception: ${e.javaClass.simpleName} : ${e.message} " )
167+ e.stackTrace.forEach { addLog(" [ERROR] at ${it} " ) }
129168 _state .value = TorState .FAILED
130- _statusText .value = " Launch error : ${e.message} "
169+ _statusText .value = " Error : ${e.message} "
131170 }
132171 }
133172
0 commit comments