diodemail/cmd/server/main.go
2024-10-02 03:44:20 +00:00

117 lines
2.5 KiB
Go

/* diodemail - send-only smtp server
* Copyright (c) 2024 Gnarwhal
*
* This file is part of SSHare.
*
* SSHare is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* SSHare is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* SSHare. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"flag"
"crypto/tls"
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"forge.monodon.me/Gnarwhal/diodemail/smtp"
)
const Version = "1.0.0"
func main() {
log.Logger = zerolog.
New(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.DateTime}).
With().
Timestamp().
Logger().
Level(zerolog.ErrorLevel)
var config_path string
var log_level string
flag.StringVar(
&config_path,
"config-file",
"/etc/diodemail/config.json",
"Path to config file",
)
flag.StringVar(
&log_level,
"log-level",
"",
"Maximum log level to log",
)
flag.Parse()
config, err := LoadConfig(config_path)
if err != nil {
log.Fatal().Msgf("%v", err)
}
if log_level == "" {
if config.LogLevel != "" {
log_level = config.LogLevel
} else {
log_level = "info"
}
}
parsed_log_level, err := zerolog.ParseLevel(log_level)
if err != nil {
log.Fatal().Msgf("%v", err)
}
log.Logger = zerolog.
New(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.DateTime}).
With().
Timestamp().
Logger().
Level(parsed_log_level)
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,
}
}
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}},
}
}
}
err = smtp.Run(
config.Host,
plain_config,
tls_config,
)
if err != nil {
log.Fatal().Msgf("%v", err)
}
}