fix(captcha): -platform mobile и WebView для ручной капчи
This commit is contained in:
1 parent
3d28ec7cb2
commit
e3ca640739
5 files changed
+71
-57
No files matched your search
@@ -44,6 +44,8 @@ CoreArgs {
|
||||
// -browser: профиль VK-auth, только vk. firefox - дефолт ядра (не шлём).
|
||||
val browser = cfg.browser.takeIf { it in Browser.VALUES } ?: Browser.DEFAULT
|
||||
if (cfg.provider == Provider.VK && browser != Browser.FIREFOX) { add("-browser"); add(browser) }
|
||||
// Персона VK-auth всегда мобильная: UA/device/client-hints согласованы с телефоном.
|
||||
if (cfg.provider == Provider.VK) { add("-platform"); add("mobile") }
|
||||
if (cfg.debugMode) add("-debug")
|
||||
// Ручной список DNS имеет приоритет над DNS оператора.
|
||||
val manualDns = DnsList.normalize(cfg.customDns)
|
||||
|
||||
@@ -7,8 +7,6 @@ import android.os.SystemClock
|
||||
import com.freeturn.app.R
|
||||
import com.freeturn.app.data.AppPreferences
|
||||
import com.freeturn.app.data.CoreArgs
|
||||
import com.freeturn.app.data.config.Browser
|
||||
import com.freeturn.app.data.config.ClientConfig
|
||||
import com.freeturn.app.domain.CaptchaSession
|
||||
import com.freeturn.app.domain.ConnectionStats
|
||||
import com.freeturn.app.domain.proxy.CoreConnectionTracker
|
||||
@@ -169,8 +167,9 @@ class CoreProcessController(
|
||||
ProxyServiceState.addLog("Команда: ${CoreArgs.redactForLog(cmdArgs)}")
|
||||
|
||||
val proc = withContext(Dispatchers.IO) {
|
||||
// Легаси: сносим осиротевшие профили захвата (сам механизм захвата удалён).
|
||||
context.filesDir.listFiles { f -> f.name.startsWith("vk_profile") }?.forEach { it.delete() }
|
||||
val pb = ProcessBuilder(cmdArgs).redirectErrorStream(true)
|
||||
pb.environment()["VK_PROFILE_PATH"] = vkProfileFile(cfg, executableFile).absolutePath
|
||||
// CWD подменяем на writeable dir для логов кэша tls-client и т.п.
|
||||
pb.directory(context.filesDir)
|
||||
pb.start()
|
||||
@@ -366,15 +365,5 @@ class CoreProcessController(
|
||||
}, delayMs)
|
||||
}
|
||||
|
||||
private fun vkProfileFile(cfg: ClientConfig, executable: File): File {
|
||||
val browser = cfg.browser.takeIf { it in Browser.VALUES } ?: Browser.DEFAULT
|
||||
val stamp = executable.lastModified().takeIf { it > 0L } ?: 0L
|
||||
val currentName = "vk_profile_${browser}_$stamp.json"
|
||||
context.filesDir.listFiles { file ->
|
||||
file.name == "vk_profile.json" ||
|
||||
(file.name.startsWith("vk_profile_${browser}_") && file.name != currentName)
|
||||
}?.forEach { it.delete() }
|
||||
return File(context.filesDir, currentName)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,41 +5,56 @@
|
||||
|
||||
package com.freeturn.app.ui.screens.captcha
|
||||
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.annotation.SuppressLint
|
||||
import android.webkit.WebChromeClient
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.LinearProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableFloatStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.viewinterop.AndroidView
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import androidx.core.net.toUri
|
||||
import com.freeturn.app.R
|
||||
|
||||
/** Разрешённые схемы для открытия ручной капчи во внешнем браузере. */
|
||||
private val ALLOWED_SCHEMES = setOf("http", "https")
|
||||
/** Только локальный captcha-прокси ядра допускается к загрузке в WebView. */
|
||||
private fun isLocalCaptchaUrl(url: String): Boolean {
|
||||
val uri = url.toUri()
|
||||
val host = uri.host ?: return false
|
||||
return uri.scheme?.lowercase() == "http" && (host == "127.0.0.1" || host == "localhost")
|
||||
}
|
||||
|
||||
/**
|
||||
* Ручная VK-капча во встроенном WebView. Грузит локальный прокси ядра
|
||||
* (127.0.0.1:8765), который переписывает VK-страницы так, чтобы весь трафик шёл
|
||||
* через loopback. Диалог закрывается, когда ядро отрапортует о решении капчи
|
||||
* (CoreLogParser -> ProxyServiceState.captchaSession = null).
|
||||
*/
|
||||
@Composable
|
||||
fun CaptchaWebViewDialog(
|
||||
captchaUrl: String,
|
||||
onDismiss: () -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val valid = remember(captchaUrl) { isLocalCaptchaUrl(captchaUrl) }
|
||||
|
||||
Dialog(
|
||||
onDismissRequest = onDismiss,
|
||||
@@ -73,53 +88,61 @@ fun CaptchaWebViewDialog(
|
||||
)
|
||||
}
|
||||
) { padding ->
|
||||
// URL приходит из лога ядра - открываем только http(s).
|
||||
val isValidUrl = remember(captchaUrl) {
|
||||
captchaUrl.toUri().scheme?.lowercase() in ALLOWED_SCHEMES
|
||||
}
|
||||
LaunchedEffect(captchaUrl, isValidUrl) {
|
||||
if (isValidUrl) {
|
||||
openCaptchaInBrowser(context, captchaUrl)
|
||||
} else {
|
||||
onDismiss()
|
||||
}
|
||||
}
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(padding)
|
||||
) {
|
||||
if (!isValidUrl) {
|
||||
LaunchedEffect(captchaUrl) { onDismiss() }
|
||||
if (valid) {
|
||||
CaptchaWebView(url = captchaUrl)
|
||||
} else {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.align(Alignment.Center)
|
||||
.padding(horizontal = 24.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Text(
|
||||
text = stringResource(R.string.captcha_browser_hint),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
TextButton(onClick = { openCaptchaInBrowser(context, captchaUrl) }) {
|
||||
Text(stringResource(R.string.captcha_open_browser))
|
||||
}
|
||||
}
|
||||
Text(
|
||||
text = stringResource(R.string.captcha_browser_hint),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
modifier = Modifier.padding(24.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun openCaptchaInBrowser(context: Context, url: String) {
|
||||
val intent = Intent(Intent.ACTION_VIEW, url.toUri())
|
||||
.addCategory(Intent.CATEGORY_BROWSABLE)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
try {
|
||||
context.startActivity(intent)
|
||||
} catch (_: ActivityNotFoundException) {
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
@Composable
|
||||
private fun CaptchaWebView(url: String) {
|
||||
val context = LocalContext.current
|
||||
var progress by remember { mutableFloatStateOf(0f) }
|
||||
|
||||
Box(modifier = Modifier.fillMaxSize()) {
|
||||
AndroidView(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
factory = {
|
||||
WebView(context).apply {
|
||||
settings.javaScriptEnabled = true
|
||||
settings.domStorageEnabled = true
|
||||
// Капча грузит ресурсы VK через loopback-прокси - навигация не
|
||||
// должна утекать во внешний браузер.
|
||||
webViewClient = object : WebViewClient() {
|
||||
override fun shouldOverrideUrlLoading(
|
||||
view: WebView,
|
||||
request: android.webkit.WebResourceRequest
|
||||
): Boolean = !isLocalCaptchaUrl(request.url.toString())
|
||||
}
|
||||
webChromeClient = object : WebChromeClient() {
|
||||
override fun onProgressChanged(view: WebView, newProgress: Int) {
|
||||
progress = newProgress / 100f
|
||||
}
|
||||
}
|
||||
loadUrl(url)
|
||||
}
|
||||
}
|
||||
)
|
||||
if (progress < 1f) {
|
||||
LinearProgressIndicator(
|
||||
progress = { progress },
|
||||
modifier = Modifier.fillMaxWidth().align(Alignment.TopCenter)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
Binary file not shown.
Reference in new issue
Block a user