Skip to content

Commit

Permalink
implement proxy and Tor support
Browse files Browse the repository at this point in the history
  • Loading branch information
h3xagonal committed Mar 30, 2024
1 parent ca53aaa commit f562134
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
62 changes: 62 additions & 0 deletions cmd/hydroxide/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"io"
"log"
"net/http"
"net/url"
"os"

"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/armor"
imapserver "github.com/emersion/go-imap/server"
"github.com/emersion/go-mbox"
"github.com/emersion/go-smtp"
"github.com/google/uuid"
"golang.org/x/term"

"github.com/emersion/hydroxide/auth"
Expand All @@ -31,20 +33,65 @@ import (

const (
defaultAPIEndpoint = "https://mail.proton.me/api"
torAPIEndpoint = "https://mail.protonmailrmez3lotccipshtkleegetolb73fuirgj7r4o4vfu7ozyd.onion/api"
defaultAppVersion = "Other"
)

var (
debug bool
apiEndpoint string
appVersion string
proxy string
httpClient http.Client
tor bool
)

func makeHTTPClientFromProxy(proxyArg string) (*http.Client, error) {
fmtProxy := ""
client := &http.Client{}
if tor {
un, err := uuid.NewRandom()
if err != nil {
log.Fatal("Error generating UUID for stream isolation", err)
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 {
log.Fatal("Error parsing proxy argument", err)
return nil, err
}

tr := &http.Transport{
Proxy: http.ProxyURL(proxy),
}

client = &http.Client{Transport: tr}
return client, nil
}

func newClient() *protonmail.Client {
httpClient := &http.Client{}
if proxy != "" {
proxiedClient, err := makeHTTPClientFromProxy(proxy)

if err != nil {
log.Fatal("Error creating proxied http.Client", err)
}

httpClient = proxiedClient
}

return &protonmail.Client{
RootURL: apiEndpoint,
AppVersion: appVersion,
Debug: debug,
HTTPClient: httpClient,
}
}

Expand Down Expand Up @@ -216,6 +263,10 @@ Global options:
Path to the certificate key to use for incoming connections (Optional)
-tls-client-ca /path/to/ca.pem
If set, clients must provide a certificate signed by the given CA (Optional)
-proxy
SOCKS5 proxy to use for client connections
-tor
If set, connct to ProtonMail over Tor
Environment variables:
HYDROXIDE_BRIDGE_PASS Don't prompt for the bridge password, use this variable instead
Expand All @@ -225,6 +276,8 @@ func main() {
flag.BoolVar(&debug, "debug", false, "Enable debug logs")
flag.StringVar(&apiEndpoint, "api-endpoint", defaultAPIEndpoint, "ProtonMail API endpoint")
flag.StringVar(&appVersion, "app-version", defaultAppVersion, "ProtonMail app version")
flag.StringVar(&proxy, "proxy", "", "SOCKS5 proxy to use for client connections")
flag.BoolVar(&tor, "tor", false, "If set, connect to ProtonMail over Tor")

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 @@ -254,6 +307,15 @@ func main() {

flag.Parse()

if tor && proxy == "" {
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)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit f562134

Please sign in to comment.