Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Casting and reflection issues in a method argument. okhttp3.OkHttpClient$Builder;->addInterceptor(···) #453

Open
ElJaviLuki opened this issue Sep 21, 2021 · 0 comments

Comments

@ElJaviLuki
Copy link

Hi guys, I'm trying to hook the constructor of okhttp3.OkHttpClient$Builder in order to add an interceptor that converts any OkHttp call into a curl call and log it (see Ok2Curl library by @mrmike). But I'm having problems with reflection, say I must haven't understood it pretty well...

The idea is to call .addInterceptor(okhttp3.Interceptor) method JUST after the OkHttpClient$Builder instance has been constructed, but it's more painful than expected. (See code and code comments for further details)

@Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        Class builderClass = XposedHelpers.findClassIfExists("okhttp3.OkHttpClient$Builder", lpparam.classLoader);
        if(builderClass != null){
            XposedBridge.hookAllConstructors(builderClass, new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    CurlInterceptor interceptor = new CurlInterceptor(new Loggable() {
                        @Override
                        public void log(String message) {
                            Log.v("Ok2Curl", message);
                        }
                    });

                    /*
                        Attempt nº1:
                        Cannot do it this way, it throws an exception because there's not (and there's actually not) a method
                        'okhttp3.OkHttpClient$Builder#addInterceptor(CurlInterceptor)' since CurlInterceptor is from the Ok2Curl library.

                        Maybe the '.callMethod()' cannot make implicit casting?
                     */
                    Object attempt1 = XposedHelpers.callMethod(param.thisObject, "addInterceptor", interceptor);

                    /*
                        Attempt nº2:
                        Declaring explicitly the argument classes that addInterceptor(···) has, does not help.
                        I get an exception: it could not find an ".addInterceptor(java.lang.Class, ...CurlInterceptor)" method, so
                        this means Interceptor.class was taken as one of the method arguments and not as an argument type.
                        Anyways, given the results from attempt 1, it seems that .callMethod() will not do implicit casting whatsoever...
                     */
                    Object attempt2 = XposedHelpers.callMethod(param.thisObject, "addInterceptor", Interceptor.class, interceptor);

                    /*
                        Attempt nº3:
                        Explicit casting from CurlInterceptor to okhttp3.Interceptor did not help...
                        
                        java.lang.NoSuchMethodError: okhttp3.OkHttpClient$Builder#addInterceptor(com.moczul.ok2curl.CurlInterceptor)#bestmatch
                     */
                    Object attempt3 = XposedHelpers.callMethod(param.thisObject, "addInterceptor", (okhttp3.Interceptor) interceptor);
                }
            });
        }
    }

Thanks in advance!

@ElJaviLuki ElJaviLuki changed the title Casting and reflection issues in a method argument. okhttp3.OkHttpClient$Builder.addInterceptor(···) Casting and reflection issues in a method argument. okhttp3.OkHttpClient$Builder;->addInterceptor(···) Sep 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant