rabbitmq / amqp091-go

An AMQP 0-9-1 Go client maintained by the RabbitMQ team. Originally by @streadway: `streadway/amqp`
Other
1.49k stars 135 forks source link

The server name and version could not be captured by using tls.ConnectionState #209

Closed a2htray closed 1 year ago

a2htray commented 1 year ago

Describe the bug

Thanks for this great project.

When using amqp091-go v1.8.1 and Go 1.19.3, I found the server name and version could not be captured. The code is simple as follows.

// main.go
conn, err := amqp.Dial("amqp://goadmin:123456@localhost:5672/goapp-vhost")

connState := conn.ConnectionState()
fmt.Println("Server name: ", connState.ServerName)
fmt.Println("Version: ", connState.Version)
$ go run main.go

0

Reproduction steps

  1. Create connection with the plain URL
  2. call conn.ConnectionState to get ConnectionState
  3. access ConnectionState.ServerName and ConnectionState.Version

Expected behavior

print right server name and version.

Additional context

No response

Zerpet commented 1 year ago

This is working as intended. You are not dialing to an amqps address, therefore, Dial is not starting a TLS transport. Since it is not using TLS, ConnectionState returns a zero-value, as documented in the function documentation. See also tls#ConnectionState for the fields you can find and their meaning.

By setting a RabbitMQ server that serves over AMQP+TLS, and running this code:

// main.go
    conn, err := amqp.DialTLS("amqps://127.0.0.1:5671", &tls.Config{
        InsecureSkipVerify: true,
    })
    if err != nil {
        errLog.Fatalf("error dialing: %s", err)
    }
    defer conn.Close()

    connState := conn.ConnectionState()
    infoLog.Printf("Server name: '%s'", connState.ServerName)
    infoLog.Printf("Version: %#x", connState.Version)

// Output
// 2023/07/12 12:58:46 [INFO] Server name: ''
// 2023/07/12 12:58:46 [INFO] Version: 0x304

I get the expected results. Note that server name is still empty, because I dialed to an IP address. The number 0x0304 matches the constant for TLS 1.3