3 Commits

16 changed files with 91 additions and 238 deletions

View File

@ -1,5 +1,3 @@
DOCKER_DATE_TAG := $(shell date +%Y%m%d%H%M)
build:
CGO_ENABLED=0 go build -v -o bin/fake-smtp ./cmd/fake-smtp
@ -23,10 +21,8 @@ docker-run:
docker-release:
docker tag fake-smtp:latest bornholm/fake-smtp:latest
docker tag fake-smtp:latest bornholm/fake-smtp:$(DOCKER_DATE_TAG)
docker login
docker push bornholm/fake-smtp:latest
docker push bornholm/fake-smtp:$(DOCKER_DATE_TAG)
test:
go test -v -race ./...

View File

@ -55,18 +55,6 @@ smtp:
# Configuration du stockage
data:
path: fakesmtp.db
# Configuration du relais SMTP
relay:
enabled: false # Activer/désactiver le relais SMTP
address: "" # Adresse du serveur au format "host:port"
identity: "" # Identité du compte utilisateur, peut être laissé vide
username: "" # Identifiant du compte SMTP, non utilisé sur anonymous = true
password: "" # Mot de passe du compte SMTP, non utilisé sur anonymous = true
anonymous: false # Utiliser le mode d'authentification "anonyme"
useTLS: false # Utiliser TLS pour se connecter au serveur SMTP
insecureSkipVerify: true # Ne pas vérifier le certificat du serveur pour les connexions TLS/STARTTLS
fromOverride: "" # Surcharger l'adresse émetteur des courriels transmis
```
### Variables d'environnement
@ -91,14 +79,6 @@ Les valeurs des variables d'environnement surchargent les valeurs présentes dan
|`FAKESMTP_SMTP_ALLOWINSECUREAUTH`|`smtp.allowInsecureAuth`|
|`FAKESMTP_SMTP_DEBUG`|`smtp.debug`|
|`FAKESMTP_DATA_PATH`|`data.path`|
|`FAKESMTP_RELAY_ENABLED`|`relay.enabled`|
|`FAKESMTP_RELAY_ADDRESS`|`relay.address`|
|`FAKESMTP_RELAY_IDENTITY`|`relay.identity`|
|`FAKESMTP_RELAY_USERNAME`|`relay.username`|
|`FAKESMTP_RELAY_PASSWORD`|`relay.password`|
|`FAKESMTP_RELAY_ANONYMOUS`|`relay.anonymous`|
|`FAKESMTP_RELAY_INSECURE_SKIP_VERIFY`|`relay.insecureSkipVerify`|
|`FAKESMTP_RELAY_FROM_OVERRIDE`|`relay.fromOverride`|
## Démarrer avec les sources

View File

@ -53,11 +53,6 @@ func getServiceContainer(conf *config.Config) (*service.Container, error) {
cqrs.CommandHandlerFunc(command.HandleDeleteEmail),
)
bus.RegisterCommand(
cqrs.MatchCommandRequest(&command.RelayEmailRequest{}),
cqrs.CommandHandlerFunc(command.HandleRelayEmail),
)
bus.RegisterQuery(
cqrs.MatchQueryRequest(&query.GetInboxRequest{}),
cqrs.QueryHandlerFunc(query.HandleGetInbox),

View File

@ -12,7 +12,6 @@ import (
"github.com/jhillyerd/enmime"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/cqrs"
"gitlab.com/wpetit/goweb/logger"
"gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/service"
)
@ -75,31 +74,12 @@ func (s *Session) Data(r io.Reader) error {
return errors.Wrap(err, "could not retrieve cqrs service")
}
conf, err := config.From(s.ctn)
if err != nil {
return errors.Wrap(err, "could not retrieve config service")
}
if conf.Relay.Enabled {
cmd := &command.RelayEmailRequest{
Envelope: env,
}
if _, err := bus.Exec(s.ctx, cmd); err != nil {
logger.Error(s.ctx, "could not exec command", logger.E(err))
return errors.Wrapf(err, "could not exec '%T' command", cmd)
}
}
cmd := &command.StoreEmailRequest{
Envelope: env,
}
if _, err := bus.Exec(s.ctx, cmd); err != nil {
logger.Error(s.ctx, "could not exec command", logger.E(err))
return errors.Wrapf(err, "could not exec '%T' command", cmd)
return errors.Wrap(err, "could not exec command")
}
return nil

1
debian/compat vendored Normal file
View File

@ -0,0 +1 @@
9

14
debian/control vendored Normal file
View File

@ -0,0 +1,14 @@
Source: fake-smtp
Section: unknown
Priority: optional
Maintainer: William Petit <wpetit@cadoles.com>
Build-Depends: debhelper (>= 8.0.0), wget, ca-certificates, tar, curl
Standards-Version: 3.9.4
Homepage: http://forge.cadoles.com/wpetit/fake-smtp
Vcs-Git: http://forge.cadoles.com/wpetit/fake-smtp.git
Vcs-Browser: http://forge.cadoles.com/wpetit/fake-smtp
Package: fake-smtp
Architecture: amd64
Depends: ${shlibs:Depends}, ${misc:Depends}
Description: Serveur SMTP factice pour le développement avec interface web

1
debian/fake-smtp.dirs vendored Normal file
View File

@ -0,0 +1 @@
var/lib/fake-smtp

11
debian/fake-smtp.service vendored Normal file
View File

@ -0,0 +1,11 @@
[Unit]
Description=Serveur SMTP factice pour le développement avec interface web
After=network-online.target
[Service]
Type=simple
ExecStart=/usr/bin/fake-smtp -workdir /usr/share/fake-smtp -config /etc/fake-smtp/config.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target

54
debian/rules vendored Normal file
View File

@ -0,0 +1,54 @@
#!/usr/bin/make -f
# -*- makefile -*-
# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1
GO_VERSION := 1.14.2
OS := linux
ARCH := amd64
GOPATH=$(HOME)/go
ifeq (, $(shell which go 2>/dev/null))
override_dh_auto_build: install-go
endif
ifeq (, $(shell which node 2>/dev/null))
override_dh_auto_build: install-nodejs
endif
%:
dh $@ --with systemd
override_dh_auto_build: $(GOPATH)
GOPATH=$(GOPATH) PATH="$(PATH):/usr/local/go/bin:$(GOPATH)/bin" make tooling
npm install
GOPATH=$(GOPATH) PATH="$(PATH):/usr/local/go/bin:$(GOPATH)/bin" go mod vendor
GOPATH=$(GOPATH) PATH="$(PATH):/usr/local/go/bin:$(GOPATH)/bin" ARCH_TARGETS=$(ARCH) make release
$(GOPATH):
mkdir -p $(GOPATH)
install-go:
wget https://dl.google.com/go/go$(GO_VERSION).$(OS)-$(ARCH).tar.gz
tar -C /usr/local -xzf go$(GO_VERSION).$(OS)-$(ARCH).tar.gz
install-nodejs:
curl -sL https://deb.nodesource.com/setup_13.x | bash -
apt-get install -y nodejs
override_dh_auto_install:
mkdir -p debian/fake-smtp/usr/share/fake-smtp
mkdir -p debian/fake-smtp/etc/fake-smtp
mkdir -p debian/fake-smtp/usr/bin
cp -r release/fake-smtp-$(OS)-$(ARCH)/* debian/fake-smtp/usr/share/fake-smtp/
mv debian/fake-smtp/usr/share/fake-smtp/bin/fake-smtp debian/fake-smtp/usr/bin/fake-smtp
mv debian/fake-smtp/usr/share/fake-smtp/config.yml debian/fake-smtp/etc/fake-smtp/config.yml
install -d debian/fake-smtp
override_dh_strip:
override_dh_auto_test:

1
debian/source/format vendored Normal file
View File

@ -0,0 +1 @@
3.0 (native)

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.14
require (
github.com/asdine/storm/v3 v3.1.1
github.com/caarlos0/env/v6 v6.2.1
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21
github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b // indirect
github.com/emersion/go-smtp v0.12.1
github.com/go-chi/chi v4.1.1+incompatible
github.com/jhillyerd/enmime v0.8.0

4
go.sum
View File

@ -53,8 +53,8 @@ github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk
github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/emersion/go-sasl v0.0.0-20190817083125-240c8404624e h1:ba7YsgX5OV8FjGi5ZWml8Jng6oBrJAb3ahqWMJ5Ce8Q=
github.com/emersion/go-sasl v0.0.0-20190817083125-240c8404624e/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b h1:uhWtEWBHgop1rqEk2klKaxPAkVDCXexai6hSuRQ7Nvs=
github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k=
github.com/emersion/go-smtp v0.12.1 h1:1R8BDqrR2HhlGwgFYcOi+BVTvK1bMjAB65QcVpJ5sNA=
github.com/emersion/go-smtp v0.12.1/go.mod h1:SD9V/xa4ndMw77lR3Mf7htkp8RBNYuPh9UeuBs9tpUQ=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=

View File

@ -1,164 +0,0 @@
package command
import (
"context"
"crypto/tls"
"io"
"sync"
"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
"forge.cadoles.com/wpetit/fake-smtp/internal/config"
"github.com/jhillyerd/enmime"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/cqrs"
"gitlab.com/wpetit/goweb/middleware/container"
)
type RelayEmailRequest struct {
Envelope *enmime.Envelope
}
func HandleRelayEmail(ctx context.Context, cmd cqrs.Command) error {
req, ok := cmd.Request().(*RelayEmailRequest)
if !ok {
return cqrs.ErrUnexpectedRequest
}
ctn, err := container.From(ctx)
if err != nil {
return errors.Wrap(err, "could not retrieve service container")
}
conf, err := config.From(ctn)
if err != nil {
return errors.Wrap(err, "could not retrieve config service")
}
relay := conf.Relay
if err := forwardMail(req.Envelope, relay); err != nil {
return errors.Wrap(err, "could not forward mail")
}
return nil
}
func forwardMail(env *enmime.Envelope, conf config.RelayConfig) error {
var tlsConfig *tls.Config
if conf.InsecureSkipVerify {
tlsConfig = &tls.Config{
// nolint: gosec
InsecureSkipVerify: true,
}
}
addr := conf.Address
var (
client *smtp.Client
err error
)
if conf.UseTLS {
client, err = smtp.DialTLS(addr, tlsConfig)
if err != nil {
return errors.WithStack(err)
}
} else {
client, err = smtp.Dial(addr)
if err != nil {
return errors.WithStack(err)
}
}
defer client.Close()
if err = client.Hello("localhost"); err != nil {
return errors.WithStack(err)
}
if ok, _ := client.Extension("STARTTLS"); ok {
if err = client.StartTLS(tlsConfig); err != nil {
return errors.WithStack(err)
}
}
if conf.Username != "" || conf.Password != "" {
if ok, _ := client.Extension("AUTH"); ok {
var auth sasl.Client
if conf.Anonymous {
auth = sasl.NewAnonymousClient("fakesmtp")
} else {
auth = sasl.NewPlainClient(conf.Identity, conf.Username, conf.Password)
}
if err := client.Auth(auth); err != nil {
return errors.WithStack(err)
}
}
}
var from string
if conf.FromOverride != "" {
from = conf.FromOverride
} else {
from = env.GetHeader("From")
}
if err = client.Mail(from, nil); err != nil {
return errors.WithStack(err)
}
to := env.GetHeaderValues("To")
for _, addr := range to {
if err = client.Rcpt(addr); err != nil {
return errors.WithStack(err)
}
}
w, err := client.Data()
if err != nil {
return errors.WithStack(err)
}
pr, pw := io.Pipe()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
defer pw.Close()
if err = env.Root.Encode(pw); err != nil {
err = errors.WithStack(err)
}
}()
_, err = io.Copy(w, pr)
if err != nil {
return err
}
wg.Wait()
if err != nil {
return errors.WithStack(err)
}
if err = w.Close(); err != nil {
return err
}
if err := client.Quit(); err != nil {
return errors.WithStack(err)
}
return nil
}

View File

@ -14,8 +14,7 @@ import (
type Config struct {
HTTP HTTPConfig `yaml:"http"`
SMTP SMTPConfig `yaml:"smtp"`
Data DataConfig `yaml:"data"`
Relay RelayConfig `yaml:"relay"`
Data DataConfig `ymal:"data"`
}
type HTTPConfig struct {
@ -37,18 +36,6 @@ type SMTPConfig struct {
Debug bool `yaml:"debug" env:"FAKESMTP_SMTP_DEBUG"`
}
type RelayConfig struct {
Enabled bool `yaml:"enabled" env:"FAKESMTP_RELAY_ENABLED"`
Address string `yaml:"address" env:"FAKESMTP_RELAY_ADDRESS"`
Identity string `yaml:"identity" env:"FAKESMTP_RELAY_IDENTITY"`
Username string `yaml:"username" env:"FAKESMTP_RELAY_USERNAME"`
Password string `yaml:"password" env:"FAKESMTP_RELAY_PASSWORD"`
Anonymous bool `yaml:"anonymous" env:"FAKESMTP_RELAY_ANONYMOUS"`
UseTLS bool `yaml:"useTLS" env:"FAKESMTP_RELAY_USE_TLS"`
InsecureSkipVerify bool `yaml:"insecureSkipVerify" env:"FAKESMTP_RELAY_INSECURE_SKIP_VERIFY"`
FromOverride string `yaml:"fromOverride" env:"FAKESMTP_RELAY_FROM_OVERRIDE"`
}
type DataConfig struct {
Path string `yaml:"path" env:"FAKESMTP_DATA_PATH"`
}
@ -104,9 +91,6 @@ func NewDefault() *Config {
Data: DataConfig{
Path: "fakesmtp.db",
},
Relay: RelayConfig{
Enabled: false,
},
}
}

View File

@ -1,4 +1,4 @@
FROM golang:1.15 AS build
FROM golang:1.13 AS build
ARG HTTP_PROXY=
ARG HTTPS_PROXY=

View File

@ -6,7 +6,7 @@ for i in {1..10}; do
-s "Test ${i}" \
-a README.md \
-a package.json \
foo_${i}@bar${i}.com \
foo_${i}@bar_${i}.com \
<<EOF
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus eget metus ornare, placerat mi nec, ornare magna. Vivamus volutpat nisi et aliquet mollis. Phasellus eu malesuada erat, vel molestie ipsum. Etiam tincidunt ligula eget scelerisque blandit. Vivamus nec quam vitae felis rutrum cursus a eget elit. Integer vestibulum ultrices iaculis. Integer varius sapien ac ante accumsan euismod. Quisque fermentum dui nec porttitor pellentesque. Vivamus lobortis mauris eget metus sagittis tempor.