Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unix domain socket support #408

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Support Unix Domain Socket connections in SMTP client
Added the ability to connect via Unix Domain Sockets by introducing the `useUnixSocket` flag. Adjusted connection logic to handle `unix://` host prefixes and updated the dialing mechanism to use the appropriate network type (`unix` or `tcp`). This enhances flexibility in connection methods.
  • Loading branch information
wneessen committed Jan 8, 2025
commit 149ec4a3704f68bd9a0aff1f60cbabdaa1c3866c
22 changes: 20 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ type (
// user represents a username used for the SMTP authentication.
user string

// useUnixSocket indicates that a connection is established via a Unix Domain Socket instead of TCP
useUnixSocket bool

// useSSL indicates whether to use SSL/TLS encryption for network communication.
//
// https://datatracker.ietf.org/doc/html/rfc8314
Expand Down Expand Up @@ -288,6 +291,12 @@ func NewClient(host string, opts ...Option) (*Client, error) {
}
}

// We allow connecting to a UNIX Domain Socket
if strings.HasPrefix(c.host, "unix://") {
c.useUnixSocket = true
c.host = strings.TrimPrefix(c.host, "unix://")
}

// Some settings in a Client cannot be empty/unset
if c.host == "" {
return c, ErrNoHostname
Expand Down Expand Up @@ -722,6 +731,9 @@ func (c *Client) TLSPolicy() string {
// Returns:
// - A string representing the server address in the format "host:port".
func (c *Client) ServerAddr() string {
if c.useUnixSocket {
return c.host
}
return fmt.Sprintf("%s:%d", c.host, c.port)
}

Expand Down Expand Up @@ -1004,8 +1016,14 @@ func (c *Client) DialToSMTPClientWithContext(ctxDial context.Context) (*smtp.Cli
dialContextFunc = tlsDialer.DialContext
}
}
connection, err := dialContextFunc(ctx, "tcp", c.ServerAddr())
if err != nil && c.fallbackPort != 0 {

network := "tcp"
if c.useUnixSocket {
network = "unix"
}

connection, err := dialContextFunc(ctx, network, c.ServerAddr())
if err != nil && !c.useUnixSocket && c.fallbackPort != 0 {
// TODO: should we somehow log or append the previous error?
connection, err = dialContextFunc(ctx, "tcp", c.serverFallbackAddr())
}
Expand Down