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

crypto/tls: SMTP connection timeout in Go Version 1.23 #71257

Open
littlerest1 opened this issue Jan 13, 2025 · 3 comments
Open

crypto/tls: SMTP connection timeout in Go Version 1.23 #71257

littlerest1 opened this issue Jan 13, 2025 · 3 comments
Labels
BugReport Issues describing a possible bug in the Go implementation. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@littlerest1
Copy link

Go version

go version go1.23.2 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/yanz1/Library/Caches/go-build'
GOENV='/Users/yanz1/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/yanz1/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/yanz1/go'
GOPRIVATE=''
GOPROXY=''
GOROOT='/opt/homebrew/Cellar/go@1.22/1.23.2/libexec'
GOSUMDB='off'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/opt/homebrew/Cellar/go@1.23/1.23.2/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.2'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/s4/j38_716j1q1dmb564z0ynfq00000gq/T/go-build782530738=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

Was using library crypto/tls and net for smtp dialing and communication
the code is working fine in MacOs arm64 system. However the binary file built by command env GOOS=linux GOARCH=amd64 go build -o and run in AWS Linux EC2 instance will receive read tcp: timeout issue. This issue is resolved by downgrading the Go version to 1.22.


package main

import (
	"crypto/tls"
	"io"
	"fmt"
	"log"
	"net"
	"strings"
	"time"
)

func main() {
	smtpHost := "PRIVATE HOST"
	smtpPort := "587"

	tlsConfig := &tls.Config{
		ServerName: smtpHost,
	}

	dialer := &net.Dialer{
		Timeout: 10 * time.Second,
	}
	conn, err := dialer.Dial("tcp", smtpHost+":"+smtpPort)

	if err != nil {
		log.Fatal("failed to create tlsDialer:", "error", err)
	}

	fmt.Fprintf(conn, "STARTTLS\r\n")
	buf := make([]byte, 0, 4096) // big buffer
	tmp := make([]byte, 256)     // using small tmo buffer for demonstrating
	for {
		n, err := conn.Read(tmp)
		log.Println(string(tmp))
		if err != nil {
			if err != io.EOF {
				log.Fatal("read error:", err)
			}
			break
		}
		if strings.Contains(string(tmp), "Go ahead with TLS") {
			client := tls.Client(conn, tlsConfig)
			fmt.Fprintf(client, "AUTH LOGIN\r\n") // Time out happens here. Tried to send`HLEO` also received timeout issue
			log.Println("Start TLS connection and request AUTH")
		}

		///log.Println("got", string(tmp))
		buf = append(buf, tmp[:n]...)
	}
	log.Println("total size:", len(buf))

}

What did you see happen?

{
    "time": "2025-01-07T09:16:34.914889396Z",
    "level": "WARN",
    "msg": "error while sending email",
    "error": "read tcp 10.33.124.75:46246->10.248.46.8:587: read: connection timed out"
}

What did you expect to see?

AUTH LOGIN command successfully send after STARTTLS command and receive the request for username and password for further authentication

@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Jan 14, 2025
@ianlancetaylor
Copy link
Member

CC @golang/security

@seankhliao
Copy link
Member

maybe the same as #70232

@seankhliao seankhliao added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants