Skip to content

Commit

Permalink
Merge pull request #95 from rabb1t/feature/add-remoteAddr-support
Browse files Browse the repository at this point in the history
Added function to get the remote peer's IP address (conn.RemoteAddr())
  • Loading branch information
Zerpet authored Jun 30, 2022
2 parents b15520c + 3039151 commit da48d91
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,18 @@ func (c *Connection) LocalAddr() net.Addr {
return &net.TCPAddr{}
}

/*
RemoteAddr returns the remote TCP peer address, if known.
*/
func (c *Connection) RemoteAddr() net.Addr {
if conn, ok := c.conn.(interface {
RemoteAddr() net.Addr
}); ok {
return conn.RemoteAddr()
}
return &net.TCPAddr{}
}

// ConnectionState returns basic TLS details of the underlying transport.
// Returns a zero value when the underlying connection does not implement
// ConnectionState() tls.ConnectionState.
Expand Down
22 changes: 22 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ func TestIntegrationLocalAddr(t *testing.T) {
t.Logf("Connected to port %d\n", port)
}

func TestIntegrationRemoteAddr(t *testing.T) {
config := Config{}

c, err := DialConfig(amqpURL, config)
if err != nil {
t.Fatalf("expected to dial with config %+v integration server: %s", config, err)
}
defer c.Close()

a := c.RemoteAddr()
_, portString, err := net.SplitHostPort(a.String())
if err != nil {
t.Fatalf("expected to get a remote network address with config %+v integration server: %s", config, a.String())
}

port, err := strconv.Atoi(portString)
if err != nil {
t.Fatalf("expected to get a TCP port number with config %+v integration server: %s", config, err)
}
t.Logf("Connected to port %d\n", port)
}

// https://github.com/streadway/amqp/issues/94
func TestExchangePassiveOnMissingExchangeShouldError(t *testing.T) {
c := integrationConnection(t, "exch")
Expand Down

0 comments on commit da48d91

Please sign in to comment.