Skip to content

Commit

Permalink
Improve error log.
Browse files Browse the repository at this point in the history
  • Loading branch information
wi1dcard committed Mar 22, 2024
1 parent 390ff19 commit 40744ae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions fingerproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fingerproxy
import (
"context"
"crypto/tls"
"errors"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -91,12 +92,24 @@ func StartPrometheusClient(listenAddr string) {
}))
}

func proxyErrorHandler(rw http.ResponseWriter, req *http.Request, err error) {
ReverseProxyLog.Printf("proxy %s error (from %s): %v", req.URL.String(), req.RemoteAddr, err)

if errors.Is(err, context.DeadlineExceeded) ||
errors.Is(err, context.Canceled) {
rw.WriteHeader(http.StatusGatewayTimeout)
} else {
rw.WriteHeader(http.StatusBadGateway)
}
}

func DefaultReverseProxyHTTPHandler(forwardTo *url.URL) *reverseproxy.HTTPHandler {
return reverseproxy.NewHTTPHandler(
forwardTo,
&httputil.ReverseProxy{
ErrorLog: ReverseProxyLog,
FlushInterval: ReverseProxyFlushInterval,
ErrorHandler: proxyErrorHandler,
// TODO: customize transport
Transport: http.DefaultTransport.(*http.Transport).Clone(),
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/proxyserver/proxyserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ func (server *Server) serveConn(conn net.Conn) {
io.WriteString(re.Conn, "HTTP/1.0 400 Bad Request\r\n\r\nClient sent an HTTP request to an HTTPS server.\n")
}

server.logf("tls handshake: %s", err)
server.logf("tls handshake error from %s: %s", conn.RemoteAddr(), err)
server.metricsRequestsTotalInc("0", "")
return
}

// client hello stored in hajackedConn while reading for real handshake
rec, err := hijackedConn.GetClientHello()
if err != nil {
server.logf("could not read client hello from: %s", err)
server.logf("could not read client hello from %s: %s", conn.RemoteAddr(), err)
server.metricsRequestsTotalInc("0", "")
return
}
Expand Down

0 comments on commit 40744ae

Please sign in to comment.