2017-10-17 21:10:12 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-05 00:08:52 -05:00
|
|
|
require_dependency 'middleware/anonymous_cache'
|
2018-01-18 16:26:18 -05:00
|
|
|
require_dependency 'method_profiler'
|
2015-02-05 00:08:52 -05:00
|
|
|
|
|
|
|
class Middleware::RequestTracker
|
|
|
|
|
2017-10-17 21:10:12 -04:00
|
|
|
@@detailed_request_loggers = nil
|
2018-02-05 17:45:25 -05:00
|
|
|
@@ip_skipper = nil
|
2017-10-17 21:10:12 -04:00
|
|
|
|
|
|
|
# register callbacks for detailed request loggers called on every request
|
|
|
|
# example:
|
|
|
|
#
|
|
|
|
# Middleware::RequestTracker.detailed_request_logger(->|env, data| do
|
|
|
|
# # do stuff with env and data
|
|
|
|
# end
|
|
|
|
def self.register_detailed_request_logger(callback)
|
|
|
|
|
|
|
|
unless @patched_instrumentation
|
|
|
|
MethodProfiler.patch(PG::Connection, [
|
|
|
|
:exec, :async_exec, :exec_prepared, :send_query_prepared, :query
|
|
|
|
], :sql)
|
|
|
|
|
|
|
|
MethodProfiler.patch(Redis::Client, [
|
|
|
|
:call, :call_pipeline
|
|
|
|
], :redis)
|
2018-02-20 23:19:59 -05:00
|
|
|
|
|
|
|
MethodProfiler.patch(Net::HTTP, [
|
|
|
|
:request
|
2018-02-27 18:45:11 -05:00
|
|
|
], :net, no_recurse: true)
|
2018-02-20 23:19:59 -05:00
|
|
|
|
|
|
|
MethodProfiler.patch(Excon::Connection, [
|
|
|
|
:request
|
|
|
|
], :net)
|
2017-10-17 21:10:12 -04:00
|
|
|
@patched_instrumentation = true
|
|
|
|
end
|
|
|
|
|
|
|
|
(@@detailed_request_loggers ||= []) << callback
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.unregister_detailed_request_logger(callback)
|
|
|
|
@@detailed_request_loggers.delete callback
|
|
|
|
|
|
|
|
if @@detailed_request_loggers.length == 0
|
|
|
|
@detailed_request_loggers = nil
|
|
|
|
end
|
2018-02-05 17:45:25 -05:00
|
|
|
end
|
2017-10-17 21:10:12 -04:00
|
|
|
|
2018-02-05 18:38:15 -05:00
|
|
|
# used for testing
|
|
|
|
def self.unregister_ip_skipper
|
|
|
|
@@ip_skipper = nil
|
|
|
|
end
|
|
|
|
|
2018-02-05 17:45:25 -05:00
|
|
|
# Register a custom `ip_skipper`, a function that will skip rate limiting
|
|
|
|
# for any IP that returns true.
|
|
|
|
#
|
|
|
|
# For example, if you never wanted to rate limit 1.2.3.4
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# Middleware::RequestTracker.register_ip_skipper do |ip|
|
|
|
|
# ip == "1.2.3.4"
|
|
|
|
# end
|
|
|
|
# ```
|
|
|
|
def self.register_ip_skipper(&blk)
|
2018-02-05 18:38:15 -05:00
|
|
|
raise "IP skipper is already registered!" if @@ip_skipper
|
2018-02-05 17:45:25 -05:00
|
|
|
@@ip_skipper = blk
|
2017-10-17 21:10:12 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(app, settings = {})
|
2015-02-05 00:08:52 -05:00
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.log_request_on_site(data, host)
|
2015-02-05 00:08:52 -05:00
|
|
|
RailsMultisite::ConnectionManagement.with_hostname(host) do
|
2015-02-10 01:03:33 -05:00
|
|
|
log_request(data)
|
2015-02-05 00:08:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-10 01:03:33 -05:00
|
|
|
def self.log_request(data)
|
|
|
|
status = data[:status]
|
|
|
|
track_view = data[:track_view]
|
2015-02-05 00:08:52 -05:00
|
|
|
|
2015-02-10 01:03:33 -05:00
|
|
|
if track_view
|
|
|
|
if data[:is_crawler]
|
2015-02-05 22:39:04 -05:00
|
|
|
ApplicationRequest.increment!(:page_view_crawler)
|
2015-02-10 01:03:33 -05:00
|
|
|
elsif data[:has_auth_cookie]
|
2015-02-05 22:39:04 -05:00
|
|
|
ApplicationRequest.increment!(:page_view_logged_in)
|
2015-07-03 17:02:57 -04:00
|
|
|
ApplicationRequest.increment!(:page_view_logged_in_mobile) if data[:is_mobile]
|
2015-02-05 00:08:52 -05:00
|
|
|
else
|
2015-02-05 22:39:04 -05:00
|
|
|
ApplicationRequest.increment!(:page_view_anon)
|
2015-07-03 17:02:57 -04:00
|
|
|
ApplicationRequest.increment!(:page_view_anon_mobile) if data[:is_mobile]
|
2015-02-05 22:39:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ApplicationRequest.increment!(:http_total)
|
|
|
|
|
|
|
|
if status >= 500
|
|
|
|
ApplicationRequest.increment!(:http_5xx)
|
2015-02-11 17:47:32 -05:00
|
|
|
elsif data[:is_background]
|
|
|
|
ApplicationRequest.increment!(:http_background)
|
2015-02-05 22:39:04 -05:00
|
|
|
elsif status >= 400
|
|
|
|
ApplicationRequest.increment!(:http_4xx)
|
|
|
|
elsif status >= 300
|
|
|
|
ApplicationRequest.increment!(:http_3xx)
|
2015-02-11 17:47:32 -05:00
|
|
|
elsif status >= 200 && status < 300
|
|
|
|
ApplicationRequest.increment!(:http_2xx)
|
2015-02-05 00:08:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-10-17 21:10:12 -04:00
|
|
|
def self.get_data(env, result, timing)
|
2017-07-27 21:20:09 -04:00
|
|
|
status, headers = result
|
2015-02-10 01:03:33 -05:00
|
|
|
status = status.to_i
|
|
|
|
|
|
|
|
helper = Middleware::AnonymousCache::Helper.new(env)
|
|
|
|
request = Rack::Request.new(env)
|
2015-02-25 19:40:57 -05:00
|
|
|
|
2017-10-17 21:10:12 -04:00
|
|
|
env_track_view = env["HTTP_DISCOURSE_TRACK_VIEW"]
|
2015-02-25 19:40:57 -05:00
|
|
|
track_view = status == 200
|
2017-10-17 21:10:12 -04:00
|
|
|
track_view &&= env_track_view != "0" && env_track_view != "false"
|
|
|
|
track_view &&= env_track_view || (request.get? && !request.xhr? && headers["Content-Type"] =~ /text\/html/)
|
2015-09-16 21:06:21 -04:00
|
|
|
track_view = !!track_view
|
2015-02-25 19:40:57 -05:00
|
|
|
|
2015-02-10 01:03:33 -05:00
|
|
|
{
|
|
|
|
status: status,
|
|
|
|
is_crawler: helper.is_crawler?,
|
|
|
|
has_auth_cookie: helper.has_auth_cookie?,
|
2018-01-18 18:37:27 -05:00
|
|
|
is_background: !!(request.path =~ /^\/message-bus\// || request.path =~ /\/topics\/timings/),
|
2015-07-03 17:02:57 -04:00
|
|
|
is_mobile: helper.is_mobile?,
|
2017-10-17 21:10:12 -04:00
|
|
|
track_view: track_view,
|
|
|
|
timing: timing
|
2015-02-10 01:03:33 -05:00
|
|
|
}
|
2017-10-17 21:10:12 -04:00
|
|
|
|
2015-02-10 01:03:33 -05:00
|
|
|
end
|
2015-02-05 00:08:52 -05:00
|
|
|
|
2017-11-28 00:47:20 -05:00
|
|
|
def log_request_info(env, result, info)
|
2015-02-10 01:03:33 -05:00
|
|
|
|
|
|
|
# we got to skip this on error ... its just logging
|
2017-10-17 21:10:12 -04:00
|
|
|
data = self.class.get_data(env, result, info) rescue nil
|
2015-02-10 01:05:24 -05:00
|
|
|
host = RailsMultisite::ConnectionManagement.host(env)
|
2015-02-10 01:03:33 -05:00
|
|
|
|
|
|
|
if data
|
2017-07-27 21:20:09 -04:00
|
|
|
if result && (headers = result[1])
|
2016-10-27 03:08:01 -04:00
|
|
|
headers["X-Discourse-TrackView"] = "1" if data[:track_view]
|
2016-10-27 01:50:56 -04:00
|
|
|
end
|
2017-10-17 21:10:12 -04:00
|
|
|
|
|
|
|
if @@detailed_request_loggers
|
|
|
|
@@detailed_request_loggers.each { |logger| logger.call(env, data) }
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
log_later(data, host)
|
2015-02-05 00:08:52 -05:00
|
|
|
end
|
2015-02-10 01:03:33 -05:00
|
|
|
|
2015-02-05 00:08:52 -05:00
|
|
|
end
|
|
|
|
|
2017-11-28 00:47:20 -05:00
|
|
|
def call(env)
|
2017-12-11 01:21:00 -05:00
|
|
|
result = nil
|
|
|
|
|
|
|
|
if rate_limit(env)
|
|
|
|
result = [429, {}, ["Slow down, too Many Requests from this IP Address"]]
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2017-11-28 00:47:20 -05:00
|
|
|
env["discourse.request_tracker"] = self
|
2018-01-18 16:26:18 -05:00
|
|
|
MethodProfiler.start
|
2017-11-28 00:47:20 -05:00
|
|
|
result = @app.call(env)
|
2018-01-18 16:26:18 -05:00
|
|
|
info = MethodProfiler.stop
|
|
|
|
# possibly transferred?
|
2018-01-19 01:51:04 -05:00
|
|
|
if info && (headers = result[1])
|
|
|
|
headers["X-Runtime"] = "%0.6f" % info[:total_duration]
|
2018-01-18 16:26:18 -05:00
|
|
|
end
|
2018-03-06 00:49:31 -05:00
|
|
|
|
|
|
|
if env[Auth::DefaultCurrentUserProvider::BAD_TOKEN] && (headers = result[1])
|
|
|
|
headers['Discourse-Logged-Out'] = '1'
|
|
|
|
end
|
|
|
|
|
2017-11-28 00:47:20 -05:00
|
|
|
result
|
|
|
|
ensure
|
2018-03-05 23:20:39 -05:00
|
|
|
if (limiters = env['DISCOURSE_RATE_LIMITERS']) && env['DISCOURSE_IS_ASSET_PATH']
|
|
|
|
limiters.each(&:rollback!)
|
|
|
|
env['DISCOURSE_ASSET_RATE_LIMITERS'].each do |limiter|
|
|
|
|
begin
|
|
|
|
limiter.performed!
|
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
# skip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-11-28 00:47:20 -05:00
|
|
|
log_request_info(env, result, info) unless env["discourse.request_tracker.skip"]
|
|
|
|
end
|
|
|
|
|
2018-02-12 19:53:36 -05:00
|
|
|
PRIVATE_IP ||= /^(127\.)|(192\.168\.)|(10\.)|(172\.1[6-9]\.)|(172\.2[0-9]\.)|(172\.3[0-1]\.)|(::1$)|([fF][cCdD])/
|
2018-01-07 16:39:17 -05:00
|
|
|
|
|
|
|
def is_private_ip?(ip)
|
|
|
|
ip = IPAddr.new(ip) rescue nil
|
|
|
|
!!(ip && ip.to_s.match?(PRIVATE_IP))
|
|
|
|
end
|
|
|
|
|
2017-12-11 01:21:00 -05:00
|
|
|
def rate_limit(env)
|
2018-01-07 16:39:17 -05:00
|
|
|
|
2017-12-11 01:21:00 -05:00
|
|
|
if (
|
2018-01-21 21:18:30 -05:00
|
|
|
GlobalSetting.max_reqs_per_ip_mode == "block" ||
|
|
|
|
GlobalSetting.max_reqs_per_ip_mode == "warn" ||
|
|
|
|
GlobalSetting.max_reqs_per_ip_mode == "warn+block"
|
2017-12-11 01:21:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
ip = Rack::Request.new(env).ip
|
|
|
|
|
2018-01-21 21:18:30 -05:00
|
|
|
if !GlobalSetting.max_reqs_rate_limit_on_private
|
2018-01-07 16:39:17 -05:00
|
|
|
return false if is_private_ip?(ip)
|
|
|
|
end
|
|
|
|
|
2018-02-05 18:38:15 -05:00
|
|
|
return false if @@ip_skipper&.call(ip)
|
2018-02-05 17:45:25 -05:00
|
|
|
|
2017-12-11 01:21:00 -05:00
|
|
|
limiter10 = RateLimiter.new(
|
|
|
|
nil,
|
|
|
|
"global_ip_limit_10_#{ip}",
|
2018-01-21 21:18:30 -05:00
|
|
|
GlobalSetting.max_reqs_per_ip_per_10_seconds,
|
2017-12-11 01:21:00 -05:00
|
|
|
10,
|
|
|
|
global: true
|
|
|
|
)
|
|
|
|
|
|
|
|
limiter60 = RateLimiter.new(
|
|
|
|
nil,
|
|
|
|
"global_ip_limit_60_#{ip}",
|
2018-01-21 21:18:30 -05:00
|
|
|
GlobalSetting.max_reqs_per_ip_per_10_seconds,
|
2017-12-11 01:21:00 -05:00
|
|
|
10,
|
|
|
|
global: true
|
|
|
|
)
|
|
|
|
|
2018-03-05 23:20:39 -05:00
|
|
|
limiter_assets10 = RateLimiter.new(
|
|
|
|
nil,
|
|
|
|
"global_ip_limit_10_assets_#{ip}",
|
|
|
|
GlobalSetting.max_asset_reqs_per_ip_per_10_seconds,
|
|
|
|
10,
|
|
|
|
global: true
|
|
|
|
)
|
|
|
|
|
|
|
|
env['DISCOURSE_RATE_LIMITERS'] = [limiter10, limiter60]
|
|
|
|
env['DISCOURSE_ASSET_RATE_LIMITERS'] = [limiter_assets10]
|
|
|
|
|
|
|
|
warn = GlobalSetting.max_reqs_per_ip_mode == "warn" ||
|
|
|
|
GlobalSetting.max_reqs_per_ip_mode == "warn+block"
|
|
|
|
|
|
|
|
if !limiter_assets10.can_perform?
|
|
|
|
if warn
|
|
|
|
Rails.logger.warn("Global asset IP rate limit exceeded for #{ip}: 10 second rate limit, uri: #{env["REQUEST_URI"]}")
|
|
|
|
end
|
|
|
|
|
|
|
|
return !(GlobalSetting.max_reqs_per_ip_mode == "warn")
|
|
|
|
end
|
|
|
|
|
2017-12-11 01:21:00 -05:00
|
|
|
type = 10
|
|
|
|
begin
|
|
|
|
limiter10.performed!
|
|
|
|
type = 60
|
|
|
|
limiter60.performed!
|
|
|
|
rescue RateLimiter::LimitExceeded
|
2018-03-05 23:20:39 -05:00
|
|
|
if warn
|
2018-01-04 20:14:28 -05:00
|
|
|
Rails.logger.warn("Global IP rate limit exceeded for #{ip}: #{type} second rate limit, uri: #{env["REQUEST_URI"]}")
|
2018-01-21 21:18:30 -05:00
|
|
|
!(GlobalSetting.max_reqs_per_ip_mode == "warn")
|
2017-12-11 01:21:00 -05:00
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def log_later(data, host)
|
|
|
|
Scheduler::Defer.later("Track view", _db = nil) do
|
|
|
|
self.class.log_request_on_site(data, host)
|
2015-09-16 21:06:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-05 00:08:52 -05:00
|
|
|
end
|