Last active
January 18, 2022 07:51
-
-
Save xjoker/5f5d6c6d15a232e11f8344886ac93aec to your computer and use it in GitHub Desktop.
OkHttp3Hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OkHttp3Hook { | |
private static final String TAG = "OkHttp3Hook"; | |
private static final String realCallClassName = "okhttp3.RealCall"; | |
private static final String httpUrlClassName = "okhttp3.HttpUrl"; | |
private static final String requestBuilderClassName = "okhttp3.Request$Builder"; | |
private static final String getResponseWithInterceptorChainMethodName = "getResponseWithInterceptorChain"; | |
/** | |
* 请求结果处理方法,有获取请求的放在这里统一处理 | |
* | |
* @param rim | |
*/ | |
private static void ResponseHandler(RequestInfoModel rim) { | |
try { | |
new Thread(() -> SessionHandler.Handler(rim)).start(); | |
} catch (Exception ex) { | |
} | |
} | |
public static void ResponseHook(XC_LoadPackage.LoadPackageParam lpparam, String eXhsApkVersion) { | |
SerializeConfig JsonFastConfig = new SerializeConfig(); | |
JsonFastConfig.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase; | |
// 拦截方法定义 | |
XC_MethodHook xc_methodHook = new XC_MethodHook() { | |
@Override | |
protected void afterHookedMethod(MethodHookParam param) throws Throwable { | |
Object resp = param.getResult(); | |
int respCode = XposedHelpers.getIntField(resp, "code"); | |
Object request = XposedHelpers.callMethod(resp, "request"); | |
// 请求URL | |
String requestUrl = XposedHelpers.callMethod(request, "url").toString().toLowerCase(); | |
//Log.d(TAG, "Url: " + requestUrl); | |
// 请求方法 | |
String requestMethod = XposedHelpers.callMethod(request, "method").toString().toLowerCase(); | |
String requestBase64String = ""; | |
//请求Body | |
try { | |
Object requestBody = XposedHelpers.callMethod(request, "body"); | |
Class<?> classBuffer = XposedHelpers.findClass("okio.Buffer", lpparam.classLoader); | |
Object buffer = classBuffer.newInstance(); | |
XposedHelpers.callMethod(requestBody, "writeTo", buffer); | |
requestBase64String = (String) XposedHelpers.callMethod(XposedHelpers.callMethod(buffer, "readByteString"), "base64"); | |
} catch (Exception ex) { | |
} | |
// 请求头 | |
Object requestHeader = XposedHelpers.callMethod(request, "headers"); | |
Object requestHeadersToMultimap = XposedHelpers.callMethod(requestHeader, "toMultimap"); | |
String HeaderJsonStr = JSON.toJSONString(requestHeadersToMultimap, JsonFastConfig); | |
Map<String, List<String>> map = JSON.parseObject(HeaderJsonStr, new TypeReference<Map<String, List<String>>>() { | |
}); | |
Map<String, String> headerMap = new HashMap<>(); | |
for (Map.Entry<String, List<String>> entry : map.entrySet()) { | |
headerMap.put(entry.getKey(), entry.getValue().get(0)); | |
} | |
// 返回内容 | |
Object responseBody = XposedHelpers.callMethod(resp, "body"); | |
Object source = XposedHelpers.callMethod(responseBody, "source"); | |
XposedHelpers.callMethod(source, "request", Long.MAX_VALUE); | |
Object buffer = XposedHelpers.callMethod(source, "buffer"); | |
Object copy = XposedHelpers.callMethod(buffer, "clone"); | |
byte[] responseBodyByte = (byte[]) XposedHelpers.callMethod(copy, "readByteArray"); | |
RequestInfoModel rim = new RequestInfoModel(); | |
rim.StateCode = respCode; | |
rim.Url = requestUrl; | |
rim.ResponseBody = android.util.Base64.encodeToString(responseBodyByte, Base64.NO_WRAP); | |
rim.Method = requestMethod; | |
rim.RequestBody = requestBase64String; | |
rim.Headers = headerMap; | |
Utils.LogDebug(TAG, "[RequestInfo] Url:" + rim.Url + " rim: " + JSON.toJSONString(rim, JsonFastConfig)); | |
ResponseHandler(rim); | |
super.afterHookedMethod(param); | |
} | |
@Override | |
protected void beforeHookedMethod(MethodHookParam param) throws Throwable { | |
super.beforeHookedMethod(param); | |
} | |
}; | |
Class<?> ResponseClass = XposedHelpers.findClass(realCallClassName, lpparam.classLoader); | |
XposedHelpers.findAndHookMethod(ResponseClass, getResponseWithInterceptorChainMethodName, xc_methodHook); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment