mirror of
https://gitlab.futo.org/videostreaming/grayjay.git
synced 2026-05-16 04:52:39 +02:00
Merge branch 'captcha-improvements' into 'master'
feat: propagate WebView user agent to plugins via bridge.captchaUserAgent and bridge.authUserAgent See merge request videostreaming/grayjay!164
This commit is contained in:
@@ -68,11 +68,15 @@ class CaptchaActivity : AppCompatActivity() {
|
||||
intent.getStringExtra("body");
|
||||
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.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 ->
|
||||
_callback?.let {
|
||||
_callback = null;
|
||||
|
||||
@@ -61,11 +61,15 @@ class LoginActivity : AppCompatActivity() {
|
||||
else throw IllegalStateException("No valid configuration?");
|
||||
//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.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 ->
|
||||
_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.ScriptImplementationException
|
||||
import com.futo.platformplayer.engine.exceptions.ScriptValidationException
|
||||
import com.futo.platformplayer.engine.packages.PackageBridge
|
||||
import com.futo.platformplayer.logging.Logger
|
||||
import com.futo.platformplayer.models.ImageVariable
|
||||
import com.futo.platformplayer.states.AnnouncementType
|
||||
@@ -156,6 +157,7 @@ open class JSClient : IPlatformClient {
|
||||
_httpClient = JSHttpClient(this, null, _captcha, config);
|
||||
_httpClientAuth = JSHttpClient(this, _auth, _captcha, config);
|
||||
_plugin = V8Plugin(context, descriptor.config, null, _httpClient, _httpClientAuth);
|
||||
_plugin.bridge.descriptor = descriptor;
|
||||
_plugin.withDependency(context, "scripts/polyfil.js");
|
||||
_plugin.withDependency(context, "scripts/source.js");
|
||||
|
||||
@@ -189,6 +191,7 @@ open class JSClient : IPlatformClient {
|
||||
_httpClient = JSHttpClient(this, null, _captcha, config);
|
||||
_httpClientAuth = JSHttpClient(this, _auth, _captcha, config);
|
||||
_plugin = V8Plugin(context, descriptor.config, script, _httpClient, _httpClientAuth);
|
||||
_plugin.bridge.descriptor = descriptor;
|
||||
_plugin.withDependency(context, "scripts/polyfil.js");
|
||||
_plugin.withDependency(context, "scripts/source.js");
|
||||
_plugin.withScript(script);
|
||||
|
||||
@@ -5,9 +5,9 @@ import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
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 {
|
||||
return "(headers: '$headers', cookieString: '$cookieMap')";
|
||||
return "(headers: '$headers', cookieString: '$cookieMap', userAgent: '$userAgent')";
|
||||
}
|
||||
|
||||
fun toEncrypted(): String{
|
||||
@@ -15,23 +15,25 @@ data class SourceAuth(val cookieMap: HashMap<String, HashMap<String, String>>? =
|
||||
}
|
||||
|
||||
private fun serialize(): String {
|
||||
return Json.encodeToString(SerializedAuth(cookieMap, headers));
|
||||
return Json.encodeToString(SerializedAuth(cookieMap, headers, userAgent));
|
||||
}
|
||||
|
||||
companion object {
|
||||
val TAG = "SourceAuth";
|
||||
private val _json = Json { ignoreUnknownKeys = true };
|
||||
|
||||
fun fromEncrypted(encrypted: String?): SourceAuth? {
|
||||
return SourceEncrypted.decryptEncrypted(encrypted) { deserialize(it) };
|
||||
}
|
||||
|
||||
private fun deserialize(str: String): SourceAuth {
|
||||
val data = Json.decodeFromString<SerializedAuth>(str);
|
||||
return SourceAuth(data.cookieMap, data.headers);
|
||||
val data = _json.decodeFromString<SerializedAuth>(str);
|
||||
return SourceAuth(data.cookieMap, data.headers, data.userAgent);
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
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)
|
||||
}
|
||||
+8
-6
@@ -5,9 +5,9 @@ import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
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 {
|
||||
return "(headers: '$headers', cookieString: '$cookieMap')";
|
||||
return "(headers: '$headers', cookieString: '$cookieMap', userAgent: '$userAgent')";
|
||||
}
|
||||
|
||||
fun toEncrypted(): String{
|
||||
@@ -15,23 +15,25 @@ data class SourceCaptchaData(val cookieMap: HashMap<String, HashMap<String, Stri
|
||||
}
|
||||
|
||||
private fun serialize(): String {
|
||||
return Json.encodeToString(SerializedCaptchaData(cookieMap, headers));
|
||||
return Json.encodeToString(SerializedCaptchaData(cookieMap, headers, userAgent));
|
||||
}
|
||||
|
||||
companion object {
|
||||
val TAG = "SourceCaptchaData";
|
||||
private val _json = Json { ignoreUnknownKeys = true };
|
||||
|
||||
fun fromEncrypted(encrypted: String?): SourceCaptchaData? {
|
||||
return SourceEncrypted.decryptEncrypted(encrypted) { deserialize(it) };
|
||||
}
|
||||
|
||||
fun deserialize(str: String): SourceCaptchaData {
|
||||
val data = Json.decodeFromString<SerializedCaptchaData>(str);
|
||||
return SourceCaptchaData(data.cookieMap, data.headers);
|
||||
val data = _json.decodeFromString<SerializedCaptchaData>(str);
|
||||
return SourceCaptchaData(data.cookieMap, data.headers, data.userAgent);
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
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 _depsPackages : MutableList<V8Package> = mutableListOf();
|
||||
private var _script : String? = null;
|
||||
val bridge: PackageBridge;
|
||||
|
||||
var isStopped = true;
|
||||
val onStopped = Event1<V8Plugin>();
|
||||
@@ -114,7 +115,8 @@ class V8Plugin {
|
||||
this._clientAuth = clientAuth;
|
||||
this.config = config;
|
||||
this._script = script;
|
||||
withDependency(PackageBridge(this, config));
|
||||
bridge = PackageBridge(this, config);
|
||||
withDependency(bridge);
|
||||
|
||||
for(pack in config.packages)
|
||||
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.JSClientConstants
|
||||
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.engine.IV8PluginConfig
|
||||
import com.futo.platformplayer.engine.V8Plugin
|
||||
@@ -36,6 +37,9 @@ class PackageBridge : V8Package {
|
||||
private val _client: ManagedHttpClient
|
||||
@Transient
|
||||
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";
|
||||
@@ -80,6 +84,17 @@ class PackageBridge : V8Package {
|
||||
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
|
||||
fun supportedFeatures(): Array<String> {
|
||||
return arrayOf(
|
||||
|
||||
+6
-2
@@ -96,11 +96,15 @@ class LoginFragment : MainFragment() {
|
||||
else throw IllegalStateException("No valid configuration?");
|
||||
//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.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 ->
|
||||
_callback?.let {
|
||||
|
||||
@@ -16,13 +16,15 @@ class CaptchaWebViewClient : WebViewClient {
|
||||
|
||||
private val _pluginConfig: SourcePluginConfig?;
|
||||
private val _captchaConfig: SourcePluginCaptchaConfig;
|
||||
private val _userAgent: String?;
|
||||
|
||||
private var _didNotify = false;
|
||||
private val _extractor: WebViewRequirementExtractor;
|
||||
|
||||
constructor(config: SourcePluginConfig) : super() {
|
||||
constructor(config: SourcePluginConfig, userAgent: String? = null) : super() {
|
||||
_pluginConfig = config;
|
||||
_captchaConfig = config.captcha!!;
|
||||
_userAgent = userAgent;
|
||||
_extractor = WebViewRequirementExtractor(
|
||||
config.allowUrls,
|
||||
null,
|
||||
@@ -34,9 +36,10 @@ class CaptchaWebViewClient : WebViewClient {
|
||||
Logger.i(TAG, "Captcha [${config.name}]" +
|
||||
"\nRequired Cookies: ${Serializer.json.encodeToString(config.captcha!!.cookiesToFind)}",);
|
||||
}
|
||||
constructor(captcha: SourcePluginCaptchaConfig) : super() {
|
||||
constructor(captcha: SourcePluginCaptchaConfig, userAgent: String? = null) : super() {
|
||||
_pluginConfig = null;
|
||||
_captchaConfig = captcha;
|
||||
_userAgent = userAgent;
|
||||
_extractor = WebViewRequirementExtractor(
|
||||
null,
|
||||
null,
|
||||
@@ -62,7 +65,8 @@ class CaptchaWebViewClient : WebViewClient {
|
||||
_didNotify = true;
|
||||
onCaptchaFinished.emit(SourceCaptchaData(
|
||||
extracted.cookies,
|
||||
extracted.headers
|
||||
extracted.headers,
|
||||
_userAgent
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -24,23 +24,26 @@ class LoginWebViewClient : WebViewClient {
|
||||
|
||||
private val _pluginConfig: SourcePluginConfig?;
|
||||
private val _authConfig: SourcePluginAuthConfig;
|
||||
private val _userAgent: String?;
|
||||
|
||||
private val _client = ManagedHttpClient();
|
||||
|
||||
val onLogin = Event1<SourceAuth>();
|
||||
val onPageLoaded = Event2<WebView?, String?>()
|
||||
|
||||
constructor(config: SourcePluginConfig) : super() {
|
||||
constructor(config: SourcePluginConfig, userAgent: String? = null) : super() {
|
||||
_pluginConfig = config;
|
||||
_authConfig = config.authentication!!;
|
||||
_userAgent = userAgent;
|
||||
Logger.i(TAG, "Login [${config.name}]" +
|
||||
"\nRequired Headers: ${config.authentication.headersToFind?.joinToString(", ")}" +
|
||||
"\nRequired Domain Headers: ${Serializer.json.encodeToString(config.authentication.domainHeadersToFind)}" +
|
||||
"\nRequired Cookies: ${Serializer.json.encodeToString(config.authentication.cookiesToFind)}",);
|
||||
}
|
||||
constructor(auth: SourcePluginAuthConfig) : super() {
|
||||
constructor(auth: SourcePluginAuthConfig, userAgent: String? = null) : super() {
|
||||
_pluginConfig = null;
|
||||
_authConfig = auth;
|
||||
_userAgent = userAgent;
|
||||
}
|
||||
|
||||
private val headersFoundMap: HashMap<String, HashMap<String, String>> = hashMapOf();
|
||||
@@ -192,13 +195,14 @@ class LoginWebViewClient : WebViewClient {
|
||||
if (urlFound && headersFound && domainHeadersFound && cookiesFound) {
|
||||
onLogin.emit(SourceAuth(
|
||||
cookieMap = cookiesFoundMap,
|
||||
headers = headersFoundMap /*.associate { headerToFind ->
|
||||
headers = headersFoundMap, /*.associate { headerToFind ->
|
||||
headerToFind to headersFoundMap.firstNotNullOf { requestHeader ->
|
||||
if (requestHeader.key.equals(headerToFind, ignoreCase = true))
|
||||
requestHeader.value
|
||||
else null;
|
||||
}
|
||||
} ?: mapOf()*/
|
||||
userAgent = _userAgent
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user