From d97056a61a9dae87742999effa363baabae8e477 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sat, 7 Mar 2020 18:12:53 +0100 Subject: [PATCH] protonmail: add Client.Debug This prints HTTP requests and responses. --- cmd/hydroxide/main.go | 13 ++++++++----- protonmail/protonmail.go | 16 ++++++++++++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/cmd/hydroxide/main.go b/cmd/hydroxide/main.go index aad6a6b..080e586 100644 --- a/cmd/hydroxide/main.go +++ b/cmd/hydroxide/main.go @@ -26,10 +26,13 @@ import ( smtpbackend "github.com/emersion/hydroxide/smtp" ) +var debug bool + func newClient() *protonmail.Client { return &protonmail.Client{ RootURL: "https://mail.protonmail.com/api", AppVersion: "Web_3.16.6", + Debug: debug, } } @@ -134,7 +137,7 @@ Flags: CardDAV port on which hydroxide listens, defaults to 8080` func main() { - debug := flag.Bool("debug", false, "Enable debug logs") + flag.BoolVar(&debug, "debug", false, "Enable debug logs") smtpHost := flag.String("smtp-host", "127.0.0.1", "Allowed SMTP email hostname on which hydroxide listens, defaults to 127.0.0.1") smtpPort := flag.String("smtp-port", "1025", "SMTP port on which hydroxide listens, defaults to 1025") @@ -335,12 +338,12 @@ func main() { case "smtp": addr := *smtpHost + ":" + *smtpPort authManager := auth.NewManager(newClient) - log.Fatal(listenAndServeSMTP(addr, *debug, authManager)) + log.Fatal(listenAndServeSMTP(addr, debug, authManager)) case "imap": addr := *imapHost + ":" + *imapPort authManager := auth.NewManager(newClient) eventsManager := events.NewManager() - log.Fatal(listenAndServeIMAP(addr, *debug, authManager, eventsManager)) + log.Fatal(listenAndServeIMAP(addr, debug, authManager, eventsManager)) case "carddav": addr := *carddavHost + ":" + *carddavPort authManager := auth.NewManager(newClient) @@ -356,10 +359,10 @@ func main() { done := make(chan error, 3) go func() { - done <- listenAndServeSMTP(smtpAddr, *debug, authManager) + done <- listenAndServeSMTP(smtpAddr, debug, authManager) }() go func() { - done <- listenAndServeIMAP(imapAddr, *debug, authManager, eventsManager) + done <- listenAndServeIMAP(imapAddr, debug, authManager, eventsManager) }() go func() { done <- listenAndServeCardDAV(carddavAddr, authManager, eventsManager) diff --git a/protonmail/protonmail.go b/protonmail/protonmail.go index 6f5cb85..3680955 100644 --- a/protonmail/protonmail.go +++ b/protonmail/protonmail.go @@ -55,6 +55,7 @@ func (err *APIError) Error() string { type Client struct { RootURL string AppVersion string + Debug bool HTTPClient *http.Client ReAuth func() error @@ -77,7 +78,9 @@ func (c *Client) newRequest(method, path string, body io.Reader) (*http.Request, return nil, err } - //log.Printf(">> %v %v\n", method, path) + if c.Debug { + log.Printf(">> %v %v\n", req.Method, req.URL.Path) + } req.Header.Set("X-Pm-Appversion", c.AppVersion) req.Header.Set(headerAPIVersion, strconv.Itoa(Version)) @@ -92,13 +95,15 @@ func (c *Client) newJSONRequest(method, path string, body interface{}) (*http.Re } b := buf.Bytes() - //log.Printf(">> %v %v\n%v", method, path, string(b)) - req, err := c.newRequest(method, path, bytes.NewReader(b)) if err != nil { return nil, err } + if c.Debug { + log.Print(string(b)) + } + req.Header.Set("Content-Type", "application/json") req.GetBody = func() (io.ReadCloser, error) { return ioutil.NopCloser(bytes.NewReader(b)), nil @@ -157,7 +162,10 @@ func (c *Client) doJSON(req *http.Request, respData interface{}) error { return err } - //log.Printf("<< %v %v\n%#v", req.Method, req.URL.Path, respData) + if c.Debug { + log.Printf("<< %v %v", req.Method, req.URL.Path) + log.Printf("%#v", respData) + } if maybeError, ok := respData.(maybeError); ok { if err := maybeError.Err(); err != nil {