mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
feat: Add RemoteAddr to SMTPSession (#548)
Signed-off-by: James Hillyerd <james@hillyerd.com>
This commit is contained in:
@@ -116,7 +116,11 @@ type Session struct {
|
||||
// NewSession creates a new Session for the given connection
|
||||
func NewSession(server *Server, id int, conn net.Conn, logger zerolog.Logger) *Session {
|
||||
reader := bufio.NewReader(conn)
|
||||
host, _, _ := net.SplitHostPort(conn.RemoteAddr().String())
|
||||
|
||||
remoteHost := conn.RemoteAddr().String()
|
||||
if host, _, err := net.SplitHostPort(remoteHost); err == nil {
|
||||
remoteHost = host
|
||||
}
|
||||
|
||||
session := &Session{
|
||||
Server: server,
|
||||
@@ -124,7 +128,7 @@ func NewSession(server *Server, id int, conn net.Conn, logger zerolog.Logger) *S
|
||||
conn: conn,
|
||||
state: GREET,
|
||||
reader: reader,
|
||||
remoteHost: host,
|
||||
remoteHost: remoteHost,
|
||||
recipients: make([]*policy.Recipient, 0),
|
||||
logger: logger,
|
||||
debug: server.config.Debug,
|
||||
@@ -454,9 +458,14 @@ func (s *Session) parseMailFromCmd(arg string) {
|
||||
return
|
||||
}
|
||||
|
||||
// Add from to extSession for inspection.
|
||||
extSession := s.extSession()
|
||||
addrCopy := origin.Address
|
||||
extSession.From = &addrCopy
|
||||
|
||||
// Process through extensions.
|
||||
extAction := event.ActionDefer
|
||||
extResult := s.extHost.Events.BeforeMailFromAccepted.Emit(&event.SMTPSession{From: &origin.Address})
|
||||
extResult := s.extHost.Events.BeforeMailFromAccepted.Emit(extSession)
|
||||
if extResult != nil {
|
||||
extAction = extResult.Action
|
||||
}
|
||||
@@ -711,7 +720,11 @@ func (s *Session) ooSeq(cmd string) {
|
||||
|
||||
// extSession builds an SMTPSession for extensions.
|
||||
func (s *Session) extSession() *event.SMTPSession {
|
||||
from := s.from.Address
|
||||
var from *mail.Address
|
||||
if s.from != nil {
|
||||
addr := s.from.Address
|
||||
from = &addr
|
||||
}
|
||||
to := make([]*mail.Address, 0, len(s.recipients))
|
||||
for _, recip := range s.recipients {
|
||||
addr := recip.Address
|
||||
@@ -719,7 +732,8 @@ func (s *Session) extSession() *event.SMTPSession {
|
||||
}
|
||||
|
||||
return &event.SMTPSession{
|
||||
From: &from,
|
||||
To: to,
|
||||
From: from,
|
||||
To: to,
|
||||
RemoteAddr: s.remoteHost,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,6 +408,7 @@ func TestBeforeMailFromAcceptedEventEmitted(t *testing.T) {
|
||||
|
||||
assert.NotNil(t, got, "BeforeMailListener did not receive Address")
|
||||
assert.Equal(t, "john@gmail.com", got.From.Address, "Address had wrong value")
|
||||
assert.Equal(t, "pipe", got.RemoteAddr, "RemoteAddr had wrong value")
|
||||
}
|
||||
|
||||
// Test "MAIL FROM" acts on BeforeMailFromAccepted event result.
|
||||
@@ -492,6 +493,7 @@ func TestBeforeRcptToAcceptedSingleEventEmitted(t *testing.T) {
|
||||
require.NotNil(t, got, "BeforeRcptToListener did not receive SMTPSession")
|
||||
require.NotNil(t, got.From)
|
||||
require.NotNil(t, got.To)
|
||||
assert.Equal(t, "pipe", got.RemoteAddr, "RemoteAddr had wrong value")
|
||||
assert.Equal(t, "john@gmail.com", got.From.Address)
|
||||
assert.Len(t, got.To, 1)
|
||||
assert.Equal(t, "user@gmail.com", got.To[0].Address)
|
||||
|
||||
Reference in New Issue
Block a user