Skip to content

Commit 1b4a079

Browse files
committed
Added StartTLS Capability
1 parent 6721cf5 commit 1b4a079

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

.github/workflows/build.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ jobs:
4343
push: true
4444
tags: |
4545
ghcr.io/scheiblingco/smtp-relay:latest
46-
ghcr.io/scheiblingco/smtp-relay:0.5.0
46+
ghcr.io/scheiblingco/smtp-relay:0.5.1
4747
docker.io/scheibling/smtp-relay:latest
48-
docker.io/scheibling/smtp-relay:0.5.0
48+
docker.io/scheibling/smtp-relay:0.5.1
4949
5050

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ config.json
22
.env
33
env
44
__debug*
5+
tls*.crt
6+
tls*.key
7+
*.py

config.example.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"server": {
33
"host": "smtp.example.com",
44
"listen": ":2525",
5+
"startTls": true,
6+
"tlsCert": "",
7+
"tlsKey": "",
58
"allowInsecure": true,
69
"readTimeout": 10,
710
"writeTimeout": 10,

main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package main
22

33
import (
44
"bytes"
5+
"crypto/tls"
6+
"crypto/x509"
57
"encoding/json"
68
"errors"
79
"fmt"
@@ -23,6 +25,9 @@ type Credential struct {
2325
type ServerConfig struct {
2426
Host string `json:"host" default:"localhost"`
2527
Listen string `json:"listen" default:":2525"`
28+
StartTls bool `json:"startTls" default:"false"`
29+
TLSCert string `json:"tlsCert" default:""`
30+
TLSKey string `json:"tlsKey" default:""`
2631
AllowInsecure bool `json:"allowInsecure" default:"true"`
2732
ReadTimeout time.Duration `json:"readTimeout" default:"10"`
2833
WriteTimeout time.Duration `json:"writeTimeout" default:"10"`
@@ -204,6 +209,32 @@ func main() {
204209

205210
s := smtp.NewServer(be)
206211

212+
var tlsc *tls.Config
213+
214+
if config.Server.StartTls {
215+
log.Println("StartTLS enabled, checking certificates...")
216+
217+
tlsCert, err := tls.LoadX509KeyPair(
218+
config.Server.TLSCert,
219+
config.Server.TLSKey,
220+
)
221+
222+
certInfo, err := x509.ParseCertificate(tlsCert.Certificate[0])
223+
224+
if err != nil {
225+
log.Fatal(err)
226+
}
227+
228+
log.Println("Certificate hostnames: ", certInfo.DNSNames)
229+
230+
tlsc = &tls.Config{
231+
ServerName: config.Server.Host,
232+
NameToCertificate: map[string]*tls.Certificate{
233+
config.Server.Host: &tlsCert,
234+
},
235+
}
236+
}
237+
207238
s.Addr = config.Server.Listen
208239
s.Domain = config.Server.Host
209240
s.ReadTimeout = config.Server.ReadTimeout * time.Second
@@ -212,6 +243,7 @@ func main() {
212243
s.MaxRecipients = config.Server.MaxRecipients
213244
s.AllowInsecureAuth = config.Server.AllowInsecure
214245
s.AuthDisabled = false
246+
s.TLSConfig = tlsc
215247

216248
log.Println("Starting server at", s.Addr)
217249
if err := s.ListenAndServe(); err != nil {

0 commit comments

Comments
 (0)