mirror of
https://github.com/decke/smtprelay.git
synced 2025-12-25 07:43:06 -07:00
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
813bd9ebe7 | ||
|
|
ee8a5dd989 | ||
|
|
6602836166 | ||
|
|
cdc0fe931b | ||
|
|
e5f27e02e5 | ||
|
|
dffe0bb5bb | ||
|
|
20ad1e84f9 | ||
|
|
db1238bf2d | ||
|
|
d9167eece5 | ||
|
|
e98a1c6f25 | ||
|
|
f0aefc194d | ||
|
|
bfd156e5f3 | ||
|
|
682feef610 | ||
|
|
eea3c4edaa | ||
|
|
cf25c2d0a0 | ||
|
|
774651b0f6 | ||
|
|
5cd3729b1f | ||
|
|
5b38a30349 | ||
|
|
d7585bec9b | ||
|
|
c8f62e42c8 | ||
|
|
00df491340 | ||
|
|
1bf205e7d8 | ||
|
|
c83544bd90 | ||
|
|
e3ba45ede2 | ||
|
|
f69d1f0114 | ||
|
|
e9a2b9ad5a | ||
|
|
7e3dbd515d | ||
|
|
8cc31a918b | ||
|
|
26477177fe | ||
|
|
44560c9a0c |
3
.github/workflows/go.yml
vendored
3
.github/workflows/go.yml
vendored
@@ -22,3 +22,6 @@ jobs:
|
||||
|
||||
- name: Build
|
||||
run: go build -v .
|
||||
|
||||
- name: Test
|
||||
run: go test -v .
|
||||
|
||||
2
.github/workflows/release.yaml
vendored
2
.github/workflows/release.yaml
vendored
@@ -20,7 +20,7 @@ jobs:
|
||||
- name: Set BUILD_TIME env
|
||||
run: echo BUILD_TIME=$(date) >> ${GITHUB_ENV}
|
||||
|
||||
- uses: wangyoucao577/go-release-action@v1.28
|
||||
- uses: wangyoucao577/go-release-action@v1.30
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
goos: ${{ matrix.goos }}
|
||||
|
||||
@@ -22,6 +22,7 @@ device which produces mail.
|
||||
|
||||
## Main features
|
||||
|
||||
* Simple configuration with ini file .env file or environment variables
|
||||
* Supports SMTPS/TLS (465), STARTTLS (587) and unencrypted SMTP (25)
|
||||
* Checks for sender, receiver, client IP
|
||||
* Authentication support with file (LOGIN, PLAIN)
|
||||
|
||||
123
config.go
123
config.go
@@ -1,15 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/peterbourgon/ff/v3"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/vharitonsky/iniflags"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -18,36 +21,44 @@ var (
|
||||
)
|
||||
|
||||
var (
|
||||
logFile = flag.String("logfile", "", "Path to logfile")
|
||||
logFormat = flag.String("log_format", "default", "Log output format")
|
||||
logLevel = flag.String("log_level", "info", "Minimum log level to output")
|
||||
hostName = flag.String("hostname", "localhost.localdomain", "Server hostname")
|
||||
welcomeMsg = flag.String("welcome_msg", "", "Welcome message for SMTP session")
|
||||
listenStr = flag.String("listen", "127.0.0.1:25 [::1]:25", "Address and port to listen for incoming SMTP")
|
||||
flagset = flag.NewFlagSet("smtprelay", flag.ContinueOnError)
|
||||
|
||||
// config flags
|
||||
logFile = flagset.String("logfile", "", "Path to logfile")
|
||||
logFormat = flagset.String("log_format", "default", "Log output format")
|
||||
logLevel = flagset.String("log_level", "info", "Minimum log level to output")
|
||||
hostName = flagset.String("hostname", "localhost.localdomain", "Server hostname")
|
||||
welcomeMsg = flagset.String("welcome_msg", "", "Welcome message for SMTP session")
|
||||
listenStr = flagset.String("listen", "127.0.0.1:25 [::1]:25", "Address and port to listen for incoming SMTP")
|
||||
localCert = flagset.String("local_cert", "", "SSL certificate for STARTTLS/TLS")
|
||||
localKey = flagset.String("local_key", "", "SSL private key for STARTTLS/TLS")
|
||||
localForceTLS = flagset.Bool("local_forcetls", false, "Force STARTTLS (needs local_cert and local_key)")
|
||||
readTimeoutStr = flagset.String("read_timeout", "60s", "Socket timeout for read operations")
|
||||
writeTimeoutStr = flagset.String("write_timeout", "60s", "Socket timeout for write operations")
|
||||
dataTimeoutStr = flagset.String("data_timeout", "5m", "Socket timeout for DATA command")
|
||||
maxConnections = flagset.Int("max_connections", 100, "Max concurrent connections, use -1 to disable")
|
||||
maxMessageSize = flagset.Int("max_message_size", 10240000, "Max message size in bytes")
|
||||
maxRecipients = flagset.Int("max_recipients", 100, "Max RCPT TO calls for each envelope")
|
||||
allowedNetsStr = flagset.String("allowed_nets", "127.0.0.0/8 ::1/128", "Networks allowed to send mails")
|
||||
allowedSenderStr = flagset.String("allowed_sender", "", "Regular expression for valid FROM EMail addresses")
|
||||
allowedRecipStr = flagset.String("allowed_recipients", "", "Regular expression for valid TO EMail addresses")
|
||||
allowedUsers = flagset.String("allowed_users", "", "Path to file with valid users/passwords")
|
||||
command = flagset.String("command", "", "Path to pipe command")
|
||||
remotesStr = flagset.String("remotes", "", "Outgoing SMTP servers")
|
||||
|
||||
// additional flags
|
||||
_ = flagset.String("config", "", "Path to config file (ini format)")
|
||||
versionInfo = flagset.Bool("version", false, "Show version information")
|
||||
|
||||
// internal
|
||||
listenAddrs = []protoAddr{}
|
||||
localCert = flag.String("local_cert", "", "SSL certificate for STARTTLS/TLS")
|
||||
localKey = flag.String("local_key", "", "SSL private key for STARTTLS/TLS")
|
||||
localForceTLS = flag.Bool("local_forcetls", false, "Force STARTTLS (needs local_cert and local_key)")
|
||||
readTimeoutStr = flag.String("read_timeout", "60s", "Socket timeout for read operations")
|
||||
readTimeout time.Duration
|
||||
writeTimeoutStr = flag.String("write_timeout", "60s", "Socket timeout for write operations")
|
||||
writeTimeout time.Duration
|
||||
dataTimeoutStr = flag.String("data_timeout", "5m", "Socket timeout for DATA command")
|
||||
dataTimeout time.Duration
|
||||
maxConnections = flag.Int("max_connections", 100, "Max concurrent connections, use -1 to disable")
|
||||
maxMessageSize = flag.Int("max_message_size", 10240000, "Max message size in bytes")
|
||||
maxRecipients = flag.Int("max_recipients", 100, "Max RCPT TO calls for each envelope")
|
||||
allowedNetsStr = flag.String("allowed_nets", "127.0.0.0/8 ::1/128", "Networks allowed to send mails")
|
||||
allowedNets = []*net.IPNet{}
|
||||
allowedSenderStr = flag.String("allowed_sender", "", "Regular expression for valid FROM EMail addresses")
|
||||
allowedSender *regexp.Regexp
|
||||
allowedRecipStr = flag.String("allowed_recipients", "", "Regular expression for valid TO EMail addresses")
|
||||
allowedRecipients *regexp.Regexp
|
||||
allowedUsers = flag.String("allowed_users", "", "Path to file with valid users/passwords")
|
||||
command = flag.String("command", "", "Path to pipe command")
|
||||
remotesStr = flag.String("remotes", "", "Outgoing SMTP servers")
|
||||
remotes = []*Remote{}
|
||||
versionInfo = flag.Bool("version", false, "Show version information")
|
||||
)
|
||||
|
||||
func localAuthRequired() bool {
|
||||
@@ -183,11 +194,36 @@ func setupTimeouts() {
|
||||
}
|
||||
|
||||
func ConfigLoad() {
|
||||
iniflags.Parse()
|
||||
// use .env file if it exists
|
||||
if _, err := os.Stat(".env"); err == nil {
|
||||
if err := ff.Parse(flagset, os.Args[1:],
|
||||
ff.WithEnvVarPrefix("smtprelay"),
|
||||
ff.WithConfigFile(".env"),
|
||||
ff.WithConfigFileParser(ff.EnvParser),
|
||||
); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
// use env variables and smtprelay.ini file
|
||||
if err := ff.Parse(flagset, os.Args[1:],
|
||||
ff.WithEnvVarPrefix("smtprelay"),
|
||||
ff.WithConfigFileFlag("config"),
|
||||
ff.WithConfigFileParser(IniParser),
|
||||
); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Set up logging as soon as possible
|
||||
setupLogger()
|
||||
|
||||
if *versionInfo {
|
||||
fmt.Printf("smtprelay/%s (%s)\n", appVersion, buildTime)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if *remotesStr == "" && *command == "" {
|
||||
log.Warn("no remotes or command set; mail will not be forwarded!")
|
||||
}
|
||||
@@ -198,3 +234,42 @@ func ConfigLoad() {
|
||||
setupListeners()
|
||||
setupTimeouts()
|
||||
}
|
||||
|
||||
// IniParser is a parser for config files in classic key/value style format. Each
|
||||
// line is tokenized as a single key/value pair. The first "=" delimited
|
||||
// token in the line is interpreted as the flag name, and all remaining tokens
|
||||
// are interpreted as the value. Any leading hyphens on the flag name are
|
||||
// ignored.
|
||||
func IniParser(r io.Reader, set func(name, value string) error) error {
|
||||
s := bufio.NewScanner(r)
|
||||
for s.Scan() {
|
||||
line := strings.TrimSpace(s.Text())
|
||||
if line == "" {
|
||||
continue // skip empties
|
||||
}
|
||||
|
||||
if line[0] == '#' || line[0] == ';' {
|
||||
continue // skip comments
|
||||
}
|
||||
|
||||
var (
|
||||
name string
|
||||
value string
|
||||
index = strings.IndexRune(line, '=')
|
||||
)
|
||||
if index < 0 {
|
||||
name, value = line, "true" // boolean option
|
||||
} else {
|
||||
name, value = strings.TrimSpace(line[:index]), strings.Trim(strings.TrimSpace(line[index+1:]), "\"")
|
||||
}
|
||||
|
||||
if i := strings.Index(value, " #"); i >= 0 {
|
||||
value = strings.TrimSpace(value[:i])
|
||||
}
|
||||
|
||||
if err := set(name, value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
10
go.mod
10
go.mod
@@ -3,17 +3,17 @@ module github.com/decke/smtprelay
|
||||
require (
|
||||
github.com/chrj/smtpd v0.3.1
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
github.com/stretchr/testify v1.7.1
|
||||
github.com/vharitonsky/iniflags v0.0.0-20180513140207-a33cd0b5f3de
|
||||
github.com/peterbourgon/ff/v3 v3.3.0
|
||||
github.com/sirupsen/logrus v1.9.0
|
||||
github.com/stretchr/testify v1.8.0
|
||||
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
go 1.18
|
||||
|
||||
22
go.sum
22
go.sum
@@ -5,22 +5,24 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/peterbourgon/ff/v3 v3.3.0 h1:PaKe7GW8orVFh8Unb5jNHS+JZBwWUMa2se0HM6/BI24=
|
||||
github.com/peterbourgon/ff/v3 v3.3.0/go.mod h1:zjJVUhx+twciwfDl0zBcFzl4dW8axCRyXE/eKY9RztQ=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
|
||||
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/vharitonsky/iniflags v0.0.0-20180513140207-a33cd0b5f3de h1:fkw+7JkxF3U1GzQoX9h69Wvtvxajo5Rbzy6+YMMzPIg=
|
||||
github.com/vharitonsky/iniflags v0.0.0-20180513140207-a33cd0b5f3de/go.mod h1:irMhzlTz8+fVFj6CH2AN2i+WI5S6wWFtK3MBCIxIpyI=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 h1:kUhD7nTDoI3fVd9G4ORWrbV5NY0liEs/Jg2pv5f+bBA=
|
||||
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc=
|
||||
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
6
main.go
6
main.go
@@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/textproto"
|
||||
"os"
|
||||
@@ -282,11 +281,6 @@ func getTLSConfig() *tls.Config {
|
||||
func main() {
|
||||
ConfigLoad()
|
||||
|
||||
if *versionInfo {
|
||||
fmt.Printf("smtprelay/%s (%s)\n", appVersion, buildTime)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
log.WithField("version", appVersion).
|
||||
Debug("starting smtprelay")
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@ type Remote struct {
|
||||
// Supported Params:
|
||||
// - skipVerify: can be "true" or empty to prevent ssl verification of remote server's certificate.
|
||||
// - auth: can be "login" to trigger "LOGIN" auth instead of "PLAIN" auth
|
||||
//
|
||||
func ParseRemote(remoteURL string) (*Remote, error) {
|
||||
u, err := url.Parse(remoteURL)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,19 +1,23 @@
|
||||
; smtprelay configuration
|
||||
;
|
||||
; All config parameters can also be provided as environment
|
||||
; variables in uppercase and the prefix "SMTPRELAY_".
|
||||
; (eg. SMTPRELAY_LOGFILE, SMTPRELAY_LOG_FORMAT)
|
||||
|
||||
; Logfile (blank/default is stderr)
|
||||
;logfile =
|
||||
|
||||
; Log format: default, plain (no timestamp), json
|
||||
;log_format = "default"
|
||||
;log_format = default
|
||||
|
||||
; Log level: panic, fatal, error, warn, info, debug, trace
|
||||
;log_level = "info"
|
||||
;log_level = info
|
||||
|
||||
; Hostname for this SMTP server
|
||||
;hostname = "localhost.localdomain"
|
||||
;hostname = localhost.localdomain
|
||||
|
||||
; Welcome message for clients
|
||||
;welcome_msg = "<hostname> ESMTP ready."
|
||||
;welcome_msg = <hostname> ESMTP ready.
|
||||
|
||||
; Listen on the following addresses for incoming
|
||||
; unencrypted connections.
|
||||
|
||||
Reference in New Issue
Block a user