Commit 4b590d5a authored by Ivar Derksen's avatar Ivar Derksen Committed by Sietse Ringers
Browse files

Improved logs of incoming HTTP traffic at IRMA server

parent 2f4b8a7b
......@@ -298,11 +298,9 @@ func (s *Server) HandleProtocolMessage(
}
}
s.conf.Logger.WithFields(logrus.Fields{"method": method, "path": path}).Debugf("Routing protocol message")
if len(message) > 0 {
s.conf.Logger.Trace("POST body: ", string(message))
}
s.conf.Logger.Trace("HTTP headers: ", server.ToJson(headers))
token, noun, err := ParsePath(path)
if err != nil {
status, output = server.JsonResponse(nil, server.RemoteError(server.ErrorUnsupported, ""))
......
......@@ -184,7 +184,6 @@ func JsonResponse(v interface{}, err *irma.RemoteError) (int, []byte) {
Logger.Error("Failed to serialize response:", e.Error())
return http.StatusInternalServerError, nil
}
Logger.Tracef("HTTP JSON response: %d %s", status, string(b))
return status, b
}
......
......@@ -5,6 +5,7 @@
package requestorserver
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
......@@ -14,6 +15,7 @@ import (
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
......@@ -177,6 +179,7 @@ func (s *Server) ClientHandler() http.Handler {
func (s *Server) Handler() http.Handler {
router := chi.NewRouter()
router.Use(cors.New(corsOptions).Handler)
router.Use(s.logHandler)
if !s.conf.separateClientServer() {
// Mount server for irmaclient
......@@ -202,6 +205,43 @@ func (s *Server) Handler() http.Handler {
return router
}
// Wrapper for logging HTTP traffic
func (s *Server) logHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
if s.conf.Verbose >= 2 {
t1 := time.Now()
respBuf := bytes.NewBufferString(": ") // : for printing format
ww.Tee(respBuf) // Copy result into buffer for logging
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
recipient := ""
// IP addresses of irmaclient users are hidden in logs for privacy
if !strings.HasPrefix(r.URL.EscapedPath(), "/irma/") {
recipient = " from " + r.RemoteAddr
}
s.conf.Logger.Tracef("=> New message \"%s %s://%s%s %s\"%s",
r.Method, scheme, r.Host, r.RequestURI, r.Proto, recipient)
s.conf.Logger.Trace("HTTP headers: ", server.ToJson(r.Header))
defer func() {
// Printing response after HTTP is served
resp := ""
if ww.BytesWritten() > 0 {
resp = respBuf.String()
}
s.conf.Logger.Tracef("<= Responded with %d in %s%s", ww.Status(), time.Since(t1).String(), resp)
}()
}
next.ServeHTTP(ww, r)
return
})
}
func (s *Server) StaticFilesHandler() http.Handler {
if len(s.conf.URL) > 6 {
url := s.conf.URL[:len(s.conf.URL)-6] + s.conf.StaticPrefix
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment