From d4451a8a151ba656c16a2093f86b29d7c52dad75 Mon Sep 17 00:00:00 2001 From: Jarno Rankinen Date: Tue, 26 Mar 2024 21:26:28 +0200 Subject: [PATCH] Rename package, don't serve IMAP --- auth/auth.go | 4 +- carddav/carddav.go | 2 +- cmd/hydroxide-push/main.go | 119 ++++++++++--------------------------- cmd/hydroxide/main.go | 18 +++--- events/events.go | 2 +- exports/messages.go | 2 +- imap/backend.go | 4 +- imap/database/mailbox.go | 2 +- imap/database/user.go | 4 +- imap/mailbox.go | 4 +- imap/message.go | 2 +- imap/user.go | 10 ++-- imports/messages.go | 2 +- ntfy/ntfy.go | 95 +++++++++++++++++++++++++++++ smtp/smtp.go | 4 +- 15 files changed, 156 insertions(+), 118 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index 5d8fa4e..8a346bc 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -13,8 +13,8 @@ import ( "golang.org/x/crypto/bcrypt" "golang.org/x/crypto/nacl/secretbox" - "github.com/emersion/hydroxide/config" - "github.com/emersion/hydroxide/protonmail" + "github.com/0ranki/hydroxide-push/config" + "github.com/0ranki/hydroxide-push/protonmail" ) func authFilePath() (string, error) { diff --git a/carddav/carddav.go b/carddav/carddav.go index bd7e58a..34ac688 100644 --- a/carddav/carddav.go +++ b/carddav/carddav.go @@ -13,10 +13,10 @@ import ( "strings" "sync" + "github.com/0ranki/hydroxide-push/protonmail" "github.com/ProtonMail/go-crypto/openpgp" "github.com/emersion/go-vcard" "github.com/emersion/go-webdav/carddav" - "github.com/emersion/hydroxide/protonmail" ) // TODO: use a HTTP error diff --git a/cmd/hydroxide-push/main.go b/cmd/hydroxide-push/main.go index 4db87e9..f147604 100644 --- a/cmd/hydroxide-push/main.go +++ b/cmd/hydroxide-push/main.go @@ -5,16 +5,15 @@ import ( "crypto/tls" "flag" "fmt" - "github.com/emersion/go-imap" + "github.com/0ranki/hydroxide-push/auth" + "github.com/0ranki/hydroxide-push/config" + "github.com/0ranki/hydroxide-push/events" + imapbackend "github.com/0ranki/hydroxide-push/imap" + "github.com/0ranki/hydroxide-push/ntfy" + "github.com/0ranki/hydroxide-push/protonmail" imapserver "github.com/emersion/go-imap/server" - "github.com/emersion/hydroxide/auth" - "github.com/emersion/hydroxide/config" - "github.com/emersion/hydroxide/events" - imapbackend "github.com/emersion/hydroxide/imap" - "github.com/emersion/hydroxide/protonmail" "golang.org/x/term" "log" - "net" "os" "time" ) @@ -28,9 +27,7 @@ var ( debug bool apiEndpoint string appVersion string - - //imapUser *backend.User - ntfyTopic string + cfg ntfy.NtfyConfig ) func newClient() *protonmail.Client { @@ -60,7 +57,7 @@ func askPass(prompt string) ([]byte, error) { return b, err } -func listenAndServeIMAP(addr string, debug bool, authManager *auth.Manager, eventsManager *events.Manager, tlsConfig *tls.Config) error { +func listenEventsAndNotify(addr string, debug bool, authManager *auth.Manager, eventsManager *events.Manager, tlsConfig *tls.Config) error { be := imapbackend.New(authManager, eventsManager) s := imapserver.New(be) s.Addr = addr @@ -69,53 +66,19 @@ func listenAndServeIMAP(addr string, debug bool, authManager *auth.Manager, even if debug { s.Debug = os.Stdout } - - if s.TLSConfig != nil { - log.Println("IMAP server listening with TLS on", s.Addr) - return s.ListenAndServeTLS() + ntfy.Login(&cfg, be) + log.Println("Listening for events", s.Addr) + for { + time.Sleep(10 * time.Second) } - go func() { - time.Sleep(1 * time.Second) - c, _ := net.ResolveIPAddr("ip", "127.0.0.1") - conn := imap.ConnInfo{ - RemoteAddr: c, - LocalAddr: c, - TLS: nil, - } - usernames, err := auth.ListUsernames() - if err != nil { - log.Fatal(err) - } - if len(usernames) > 1 { - log.Fatal("only one login supported for now") - } - if len(usernames) == 0 { - executable, _ := os.Executable() - log.Fatal("login first using " + executable + " auth ") - } - // TODO: bridge password - _, err = be.Login(&conn, usernames[0], os.Getenv("HYDROXIDE_BRIDGE_PASS")) - if err != nil { - log.Fatal(err) - } - }() - - log.Println("IMAP server listening on", s.Addr) - return s.ListenAndServe() + return nil } -const usage = `usage: hydroxide [options...] +const usage = `usage: hydroxide-push [options...] Commands: auth Login to ProtonMail via hydroxide - carddav Run hydroxide as a CardDAV server - export-secret-keys Export secret keys - imap Run hydroxide as an IMAP server - import-messages [file] Import messages - export-messages [options...] Export messages - sendmail -- sendmail(1) interface - serve Run all servers - smtp Run hydroxide as an SMTP server - status View hydroxide status + status View hydroxide status + notify Start the notification daemon Global options: -debug @@ -124,30 +87,6 @@ Global options: ProtonMail API endpoint -app-version ProtonMail application version - -smtp-host example.com - Allowed SMTP email hostname on which hydroxide listens, defaults to 127.0.0.1 - -imap-host example.com - Allowed IMAP email hostname on which hydroxide listens, defaults to 127.0.0.1 - -carddav-host example.com - Allowed SMTP email hostname on which hydroxide listens, defaults to 127.0.0.1 - -smtp-port example.com - SMTP port on which hydroxide listens, defaults to 1025 - -imap-port example.com - IMAP port on which hydroxide listens, defaults to 1143 - -carddav-port example.com - CardDAV port on which hydroxide listens, defaults to 8080 - -disable-imap - Disable IMAP for hydroxide serve - -disable-smtp - Disable SMTP for hydroxide serve - -disable-carddav - Disable CardDAV for hydroxide serve - -tls-cert /path/to/cert.pem - Path to the certificate to use for incoming connections (Optional) - -tls-key /path/to/key.pem - 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) Environment variables: HYDROXIDE_BRIDGE_PASS Don't prompt for the bridge password, use this variable instead @@ -157,10 +96,6 @@ 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(&ntfyTopic, "topic", "", "ntfy.sh/NextPush topic to push notifications to") - - imapHost := "127.0.0.1" // flag.String("imap-host", "127.0.0.1", "Allowed IMAP email hostname on which hydroxide listens, defaults to 127.0.0.1") - imapPort := "1143" // flag.String("imap-port", "1143", "IMAP port on which hydroxide listens, defaults to 1143") tlsCert := flag.String("tls-cert", "", "Path to the certificate to use for incoming connections") tlsCertKey := flag.String("tls-key", "", "Path to the certificate key to use for incoming connections") @@ -294,16 +229,24 @@ func main() { } case "setup-ntfy": - - case "notify": - if ntfyTopic == "" { - log.Fatal("please set ntfy.sh topic using --topic") + scanner := bufio.NewScanner(os.Stdin) + fmt.Printf("Input push server URL (e.g. 'http://ntfy.sh') : ") + scanner.Scan() + cfg.URL = scanner.Text() + scanner = bufio.NewScanner(os.Stdin) + fmt.Printf("Input push topic (e.g. my-proton-notifications)\nLeave blank to generate a random one: ") + scanner.Scan() + cfg.Topic = scanner.Text() + fmt.Printf("Using URL %s\n", cfg.String()) + err = cfg.Save() + if err != nil { + log.Fatal(err) } - addr := imapHost + ":" + imapPort + log.Println("Notification configuration saved") + case "notify": authManager := auth.NewManager(newClient) eventsManager := events.NewManager() - - log.Fatal(listenAndServeIMAP(addr, debug, authManager, eventsManager, tlsConfig)) + log.Fatal(listenEventsAndNotify("0", debug, authManager, eventsManager, tlsConfig)) default: fmt.Print(usage) diff --git a/cmd/hydroxide/main.go b/cmd/hydroxide/main.go index b32780f..c1456c4 100644 --- a/cmd/hydroxide/main.go +++ b/cmd/hydroxide/main.go @@ -18,15 +18,15 @@ import ( "github.com/emersion/go-smtp" "golang.org/x/term" - "github.com/emersion/hydroxide/auth" - "github.com/emersion/hydroxide/carddav" - "github.com/emersion/hydroxide/config" - "github.com/emersion/hydroxide/events" - "github.com/emersion/hydroxide/exports" - imapbackend "github.com/emersion/hydroxide/imap" - "github.com/emersion/hydroxide/imports" - "github.com/emersion/hydroxide/protonmail" - smtpbackend "github.com/emersion/hydroxide/smtp" + "github.com/0ranki/hydroxide-push/auth" + "github.com/0ranki/hydroxide-push/carddav" + "github.com/0ranki/hydroxide-push/config" + "github.com/0ranki/hydroxide-push/events" + "github.com/0ranki/hydroxide-push/exports" + imapbackend "github.com/0ranki/hydroxide-push/imap" + "github.com/0ranki/hydroxide-push/imports" + "github.com/0ranki/hydroxide-push/protonmail" + smtpbackend "github.com/0ranki/hydroxide-push/smtp" ) const ( diff --git a/events/events.go b/events/events.go index f24a97d..48482f8 100644 --- a/events/events.go +++ b/events/events.go @@ -5,7 +5,7 @@ import ( "sync" "time" - "github.com/emersion/hydroxide/protonmail" + "github.com/0ranki/hydroxide-push/protonmail" ) const pollInterval = 10 * time.Second diff --git a/exports/messages.go b/exports/messages.go index 3eddb7c..c915530 100644 --- a/exports/messages.go +++ b/exports/messages.go @@ -12,7 +12,7 @@ import ( "github.com/emersion/go-message/mail" "github.com/emersion/go-message/textproto" - "github.com/emersion/hydroxide/protonmail" + "github.com/0ranki/hydroxide-push/protonmail" ) func writeMessage(c *protonmail.Client, privateKeys openpgp.KeyRing, w io.Writer, msg *protonmail.Message) error { diff --git a/imap/backend.go b/imap/backend.go index e44769b..803a019 100644 --- a/imap/backend.go +++ b/imap/backend.go @@ -7,8 +7,8 @@ import ( "github.com/emersion/go-imap" imapbackend "github.com/emersion/go-imap/backend" - "github.com/emersion/hydroxide/auth" - "github.com/emersion/hydroxide/events" + "github.com/0ranki/hydroxide-push/auth" + "github.com/0ranki/hydroxide-push/events" ) var errNotYetImplemented = errors.New("not yet implemented") diff --git a/imap/database/mailbox.go b/imap/database/mailbox.go index fd9b053..ab06270 100644 --- a/imap/database/mailbox.go +++ b/imap/database/mailbox.go @@ -7,7 +7,7 @@ import ( "github.com/boltdb/bolt" - "github.com/emersion/hydroxide/protonmail" + "github.com/0ranki/hydroxide-push/protonmail" ) func serializeUID(uid uint32) []byte { diff --git a/imap/database/user.go b/imap/database/user.go index bc3e8b2..d7bc547 100644 --- a/imap/database/user.go +++ b/imap/database/user.go @@ -6,8 +6,8 @@ import ( "github.com/boltdb/bolt" - "github.com/emersion/hydroxide/config" - "github.com/emersion/hydroxide/protonmail" + "github.com/0ranki/hydroxide-push/config" + "github.com/0ranki/hydroxide-push/protonmail" ) var ErrNotFound = errors.New("message not found in local database") diff --git a/imap/mailbox.go b/imap/mailbox.go index 1068c28..09fe15a 100644 --- a/imap/mailbox.go +++ b/imap/mailbox.go @@ -10,8 +10,8 @@ import ( "github.com/emersion/go-imap" imapbackend "github.com/emersion/go-imap/backend" - "github.com/emersion/hydroxide/imap/database" - "github.com/emersion/hydroxide/protonmail" + "github.com/0ranki/hydroxide-push/imap/database" + "github.com/0ranki/hydroxide-push/protonmail" ) const delimiter = "/" diff --git a/imap/message.go b/imap/message.go index 5051259..4472c38 100644 --- a/imap/message.go +++ b/imap/message.go @@ -15,7 +15,7 @@ import ( "github.com/emersion/go-message" "github.com/emersion/go-message/mail" - "github.com/emersion/hydroxide/protonmail" + "github.com/0ranki/hydroxide-push/protonmail" ) func messageID(msg *protonmail.Message) string { diff --git a/imap/user.go b/imap/user.go index dfd879d..82c0127 100644 --- a/imap/user.go +++ b/imap/user.go @@ -1,17 +1,17 @@ package imap import ( - "github.com/emersion/hydroxide/ntfy" + "github.com/0ranki/hydroxide-push/ntfy" "log" "strings" "sync" + "github.com/0ranki/hydroxide-push/events" + "github.com/0ranki/hydroxide-push/imap/database" + "github.com/0ranki/hydroxide-push/protonmail" "github.com/ProtonMail/go-crypto/openpgp" "github.com/emersion/go-imap" imapbackend "github.com/emersion/go-imap/backend" - "github.com/emersion/hydroxide/events" - "github.com/emersion/hydroxide/imap/database" - "github.com/emersion/hydroxide/protonmail" ) var systemMailboxes = []struct { @@ -118,7 +118,7 @@ func newUser(be *backend, username string, c *protonmail.Client, privateKeys ope go uu.receiveEvents(be.updates, ch) uu.eventsReceiver = be.eventsManager.Register(c, u.Name, ch, done) - log.Printf("User %q logged in via IMAP", u.Name) + log.Printf("Logged in as user %q", u.Name) return uu, nil } diff --git a/imports/messages.go b/imports/messages.go index 60fe9fd..0c8ae40 100644 --- a/imports/messages.go +++ b/imports/messages.go @@ -8,7 +8,7 @@ import ( "github.com/ProtonMail/go-crypto/openpgp/armor" "github.com/emersion/go-message/mail" - "github.com/emersion/hydroxide/protonmail" + "github.com/0ranki/hydroxide-push/protonmail" ) func ImportMessage(c *protonmail.Client, r io.Reader) error { diff --git a/ntfy/ntfy.go b/ntfy/ntfy.go index 6fc7d2e..8388e1c 100644 --- a/ntfy/ntfy.go +++ b/ntfy/ntfy.go @@ -1,10 +1,46 @@ package ntfy import ( + "bufio" + "encoding/json" + "fmt" + "github.com/0ranki/hydroxide-push/auth" + "github.com/0ranki/hydroxide-push/config" + "github.com/emersion/go-imap" + "github.com/emersion/go-imap/backend" + "log" + "net" "net/http" + "os" "strings" ) +type NtfyConfig struct { + URL string `json:"url"` + Topic string `json:"topic"` + BridgePw string `json:"bridgePw"` +} + +func (cfg *NtfyConfig) String() string { + return fmt.Sprintf("%s/%s", cfg.URL, cfg.Topic) +} + +func (cfg *NtfyConfig) Save() error { + b, err := json.Marshal(cfg) + if err != nil { + return err + } + path, err := ntfyConfigFile() + if err != nil { + return err + } + return os.WriteFile(path, b, 0600) +} + +func ntfyConfigFile() (string, error) { + return config.Path("notify.json") +} + func Notify() { req, _ := http.NewRequest("POST", "https://push.oranki.net/testing20240325", strings.NewReader("New message received")) req.Header.Set("Title", "ProtoMail") @@ -12,3 +48,62 @@ func Notify() { req.Header.Set("Tags", "envelope") http.DefaultClient.Do(req) } + +func (cfg *NtfyConfig) Read() error { + f, err := ntfyConfigFile() + if err == nil { + b, err := os.ReadFile(f) + if err == nil { + err = json.Unmarshal(b, &cfg) + } + if err != nil { + return err + } + } + return nil +} + +func Login(cfg *NtfyConfig, be backend.Backend) { + //time.Sleep(1 * time.Second) + c, _ := net.ResolveIPAddr("ip", "127.0.0.1") + conn := imap.ConnInfo{ + RemoteAddr: c, + LocalAddr: c, + TLS: nil, + } + usernames, err := auth.ListUsernames() + if err != nil { + log.Fatal(err) + } + if len(usernames) > 1 { + log.Fatal("only one login supported for now") + } + err = cfg.Read() + if err != nil { + log.Println(err) + } + if len(usernames) == 0 || cfg.URL == "" || cfg.Topic == "" { + executable, _ := os.Executable() + log.Println("login first using " + executable + " auth ") + log.Fatalln("then setup ntfy using " + executable + "setup-ntfy") + } + cfg.BridgePw = os.Getenv("HYDROXIDE_BRIDGE_PASSWORD") + if cfg.BridgePw == "" { + scanner := bufio.NewScanner(os.Stdin) + fmt.Printf("Bridge password: ") + scanner.Scan() + cfg.BridgePw = scanner.Text() + scanner = bufio.NewScanner(os.Stdin) + fmt.Printf("Save password to config? The password is stored in plain text! (yes/n): ") + scanner.Scan() + if scanner.Text() == "yes" { + if err = cfg.Save(); err != nil { + log.Fatal("failed to save notification config") + } + } + } + _, err = be.Login(&conn, usernames[0], cfg.BridgePw) + if err != nil { + log.Fatal(err) + } +} diff --git a/smtp/smtp.go b/smtp/smtp.go index 31ecb99..76b9bd7 100644 --- a/smtp/smtp.go +++ b/smtp/smtp.go @@ -14,8 +14,8 @@ import ( "github.com/emersion/go-sasl" "github.com/emersion/go-smtp" - "github.com/emersion/hydroxide/auth" - "github.com/emersion/hydroxide/protonmail" + "github.com/0ranki/hydroxide-push/auth" + "github.com/0ranki/hydroxide-push/protonmail" ) func toPMAddressList(addresses []*mail.Address) []*protonmail.MessageAddress {