Compare commits

..

4 commits

Author SHA1 Message Date
de174da6fb
Merge pull request #208 from wneessen/fix/logger_test
Update format string in Debugf method calls in tests
2024-04-06 17:21:19 +02:00
07783d24e5
Update format string in Debugf method calls in tests
The format string for the Debugf method calls within the smtp_test.go file have been updated. Previously, the format string was empty, but it has now been changed to "%s" to align with the standard formatting expectations, improving the correctness of the tests.
2024-04-06 17:13:45 +02:00
2fd3167de3
Merge pull request #207 from wneessen/enhancement/181_rework-tlsportpolicy
Refine SMTP port selection and fallback logic
2024-04-06 17:01:43 +02:00
94138b6d23
Refine SMTP port selection and fallback logic
The update modifies the client's handling of port selection when configuring SSL/TLS connections. The clients' functions `WithSSLPort`, `WithTLSPortPolicy`, `SetTLSPortPolicy`, and `SetSSLPort` are revised to avoid overriding previously set ports. Additionally, the deprecation notes have been removed and replaced with notes on best-practice recommendations, referring the new *Port*() methods. This change revises #105 and takes the comments made in #181 into account.
2024-04-06 16:49:34 +02:00
2 changed files with 40 additions and 21 deletions

View file

@ -253,8 +253,6 @@ func WithTimeout(timeout time.Duration) Option {
}
// WithSSL tells the client to use a SSL/TLS connection
//
// Deprecated: use WithSSLPort instead.
func WithSSL() Option {
return func(c *Client) error {
c.useSSL = true
@ -262,11 +260,16 @@ func WithSSL() Option {
}
}
// WithSSLPort tells the client to use a SSL/TLS connection.
// It automatically sets the port to 465.
// WithSSLPort tells the Client wether or not to use SSL and fallback.
// The correct port is automatically set.
//
// When the SSL connection fails and fallback is set to true,
// Port 465 is used when SSL set (true).
// Port 25 is used when SSL is unset (false).
// When the SSL connection fails and fb is set to true,
// the client will attempt to connect on port 25 using plaintext.
//
// Note: If a different port has already been set otherwise, the port-choosing
// and fallback automatism will be skipped.
func WithSSLPort(fallback bool) Option {
return func(c *Client) error {
c.SetSSLPort(true, fallback)
@ -304,7 +307,8 @@ func WithHELO(helo string) Option {
// WithTLSPolicy tells the client to use the provided TLSPolicy
//
// Deprecated: use WithTLSPortPolicy instead.
// Note: To follow best-practices for SMTP TLS connections, it is recommended
// to use WithTLSPortPolicy instead.
func WithTLSPolicy(policy TLSPolicy) Option {
return func(c *Client) error {
c.tlspolicy = policy
@ -319,6 +323,9 @@ func WithTLSPolicy(policy TLSPolicy) Option {
// If the connection fails with TLSOpportunistic,
// a plaintext connection is attempted on port 25 as a fallback.
// NoTLS will allways use port 25.
//
// Note: If a different port has already been set otherwise, the port-choosing
// and fallback automatism will be skipped.
func WithTLSPortPolicy(policy TLSPolicy) Option {
return func(c *Client) error {
c.SetTLSPortPolicy(policy)
@ -463,6 +470,9 @@ func (c *Client) ServerAddr() string {
}
// SetTLSPolicy overrides the current TLSPolicy with the given TLSPolicy value
//
// Note: To follow best-practices for SMTP TLS connections, it is recommended
// to use SetTLSPortPolicy instead.
func (c *Client) SetTLSPolicy(policy TLSPolicy) {
c.tlspolicy = policy
}
@ -474,7 +484,11 @@ func (c *Client) SetTLSPolicy(policy TLSPolicy) {
// If the connection fails with TLSOpportunistic, a plaintext connection is
// attempted on port 25 as a fallback.
// NoTLS will allways use port 25.
//
// Note: If a different port has already been set otherwise, the port-choosing
// and fallback automatism will be skipped.
func (c *Client) SetTLSPortPolicy(policy TLSPolicy) {
if c.port == DefaultPort {
c.port = DefaultPortTLS
if policy == TLSOpportunistic {
@ -483,6 +497,7 @@ func (c *Client) SetTLSPortPolicy(policy TLSPolicy) {
if policy == NoTLS {
c.port = DefaultPort
}
}
c.tlspolicy = policy
}
@ -499,8 +514,11 @@ func (c *Client) SetSSL(ssl bool) {
// Port 25 is used when SSL is unset (false).
// When the SSL connection fails and fb is set to true,
// the client will attempt to connect on port 25 using plaintext.
//
// Note: If a different port has already been set otherwise, the port-choosing
// and fallback automatism will be skipped.
func (c *Client) SetSSLPort(ssl bool, fallback bool) {
c.port = DefaultPort
if c.port == DefaultPort {
if ssl {
c.port = DefaultPortSSL
}
@ -509,6 +527,7 @@ func (c *Client) SetSSLPort(ssl bool, fallback bool) {
if fallback {
c.fallbackPort = DefaultPort
}
}
c.useSSL = ssl
}

View file

@ -777,9 +777,9 @@ func TestClient_SetLogger(t *testing.T) {
if c.logger == nil {
t.Errorf("Expected Logger to be set but received nil")
}
c.logger.Debugf(log.Log{Direction: log.DirServerToClient, Format: "", Messages: []interface{}{"test"}})
c.logger.Debugf(log.Log{Direction: log.DirServerToClient, Format: "%s", Messages: []interface{}{"test"}})
c.SetLogger(nil)
c.logger.Debugf(log.Log{Direction: log.DirServerToClient, Format: "", Messages: []interface{}{"test"}})
c.logger.Debugf(log.Log{Direction: log.DirServerToClient, Format: "%s", Messages: []interface{}{"test"}})
}
var newClientServer = `220 hello world