igrigorik / em-websocket

EventMachine based WebSocket server
http://www.igvita.com/2009/12/22/ruby-websockets-tcp-for-the-browser/
MIT License
1.69k stars 187 forks source link

Unable to use remote_ip method #141

Open IMcPwn opened 8 years ago

IMcPwn commented 8 years ago

Here's an example WebSocket connection:

[31] pry(main)> wss.getWsList[0]
=> #<EventMachine::WebSocket::Connection:0x00000001f35150
 @close_timeout=nil,
 @debug=false,
 @handler=
  #<EventMachine::WebSocket::Handler13:0x00000001f39ef8 @application_data_buffer="", @close_timer=nil, @connection=#<EventMachine::WebSocket::Connection:0x00000001f35150 ...>, @data="", @debug=false, @frame_type=nil, @state=:connected>,
 @handshake=nil,
 @onclose=#<Proc:0x00000001f36af0@/[REDACTED]>,
 @onerror=#<Proc:0x00000001f366e0@/[REDACTED]>,
 @onmessage=#<Proc:0x00000001f367a8@/[REDACTED]>,
 @onopen=#<Proc:0x00000001f36b90@/[REDACTED]>,
 @options={:host=>"0.0.0.0", :port=>443, :secure=>true, :tls_options=>{:private_key_file=>"privkey.pem", :cert_chain_file=>"cert.pem"}},
 @secure=true,
 @secure_proxy=false,
 @signature=3,
 @tls_options={:private_key_file=>"privkey.pem", :cert_chain_file=>"cert.pem"}>

I want to get the IP address of the remote host, so I use remote_ip like in commit 705c9f1436e830309a51d68ba9795d10deb0da4e.

[33] pry(main)> wss.getWsList[0].remote_ip
NoMethodError: undefined method `remote_ip' for #<EventMachine::WebSocket::Connection:0x00000001f35150>
from (pry):52:in `cmdLine'

However if I use the code inside of the remote_ip method I get this:

[34] pry(main)> wss.getWsList[0].get_peername[2,6].unpack('nC4')[1..4].join('.')
=> "[REDACTED_IP]"

Why wouldn't I have the remote_ip method? I'm using version 0.5.1 of the gem.

igrigorik commented 8 years ago

Odd, to say the least. Are you sure your runtime is not pulling in an older version by accident?

IMcPwn commented 8 years ago

Here is my Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    coderay (1.1.1)
    colorize (0.8.1)
    em-websocket (0.5.1)
      eventmachine (>= 0.12.9)
      http_parser.rb (~> 0.6.0)
    eventmachine (1.2.0.1)
    execjs (2.7.0)
    http_parser.rb (0.6.0)
    method_source (0.8.2)
    pry (0.10.4)
      coderay (~> 1.1.0)
      method_source (~> 0.8.1)
      slop (~> 3.4)
    rb-readline (0.5.3)
    slop (3.6.0)
    uglifier (3.0.2)
      execjs (>= 0.3.0, < 3)

PLATFORMS
  ruby

DEPENDENCIES
  colorize
  em-websocket
  eventmachine
  pry
  rb-readline
  uglifier

BUNDLED WITH
   1.12.5
juancolacelli commented 7 years ago

+1 undefined method `remote_ip' for #EventMachine::WebSocket::Connection:0x007fe4f3a315f8 (NoMethodError)

@IMcPwn temporal "monkey patch": class EM::WebSocket::Connection def remote_ip get_peername[2,6].unpack('nC4')[1..4].join('.') end end

ghost commented 5 years ago

I found that for some reason the em-websocket gem from https://rubygems.org/gems/em-websocket doesn't contain remote_ip method even though it's version is 0.5.1.

jahio commented 4 years ago

I ran into this tonight myself. I've never used this library before, and it's never been installed anywhere on my machine before tonight.

[5] pry(Minion::Service)> ws
=> #<EventMachine::WebSocket::Connection:0x00007fc7a38cf970
 @close_timeout=nil,
 @debug=false,
 @handler=#<EventMachine::WebSocket::Handler13:0x00007fc7a61af5c0 @application_data_buffer="", @close_timer=nil, @connection=#<EventMachine::WebSocket::Connection:0x00007fc7a38cf970 ...>, @data="", @debug=false, @frame_type=nil, @state=:connected>,
 @handshake=nil,
 @onclose=#<Proc:0x00007fc7a38cf3f8 /Users/jah/OneDrive/Projects/minion/api/service.rb:28>,
 @onmessage=#<Proc:0x00007fc7a38cf380 /Users/jah/OneDrive/Projects/minion/api/service.rb:30>,
 @onopen=#<Proc:0x00007fc7a38cf420 /Users/jah/OneDrive/Projects/minion/api/service.rb:16>,
 @options={:host=>"0.0.0.0", :port=>9000, :secure=>true, :tls_options=>{:private_key_file=>"/Users/jah/OneDrive/Projects/minion/api/service/ssl/server.key", :cert_chain_file=>"/Users/jah/OneDrive/Projects/minion/api/service/ssl/server.crt"}},
 @secure=true,
 @secure_proxy=false,
 @signature=3,
 @tls_options={:private_key_file=>"/Users/jah/OneDrive/Projects/minion/api/service/ssl/server.key", :cert_chain_file=>"/Users/jah/OneDrive/Projects/minion/api/service/ssl/server.crt"}>
[6] pry(Minion::Service)> ws.remote_ip
NoMethodError: undefined method `remote_ip' for #<EventMachine::WebSocket::Connection:0x00007fc7a38cf970>
from (pry):6:in `block (3 levels) in start'
[7] pry(Minion::Service)>

Gemfile.lock:

GEM
  remote: https://rubygems.org/
  specs:
    coderay (1.1.2)
    em-websocket (0.5.1)
      eventmachine (>= 0.12.9)
      http_parser.rb (~> 0.6.0)
    eventmachine (1.2.7)
    http_parser.rb (0.6.0)
    jwt (2.2.1)
    method_source (1.0.0)
    mustermann (1.1.1)
      ruby2_keywords (~> 0.0.1)
    nio4r (2.5.2)
    pry (0.13.1)
      coderay (~> 1.1)
      method_source (~> 1.0)
    puma (4.3.5)
      nio4r (~> 2.0)
    rack (2.2.2)
    rack-protection (2.0.8.1)
      rack
    rethinkdb (2.4.0.0)
    ruby2_keywords (0.0.2)
    sinatra (2.0.8.1)
      mustermann (~> 1.0)
      rack (~> 2.0)
      rack-protection (= 2.0.8.1)
      tilt (~> 2.0)
    tilt (2.0.10)

PLATFORMS
  ruby

DEPENDENCIES
  em-websocket
  jwt
  pry
  puma
  rethinkdb
  sinatra

BUNDLED WITH
   2.1.4

Following @smallfish135's comment, I also tested this with the version from GitHub, tag 'v0.5.1', and got the same problem. So it looks like both Rubygems.org and Github don't have this available (in other words, the gems are probably the same).

For now I've just implemented the monkey patch above and it works fine, but I figured since it's been over a year since the last response, I'd "bump" this issue with some timely status information.