feat: propagate WebView user agent to plugins via bridge.captchaUserAgent and bridge.authUserAgent

This commit is contained in:
Stefan
2026-02-27 06:59:35 +00:00
committed by Koen
parent a8decdb0d9
commit 8a0e49232e
10 changed files with 69 additions and 25 deletions
@@ -68,11 +68,15 @@ class CaptchaActivity : AppCompatActivity() {
intent.getStringExtra("body"); intent.getStringExtra("body");
else null; else null;
_webView.settings.userAgentString = captchaConfig.userAgent ?: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"; // Only override UA if config specifies one, otherwise keep WebView default (matches Desktop CEF behavior)
if (captchaConfig.userAgent != null)
_webView.settings.userAgentString = captchaConfig.userAgent;
// Capture UA on main thread - callback fires on WebView background thread where settings access is not allowed
val capturedUserAgent = _webView.settings.userAgentString;
_webView.settings.useWideViewPort = true; _webView.settings.useWideViewPort = true;
_webView.settings.loadWithOverviewMode = true; _webView.settings.loadWithOverviewMode = true;
val webViewClient = if(config != null) CaptchaWebViewClient(config) else CaptchaWebViewClient(captchaConfig); val webViewClient = if(config != null) CaptchaWebViewClient(config, capturedUserAgent) else CaptchaWebViewClient(captchaConfig, capturedUserAgent);
webViewClient.onCaptchaFinished.subscribe { captcha -> webViewClient.onCaptchaFinished.subscribe { captcha ->
_callback?.let { _callback?.let {
_callback = null; _callback = null;
@@ -61,11 +61,15 @@ class LoginActivity : AppCompatActivity() {
else throw IllegalStateException("No valid configuration?"); else throw IllegalStateException("No valid configuration?");
//TODO: Backwards compat removal? //TODO: Backwards compat removal?
_webView.settings.userAgentString = authConfig.userAgent ?: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"; // Only override UA if config specifies one, otherwise keep WebView default (matches Desktop CEF behavior)
if (authConfig.userAgent != null)
_webView.settings.userAgentString = authConfig.userAgent;
// Capture UA on main thread - callback fires on WebView background thread where settings access is not allowed
val capturedUserAgent = _webView.settings.userAgentString;
_webView.settings.useWideViewPort = true; _webView.settings.useWideViewPort = true;
_webView.settings.loadWithOverviewMode = true; _webView.settings.loadWithOverviewMode = true;
val webViewClient = if(config != null) LoginWebViewClient(config) else LoginWebViewClient(authConfig); val webViewClient = if(config != null) LoginWebViewClient(config, capturedUserAgent) else LoginWebViewClient(authConfig, capturedUserAgent);
webViewClient.onLogin.subscribe { auth -> webViewClient.onLogin.subscribe { auth ->
_callback?.let { _callback?.let {
@@ -55,6 +55,7 @@ import com.futo.platformplayer.engine.exceptions.ScriptCaptchaRequiredException
import com.futo.platformplayer.engine.exceptions.ScriptException import com.futo.platformplayer.engine.exceptions.ScriptException
import com.futo.platformplayer.engine.exceptions.ScriptImplementationException import com.futo.platformplayer.engine.exceptions.ScriptImplementationException
import com.futo.platformplayer.engine.exceptions.ScriptValidationException import com.futo.platformplayer.engine.exceptions.ScriptValidationException
import com.futo.platformplayer.engine.packages.PackageBridge
import com.futo.platformplayer.logging.Logger import com.futo.platformplayer.logging.Logger
import com.futo.platformplayer.models.ImageVariable import com.futo.platformplayer.models.ImageVariable
import com.futo.platformplayer.states.AnnouncementType import com.futo.platformplayer.states.AnnouncementType
@@ -156,6 +157,7 @@ open class JSClient : IPlatformClient {
_httpClient = JSHttpClient(this, null, _captcha, config); _httpClient = JSHttpClient(this, null, _captcha, config);
_httpClientAuth = JSHttpClient(this, _auth, _captcha, config); _httpClientAuth = JSHttpClient(this, _auth, _captcha, config);
_plugin = V8Plugin(context, descriptor.config, null, _httpClient, _httpClientAuth); _plugin = V8Plugin(context, descriptor.config, null, _httpClient, _httpClientAuth);
_plugin.bridge.descriptor = descriptor;
_plugin.withDependency(context, "scripts/polyfil.js"); _plugin.withDependency(context, "scripts/polyfil.js");
_plugin.withDependency(context, "scripts/source.js"); _plugin.withDependency(context, "scripts/source.js");
@@ -189,6 +191,7 @@ open class JSClient : IPlatformClient {
_httpClient = JSHttpClient(this, null, _captcha, config); _httpClient = JSHttpClient(this, null, _captcha, config);
_httpClientAuth = JSHttpClient(this, _auth, _captcha, config); _httpClientAuth = JSHttpClient(this, _auth, _captcha, config);
_plugin = V8Plugin(context, descriptor.config, script, _httpClient, _httpClientAuth); _plugin = V8Plugin(context, descriptor.config, script, _httpClient, _httpClientAuth);
_plugin.bridge.descriptor = descriptor;
_plugin.withDependency(context, "scripts/polyfil.js"); _plugin.withDependency(context, "scripts/polyfil.js");
_plugin.withDependency(context, "scripts/source.js"); _plugin.withDependency(context, "scripts/source.js");
_plugin.withScript(script); _plugin.withScript(script);
@@ -5,9 +5,9 @@ import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
data class SourceAuth(val cookieMap: HashMap<String, HashMap<String, String>>? = null, val headers: Map<String, Map<String, String>> = mapOf()) { data class SourceAuth(val cookieMap: HashMap<String, HashMap<String, String>>? = null, val headers: Map<String, Map<String, String>> = mapOf(), val userAgent: String? = null) {
override fun toString(): String { override fun toString(): String {
return "(headers: '$headers', cookieString: '$cookieMap')"; return "(headers: '$headers', cookieString: '$cookieMap', userAgent: '$userAgent')";
} }
fun toEncrypted(): String{ fun toEncrypted(): String{
@@ -15,23 +15,25 @@ data class SourceAuth(val cookieMap: HashMap<String, HashMap<String, String>>? =
} }
private fun serialize(): String { private fun serialize(): String {
return Json.encodeToString(SerializedAuth(cookieMap, headers)); return Json.encodeToString(SerializedAuth(cookieMap, headers, userAgent));
} }
companion object { companion object {
val TAG = "SourceAuth"; val TAG = "SourceAuth";
private val _json = Json { ignoreUnknownKeys = true };
fun fromEncrypted(encrypted: String?): SourceAuth? { fun fromEncrypted(encrypted: String?): SourceAuth? {
return SourceEncrypted.decryptEncrypted(encrypted) { deserialize(it) }; return SourceEncrypted.decryptEncrypted(encrypted) { deserialize(it) };
} }
private fun deserialize(str: String): SourceAuth { private fun deserialize(str: String): SourceAuth {
val data = Json.decodeFromString<SerializedAuth>(str); val data = _json.decodeFromString<SerializedAuth>(str);
return SourceAuth(data.cookieMap, data.headers); return SourceAuth(data.cookieMap, data.headers, data.userAgent);
} }
} }
@Serializable @Serializable
data class SerializedAuth(val cookieMap: HashMap<String, HashMap<String, String>>?, data class SerializedAuth(val cookieMap: HashMap<String, HashMap<String, String>>?,
val headers: Map<String, Map<String, String>> = mapOf()) val headers: Map<String, Map<String, String>> = mapOf(),
val userAgent: String? = null)
} }
@@ -5,9 +5,9 @@ import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
data class SourceCaptchaData(val cookieMap: HashMap<String, HashMap<String, String>>? = null, val headers: Map<String, Map<String, String>> = mapOf()) { data class SourceCaptchaData(val cookieMap: HashMap<String, HashMap<String, String>>? = null, val headers: Map<String, Map<String, String>> = mapOf(), val userAgent: String? = null) {
override fun toString(): String { override fun toString(): String {
return "(headers: '$headers', cookieString: '$cookieMap')"; return "(headers: '$headers', cookieString: '$cookieMap', userAgent: '$userAgent')";
} }
fun toEncrypted(): String{ fun toEncrypted(): String{
@@ -15,23 +15,25 @@ data class SourceCaptchaData(val cookieMap: HashMap<String, HashMap<String, Stri
} }
private fun serialize(): String { private fun serialize(): String {
return Json.encodeToString(SerializedCaptchaData(cookieMap, headers)); return Json.encodeToString(SerializedCaptchaData(cookieMap, headers, userAgent));
} }
companion object { companion object {
val TAG = "SourceCaptchaData"; val TAG = "SourceCaptchaData";
private val _json = Json { ignoreUnknownKeys = true };
fun fromEncrypted(encrypted: String?): SourceCaptchaData? { fun fromEncrypted(encrypted: String?): SourceCaptchaData? {
return SourceEncrypted.decryptEncrypted(encrypted) { deserialize(it) }; return SourceEncrypted.decryptEncrypted(encrypted) { deserialize(it) };
} }
fun deserialize(str: String): SourceCaptchaData { fun deserialize(str: String): SourceCaptchaData {
val data = Json.decodeFromString<SerializedCaptchaData>(str); val data = _json.decodeFromString<SerializedCaptchaData>(str);
return SourceCaptchaData(data.cookieMap, data.headers); return SourceCaptchaData(data.cookieMap, data.headers, data.userAgent);
} }
} }
@Serializable @Serializable
data class SerializedCaptchaData(val cookieMap: HashMap<String, HashMap<String, String>>?, data class SerializedCaptchaData(val cookieMap: HashMap<String, HashMap<String, String>>?,
val headers: Map<String, Map<String, String>> = mapOf()) val headers: Map<String, Map<String, String>> = mapOf(),
val userAgent: String? = null)
} }
@@ -87,6 +87,7 @@ class V8Plugin {
private val _deps : LinkedHashMap<String, String> = LinkedHashMap(); private val _deps : LinkedHashMap<String, String> = LinkedHashMap();
private val _depsPackages : MutableList<V8Package> = mutableListOf(); private val _depsPackages : MutableList<V8Package> = mutableListOf();
private var _script : String? = null; private var _script : String? = null;
val bridge: PackageBridge;
var isStopped = true; var isStopped = true;
val onStopped = Event1<V8Plugin>(); val onStopped = Event1<V8Plugin>();
@@ -114,7 +115,8 @@ class V8Plugin {
this._clientAuth = clientAuth; this._clientAuth = clientAuth;
this.config = config; this.config = config;
this._script = script; this._script = script;
withDependency(PackageBridge(this, config)); bridge = PackageBridge(this, config);
withDependency(bridge);
for(pack in config.packages) for(pack in config.packages)
withDependency(getPackage(pack)!!); withDependency(getPackage(pack)!!);
@@ -17,6 +17,7 @@ import com.futo.platformplayer.api.media.models.contents.ContentType
import com.futo.platformplayer.api.media.platforms.js.JSClient import com.futo.platformplayer.api.media.platforms.js.JSClient
import com.futo.platformplayer.api.media.platforms.js.JSClientConstants import com.futo.platformplayer.api.media.platforms.js.JSClientConstants
import com.futo.platformplayer.api.media.platforms.js.SourcePluginConfig import com.futo.platformplayer.api.media.platforms.js.SourcePluginConfig
import com.futo.platformplayer.api.media.platforms.js.SourcePluginDescriptor
import com.futo.platformplayer.api.media.platforms.js.internal.JSHttpClient import com.futo.platformplayer.api.media.platforms.js.internal.JSHttpClient
import com.futo.platformplayer.engine.IV8PluginConfig import com.futo.platformplayer.engine.IV8PluginConfig
import com.futo.platformplayer.engine.V8Plugin import com.futo.platformplayer.engine.V8Plugin
@@ -36,6 +37,9 @@ class PackageBridge : V8Package {
private val _client: ManagedHttpClient private val _client: ManagedHttpClient
@Transient @Transient
private val _clientAuth: ManagedHttpClient private val _clientAuth: ManagedHttpClient
// Set by JSClient after construction to provide access to auth/captcha data
@Transient
var descriptor: SourcePluginDescriptor? = null
override val name: String get() = "Bridge"; override val name: String get() = "Bridge";
@@ -80,6 +84,17 @@ class PackageBridge : V8Package {
return "android"; return "android";
} }
// User agent captured during captcha/auth WebView flows, matching Desktop's bridge.captchaUserAgent/bridge.authUserAgent.
// Plugins use these to make HTTP requests with the same UA that was used in the WebView.
@V8Property
fun captchaUserAgent(): String? {
return descriptor?.getCaptchaData()?.userAgent
}
@V8Property
fun authUserAgent(): String? {
return descriptor?.getAuth()?.userAgent
}
@V8Property @V8Property
fun supportedFeatures(): Array<String> { fun supportedFeatures(): Array<String> {
return arrayOf( return arrayOf(
@@ -96,11 +96,15 @@ class LoginFragment : MainFragment() {
else throw IllegalStateException("No valid configuration?"); else throw IllegalStateException("No valid configuration?");
//TODO: Backwards compat removal? //TODO: Backwards compat removal?
_webView.settings.userAgentString = authConfig.userAgent ?: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"; // Only override UA if config specifies one, otherwise keep WebView default (matches Desktop CEF behavior)
if (authConfig.userAgent != null)
_webView.settings.userAgentString = authConfig.userAgent;
// Capture UA on main thread - callback fires on WebView background thread where settings access is not allowed
val capturedUserAgent = _webView.settings.userAgentString;
_webView.settings.useWideViewPort = true; _webView.settings.useWideViewPort = true;
_webView.settings.loadWithOverviewMode = true; _webView.settings.loadWithOverviewMode = true;
val webViewClient = if(config != null) LoginWebViewClient(config) else LoginWebViewClient(authConfig); val webViewClient = if(config != null) LoginWebViewClient(config, capturedUserAgent) else LoginWebViewClient(authConfig, capturedUserAgent);
webViewClient.onLogin.subscribe { auth -> webViewClient.onLogin.subscribe { auth ->
_callback?.let { _callback?.let {
@@ -16,13 +16,15 @@ class CaptchaWebViewClient : WebViewClient {
private val _pluginConfig: SourcePluginConfig?; private val _pluginConfig: SourcePluginConfig?;
private val _captchaConfig: SourcePluginCaptchaConfig; private val _captchaConfig: SourcePluginCaptchaConfig;
private val _userAgent: String?;
private var _didNotify = false; private var _didNotify = false;
private val _extractor: WebViewRequirementExtractor; private val _extractor: WebViewRequirementExtractor;
constructor(config: SourcePluginConfig) : super() { constructor(config: SourcePluginConfig, userAgent: String? = null) : super() {
_pluginConfig = config; _pluginConfig = config;
_captchaConfig = config.captcha!!; _captchaConfig = config.captcha!!;
_userAgent = userAgent;
_extractor = WebViewRequirementExtractor( _extractor = WebViewRequirementExtractor(
config.allowUrls, config.allowUrls,
null, null,
@@ -34,9 +36,10 @@ class CaptchaWebViewClient : WebViewClient {
Logger.i(TAG, "Captcha [${config.name}]" + Logger.i(TAG, "Captcha [${config.name}]" +
"\nRequired Cookies: ${Serializer.json.encodeToString(config.captcha!!.cookiesToFind)}",); "\nRequired Cookies: ${Serializer.json.encodeToString(config.captcha!!.cookiesToFind)}",);
} }
constructor(captcha: SourcePluginCaptchaConfig) : super() { constructor(captcha: SourcePluginCaptchaConfig, userAgent: String? = null) : super() {
_pluginConfig = null; _pluginConfig = null;
_captchaConfig = captcha; _captchaConfig = captcha;
_userAgent = userAgent;
_extractor = WebViewRequirementExtractor( _extractor = WebViewRequirementExtractor(
null, null,
null, null,
@@ -62,7 +65,8 @@ class CaptchaWebViewClient : WebViewClient {
_didNotify = true; _didNotify = true;
onCaptchaFinished.emit(SourceCaptchaData( onCaptchaFinished.emit(SourceCaptchaData(
extracted.cookies, extracted.cookies,
extracted.headers extracted.headers,
_userAgent
)); ));
} }
@@ -24,23 +24,26 @@ class LoginWebViewClient : WebViewClient {
private val _pluginConfig: SourcePluginConfig?; private val _pluginConfig: SourcePluginConfig?;
private val _authConfig: SourcePluginAuthConfig; private val _authConfig: SourcePluginAuthConfig;
private val _userAgent: String?;
private val _client = ManagedHttpClient(); private val _client = ManagedHttpClient();
val onLogin = Event1<SourceAuth>(); val onLogin = Event1<SourceAuth>();
val onPageLoaded = Event2<WebView?, String?>() val onPageLoaded = Event2<WebView?, String?>()
constructor(config: SourcePluginConfig) : super() { constructor(config: SourcePluginConfig, userAgent: String? = null) : super() {
_pluginConfig = config; _pluginConfig = config;
_authConfig = config.authentication!!; _authConfig = config.authentication!!;
_userAgent = userAgent;
Logger.i(TAG, "Login [${config.name}]" + Logger.i(TAG, "Login [${config.name}]" +
"\nRequired Headers: ${config.authentication.headersToFind?.joinToString(", ")}" + "\nRequired Headers: ${config.authentication.headersToFind?.joinToString(", ")}" +
"\nRequired Domain Headers: ${Serializer.json.encodeToString(config.authentication.domainHeadersToFind)}" + "\nRequired Domain Headers: ${Serializer.json.encodeToString(config.authentication.domainHeadersToFind)}" +
"\nRequired Cookies: ${Serializer.json.encodeToString(config.authentication.cookiesToFind)}",); "\nRequired Cookies: ${Serializer.json.encodeToString(config.authentication.cookiesToFind)}",);
} }
constructor(auth: SourcePluginAuthConfig) : super() { constructor(auth: SourcePluginAuthConfig, userAgent: String? = null) : super() {
_pluginConfig = null; _pluginConfig = null;
_authConfig = auth; _authConfig = auth;
_userAgent = userAgent;
} }
private val headersFoundMap: HashMap<String, HashMap<String, String>> = hashMapOf(); private val headersFoundMap: HashMap<String, HashMap<String, String>> = hashMapOf();
@@ -192,13 +195,14 @@ class LoginWebViewClient : WebViewClient {
if (urlFound && headersFound && domainHeadersFound && cookiesFound) { if (urlFound && headersFound && domainHeadersFound && cookiesFound) {
onLogin.emit(SourceAuth( onLogin.emit(SourceAuth(
cookieMap = cookiesFoundMap, cookieMap = cookiesFoundMap,
headers = headersFoundMap /*.associate { headerToFind -> headers = headersFoundMap, /*.associate { headerToFind ->
headerToFind to headersFoundMap.firstNotNullOf { requestHeader -> headerToFind to headersFoundMap.firstNotNullOf { requestHeader ->
if (requestHeader.key.equals(headerToFind, ignoreCase = true)) if (requestHeader.key.equals(headerToFind, ignoreCase = true))
requestHeader.value requestHeader.value
else null; else null;
} }
} ?: mapOf()*/ } ?: mapOf()*/
userAgent = _userAgent
)); ));
} }