Consent reject now works, app now intercepts redirects

This commit is contained in:
Kelvin 2023-10-20 17:20:36 +02:00
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.Call
import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response import okhttp3.Response
@ -28,7 +29,11 @@ open class ManagedHttpClient {
constructor(builder: OkHttpClient.Builder = OkHttpClient.Builder()) { constructor(builder: OkHttpClient.Builder = OkHttpClient.Builder()) {
_builderTemplate = 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 { open fun clone(): ManagedHttpClient {
@ -116,7 +121,7 @@ open class ManagedHttpClient {
fun execute(request : Request) : Response { fun execute(request : Request) : Response {
ensureNotMainThread(); ensureNotMainThread();
beforeRequest(request); //beforeRequest(request);
Logger.v(TAG, "HTTP Request [${request.method}] ${request.url} - [${if(request.body != null) request.body.size else 0}]"); 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) if(true)
Logger.v(TAG, "HTTP Response [${request.method}] ${request.url} - [${time}ms]"); Logger.v(TAG, "HTTP Response [${request.method}] ${request.url} - [${time}ms]");
afterRequest(request, resp); //afterRequest(request, resp);
return resp; return resp;
} }
//Set Listeners //Set Listeners
fun setOnBeforeRequest(listener : (Request)->Unit) { open fun beforeRequest(request: okhttp3.Request): okhttp3.Request {
this.onBeforeRequest = listener; return request;
} }
fun setOnAfterRequest(listener : (Request, Response)->Unit) { open fun afterRequest(resp: okhttp3.Response): okhttp3.Response {
this.onAfterRequest = listener; return resp;
}
open fun beforeRequest(request: Request) {
onBeforeRequest?.invoke(request);
}
open fun afterRequest(request: Request, resp: Response) {
onAfterRequest?.invoke(request, resp);
} }

View file

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