golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.7k stars 17.5k forks source link

go: go1.21 version cause gorilla/websocket failed #68522

Closed cuiweixie closed 1 month ago

cuiweixie commented 1 month ago

Go version

go version go1.22.4 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/xiecui/Library/Caches/go-build'
GOENV='/Users/xiecui/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOMODCACHE='/Users/xiecui/code/go/gopath/pkg/mod'
GONOSUMDB='on'
GOOS='darwin'
GOPATH='/Users/xiecui/code/go/gopath'
GOPROXY='https://goproxy.cn,direct'
GOROOT='/Users/xiecui/code/go/go1.22.4/go'
GOSUMDB='off'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/xiecui/code/go/go1.22.4/go/pkg/tool/darwin_arm64'
GOVCS='*:all'
GOVERSION='go1.22.4'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Volumes/Elements/code/go/broker/my_test/go.mod'
GOWORK='off'
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/d2/82q9pj7x33gc7m0xkpkx3p3w0000gn/T/go-build162556822=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

go.mod

module github.com/cuiweixie/websocket_test

go 1.21

require github.com/gorilla/websocket v1.5.3 // indirect

wss.go

package main

import (
    "log"
    "net/http"

    "github.com/gorilla/websocket"
)

func main() {
    webURL := "wss://wspap.okx.com:8443/ws/v5/public"

    header := http.Header{}
    var dialer websocket.Dialer
    _, _, err := dialer.Dial(webURL, header)
    if err != nil {
        log.Fatalf("dial failed: %v", err)
    }
    log.Println("connection ok")
}
go run wss.go

What did you see happen?

2024/07/19 19:01:13 dial failed: websocket: bad handshake
exit status 1

What did you expect to see?

if change go mod to:

module github.com/cuiweixie/websocket_test

go 1.22

require github.com/gorilla/websocket v1.5.3 // indirect

will result in:

2024/07/19 19:01:39 connection ok
cuiweixie commented 1 month ago

It's strange that using go1.21 doesn't work, but go1.22 or later does.

gabyhelp commented 1 month ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

seankhliao commented 1 month ago

I think this is controlled by the GODEBUG setting tlsrsakex where the default changed from enabled in go1.21 to disabled in go1.22. I don't think there's anything to do here as the new behavior is more correct and we aren't going to change the default GODEBUG for a previous version.

main » GODEBUG=tlsrsakex=1 go1.22.5 run .
2024/07/19 12:18:46 dial failed: websocket: bad handshake
exit status 1

main » GODEBUG=tlsrsakex=0 go1.22.5 run . 
2024/07/19 12:18:56 connection ok
cuiweixie commented 1 month ago

thanks!