From e461fa9ceb217a99f7d1c52e56bb847739f81a7e Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 5 Jan 2016 09:29:15 +0100 Subject: [PATCH] Set/gt remote ip of client connection. --- lib/sessions/event/chat_session_init.rb | 1 + script/websocket-server.rb | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/sessions/event/chat_session_init.rb b/lib/sessions/event/chat_session_init.rb index c9ce5826d..d90b2b9c0 100644 --- a/lib/sessions/event/chat_session_init.rb +++ b/lib/sessions/event/chat_session_init.rb @@ -11,6 +11,7 @@ class Sessions::Event::ChatSessionInit < Sessions::Event::ChatBase state: 'waiting', preferences: { participants: [@client_id], + remote_id: @remote_id, }, ) diff --git a/script/websocket-server.rb b/script/websocket-server.rb index 2ea375723..0cebc2aa7 100755 --- a/script/websocket-server.rb +++ b/script/websocket-server.rb @@ -98,7 +98,9 @@ EventMachine.run { EventMachine::WebSocket.start( host: @options[:b], port: @options[:p], secure: @options[:s], tls_options: tls_options ) do |ws| # register client connection - ws.onopen { + ws.onopen {|handshake| + headers = handshake.headers + remote_ip = get_remote_ip(headers) client_id = ws.object_id.to_s log 'notice', 'Client connected.', client_id Sessions.create( client_id, {}, { type: 'websocket' } ) @@ -108,6 +110,8 @@ EventMachine.run { websocket: ws, last_ping: Time.now.utc.to_i, error_count: 0, + headers: headers, + remote_id: remote_ip, } end } @@ -154,6 +158,7 @@ EventMachine.run { event: data['event'], payload: data, session: @clients[client_id][:session], + remote_ip: @clients[client_id][:remote_id], client_id: client_id, clients: @clients, options: @options, @@ -227,6 +232,11 @@ EventMachine.run { } } + def get_remote_ip(headers) + return headers['X-Forwarded-For'] if headers && headers['X-Forwarded-For'] + nil + end + def websocket_send(client_id, data) if data.class != Array msg = "[#{data.to_json}]"