-
Notifications
You must be signed in to change notification settings - Fork 0
/
imap.go
56 lines (48 loc) · 1.32 KB
/
imap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package LocalMail
import (
"crypto/tls"
"github.com/emersion/go-imap/v2"
"github.com/emersion/go-imap/v2/imapserver"
"github.com/emersion/go-imap/v2/imapserver/imapmemserver"
"log"
"net"
"os"
)
func loadTLSConfig(config *tls.Config, tlsCert string, tlsKey string) {
if tlsCert != "" || tlsKey != "" {
cert, err := tls.LoadX509KeyPair(tlsCert, tlsKey)
if err != nil {
log.Fatalf("Failed to load TLS key pair: %v", err)
}
config = &tls.Config{
Certificates: []tls.Certificate{cert},
}
}
}
func CreateServer(insecureAuth bool) (*imapmemserver.Server, *imapserver.Server) {
var tlsConfig *tls.Config
loadTLSConfig(tlsConfig, os.Getenv("CERTPATH"), os.Getenv("KEYPATH"))
memServer := imapmemserver.New()
server := imapserver.New(&imapserver.Options{
NewSession: func(conn *imapserver.Conn) (imapserver.Session, *imapserver.GreetingData, error) {
return memServer.NewSession(), nil, nil
},
Caps: imap.CapSet{
imap.CapIMAP4rev1: {},
imap.CapIMAP4rev2: {},
},
TLSConfig: tlsConfig,
InsecureAuth: insecureAuth,
DebugWriter: os.Stdout,
})
return memServer, server
}
func RunServer(server *imapserver.Server) {
ln, err := net.Listen("tcp", "0.0.0.0:143")
if err != nil {
log.Fatalf("Failed to listen: %v", err)
}
if err := server.Serve(ln); err != nil {
log.Fatalf("Serve() = %v", err)
}
}