mirror of
https://github.com/hanzoai/mail.git
synced 2026-09-23 23:53:02 +00:00
Fork of github.com/mjl-/mox at 5656ca6d1b
(upstream v0.0.15 plus 55 commits).
Rename the module path in go.mod and the import paths in 268 Go files. Also
update the five scripts that "go install" the module (genwebsite.sh,
docker-release.sh, apidiff.sh, test-upgrade.sh, Dockerfile.release), which
would otherwise build upstream rather than this fork.
Add NOTICE recording the upstream project, author, license, and fork point,
and stating which license covers which files: MIT for the mox source and our
changes, MPL-2.0 for publicsuffix/public_suffix_list.txt alone, and the
vendored dependencies' own licenses under licenses/. Embed NOTICE so
"mail licenses" prints the lineage from the running binary. LICENSE.MIT,
LICENSE.MPLv2.0 and the licenses/ tree are preserved verbatim; no code is
relicensed.
The mox command name, config keys, data directory layout and website content
are unchanged. LLM.md records the fork point, the rename, what was left alone,
and the steps to stand the server up.
Co-authored-by: Hanzo Dev <[email protected]>
113 lines
3.0 KiB
Go
113 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/hanzoai/mail/dmarcdb"
|
|
"github.com/hanzoai/mail/dns"
|
|
"github.com/hanzoai/mail/http"
|
|
"github.com/hanzoai/mail/imapserver"
|
|
"github.com/hanzoai/mail/mlog"
|
|
"github.com/hanzoai/mail/mox-"
|
|
"github.com/hanzoai/mail/mtastsdb"
|
|
"github.com/hanzoai/mail/queue"
|
|
"github.com/hanzoai/mail/smtpserver"
|
|
"github.com/hanzoai/mail/store"
|
|
"github.com/hanzoai/mail/tlsrptdb"
|
|
"github.com/hanzoai/mail/tlsrptsend"
|
|
)
|
|
|
|
func shutdown(log mlog.Log) {
|
|
// We indicate we are shutting down. Causes new connections and new SMTP commands
|
|
// to be rejected. Should stop active connections pretty quickly.
|
|
mox.ShutdownCancel()
|
|
|
|
// Now we are going to wait for all connections to be gone, up to a timeout.
|
|
done := mox.Connections.Done()
|
|
second := time.Tick(time.Second)
|
|
select {
|
|
case <-done:
|
|
log.Print("connections shutdown, waiting until 1 second passed")
|
|
<-second
|
|
|
|
case <-time.Tick(3 * time.Second):
|
|
// We now cancel all pending operations, and set an immediate deadline on sockets.
|
|
// Should get us a clean shutdown relatively quickly.
|
|
mox.ContextCancel()
|
|
mox.Connections.Shutdown()
|
|
|
|
second := time.Tick(time.Second)
|
|
select {
|
|
case <-done:
|
|
log.Print("no more connections, shutdown is clean, waiting until 1 second passed")
|
|
<-second // Still wait for second, giving processes like imports a chance to clean up.
|
|
case <-second:
|
|
log.Print("shutting down with pending sockets")
|
|
}
|
|
}
|
|
err := os.Remove(mox.DataDirPath("ctl"))
|
|
log.Check(err, "removing ctl unix domain socket during shutdown")
|
|
}
|
|
|
|
// start initializes all packages, starts all listeners and the switchboard
|
|
// goroutine, then returns.
|
|
func start(mtastsdbRefresher, sendDMARCReports, sendTLSReports, skipForkExec bool) error {
|
|
smtpserver.Listen()
|
|
imapserver.Listen()
|
|
http.Listen()
|
|
|
|
if !skipForkExec {
|
|
// If we were just launched as root, fork and exec as unprivileged user, handing
|
|
// over the bound sockets to the new process. We'll get to this same code path
|
|
// again, skipping this if block, continuing below with the actual serving.
|
|
if os.Getuid() == 0 {
|
|
mox.ForkExecUnprivileged()
|
|
panic("cannot happen")
|
|
} else {
|
|
mox.CleanupPassedFiles()
|
|
}
|
|
}
|
|
|
|
if err := mtastsdb.Init(mtastsdbRefresher); err != nil {
|
|
return fmt.Errorf("mtastsdb init: %s", err)
|
|
}
|
|
|
|
if err := tlsrptdb.Init(); err != nil {
|
|
return fmt.Errorf("tlsrptdb init: %s", err)
|
|
}
|
|
|
|
if err := dmarcdb.Init(); err != nil {
|
|
return fmt.Errorf("dmarcdb init: %s", err)
|
|
}
|
|
|
|
if err := store.Init(mox.Context); err != nil {
|
|
return fmt.Errorf("store init: %s", err)
|
|
}
|
|
|
|
done := make(chan struct{}) // Goroutines for messages and webhooks, and cleaners.
|
|
if err := queue.Start(dns.StrictResolver{Pkg: "queue"}, done); err != nil {
|
|
return fmt.Errorf("queue start: %s", err)
|
|
}
|
|
|
|
if sendDMARCReports {
|
|
dmarcdb.Start(dns.StrictResolver{Pkg: "dmarcdb"})
|
|
}
|
|
|
|
if sendTLSReports {
|
|
tlsrptsend.Start(dns.StrictResolver{Pkg: "tlsrptsend"})
|
|
}
|
|
|
|
store.StartAuthCache()
|
|
smtpserver.Serve()
|
|
imapserver.Serve()
|
|
http.Serve()
|
|
|
|
go func() {
|
|
store.Switchboard()
|
|
<-make(chan struct{})
|
|
}()
|
|
return nil
|
|
}
|