From 7643ea3ee15dd79bc72a423dc22ba1579e4e7700 Mon Sep 17 00:00:00 2001 From: Antonio Cheong Date: Thu, 11 Apr 2024 04:50:14 +0100 Subject: [PATCH] merge implement proxy and Tor support #271 --- cmd/hydroxide/main.go | 51 +++++++++++++++++++++++++++++++++++++------ go.mod | 1 + go.sum | 2 ++ 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/cmd/hydroxide/main.go b/cmd/hydroxide/main.go index b266a0e..48c4161 100644 --- a/cmd/hydroxide/main.go +++ b/cmd/hydroxide/main.go @@ -28,10 +28,12 @@ import ( "github.com/emersion/hydroxide/imports" "github.com/emersion/hydroxide/protonmail" smtpbackend "github.com/emersion/hydroxide/smtp" + "github.com/google/uuid" ) const ( defaultAPIEndpoint = "https://mail.proton.me/api" + torAPIEndpoint = "https://mail.protonmailrmez3lotccipshtkleegetolb73fuirgj7r4o4vfu7ozyd.onion/api" defaultAppVersion = "Other" ) @@ -40,19 +42,44 @@ var ( apiEndpoint string appVersion string proxyURL string + tor bool ) +func makeHTTPClientFromProxy(proxyArg string) (*http.Client, error) { + fmtProxy := "" + client := &http.Client{} + if tor { + un, err := uuid.NewRandom() + if err != nil { + return nil, err + } + fmtProxy = fmt.Sprintf("socks5://hydroxide_%s::@%s", un, proxyArg) + + } else { + fmtProxy = fmt.Sprintf("socks5://%s", proxyArg) + } + + proxy, err := url.Parse(fmtProxy) + if err != nil { + return nil, err + } + + tr := &http.Transport{ + Proxy: http.ProxyURL(proxy), + } + + client = &http.Client{Transport: tr} + return client, nil +} func newClient() *protonmail.Client { - httpClient := http.DefaultClient + httpClient := &http.Client{} if proxyURL != "" { - proxyURL, err := url.Parse(proxyURL) + proxiedClient, err := makeHTTPClientFromProxy(proxyURL) if err != nil { - log.Fatal(err) + log.Fatal("Error creating proxied http.Client: ", err) } - httpClient.Transport = &http.Transport{ - Proxy: http.ProxyURL(proxyURL), - } - http.DefaultTransport = httpClient.Transport + + httpClient = proxiedClient } return &protonmail.Client{ RootURL: apiEndpoint, @@ -227,6 +254,7 @@ func main() { configHome := flag.String("config-home", "", "Path to the directory where hydroxide stores its configuration") flag.StringVar(&proxyURL, "proxy-url", "", "HTTP proxy URL (e.g. socks5://127.0.0.1:1080)") + flag.BoolVar(&tor, "tor", false, "If set, connect to ProtonMail over Tor") authCmd := flag.NewFlagSet("auth", flag.ExitOnError) exportSecretKeysCmd := flag.NewFlagSet("export-secret-keys", flag.ExitOnError) @@ -242,6 +270,15 @@ func main() { flag.Parse() + if tor && proxyURL == "" { + log.Fatal("Need -proxy to connect to ProtonMail over Tor") + } + + if tor { + log.Println("Connecting to ProtonMail over Tor") + apiEndpoint = torAPIEndpoint + } + tlsConfig, err := config.TLS(*tlsCert, *tlsCertKey, *tlsClientCA) if err != nil { log.Fatal(err) diff --git a/go.mod b/go.mod index 6e7ec07..8e3bdf5 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/emersion/go-smtp v0.19.0 github.com/emersion/go-vcard v0.0.0-20230815062825-8fda7d206ec9 github.com/emersion/go-webdav v0.3.2-0.20220524091811-5d845721d8f7 + github.com/google/uuid v1.6.0 golang.org/x/crypto v0.15.0 golang.org/x/term v0.14.0 ) diff --git a/go.sum b/go.sum index a763765..f1e3187 100644 --- a/go.sum +++ b/go.sum @@ -28,6 +28,8 @@ github.com/emersion/go-vcard v0.0.0-20230815062825-8fda7d206ec9 h1:ATgqloALX6cHC github.com/emersion/go-vcard v0.0.0-20230815062825-8fda7d206ec9/go.mod h1:HMJKR5wlh/ziNp+sHEDV2ltblO4JD2+IdDOWtGcQBTM= github.com/emersion/go-webdav v0.3.2-0.20220524091811-5d845721d8f7 h1:HqrKOBl8HdSnlo8kz72tCU36aK3WwSmpnnz04+dD0oc= github.com/emersion/go-webdav v0.3.2-0.20220524091811-5d845721d8f7/go.mod h1:uSM1VveeKtogBVWaYccTksToczooJ0rrVGNsgnDsr4Q= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=