2018-11-29 01:00:45 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2020-05-09 03:29:59 +02:00
|
|
|
"net/smtp"
|
2018-11-29 01:00:45 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2020-05-09 03:29:59 +02:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
log "github.com/flashmob/go-guerrilla/log"
|
2018-11-29 01:00:45 +01:00
|
|
|
)
|
|
|
|
|
2020-06-08 06:28:59 +02:00
|
|
|
const (
|
|
|
|
DefaultSTMPPort = 465
|
|
|
|
DefaultMaxEmailSize = (10 << 23) // 83 MB
|
|
|
|
DefaultLocalListenIP = "0.0.0.0"
|
|
|
|
DefaultLocalListenPort = 2525
|
|
|
|
DefaultTimeoutSecs = 300 // 5 minutes
|
|
|
|
)
|
|
|
|
|
2020-05-17 06:27:05 +02:00
|
|
|
// Logger is the global logger
|
2020-05-09 03:29:59 +02:00
|
|
|
var Logger log.Logger
|
2018-11-29 01:00:45 +01:00
|
|
|
|
|
|
|
type mailRelayConfig struct {
|
2020-05-17 06:15:49 +02:00
|
|
|
SMTPServer string `json:"smtp_server"`
|
|
|
|
SMTPPort int `json:"smtp_port"`
|
|
|
|
SMTPStartTLS bool `json:"smtp_starttls"`
|
|
|
|
SMTPLoginAuthType bool `json:"smtp_login_auth_type"`
|
|
|
|
SMTPUsername string `json:"smtp_username"`
|
|
|
|
SMTPPassword string `json:"smtp_password"`
|
|
|
|
SkipCertVerify bool `json:"smtp_skip_cert_verify"`
|
2020-06-08 06:28:59 +02:00
|
|
|
MaxEmailSize int64 `json:"smtp_max_email_size"`
|
2020-05-17 06:15:49 +02:00
|
|
|
LocalListenIP string `json:"local_listen_ip"`
|
|
|
|
LocalListenPort int `json:"local_listen_port"`
|
|
|
|
AllowedHosts []string `json:"allowed_hosts"`
|
2020-06-08 06:28:59 +02:00
|
|
|
TimeoutSecs int `json:"timeout_secs"`
|
2018-11-29 01:00:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-05-09 03:29:59 +02:00
|
|
|
err := run()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2018-11-29 01:00:45 +01:00
|
|
|
|
2020-05-09 03:29:59 +02:00
|
|
|
func run() error {
|
2018-11-29 01:00:45 +01:00
|
|
|
var configFile string
|
2020-05-09 03:29:59 +02:00
|
|
|
var test bool
|
|
|
|
var testsender string
|
|
|
|
var testrcpt string
|
|
|
|
var verbose bool
|
2018-11-29 01:00:45 +01:00
|
|
|
flag.StringVar(&configFile, "config", "/etc/mailrelay.json", "specifies JSON config file")
|
2020-05-09 03:29:59 +02:00
|
|
|
flag.BoolVar(&test, "test", false, "sends a test message to SMTP server")
|
|
|
|
flag.StringVar(&testsender, "sender", "", "used with 'test' to specify sender email address")
|
|
|
|
flag.StringVar(&testrcpt, "rcpt", "", "used with 'test' to specify recipient email address")
|
|
|
|
flag.BoolVar(&verbose, "verbose", false, "verbose output")
|
2018-11-29 01:00:45 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
appConfig, err := loadConfig(configFile)
|
|
|
|
if err != nil {
|
2020-05-09 03:29:59 +02:00
|
|
|
flag.Usage()
|
|
|
|
return fmt.Errorf("loading config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = Start(appConfig, verbose)
|
|
|
|
if err != nil {
|
|
|
|
flag.Usage()
|
|
|
|
return fmt.Errorf("starting server: %w", err)
|
2018-11-29 01:00:45 +01:00
|
|
|
}
|
|
|
|
|
2020-05-09 03:29:59 +02:00
|
|
|
logLevel := "info"
|
|
|
|
if verbose {
|
|
|
|
logLevel = "debug"
|
|
|
|
}
|
|
|
|
Logger, err = log.GetLogger("stdout", logLevel)
|
2018-11-29 01:00:45 +01:00
|
|
|
if err != nil {
|
2020-05-09 03:29:59 +02:00
|
|
|
return fmt.Errorf("creating logger: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if test {
|
|
|
|
err = sendTest(testsender, testrcpt, appConfig.LocalListenPort)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("sending test message: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2018-11-29 01:00:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for SIGINT
|
|
|
|
c := make(chan os.Signal, 1)
|
2020-05-09 03:29:59 +02:00
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
2018-11-29 01:00:45 +01:00
|
|
|
|
|
|
|
// Block until a signal is received.
|
2020-05-09 03:29:59 +02:00
|
|
|
<-c
|
|
|
|
return nil
|
2018-11-29 01:00:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfig(path string) (*mailRelayConfig, error) {
|
|
|
|
var cfg mailRelayConfig
|
2020-06-08 06:28:59 +02:00
|
|
|
configDefaults(&cfg)
|
|
|
|
|
2018-11-29 01:00:45 +01:00
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
parser := json.NewDecoder(file)
|
|
|
|
if err := parser.Decode(&cfg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &cfg, nil
|
|
|
|
}
|
2020-05-09 03:29:59 +02:00
|
|
|
|
2020-06-08 06:28:59 +02:00
|
|
|
func configDefaults(config *mailRelayConfig) {
|
|
|
|
config.SMTPPort = DefaultSTMPPort
|
|
|
|
config.SMTPStartTLS = false
|
|
|
|
config.SMTPLoginAuthType = false
|
|
|
|
config.MaxEmailSize = DefaultMaxEmailSize
|
|
|
|
config.SkipCertVerify = false
|
|
|
|
config.LocalListenIP = DefaultLocalListenIP
|
|
|
|
config.LocalListenPort = DefaultLocalListenPort
|
|
|
|
config.AllowedHosts = []string{"*"}
|
|
|
|
config.TimeoutSecs = DefaultTimeoutSecs
|
|
|
|
}
|
|
|
|
|
2020-05-09 03:29:59 +02:00
|
|
|
// sendTest sends a test message to the SMTP server specified in mailrelay.json
|
|
|
|
func sendTest(sender string, rcpt string, port int) error {
|
|
|
|
conn, err := smtp.Dial(fmt.Sprintf("localhost:%d", port))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := conn.Mail(sender); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := conn.Rcpt(rcpt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-17 06:15:49 +02:00
|
|
|
if err := writeBody(conn, sender); err != nil {
|
2020-05-09 03:29:59 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-05-17 06:15:49 +02:00
|
|
|
return conn.Quit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeBody(conn *smtp.Client, sender string) error {
|
|
|
|
wc, err := conn.Data()
|
|
|
|
if err != nil {
|
2020-05-09 03:29:59 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-05-17 06:15:49 +02:00
|
|
|
defer wc.Close()
|
|
|
|
_, err = fmt.Fprintf(wc, "From: %s\nSubject: Test message\n\nThis is a test email from mailrelay.\n", sender)
|
|
|
|
return err
|
2020-05-09 03:29:59 +02:00
|
|
|
}
|