lint fixes

This commit is contained in:
Aine
2023-10-03 00:03:29 +03:00
parent ef221038f7
commit 0d0dcf20b9
18 changed files with 196 additions and 152 deletions

View File

@@ -11,7 +11,7 @@ import (
)
type MailSender interface {
Send(from string, to string, data string) error
Send(from, to, data string) error
}
// SMTP client
@@ -28,7 +28,7 @@ func newClient(cfg *RelayConfig, log *zerolog.Logger) *Client {
}
// Send email
func (c Client) Send(from string, to string, data string) error {
func (c Client) Send(from, to, data string) error {
c.log.Debug().Str("from", from).Str("to", to).Msg("sending email")
var conn *smtp.Client
@@ -67,7 +67,7 @@ func (c Client) Send(from string, to string, data string) error {
}
// createDirectClient connects directly to the provided smtp host
func (c *Client) createDirectClient(from string, to string) (*smtp.Client, error) {
func (c *Client) createDirectClient(from, to string) (*smtp.Client, error) {
localname := strings.SplitN(from, "@", 2)[1]
target := c.config.Host + ":" + c.config.Port
conn, err := smtp.Dial(target)
@@ -81,8 +81,8 @@ func (c *Client) createDirectClient(from string, to string) (*smtp.Client, error
}
if ok, _ := conn.Extension("STARTTLS"); ok {
config := &tls.Config{ServerName: c.config.Host}
conn.StartTLS(config) //nolint:errcheck // if it doesn't work - we can't do anything anyway
config := &tls.Config{ServerName: c.config.Host} //nolint:gosec // it's smtp, even that is too strict sometimes
conn.StartTLS(config) //nolint:errcheck // if it doesn't work - we can't do anything anyway
}
if c.config.Usename != "" {

View File

@@ -11,24 +11,24 @@ type validatorLoggerWrapper struct {
log *zerolog.Logger
}
func (l validatorLoggerWrapper) Info(msg string, args ...interface{}) {
func (l validatorLoggerWrapper) Info(msg string, args ...any) {
l.log.Info().Msgf(msg, args...)
}
func (l validatorLoggerWrapper) Error(msg string, args ...interface{}) {
func (l validatorLoggerWrapper) Error(msg string, args ...any) {
l.log.Error().Msgf(msg, args...)
}
// loggerWrapper is a wrapper around any logger to implement smtp.Logger interface
type loggerWrapper struct {
log func(string, ...interface{})
log func(string, ...any)
}
func (l loggerWrapper) Printf(format string, v ...interface{}) {
func (l loggerWrapper) Printf(format string, v ...any) {
l.log(format, v...)
}
func (l loggerWrapper) Println(v ...interface{}) {
func (l loggerWrapper) Println(v ...any) {
msg := strings.Repeat("%v ", len(v))
l.log(msg, v...)
}

View File

@@ -90,7 +90,7 @@ func NewManager(cfg *Config) *Manager {
}
s := smtp.NewServer(mailsrv)
s.ErrorLog = loggerWrapper{func(s string, i ...interface{}) { cfg.Logger.Error().Msgf(s, i...) }}
s.ErrorLog = loggerWrapper{func(s string, i ...any) { cfg.Logger.Error().Msgf(s, i...) }}
s.ReadTimeout = 10 * time.Second
s.WriteTimeout = 10 * time.Second
s.MaxMessageBytes = cfg.MaxSize * 1024 * 1024
@@ -209,7 +209,7 @@ func (m *Manager) loadTLSConfig() bool {
return false
}
m.tls.Config = &tls.Config{Certificates: certificates}
m.tls.Config = &tls.Config{Certificates: certificates} //nolint:gosec // it's email, even that config is too strict sometimes
m.smtp.TLSConfig = m.tls.Config
return true
}

View File

@@ -10,17 +10,28 @@ import (
"gitlab.com/etke.cc/postmoogle/email"
)
const (
// NoUserCode SMTP code
NoUserCode = 550
// BannedCode SMTP code
BannedCode = 554
)
var (
// NoUserEnhancedCode enhanced SMTP code
NoUserEnhancedCode = smtp.EnhancedCode{5, 5, 0}
// BannedEnhancedCode enhanced SMTP code
BannedEnhancedCode = smtp.EnhancedCode{5, 5, 4}
// ErrBanned returned to banned hosts
ErrBanned = &smtp.SMTPError{
Code: 554,
EnhancedCode: smtp.EnhancedCode{5, 5, 4},
Code: BannedCode,
EnhancedCode: BannedEnhancedCode,
Message: "please, don't bother me anymore, kupo.",
}
// ErrNoUser returned when no such mailbox found
ErrNoUser = &smtp.SMTPError{
Code: 550,
EnhancedCode: smtp.EnhancedCode{5, 5, 0},
Code: NoUserCode,
EnhancedCode: NoUserEnhancedCode,
Message: "no such user here, kupo.",
}
)

View File

@@ -20,6 +20,16 @@ import (
"gitlab.com/etke.cc/postmoogle/utils"
)
// GraylistCode SMTP code
const GraylistCode = 451
var (
// ErrInvalidEmail for invalid emails :)
ErrInvalidEmail = errors.New("please, provide valid email address")
// GraylistEnhancedCode is GraylistCode in enhanced code notation
GraylistEnhancedCode = smtp.EnhancedCode{4, 5, 1}
)
// incomingSession represents an SMTP-submission session receiving emails from remote servers
type incomingSession struct {
log *zerolog.Logger
@@ -32,7 +42,7 @@ type incomingSession struct {
domains []string
roomID id.RoomID
ctx context.Context
ctx context.Context //nolint:containedctx // that's session
addr net.Addr
tos []string
from string
@@ -89,13 +99,13 @@ func (s *incomingSession) getAddr(envelope *enmime.Envelope) net.Addr {
return s.addr
}
host, portString, _ := net.SplitHostPort(addrHeader) //nolint:errcheck
host, portString, _ := net.SplitHostPort(addrHeader) //nolint:errcheck // it is real addr
if host == "" {
return s.addr
}
var port int
port, _ = strconv.Atoi(portString) //nolint:errcheck
port, _ = strconv.Atoi(portString) //nolint:errcheck // it's a real addr
realAddr := &net.TCPAddr{IP: net.ParseIP(host), Port: port}
s.log.Info().Str("addr", realAddr.String()).Msg("real address")
@@ -115,7 +125,7 @@ func (s *incomingSession) Data(r io.Reader) error {
return err
}
addr := s.getAddr(envelope)
reader.Seek(0, io.SeekStart) //nolint:errcheck
reader.Seek(0, io.SeekStart) //nolint:errcheck // becase we're sure that's ok
validations := s.getFilters(s.roomID)
if !validateIncoming(s.from, s.tos[0], addr, s.log, validations) {
s.ban(addr)
@@ -123,8 +133,8 @@ func (s *incomingSession) Data(r io.Reader) error {
}
if s.greylisted(addr) {
return &smtp.SMTPError{
Code: 451,
EnhancedCode: smtp.EnhancedCode{4, 5, 1},
Code: GraylistCode,
EnhancedCode: GraylistEnhancedCode,
Message: "You have been greylisted, try again a bit later.",
}
}
@@ -164,16 +174,16 @@ type outgoingSession struct {
domains []string
getRoomID func(string) (id.RoomID, bool)
ctx context.Context
ctx context.Context //nolint:containedctx // that's session
tos []string
from string
fromRoom id.RoomID
}
func (s *outgoingSession) Mail(from string, opts smtp.MailOptions) error {
func (s *outgoingSession) Mail(from string, _ smtp.MailOptions) error {
sentry.GetHubFromContext(s.ctx).Scope().SetTag("from", from)
if !email.AddressValid(from) {
return errors.New("please, provide email address")
return ErrInvalidEmail
}
hostname := utils.Hostname(from)
var domainok bool
@@ -234,7 +244,7 @@ func validateIncoming(from, to string, senderAddr net.Addr, log *zerolog.Logger,
case *net.TCPAddr:
sender = netaddr.IP
default:
host, _, _ := net.SplitHostPort(senderAddr.String()) // nolint:errcheck
host, _, _ := net.SplitHostPort(senderAddr.String()) //nolint:errcheck // interface constraints
sender = net.ParseIP(host)
}