Rename package, don't serve IMAP

This commit is contained in:
Jarno Rankinen 2024-03-26 21:26:28 +02:00
parent 308eec6c69
commit d4451a8a15
15 changed files with 156 additions and 118 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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 <protonmail username>")
}
// 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...] <command>
const usage = `usage: hydroxide-push [options...] <command>
Commands:
auth <username> Login to ProtonMail via hydroxide
carddav Run hydroxide as a CardDAV server
export-secret-keys <username> Export secret keys
imap Run hydroxide as an IMAP server
import-messages <username> [file] Import messages
export-messages [options...] <username> Export messages
sendmail <username> -- <args...> 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 <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)

View File

@ -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 (

View File

@ -5,7 +5,7 @@ import (
"sync"
"time"
"github.com/emersion/hydroxide/protonmail"
"github.com/0ranki/hydroxide-push/protonmail"
)
const pollInterval = 10 * time.Second

View File

@ -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 {

View File

@ -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")

View File

@ -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 {

View File

@ -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")

View File

@ -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 = "/"

View File

@ -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 {

View File

@ -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
}

View File

@ -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 {

View File

@ -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 <protonmail username>")
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)
}
}

View File

@ -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 {