Skip to content

Commit

Permalink
protonmail: add Client.Debug
Browse files Browse the repository at this point in the history
This prints HTTP requests and responses.
  • Loading branch information
emersion committed Mar 7, 2020
1 parent 4ede51e commit d97056a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
13 changes: 8 additions & 5 deletions cmd/hydroxide/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
16 changes: 12 additions & 4 deletions protonmail/protonmail.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (err *APIError) Error() string {
type Client struct {
RootURL string
AppVersion string
Debug bool

HTTPClient *http.Client
ReAuth func() error
Expand All @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d97056a

Please sign in to comment.