Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dumping har logs to file #346

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ import (
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -247,6 +248,7 @@ var (
validity = flag.Duration("validity", time.Hour, "window of time that MITM certificates are valid")
allowCORS = flag.Bool("cors", false, "allow CORS requests to configure the proxy")
harLogging = flag.Bool("har", false, "enable HAR logging API")
harFile = flag.String("harFile", "./martian.har", "har file path")
marblLogging = flag.Bool("marbl", false, "enable MARBL logging API")
trafficShaping = flag.Bool("traffic-shaping", false, "enable traffic shaping API")
skipTLSVerify = flag.Bool("skip-tls-verify", false, "skip TLS server verification; insecure")
Expand Down Expand Up @@ -389,6 +391,7 @@ func main() {

configure("/logs", har.NewExportHandler(hl), mux)
configure("/logs/reset", har.NewResetHandler(hl), mux)
configure("/logs/dump", har.NewDumpHandler(hl, *harFile), mux)
}

logger := martianlog.NewLogger()
Expand Down Expand Up @@ -442,6 +445,18 @@ func main() {

<-sigc

if *harLogging {
log.Println("martian: dumping har file")
u := url.URL{}
proxy, _ := u.Parse(fmt.Sprintf("http://%v", *addr))
transport := http.Transport{}
transport.Proxy = http.ProxyURL(proxy) // set proxy

client := &http.Client{}
client.Transport = &transport
client.Get("http://martian.proxy/logs/dump")
}

log.Println("martian: shutting down")
os.Exit(0)
}
Expand Down
64 changes: 63 additions & 1 deletion har/har_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ package har

import (
"encoding/json"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"

"github.com/google/martian/v3/log"
Expand All @@ -31,6 +34,11 @@ type resetHandler struct {
logger *Logger
}

type dumpHandler struct {
logger *Logger
filePath string
}

// NewExportHandler returns an http.Handler for requesting HAR logs.
func NewExportHandler(l *Logger) http.Handler {
return &exportHandler{
Expand All @@ -45,6 +53,61 @@ func NewResetHandler(l *Logger) http.Handler {
}
}

// NewDumpHandler returns an http.Handler for dumping log entries to file.
func NewDumpHandler(l *Logger, path string) http.Handler {
return &dumpHandler{
logger: l,
filePath: path,
}
}

type fileResponseWriter struct {
file io.Writer
multi io.Writer
}

func (w *fileResponseWriter) Write(b []byte) (int, error) {
return w.multi.Write(b)
}

func newFileResponseWriter(file io.Writer) io.Writer {
multi := io.MultiWriter(file)
return &fileResponseWriter{
file: file,
multi: multi,
}
}

func (h *dumpHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
rw.Header().Add("Allow", "GET")
rw.WriteHeader(http.StatusMethodNotAllowed)
log.Errorf("har.ServeHTTP: method not allowed: %s", req.Method)
return
}
log.Debugf("exportHandler.ServeHTTP: writing HAR logs to ResponseWriter")
rw.Header().Set("Content-Type", "application/json; charset=utf-8")

dir := filepath.Dir(h.filePath)
err := os.MkdirAll(dir, 0764)
if err != nil {
log.Errorf("mkdir", err)
}

file, err := os.Create(h.filePath)
defer file.Close()
mrsiano marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Errorf("create file", err)
return
}

writer := newFileResponseWriter(file)
hl := h.logger.Export()
encoder := json.NewEncoder(writer)
encoder.SetIndent("", "\t")
encoder.Encode(hl)
}

// ServeHTTP writes the log in HAR format to the response body.
func (h *exportHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
Expand All @@ -70,7 +133,6 @@ func (h *resetHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
return
}


v, err := parseBoolQueryParam(req.URL.Query(), "return")
if err != nil {
log.Errorf("har: invalid value for return param: %s", err)
Expand Down