Protected map, better check for RCPT

This commit is contained in:
LowEel 2021-07-30 23:20:49 +02:00
parent c101731bcd
commit 71a9b231e3
5 changed files with 35 additions and 16 deletions

View File

@ -5,6 +5,7 @@ import (
"log"
"os"
"strings"
"sync"
)
// The Backend implements SMTP server methods.
@ -17,6 +18,8 @@ type Backend struct {
var SmtpBackend *Backend
var mutex = &sync.Mutex{}
// Load Valid Recipients
func (bkd *Backend) LoadValidRecipients() error {
bkd.MaxRecipients = 0
@ -41,9 +44,13 @@ func (bkd *Backend) LoadValidRecipients() error {
}
// Checks if Mail is a valid recipient
// Checks if Mail is a valid local recipient
func (bkd *Backend) CheckValidRcpt(to string) bool {
defer mutex.Unlock()
mutex.Lock()
_, isValid := bkd.ValidRecipientsMap[to]
return isValid

5
go.mod
View File

@ -2,7 +2,4 @@ module zangtumb
go 1.13
require (
github.com/amalfra/maildir v0.0.7
github.com/emersion/go-smtp v0.12.1 // indirect
)
require github.com/amalfra/maildir v0.0.7

4
go.sum
View File

@ -1,6 +1,2 @@
github.com/amalfra/maildir v0.0.7 h1:quK6vVGmjqP1zbeo9g7jtx37htGoYrKKlSpcDcplD04=
github.com/amalfra/maildir v0.0.7/go.mod h1:+ynYowCpUHTWebUhg3Sb6Jp2dq+SUSWLTSLIf7Vy+ak=
github.com/emersion/go-sasl v0.0.0-20190817083125-240c8404624e h1:ba7YsgX5OV8FjGi5ZWml8Jng6oBrJAb3ahqWMJ5Ce8Q=
github.com/emersion/go-sasl v0.0.0-20190817083125-240c8404624e/go.mod h1:G/dpzLu16WtQpBfQ/z3LYiYJn3ZhKSGWn83fyoyQe/k=
github.com/emersion/go-smtp v0.12.1 h1:1R8BDqrR2HhlGwgFYcOi+BVTvK1bMjAB65QcVpJ5sNA=
github.com/emersion/go-smtp v0.12.1/go.mod h1:SD9V/xa4ndMw77lR3Mf7htkp8RBNYuPh9UeuBs9tpUQ=

View File

@ -4,8 +4,14 @@ import (
"bytes"
"log"
"net"
"time"
)
func init() {
ZangSmtpServer.HandlerRcpt = handlerRcpt
}
func mailHandler(origin net.Addr, from string, to []string, data []byte) {
SmtpSession := new(Session)
@ -28,3 +34,20 @@ func mailHandler(origin net.Addr, from string, to []string, data []byte) {
}
}
func handlerRcpt(remoteAddr net.Addr, from string, to string) bool {
log.Println("CHECK #2 Rcpt to:", to)
if !SmtpBackend.CheckValidRcpt(to) {
log.Printf("%s [ %s -> $s ] %s BAD!!!!", "YU NO MAIL!!! WE NU RELAY!!! ", from, to, remoteAddr.String() )
time.Sleep(10 * time.Second)
return false
}else{
log.Printf("%s [ %s -> $s ] %s GOOD!!!!", "WE DO!!!", from, to, remoteAddr.String() )
}
return true
}

View File

@ -1,13 +1,13 @@
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"strings"
"time"
"github.com/amalfra/maildir"
)
@ -22,11 +22,7 @@ type Session struct {
}
func (s *Session) Rcpt(to string) error {
log.Println("Rcpt to:", to)
if !SmtpBackend.CheckValidRcpt(to) {
time.Sleep(10 * time.Second)
return errors.New("YU NO MAIL!!! WE NU RELAY")
}
log.Println("CHECK #1 Rcpt to:", to)
s.RcptTo = to
if strings.Contains(to, "@") {
rcptArray := strings.Split(to, "@")