Consent reject now works, app now intercepts redirects

This commit is contained in:
Kelvin 2023-10-20 17:20:36 +02:00
parent f344dbf35c
commit 5b7fb2c818
3 changed files with 52 additions and 43 deletions

View file

@ -6,6 +6,7 @@ import com.futo.platformplayer.logging.Logger
import okhttp3.Call
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
@ -28,7 +29,11 @@ open class ManagedHttpClient {
constructor(builder: OkHttpClient.Builder = OkHttpClient.Builder()) {
_builderTemplate = builder;
client = builder.build();
client = builder.addNetworkInterceptor { chain ->
val request = beforeRequest(chain.request());
val response = afterRequest(chain.proceed(request));
return@addNetworkInterceptor response;
}.build();
}
open fun clone(): ManagedHttpClient {
@ -116,7 +121,7 @@ open class ManagedHttpClient {
fun execute(request : Request) : Response {
ensureNotMainThread();
beforeRequest(request);
//beforeRequest(request);
Logger.v(TAG, "HTTP Request [${request.method}] ${request.url} - [${if(request.body != null) request.body.size else 0}]");
@ -156,23 +161,16 @@ open class ManagedHttpClient {
if(true)
Logger.v(TAG, "HTTP Response [${request.method}] ${request.url} - [${time}ms]");
afterRequest(request, resp);
//afterRequest(request, resp);
return resp;
}
//Set Listeners
fun setOnBeforeRequest(listener : (Request)->Unit) {
this.onBeforeRequest = listener;
open fun beforeRequest(request: okhttp3.Request): okhttp3.Request {
return request;
}
fun setOnAfterRequest(listener : (Request, Response)->Unit) {
this.onAfterRequest = listener;
}
open fun beforeRequest(request: Request) {
onBeforeRequest?.invoke(request);
}
open fun afterRequest(request: Request, resp: Response) {
onAfterRequest?.invoke(request, resp);
open fun afterRequest(resp: okhttp3.Response): okhttp3.Response {
return resp;
}

View file

@ -31,8 +31,12 @@ class JSHttpClient : ManagedHttpClient {
_currentCookieMap.put(domainCookies.key, HashMap(domainCookies.value));
}
if(!captcha?.cookieMap.isNullOrEmpty()) {
for(domainCookies in captcha!!.cookieMap!!)
_currentCookieMap.put(domainCookies.key, HashMap(domainCookies.value));
for(domainCookies in captcha!!.cookieMap!!) {
if(_currentCookieMap.containsKey(domainCookies.key))
_currentCookieMap[domainCookies.key]?.putAll(domainCookies.value);
else
_currentCookieMap.put(domainCookies.key, HashMap(domainCookies.value));
}
}
}
@ -46,14 +50,18 @@ class JSHttpClient : ManagedHttpClient {
return newClient;
}
override fun beforeRequest(request: Request) {
val domain = Uri.parse(request.url).host!!.lowercase();
override fun beforeRequest(request: okhttp3.Request): okhttp3.Request {
val domain = request.url.host.lowercase();
val auth = _auth;
val newBuilder = if(auth != null || doApplyCookies)
request.newBuilder();
else
null;
if (auth != null) {
//TODO: Possibly add doApplyHeaders
for (header in auth.headers.filter { domain.matchesDomain(it.key) }.flatMap { it.value.entries })
request.headers[header.key] = header.value;
newBuilder?.header(header.key, header.value);
}
if(doApplyCookies) {
@ -71,36 +79,34 @@ class JSHttpClient : ManagedHttpClient {
val existingCookies = request.headers["Cookie"];
if(!existingCookies.isNullOrEmpty())
request.headers["Cookie"] = existingCookies.trim(';') + "; " + cookieString;
newBuilder?.header("Cookie", existingCookies.trim(';') + "; " + cookieString);
else
request.headers["Cookie"] = cookieString;
newBuilder?.header("Cookie", cookieString);
}
//printTestCode(request.url, request.body, auth.headers, cookieString, request.headers.filter { !auth.headers.containsKey(it.key) });
}
}
_jsClient?.validateUrlOrThrow(request.url);
super.beforeRequest(request)
_jsClient?.validateUrlOrThrow(request.url.toString());
return newBuilder?.let { it.build() } ?: request;
}
override fun afterRequest(request: Request, resp: Response) {
super.afterRequest(request, resp)
override fun afterRequest(resp: okhttp3.Response): okhttp3.Response {
if(doUpdateCookies) {
val domain = Uri.parse(request.url).host!!.lowercase();
val domainParts = domain!!.split(".");
val domain = resp.request.url.host.lowercase();
val domainParts = domain.split(".");
val defaultCookieDomain =
"." + domainParts.drop(domainParts.size - 2).joinToString(".");
for (header in resp.headers) {
if ((_auth != null || _currentCookieMap.isNotEmpty()) && header.key.lowercase() == "set-cookie") {
val newCookies = cookieStringToMap(header.value);
for (cookie in newCookies) {
val endIndex = cookie.value.indexOf(";");
var cookieValue = cookie.value;
if ((_auth != null || _currentCookieMap.isNotEmpty()) && header.first.lowercase() == "set-cookie") {
//val newCookies = cookieStringToMap(header.second.split("; "));
val cookie = cookieStringToPair(header.second);
//for (cookie in newCookies) {
var cookieValue = cookie.second;
var domainToUse = domain;
if (endIndex > 0) {
val cookieParts = cookie.value.split(";");
if (!cookie.first.isNullOrEmpty() && !cookie.second.isNullOrEmpty()) {
val cookieParts = cookie.second.split(";");
if (cookieParts.size == 0)
continue;
cookieValue = cookieParts[0].trim();
@ -126,24 +132,29 @@ class JSHttpClient : ManagedHttpClient {
_currentCookieMap!!.put(domainToUse, newMap)
newMap;
}
if(cookieMap.containsKey(cookie.key) || doAllowNewCookies)
cookieMap.put(cookie.key, cookieValue);
}
if(cookieMap.containsKey(cookie.first) || doAllowNewCookies)
cookieMap.put(cookie.first, cookieValue);
//}
}
}
}
return resp;
}
private fun cookieStringToMap(parts: List<String>): Map<String, String> {
val map = hashMapOf<String, String>();
for(cookie in parts) {
val cookieKey = cookie.substring(0, cookie.indexOf("="));
val cookieVal = cookie.substring(cookie.indexOf("=") + 1);
map.put(cookieKey.trim(), cookieVal.trim());
val pair = cookieStringToPair(cookie)
map.put(pair.first, pair.second);
}
return map;
}
private fun cookieStringToPair(cookie: String): Pair<String, String> {
val cookieKey = cookie.substring(0, cookie.indexOf("="));
val cookieVal = cookie.substring(cookie.indexOf("=") + 1);
return Pair(cookieKey.trim(), cookieVal.trim());
}
//Prints out code for test reproduction..
fun printTestCode(url: String, body: ByteArray?, headers: Map<String, String>, cookieString: String, allHeaders: Map<String, String>? = null) {

@ -1 +1 @@
Subproject commit 4ffd2a48c7ebed2c8512e092a6bae4b5447aff34
Subproject commit 239960b932c00a194cf2af6350bb5bde4207acf8