gumaro
gumaro

Reputation: 67

get client port number from server

There's a way to get the client port through the rails server, on controller?

I know which is possible to get the remote ip by request.remote_ip, but I need something like request.remote_port.

Upvotes: 2

Views: 422

Answers (1)

Stefan
Stefan

Reputation: 114158

Rack's Hijacking feature exposes the underlying Socket or IO object.

Here's an example using Puma, it returns a TCPSocket:

if request.env['rack.hijack?']
  request.env['rack.hijack'].call
  io = request.env['rack.hijack_io']
  io.class    #=> TCPSocket
  io.addr     #=> ["AF_INET", 3000, "127.0.0.1", "127.0.0.1"]
  io.peeraddr #=> ["AF_INET", 51464, "127.0.0.1", "127.0.0.1"]
end

Upvotes: 1

Related Questions