Skip to content

Commit

Permalink
merge implement proxy and Tor support emersion#271
Browse files Browse the repository at this point in the history
  • Loading branch information
acheong08 committed Apr 11, 2024
1 parent 8d37047 commit 7643ea3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
51 changes: 44 additions & 7 deletions cmd/hydroxide/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
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 7643ea3

Please sign in to comment.