func (proxy *ProxyHttpServer) harLogAggregator() { proxy.Logf("Launching harLogAggregator()") for { select { case reqAndResp := <-proxy.harLogEntryCh: harEntry := new(har.Entry) harEntry.Request = har.ParseRequest(reqAndResp.req, reqAndResp.captureContent) harEntry.StartedDateTime = reqAndResp.start harEntry.Response = har.ParseResponse(reqAndResp.resp, reqAndResp.captureContent) harEntry.Time = reqAndResp.end.Sub(reqAndResp.start).Nanoseconds() / 1e6 harEntry.FillIPAddress(reqAndResp.req) // should take it from the actual conn? if len(proxy.harLog.Log.Entries) == 0 { proxy.harLog.AppendPage(har.Page{ ID: "0", StartedDateTime: harEntry.StartedDateTime, Title: "GoProxy Log", }) } harEntry.PageRef = "0" proxy.harLog.AppendEntry(*harEntry) case filename := <-proxy.harFlushRequest: proxy.Logf("Received HAR flush request to %q", filename) if len(proxy.harLog.Log.Entries) == 0 { proxy.Logf("No HAR entries to flush") continue } err := flushHarToDisk(proxy.harLog, filename) if err != nil { proxy.Logf("Error flushing HAR file to disk: %s", err) } else { proxy.Logf("Wrote HAR file to disk: %s", filename) } proxy.harLog = har.New() // reset } } }
// New proxy server, logs to StdErr by default func NewProxyHttpServer() *ProxyHttpServer { proxy := ProxyHttpServer{ Logger: log.New(os.Stderr, "", log.LstdFlags), requestHandlers: []Handler{}, responseHandlers: []Handler{}, connectHandlers: []Handler{}, NonProxyHandler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { http.Error(w, "This is a proxy server. Does not respond to non-proxy requests.", 500) }), Transport: &http.Transport{ TLSClientConfig: tlsClientSkipVerify, Proxy: http.ProxyFromEnvironment, }, MITMCertConfig: GoproxyCaConfig, harLog: har.New(), harLogEntryCh: make(chan harReqAndResp, 10), harFlushRequest: make(chan string, 10), } proxy.ConnectDial = dialerFromEnv(&proxy) return &proxy }