Chrome DevTools 協(xié)議 - 在 Golang 中使用 gzip 主體的
我一直在研究一個(gè) golang 腳本,它使用chrome devtools 協(xié)議來:1)攔截一個(gè)請(qǐng)求2)抓取攔截請(qǐng)求的響應(yīng)體3)對(duì)html文檔進(jìn)行一些修改4)繼續(xù)攔截的請(qǐng)求該腳本適用于 HTML 文檔,除非Content-Encoding設(shè)置為gzip. 分步過程如下所示”1)攔截請(qǐng)求 s.Debugger.CallbackEvent("Network.requestIntercepted", func(params godet.Params) { iid := params.String("interceptionId") rtype := params.String("resourceType") reason := responses[rtype] headers := getHeadersString(params["responseHeaders"]) log.Println("[+] Request intercepted for", iid, rtype, params.Map("request")["url"]) if reason != "" { log.Println(" abort with reason", reason) } // Alter HTML in request response if s.Options.AlterDocument && rtype == "Document" && iid != "" { res, err := s.Debugger.GetResponseBodyForInterception(iid) if err != nil { log.Println("[-] Unable to get intercepted response body!") } rawAlteredResponse, err := AlterDocument(res, headers) if err != nil{ log.Println("[-] Unable to alter HTML") } if rawAlteredResponse != "" { log.Println("[+] Sending modified body") err := s.Debugger.ContinueInterceptedRequest(iid, godet.ErrorReason(reason), rawAlteredResponse, "", "", "", nil) if err != nil { fmt.Println("OH NOES AN ERROR!") log.Println(err) } } } else { s.Debugger.ContinueInterceptedRequest(iid, godet.ErrorReason(reason), "", "", "", "", nil) }})2)改變響應(yīng)體在這里,我對(duì) HTML 標(biāo)記做了一些小改動(dòng)procesHtml()(但該功能的代碼與此問題無關(guān),因此不會(huì)在此處發(fā)布)。我還從請(qǐng)求中獲取標(biāo)頭,并在必要時(shí)更新content-length和date繼續(xù)響應(yīng)。然后,我在調(diào)用時(shí)對(duì)主體進(jìn)行 gzip 壓縮r := gZipCompress([]byte(alteredBody),它返回一個(gè)字符串。然后將該字符串連接到標(biāo)頭,這樣我就可以制作rawResponse.func AlterDocument(debuggerResponse []byte, headers map[string]string) (string, error) { alteredBody, err := processHtml(debuggerResponse) if err != nil { return "", err }
查看完整描述