1
0
mirror of https://github.com/axllent/mailpit.git synced 2025-01-08 00:39:22 +02:00
mailpit/config/config.go

92 lines
1.8 KiB
Go
Raw Normal View History

2022-07-29 13:23:08 +02:00
package config
import (
"errors"
2022-08-05 14:09:20 +02:00
"fmt"
"os"
2022-07-29 13:23:08 +02:00
"regexp"
2022-08-04 07:18:07 +02:00
"github.com/tg123/go-htpasswd"
2022-07-29 13:23:08 +02:00
)
var (
// SMTPListen to listen on <interface>:<port>
SMTPListen = "0.0.0.0:1025"
// HTTPListen to listen on <interface>:<port>
HTTPListen = "0.0.0.0:8025"
// DataDir for mail (optional)
DataDir string
// MaxMessages is the maximum number of messages a mailbox can have (auto-pruned every minute)
MaxMessages = 500
// VerboseLogging for console output
VerboseLogging = false
2022-08-04 07:18:07 +02:00
// NoLogging for tests
2022-07-29 13:23:08 +02:00
NoLogging = false
2022-08-05 14:09:20 +02:00
// SSLCert file
2022-07-29 13:23:08 +02:00
SSLCert string
2022-08-05 14:09:20 +02:00
// SSLKey file
2022-07-29 13:23:08 +02:00
SSLKey string
2022-08-04 07:18:07 +02:00
// AuthFile for basic authentication
AuthFile string
// Auth used for euthentication
Auth *htpasswd.File
2022-07-29 13:23:08 +02:00
)
// VerifyConfig wil do some basic checking
func VerifyConfig() error {
re := regexp.MustCompile(`^[a-zA-Z0-9\.\-]{3,}:\d{2,}$`)
if !re.MatchString(SMTPListen) {
return errors.New("SMTP bind should be in the format of <ip>:<port>")
}
if !re.MatchString(HTTPListen) {
return errors.New("HTTP bind should be in the format of <ip>:<port>")
}
2022-08-04 07:18:07 +02:00
if AuthFile != "" {
2022-08-05 14:09:20 +02:00
if !isFile(AuthFile) {
return fmt.Errorf("password file not found: %s", AuthFile)
}
2022-08-04 07:18:07 +02:00
a, err := htpasswd.New(AuthFile, htpasswd.DefaultSystems, nil)
if err != nil {
return err
}
Auth = a
}
2022-08-05 14:09:20 +02:00
if SSLCert != "" && SSLKey == "" || SSLCert == "" && SSLKey != "" {
return errors.New("you must provide both an SSL certificate and a key")
}
if SSLCert != "" {
if !isFile(SSLCert) {
return fmt.Errorf("SSL certificate not found: %s", SSLCert)
}
if !isFile(SSLKey) {
return fmt.Errorf("SSL key not found: %s", SSLKey)
}
}
2022-07-29 13:23:08 +02:00
return nil
}
2022-08-05 14:09:20 +02:00
// IsFile returns if a path is a file
func isFile(path string) bool {
info, err := os.Stat(path)
if os.IsNotExist(err) || !info.Mode().IsRegular() {
return false
}
return true
}