mirror of
https://github.com/greenart7c3/Amber.git
synced 2026-10-05 19:08:23 +00:00
Use TorManager's socksPort when checking proxy liveness for built-in Tor
Expose the actual SOCKS port assigned by kmp-tor as TorManager.socksPort (StateFlow<Int>, 0 when not running). isSocksProxyAlive now connects to that port instead of unconditionally returning isRunning, giving a real liveness check. Also clears _socksPort to 0 on stop. https://claude.ai/code/session_01JwcwxdYrDnr3QyHqTBz33a
This commit is contained in:
@@ -45,6 +45,9 @@ object TorManager {
|
||||
private val _isRunning = MutableStateFlow(false)
|
||||
val isRunning: StateFlow<Boolean> = _isRunning.asStateFlow()
|
||||
|
||||
private val _socksPort = MutableStateFlow(0)
|
||||
val socksPort: StateFlow<Int> = _socksPort.asStateFlow()
|
||||
|
||||
fun init(context: Context) {
|
||||
if (appContext != null) return
|
||||
appContext = context.applicationContext
|
||||
@@ -122,6 +125,7 @@ object TorManager {
|
||||
try {
|
||||
val port = addr.port.value
|
||||
Log.i(TAG, "Built-in Tor SOCKS proxy on port $port")
|
||||
_socksPort.value = port
|
||||
HttpClientManager.setDefaultProxyOnPort(port)
|
||||
_isRunning.value = true
|
||||
appContext?.let { showNotification(it.getString(R.string.builtin_tor_active)) }
|
||||
@@ -151,6 +155,7 @@ object TorManager {
|
||||
val runtime = torRuntime ?: return
|
||||
torRuntime = null
|
||||
_isRunning.value = false
|
||||
_socksPort.value = 0
|
||||
cancelNotification()
|
||||
try {
|
||||
runBlocking(Dispatchers.IO) { runtime.stopDaemonAsync() }
|
||||
|
||||
@@ -143,7 +143,16 @@ class Amber :
|
||||
|
||||
fun isSocksProxyAlive(proxyHost: String, proxyPort: Int): Boolean {
|
||||
if (settings.torMode == TorMode.BUILTIN) {
|
||||
return TorManager.isRunning.value
|
||||
val port = TorManager.socksPort.value
|
||||
if (port == 0) return false
|
||||
return try {
|
||||
val socket = Socket()
|
||||
socket.connect(InetSocketAddress(proxyHost, port), 5000)
|
||||
socket.close()
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
try {
|
||||
val socket = Socket()
|
||||
|
||||
Reference in New Issue
Block a user