Slight reworking of config file. Connect to MTAs via port 25 and use certificate for StartTLS.

This commit is contained in:
Gnarwhal 2024-10-04 20:41:21 +00:00
parent e8979dc08e
commit 1750872280
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
4 changed files with 67 additions and 77 deletions

View file

@ -24,27 +24,16 @@ import (
)
type Config struct {
General GeneralConfig
Plain PlainConfig
TLS TLSConfig
LogLevel string
Host string
PasswordHash string
Plain string
TLS string
CertPath string
PrivateKeyPath string
}
type GeneralConfig struct {
LogLevel string
Host string
PasswordHash string
}
type PlainConfig struct {
Enabled bool
Port string
}
type TLSConfig struct {
Enabled bool
Port string
CertPath string
PrivateKeyPath string
}
func LoadConfig(path string) (*Config, error) {
@ -53,10 +42,8 @@ func LoadConfig(path string) (*Config, error) {
return nil, err
}
var config Config
config.Plain.Enabled = false
config.Plain.Port = "25"
config.TLS.Enabled = false
config.TLS.Port = "465"
config.Plain = "disabled"
config.TLS = "disabled"
err = json.Unmarshal(contents, &config)
if err != nil {
return nil, err

View file

@ -62,8 +62,8 @@ func main() {
log.Fatal().Msgf("%v", err)
}
if log_level == "" {
if config.General.LogLevel != "" {
log_level = config.General.LogLevel
if config.LogLevel != "" {
log_level = config.LogLevel
} else {
log_level = "info"
}
@ -83,34 +83,28 @@ func main() {
log.Info().Msgf("Starting diodemail v%v", Version)
log.Info().Msgf("Loaded config from: %v", config_path)
var plain_config *smtp.PlainConfig
if config.Plain.Enabled {
plain_config = &smtp.PlainConfig {
config.Plain.Port,
}
if config.CertPath == "" || config.PrivateKeyPath == "" {
log.Fatal().Msgf(
"Must provide CertPath (got '%v') and PrivateKeyPath (got '%v')",
config.CertPath,
config.PrivateKeyPath,
)
}
var tls_config *smtp.TLSConfig
if config.TLS.Enabled {
certificate, err := tls.LoadX509KeyPair(
config.TLS.CertPath,
config.TLS.PrivateKeyPath,
)
if err != nil {
log.Error().Msgf("Failed to load TLS config: %v", err)
} else {
tls_config = &smtp.TLSConfig {
config.TLS.Port,
tls.Config{Certificates: []tls.Certificate{certificate}},
}
}
certificate, err := tls.LoadX509KeyPair(
config.CertPath,
config.PrivateKeyPath,
)
if err != nil {
log.Fatal().Msgf("Failed to load TLS config: %v", err)
}
err = smtp.Run(
config.General.Host,
config.General.PasswordHash,
plain_config,
tls_config,
config.Host,
config.PasswordHash,
config.Plain,
config.TLS,
tls.Config{Certificates: []tls.Certificate{certificate}},
)
if err != nil {
log.Fatal().Msgf("%v", err)