Skip to content

Commit

Permalink
Added StartTLS Capability
Browse files Browse the repository at this point in the history
  • Loading branch information
scheibling committed Sep 10, 2023
1 parent 6721cf5 commit 1b4a079
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ jobs:
push: true
tags: |
ghcr.io/scheiblingco/smtp-relay:latest
ghcr.io/scheiblingco/smtp-relay:0.5.0
ghcr.io/scheiblingco/smtp-relay:0.5.1
docker.io/scheibling/smtp-relay:latest
docker.io/scheibling/smtp-relay:0.5.0
docker.io/scheibling/smtp-relay:0.5.1
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ config.json
.env
env
__debug*
tls*.crt
tls*.key
*.py
3 changes: 3 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"server": {
"host": "smtp.example.com",
"listen": ":2525",
"startTls": true,
"tlsCert": "",
"tlsKey": "",
"allowInsecure": true,
"readTimeout": 10,
"writeTimeout": 10,
Expand Down
32 changes: 32 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
Expand All @@ -23,6 +25,9 @@ type Credential struct {
type ServerConfig struct {
Host string `json:"host" default:"localhost"`
Listen string `json:"listen" default:":2525"`
StartTls bool `json:"startTls" default:"false"`
TLSCert string `json:"tlsCert" default:""`
TLSKey string `json:"tlsKey" default:""`
AllowInsecure bool `json:"allowInsecure" default:"true"`
ReadTimeout time.Duration `json:"readTimeout" default:"10"`
WriteTimeout time.Duration `json:"writeTimeout" default:"10"`
Expand Down Expand Up @@ -204,6 +209,32 @@ func main() {

s := smtp.NewServer(be)

var tlsc *tls.Config

if config.Server.StartTls {
log.Println("StartTLS enabled, checking certificates...")

tlsCert, err := tls.LoadX509KeyPair(
config.Server.TLSCert,
config.Server.TLSKey,
)

certInfo, err := x509.ParseCertificate(tlsCert.Certificate[0])

if err != nil {
log.Fatal(err)
}

log.Println("Certificate hostnames: ", certInfo.DNSNames)

tlsc = &tls.Config{
ServerName: config.Server.Host,
NameToCertificate: map[string]*tls.Certificate{
config.Server.Host: &tlsCert,
},
}
}

s.Addr = config.Server.Listen
s.Domain = config.Server.Host
s.ReadTimeout = config.Server.ReadTimeout * time.Second
Expand All @@ -212,6 +243,7 @@ func main() {
s.MaxRecipients = config.Server.MaxRecipients
s.AllowInsecureAuth = config.Server.AllowInsecure
s.AuthDisabled = false
s.TLSConfig = tlsc

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

0 comments on commit 1b4a079

Please sign in to comment.