Skip to content

Commit

Permalink
Log subscriber client ip, referer & user agent for deprecated channel…
Browse files Browse the repository at this point in the history
… subscriptions
  • Loading branch information
bakkdoor committed Aug 2, 2018
1 parent 33f948e commit fc26538
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/firehose/rack/consumer/http_long_poll.rb
Expand Up @@ -94,6 +94,7 @@ def respond_async(channel, last_sequence, params, env)
begin
chan_sub = Server::ChannelSubscription.new(
channel,
env,
params: params,
sequence: last_sequence,
timeout: @timeout
Expand Down
2 changes: 2 additions & 0 deletions lib/firehose/rack/consumer/web_socket.rb
Expand Up @@ -104,6 +104,7 @@ def subscribe(last_sequence, params)
begin
@subscribed = true
@chan_sub = Server::ChannelSubscription.new @req.path,
@ws.env,
params: params,
sequence: last_sequence
@deferrable = @chan_sub.next_messages
Expand Down Expand Up @@ -197,6 +198,7 @@ def subscribe_multiplexed(subscriptions)
def subscribe(channel_name, last_sequence, params)
chan_sub = Server::ChannelSubscription.new(
channel_name,
@ws.env,
params: params,
sequence: last_sequence
)
Expand Down
18 changes: 16 additions & 2 deletions lib/firehose/server/channel_subscription.rb
Expand Up @@ -16,16 +16,30 @@ def self.subscriber
@subscriber ||= Server::Subscriber.new
end

def initialize(channel_key, sequence: 0, params: {}, timeout: nil)
class ClientInfo
attr_reader :ip, :referer, :user_agent
def initialize(env)
@ip = env["REMOTE_ADDR"]
@referer = env["HTTP_REFERER"]
@user_agent = env["HTTP_USER_AGENT"]
end

def to_s
"ip=#{@ip.inspect} referer=#{@referer.inspect} user_agent=#{@user_agent.inspect}"
end
end

def initialize(channel_key, env, sequence: 0, params: {}, timeout: nil)
@redis = self.class.redis
@subscriber = self.class.subscriber
@sequence = sequence
@timeout = timeout
@channel_key = channel_key
@client_info = ClientInfo.new(env)
@deferrable = EM::DefaultDeferrable.new
@deferrable.errback {|e| EM.next_tick { raise e } unless [:timeout, :disconnect].include?(e) }
if Server.configuration.channel_deprecated?(channel_key)
Firehose.logger.warn "Subscription to DEPRECATED Channel: #{channel_key}"
Firehose.logger.warn "Subscription to DEPRECATED Channel: #{channel_key} from client: #{@client_info}"
end
on_subscribe(params)
end
Expand Down
9 changes: 8 additions & 1 deletion spec/lib/server/channel_subscription_spec.rb
Expand Up @@ -4,8 +4,15 @@
include EM::TestHelper

let(:channel_key) { '/bears/are/mean' }
let(:env) {
{
"REMOTE_ADDR" => "192.168.0.1",
"HTTP_REFERER"=> "http://localhost:80/test",
"HTTP_USER_AGENT" => "test/runner"
}
}
let(:channel) do
Firehose::Server::ChannelSubscription.new(channel_key,
Firehose::Server::ChannelSubscription.new(channel_key, env,
sequence: sequence,
timeout: timeout)
end
Expand Down
9 changes: 8 additions & 1 deletion spec/lib/server/subscriber_spec.rb
Expand Up @@ -4,7 +4,14 @@
include EM::TestHelper

let(:channel_key) { '/bears/are/mean' }
let(:chan_sub) { Firehose::Server::ChannelSubscription.new(channel_key) }
let(:env) {
{
"REMOTE_ADDR" => "192.168.0.1",
"HTTP_REFERER"=> "http://localhost:80/test",
"HTTP_USER_AGENT" => "test/runner"
}
}
let(:chan_sub) { Firehose::Server::ChannelSubscription.new(channel_key, env) }
let(:subscriber) { Firehose::Server::Subscriber.new(Firehose::Server.redis.connection) }
let(:dummy_subscriber){ Firehose::Server::Subscriber.new(double('redis', :pubsub => double('pubsub', :subscribe => EM::DefaultDeferrable.new, :on => nil))) }
let(:message) { 'Raaaarrrrrr!!!!' }
Expand Down

0 comments on commit fc26538

Please sign in to comment.