localserve: change queue to deliver to localserve smtp server

instead of skipping on any smtp and delivering messages to accounts.
we dial the ip of the smtp listener, which is localhost:1025 by default.

the smtp server now uses a mock dns resolver during spf & dkim verification for
hosted domains (localhost by default), so they should pass.

the advantage is that we get regular full smtp server behaviour for delivering
in localserve, including webhooks, and potential first-time sender delays
(though this is disabled by default now).

incoming deliveries now go through normal address resolution, where before we
would always deliver to mox@localhost. we still accept email for unknown
recipients to mox@localhost.

this will be useful upcoming alias/list functionality.

localserve will now generate a dkim key when creating a new config. existing
users may wish to reset (remove) their localserve directory, or add a dkim key.
This commit is contained in:
Mechiel Lukkien
2024-04-24 11:35:07 +02:00
parent 2bb4f78657
commit 1cf7477642
9 changed files with 209 additions and 222 deletions

View File

@ -1284,84 +1284,6 @@ func TestQueueStart(t *testing.T) {
time.Sleep(100 * time.Millisecond) // Racy... give time to finish.
}
// Localserve should cause deliveries to go to sender account, with failure (DSN)
// for recipient addresses that start with "queue" and end with
// "temperror"/"permerror"/"timeout".
func TestLocalserve(t *testing.T) {
Localserve = true
defer func() {
Localserve = false
}()
path := smtp.Path{Localpart: "mjl", IPDomain: dns.IPDomain{Domain: dns.Domain{ASCII: "mox.example"}}}
testDeliver := func(to smtp.Path, expSuccess bool) {
t.Helper()
_, cleanup := setup(t)
defer cleanup()
err := Init()
tcheck(t, err, "queue init")
accret, err := store.OpenAccount(pkglog, "retired")
tcheck(t, err, "open account")
defer func() {
err := accret.Close()
tcheck(t, err, "closing account")
accret.CheckClosed()
}()
mf := prepareFile(t)
defer os.Remove(mf.Name())
defer mf.Close()
// Regular message.
qm := MakeMsg(path, to, false, false, int64(len(testmsg)), "<test@localhost>", nil, nil, time.Now(), "test")
qml := []Msg{qm}
err = Add(ctxbg, pkglog, accret.Name, mf, qml...)
tcheck(t, err, "add message to queue")
qm = qml[0]
deliver(pkglog, nil, qm)
<-deliveryResults
// Message should be delivered to account.
n, err := bstore.QueryDB[store.Message](ctxbg, accret.DB).Count()
tcheck(t, err, "count messages in account")
tcompare(t, n, 1)
n, err = Count(ctxbg)
tcheck(t, err, "count message queue")
tcompare(t, n, 0)
_, err = bstore.QueryDB[MsgRetired](ctxbg, DB).Count()
tcheck(t, err, "get retired message")
hl, err := bstore.QueryDB[Hook](ctxbg, DB).List()
tcheck(t, err, "get webhooks")
if expSuccess {
tcompare(t, len(hl), 2)
tcompare(t, hl[0].IsIncoming, false)
tcompare(t, hl[1].IsIncoming, true)
} else {
tcompare(t, len(hl), 1)
tcompare(t, hl[0].IsIncoming, false)
}
var out webhook.Outgoing
err = json.Unmarshal([]byte(hl[0].Payload), &out)
tcheck(t, err, "unmarshal outgoing webhook payload")
if expSuccess {
tcompare(t, out.Event, webhook.EventDelivered)
} else {
tcompare(t, out.Event, webhook.EventFailed)
}
}
testDeliver(path, true)
badpath := path
badpath.Localpart = smtp.Localpart("queuepermerror")
testDeliver(badpath, false)
}
func TestListFilterSort(t *testing.T) {
_, cleanup := setup(t)
defer cleanup()