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

feat(GODT-1602): add more logs to proxy requests. #141

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions server/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"

"github.com/ProtonMail/go-proton-api"
"github.com/gin-gonic/gin"
)

func newProxy(proxyOrigin, base, path string, transport http.RoundTripper) http.HandlerFunc {
origin, err := url.Parse(proxyOrigin)
func (s *proxyServer) newProxy(path string) http.HandlerFunc {
origin, err := url.Parse(s.origin)
if err != nil {
panic(err)
}
Expand All @@ -24,14 +26,42 @@ func newProxy(proxyOrigin, base, path string, transport http.RoundTripper) http.
Director: func(req *http.Request) {
req.URL.Scheme = origin.Scheme
req.URL.Host = origin.Host
req.URL.Path = origin.Path + strings.TrimPrefix(path, base)
req.URL.Path = origin.Path + strings.TrimPrefix(path, s.base)
req.Host = origin.Host
},

Transport: transport,
ModifyResponse: s.targetResponseModifier,
ErrorHandler: s.targetErrorHandler,

Transport: s.transport,
}).ServeHTTP
}

func (s *proxyServer) targetResponseModifier(r *http.Response) error {
if s.debug {
fmt.Printf("PROXY RESP %d: %s %s\n", r.StatusCode, r.Request.Method, r.Request.RequestURI)
}
if r.StatusCode == http.StatusInternalServerError || r.StatusCode == http.StatusBadGateway {
body, err := io.ReadAll(r.Body)
if err != nil {
body = []byte(fmt.Sprintf(`{"read_error": "%v"}`, err))
}

fmt.Printf("PROXY RESP %d CHANGING TO 429: %s %s\n=====BODY=====\n%s\n", r.StatusCode, r.Request.Method, r.Request.RequestURI, string(body))
r.StatusCode = http.StatusTooManyRequests
r.Body = io.NopCloser(bytes.NewReader(body))
}
return nil
}

func (s *proxyServer) targetErrorHandler(rw http.ResponseWriter, r *http.Request, targetError error) {
if s.debug {
fmt.Printf("PROXY ERROR: %s %s: %v\n", r.Method, r.RequestURI, targetError)
}

rw.WriteHeader(http.StatusTooManyRequests)
}

func (s *Server) handleProxy(base string) gin.HandlerFunc {
return func(c *gin.Context) {
proxy := newProxyServer(s.proxyOrigin, base, s.proxyTransport)
Expand Down Expand Up @@ -140,6 +170,8 @@ type proxyServer struct {
origin, base string

transport http.RoundTripper

debug bool
}

func newProxyServer(origin, base string, transport http.RoundTripper) *proxyServer {
Expand All @@ -148,6 +180,7 @@ func newProxyServer(origin, base string, transport http.RoundTripper) *proxyServ
origin: origin,
base: base,
transport: transport,
debug: os.Getenv("GO_PROTON_API_SERVER_LOGGER_ENABLED") != "",
}
}

Expand All @@ -161,7 +194,7 @@ func (s *proxyServer) handle(path string, h func(func(string) HandlerFunc) http.
buf := new(bytes.Buffer)

// Call the proxy, capturing whatever data it writes.
newProxy(s.origin, s.base, path, s.transport)(&writerWrapper{w, buf}, r)
s.newProxy(path)(&writerWrapper{w, buf}, r)

// If there is a gzip header entry, decode it.
if strings.Contains(w.Header().Get("Content-Encoding"), "gzip") {
Expand Down
Loading