-
Notifications
You must be signed in to change notification settings - Fork 267
/
okhttp_cat.js
488 lines (391 loc) · 19.5 KB
/
okhttp_cat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
放弃维护了
此实现是通过添加Interceptor的方法进行的打印okhttp日志,
动态代理创建Interceptor的实现类对象,添加到OkhttpClient的interceptors中
主要实现过程和之前写的Xposed实现的okhttp的Interceptor同一流程
https://github.com/siyujie/OkHttpLoggerInterceptor-hook
*/
/*
var Cls_OkHttpClient = "okhttp3.OkHttpClient";
var F_Client_interceptors = "interceptors";
var Cls_OkHttpClient$Builder = "okhttp3.OkHttpClient$Builder";
var M_Builder_build = "build";
var F_Builder_interceptors = "interceptors";
var Cls_Interceptor = "okhttp3.Interceptor";
var M_Interceptor_intercept = "intercept";
var M_chain_connection = "connection";
var M_connection_protocol = "protocol";
var M_chain_proceed = "proceed";
var M_req_body = "body";
var M_reqbody_contentLength = "contentLength";
var M_reqbody_contentType = "contentType";
var M_contentType_charset = "charset";
var M_reqbody_writeTo = "writeTo";
var M_req_headers = "headers";
var M_header_get = "get";
var M_header_name = "name";
var M_header_size = "size";
var M_header_value = "value";
var M_req_method = "method";
var M_req_newBuilder = "newBuilder";
var M_req_url = "url";
var M_rsp$builder_body = "body";
var M_rsp$builder_build = "build";
var M_rsp_body = "body";
var Cls_ResponseBody = "okhttp3.ResponseBody";
var M_rspBody_create = "create";
var M_rspBody_contentLength = "contentLength";
var M_rspBody_contentType = "contentType";
var M_rspBody_source = "source";
var M_source_request = "request";
var M_rspBody_string = "string";
var M_rsp_code = "code";
var M_rsp_headers = "headers";
var M_rsp_message = "message";
var M_rsp_newBuilder = "newBuilder";
var M_rsp_request = "request";
var M_rsp_headers = "trailers";
var M_chain_request = "request";
var Cls_okio_Buffer = "okio.Buffer";
var M_buffer_readByteArray = "readByteArray";
var M_buffer_readUtf8CodePoint = "readUtf8CodePoint";
function newInterceptor(InterceptorImplName){
var loggerInterceptor = null;
Java.perform(function(){
var classLoader = Java.classFactory.loader
var javaString = Java.use("java.lang.String")
var Charset = Java.use("java.nio.charset.Charset")
var defChatset = Charset.forName("UTF-8")
var interceptor = Java.use(InterceptorImplName);
var Proxy = Java.use("java.lang.reflect.Proxy");
var interfaces = interceptor.class.getInterfaces()
var InvocationHandler = Java.use("java.lang.reflect.InvocationHandler");
var mInvocationHandler = Java.registerClass({
name:"okhttp.MyInvocationHandler",
implements:[InvocationHandler],
methods:{
//public Object invoke(Object proxy, Method method, Object[] args)
invoke: function(param_obj,param_method,param_arg){
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Invoke .....")
if(param_method.getName() == M_Interceptor_intercept){
try {
var chain = getWrapper(param_arg[0]);
var request = chain[M_chain_request]()
var requestBody = request[M_req_body]();
var hasRequestBody = true
if(null == requestBody){
hasRequestBody = false
}
var connection = chain[M_chain_connection]()
var protocol = "http/1.1"
if(null != connection){
protocol = connection[M_connection_protocol]()
}
var httpUrl = request[M_req_url]()
console.log("");
console.log("┌─────────────────────────────────────────────────────────────────────────────────────");
var requestStartMessage = "--->"+request[M_req_method]()+" "+httpUrl+" "+protocol
if(hasRequestBody){
requestStartMessage += "("+requestBody[M_reqbody_contentLength]()+"-byte body)"
}
console.log("| "+requestStartMessage)
if(hasRequestBody){
var contentType = requestBody[M_reqbody_contentType]()
if(null != contentType){
console.log("| "+"Content-Type: " + contentType)
}
var contentLength = requestBody[M_reqbody_contentLength]()
if(contentLength != -1){
console.log("| "+"Content-Length: "+contentLength)
}
}
var requestHeaders = request[M_req_headers]()
var headersSize = requestHeaders[M_header_size]()
console.log("| headersSize : "+headersSize)
for(var i=0;i<headersSize;i++){
var name = requestHeaders[M_header_name](i)
if(!javaString.$new("Content-Type").equalsIgnoreCase(name) && !javaString.$new("Content-Length").equalsIgnoreCase(name)){
var value = requestHeaders[M_header_value](i)
console.log("| "+name + ": "+value)
}
}
if(!hasRequestBody){
console.log("|"+"--> END " + request[M_req_method]());
}else if(bodyEncoded(requestHeaders)){
console.log("|"+"--> END " + request[M_req_method]() + " (encoded body omitted)");
}else {
var BufferCls = Java.use(Cls_okio_Buffer)
var buffer = BufferCls.$new()
requestBody[M_reqbody_writeTo](buffer)
var reqByteString = getByteString(buffer)
var charset = defChatset
var contentType = requestBody[M_reqbody_contentType]()
if(null != contentType){
var appcharset = contentType[M_contentType_charset]();
if(null != appcharset){
charset = appcharset;
}
}
console.log("|")
if(isPlaintext(reqByteString)){
console.log("|"+readBufferString(reqByteString,charset))
console.log("|"+"--> END "+request[M_req_method]()+" ("+requestBody[M_reqbody_contentLength]()+"-byte body)")
}else{
console.log("|"+"--> END "+request[M_req_method]()+" (binary "+requestBody[M_reqbody_contentLength]()+"-byte body omitted)")
}
}
var startNs = new Date().getTime()
var response = null;
try {
response = chain[M_chain_proceed](request)
} catch (error) {
console.log("M_chain_proceed error : "+ error)
}
if(null == response){
console.log("| <-- HTTP FAILED")
return param_method.invoke(param_obj,param_arg)
}
var tookMs = new Date().getTime() - startNs
var responseBody = response[M_rsp_body]()
// console.log("responseBody : "+responseBody)
var contentLength = responseBody[M_rspBody_contentLength]()
// console.log("contentLength : "+contentLength)
var bodySize = contentLength != -1 ? contentLength + "-byte" : "unknown-length";
console.log("|<---"+response[M_rsp_code]()+" "+ response[M_rsp_message]()+" "+
response[M_rsp_request]()[M_req_url]()+
" (" + tookMs + "ms ," + bodySize + " body"+")"
)
var resp_headers = response[M_rsp_headers]()
var respHeaderSize = resp_headers[M_header_size]()
for (var i = 0; i < respHeaderSize;i++) {
console.log("| "+resp_headers[M_header_name](i)+": "+resp_headers[M_header_value](i))
}
var hasbody = !hasBody(response)
// console.log("hasbody : ",hasbody)
if(hasbody){
console.log("| No Body : ",response)
console.log("|"+"<-- END HTTP")
}else if(bodyEncoded(resp_headers)){
console.log("|"+"<-- END HTTP (encoded body omitted)")
}else{
var source = responseBody[M_rspBody_source]()
var rspByteString = getByteString(source)
var charset = defChatset
var contentType = responseBody[M_rspBody_contentType]()
if(null != contentType){
var appcharset = contentType[M_contentType_charset]()
if(null != appcharset){
charset = appcharset
}
}
if(!isPlaintext(rspByteString)){
console.log("| ");
console.log("|"+"<-- END HTTP (binary " + rspByteString.size() + "-byte body omitted)");
return param_method.invoke(param_obj,param_arg);
}
if (contentLength != 0) {
console.log("| "+response)
console.log("| "+JSON.stringify(JSON.parse(readBufferString(rspByteString, charset)), null, 4));
console.log("| ");
}
console.log("|"+"<-- END HTTP (" + rspByteString.size() + "-byte body)");
}
console.log("└───────────────────────────────────────────────────────────────────────────────────────");
console.log("");
var content = responseBody[M_rspBody_string]()
var mediaType = responseBody[M_rspBody_contentType]()
var class_responseBody = Java.use(Cls_ResponseBody)
var newBody = class_responseBody[M_rspBody_create](mediaType, content)
var newBuilder = response[M_rsp_newBuilder]()
return newBuilder[M_rsp$builder_body](newBody)[M_rsp$builder_build]()
} catch (error) {
console.log("Error MyInvocationHandler : ",error)
}
}else{
// console.log(">>>>>>>>>>>>error ");
}
return param_method.invoke(param_obj,param_arg);
}
}
})
loggerInterceptor = Proxy.newProxyInstance(classLoader,interfaces,mInvocationHandler.$new())
})
return loggerInterceptor;
}
function getWrapper(handle){
var chooseInstance = null;
Java.choose(handle.getClass().getName(),{
onMatch:function(instance){
if(instance.hashCode() == handle.hashCode()){
chooseInstance = instance
}
},
onComplete : function(){
// console.log("invokeMethod choose complete")
}
})
return chooseInstance;
}
function bodyEncoded(headers){
if(null == headers) return false;
var javaString = Java.use("java.lang.String")
var contentEncoding = headers[M_header_get]("Content-Encoding")
return contentEncoding != null && !contentEncoding.equalsIgnoreCase(javaString.$new("identity"))
}
function hasBody(response){
var javaString = Java.use("java.lang.String")
var m = response[M_rsp_request]()[M_req_method]();
// console.log(" >>>> hasBody mmmm : ",m)
if(javaString.$new("HEAD").equals(m)){
return false;
}
var Transfer_Encoding = "";
var resp_headers = response[M_rsp_headers]()
var respHeaderSize = resp_headers[M_header_size]()
for (var i = 0; i < respHeaderSize;i++) {
if(javaString.$new("Transfer-Encoding").equals(resp_headers[M_header_name](i))){
Transfer_Encoding = resp_headers[M_header_value](i);
break
}
}
// console.log(" >>>> hasBody Transfer_Encoding : ",Transfer_Encoding)
var code = response[M_rsp_code]()
// console.log(" >>>> hasBody code : ",code)
if(((code >= 100 && code < 200) || code == 204 || code == 304)
&& response[M_rspBody_contentLength] == -1
&& !javaString.$new("chunked").equalsIgnoreCase(Transfer_Encoding)
){
return false;
}
return true;
}
function isPlaintext(byteString){
try {
var bufferSize = byteString.size()
// console.log(" isPlaintext > bufferSize >>> "+ bufferSize)
var buffer = NewBuffer(byteString)
for (var i = 0; i < 16; i++) {
if(bufferSize == 0){
break
}
var codePoint = buffer.readUtf8CodePoint()
// console.log(" isPlaintext > codePoint >>> "+ codePoint)
var Character = Java.use("java.lang.Character")
//console.log(" isPlaintext ???? "+(Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)))
if(Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)){
return false;
}
}
return true;
} catch (error) {
// return false;
return true;
}
}
function getByteString(buffer){
var bytearray = buffer[M_buffer_readByteArray]();
var byteString = Java.use("com.singleman.okio.ByteString").of(bytearray)
return byteString;
}
function NewBuffer(byteString){
var bufferCls = Java.use("com.singleman.okio.Buffer");
var buffer = bufferCls.$new()
byteString.write(buffer)
return buffer;
}
function readBufferString(byteString, chatset){
var byteArray = byteString.toByteArray();
var str = Java.use("java.lang.String").$new(byteArray,chatset)
return str;
}
function findClassLoader(){
Java.perform(function(){
Java.enumerateClassLoaders({
onMatch:function(loader){
// console.log("loader : "+loader)
try {
if(loader.findClass(Cls_OkHttpClient$Builder)){
Java.classFactory.loader = loader
console.log("")
console.log("Change ClassLoader Success !")
console.log("")
}
} catch (error) {
// console.log(error)
}
},
onComplete:function(){
console.log("")
console.log("CenumerateClassLoaders onComplete !")
console.log("")
}
})
})
}
function hookbychoose(){
Java.perform(function(){
var okhttpDex = Java.openClassFile("/mnt/sdcard/okhttpfind.dex").load()
Java.choose(Cls_OkHttpClient,{
onMatch:function(instance){
try {
var interceptorsF = instance.class.getDeclaredField(F_Client_interceptors)
interceptorsF.setAccessible(true)
var interceptors = interceptorsF.get(instance)
var newInterceptors = Java.use("java.util.ArrayList").$new()
if(null != interceptors){
newInterceptors.addAll(interceptors)
}
if(newInterceptors.size() > 0){
var interImplClassName = newInterceptors.get(0).getClass().getName()
console.log("already Interceptor : "+interImplClassName)
var proxyObj = newInterceptor(interImplClassName);
newInterceptors.add(proxyObj)
console.log("")
console.log("Add Interceptor Success !! ")
console.log("")
}
instance[F_Client_interceptors].value = newInterceptors
} catch (error) {
console.log("Error : "+ error)
}
},
onComplete : function(){
}
})
})
}
function hookbyuse(){
Java.perform(function(){
var okhttpDex = Java.openClassFile("/mnt/sdcard/okhttpfind.dex").load()
Java.use(Cls_OkHttpClient$Builder)[M_Builder_build].overload().implementation = function(){
try {
var interceptorsF = this.class.getDeclaredField(F_Builder_interceptors)
interceptorsF.setAccessible(true)
var interceptors = interceptorsF.get(this)
var newInterceptors = Java.use("java.util.ArrayList").$new()
if(null != interceptors){
newInterceptors.addAll(interceptors)
}
if(newInterceptors.size() > 0){
// var interImplClassName = newInterceptors.get(0).getClass().getName()
// console.log("already Interceptor : "+interImplClassName)
// var proxyObj = newInterceptor(interImplClassName);
var proxyObj = newInterceptor("com.xingin.xhs.model.b.a");
newInterceptors.add(proxyObj)
console.log("Add Interceptor Success !! ")
}else{
var proxyObj = newInterceptor("com.xingin.xhs.model.b.a");
newInterceptors.add(proxyObj)
console.log("....Add Interceptor Success !! ")
}
this[F_Builder_interceptors].value = newInterceptors
} catch (error) {
console.log("Error : "+ error)
}
return this[M_Builder_build]()
}
})
}
// setImmediate(hookbychoose)
setImmediate(hookbyuse)
*/