2017-10-18 09:10:12 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-05 13:08:52 +08:00
|
|
|
require_dependency 'middleware/anonymous_cache'
|
|
|
|
|
|
|
|
class Middleware::RequestTracker
|
|
|
|
|
2017-10-18 09:10:12 +08:00
|
|
|
@@detailed_request_loggers = nil
|
|
|
|
|
|
|
|
# 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
|
|
|
|
require_dependency "method_profiler"
|
|
|
|
MethodProfiler.patch(PG::Connection, [
|
|
|
|
:exec, :async_exec, :exec_prepared, :send_query_prepared, :query
|
|
|
|
], :sql)
|
|
|
|
|
|
|
|
MethodProfiler.patch(Redis::Client, [
|
|
|
|
:call, :call_pipeline
|
|
|
|
], :redis)
|
|
|
|
@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
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def initialize(app, settings = {})
|
2015-02-05 13:08:52 +08:00
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.log_request_on_site(data, host)
|
2015-02-05 13:08:52 +08:00
|
|
|
RailsMultisite::ConnectionManagement.with_hostname(host) do
|
2015-02-10 14:03:33 +08:00
|
|
|
log_request(data)
|
2015-02-05 13:08:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-10 14:03:33 +08:00
|
|
|
def self.log_request(data)
|
|
|
|
status = data[:status]
|
|
|
|
track_view = data[:track_view]
|
2015-02-05 13:08:52 +08:00
|
|
|
|
2015-02-10 14:03:33 +08:00
|
|
|
if track_view
|
|
|
|
if data[:is_crawler]
|
2015-02-06 11:39:04 +08:00
|
|
|
ApplicationRequest.increment!(:page_view_crawler)
|
2015-02-10 14:03:33 +08:00
|
|
|
elsif data[:has_auth_cookie]
|
2015-02-06 11:39:04 +08:00
|
|
|
ApplicationRequest.increment!(:page_view_logged_in)
|
2015-07-04 05:02:57 +08:00
|
|
|
ApplicationRequest.increment!(:page_view_logged_in_mobile) if data[:is_mobile]
|
2015-02-05 13:08:52 +08:00
|
|
|
else
|
2015-02-06 11:39:04 +08:00
|
|
|
ApplicationRequest.increment!(:page_view_anon)
|
2015-07-04 05:02:57 +08:00
|
|
|
ApplicationRequest.increment!(:page_view_anon_mobile) if data[:is_mobile]
|
2015-02-06 11:39:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ApplicationRequest.increment!(:http_total)
|
|
|
|
|
|
|
|
if status >= 500
|
|
|
|
ApplicationRequest.increment!(:http_5xx)
|
2015-02-12 06:47:32 +08:00
|
|
|
elsif data[:is_background]
|
|
|
|
ApplicationRequest.increment!(:http_background)
|
2015-02-06 11:39:04 +08:00
|
|
|
elsif status >= 400
|
|
|
|
ApplicationRequest.increment!(:http_4xx)
|
|
|
|
elsif status >= 300
|
|
|
|
ApplicationRequest.increment!(:http_3xx)
|
2015-02-12 06:47:32 +08:00
|
|
|
elsif status >= 200 && status < 300
|
|
|
|
ApplicationRequest.increment!(:http_2xx)
|
2015-02-05 13:08:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-10-18 09:10:12 +08:00
|
|
|
def self.get_data(env, result, timing)
|
2017-07-28 09:20:09 +08:00
|
|
|
status, headers = result
|
2015-02-10 14:03:33 +08:00
|
|
|
status = status.to_i
|
|
|
|
|
|
|
|
helper = Middleware::AnonymousCache::Helper.new(env)
|
|
|
|
request = Rack::Request.new(env)
|
2015-02-26 08:40:57 +08:00
|
|
|
|
2017-10-18 09:10:12 +08:00
|
|
|
env_track_view = env["HTTP_DISCOURSE_TRACK_VIEW"]
|
2015-02-26 08:40:57 +08:00
|
|
|
track_view = status == 200
|
2017-10-18 09:10:12 +08: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-17 09:06:21 +08:00
|
|
|
track_view = !!track_view
|
2015-02-26 08:40:57 +08:00
|
|
|
|
2015-02-10 14:03:33 +08:00
|
|
|
{
|
|
|
|
status: status,
|
|
|
|
is_crawler: helper.is_crawler?,
|
|
|
|
has_auth_cookie: helper.has_auth_cookie?,
|
|
|
|
is_background: request.path =~ /^\/message-bus\// || request.path == /\/topics\/timings/,
|
2015-07-04 05:02:57 +08:00
|
|
|
is_mobile: helper.is_mobile?,
|
2017-10-18 09:10:12 +08:00
|
|
|
track_view: track_view,
|
|
|
|
timing: timing
|
2015-02-10 14:03:33 +08:00
|
|
|
}
|
2017-10-18 09:10:12 +08:00
|
|
|
|
2015-02-10 14:03:33 +08:00
|
|
|
end
|
2015-02-05 13:08:52 +08:00
|
|
|
|
2017-11-28 13:47:20 +08:00
|
|
|
def log_request_info(env, result, info)
|
2015-02-10 14:03:33 +08:00
|
|
|
|
|
|
|
# we got to skip this on error ... its just logging
|
2017-10-18 09:10:12 +08:00
|
|
|
data = self.class.get_data(env, result, info) rescue nil
|
2015-02-10 14:05:24 +08:00
|
|
|
host = RailsMultisite::ConnectionManagement.host(env)
|
2015-02-10 14:03:33 +08:00
|
|
|
|
|
|
|
if data
|
2017-07-28 09:20:09 +08:00
|
|
|
if result && (headers = result[1])
|
2016-10-27 15:08:01 +08:00
|
|
|
headers["X-Discourse-TrackView"] = "1" if data[:track_view]
|
2016-10-27 13:50:56 +08:00
|
|
|
end
|
2017-10-18 09:10:12 +08:00
|
|
|
|
|
|
|
if @@detailed_request_loggers
|
|
|
|
@@detailed_request_loggers.each { |logger| logger.call(env, data) }
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
log_later(data, host)
|
2015-02-05 13:08:52 +08:00
|
|
|
end
|
2015-02-10 14:03:33 +08:00
|
|
|
|
2015-02-05 13:08:52 +08:00
|
|
|
end
|
|
|
|
|
2017-11-28 13:47:20 +08:00
|
|
|
def call(env)
|
2017-12-11 14:21:00 +08:00
|
|
|
result = nil
|
|
|
|
|
|
|
|
if rate_limit(env)
|
|
|
|
result = [429, {}, ["Slow down, too Many Requests from this IP Address"]]
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2017-11-28 13:47:20 +08:00
|
|
|
env["discourse.request_tracker"] = self
|
|
|
|
MethodProfiler.start if @@detailed_request_loggers
|
|
|
|
result = @app.call(env)
|
|
|
|
info = MethodProfiler.stop if @@detailed_request_loggers
|
|
|
|
result
|
|
|
|
ensure
|
|
|
|
log_request_info(env, result, info) unless env["discourse.request_tracker.skip"]
|
|
|
|
end
|
|
|
|
|
2017-12-11 14:21:00 +08:00
|
|
|
def rate_limit(env)
|
|
|
|
if (
|
|
|
|
GlobalSetting.max_requests_per_ip_mode == "block" ||
|
|
|
|
GlobalSetting.max_requests_per_ip_mode == "warn"
|
|
|
|
)
|
|
|
|
|
|
|
|
ip = Rack::Request.new(env).ip
|
|
|
|
|
|
|
|
limiter10 = RateLimiter.new(
|
|
|
|
nil,
|
|
|
|
"global_ip_limit_10_#{ip}",
|
|
|
|
GlobalSetting.max_requests_per_ip_per_10_seconds,
|
|
|
|
10,
|
|
|
|
global: true
|
|
|
|
)
|
|
|
|
|
|
|
|
limiter60 = RateLimiter.new(
|
|
|
|
nil,
|
|
|
|
"global_ip_limit_60_#{ip}",
|
|
|
|
GlobalSetting.max_requests_per_ip_per_10_seconds,
|
|
|
|
10,
|
|
|
|
global: true
|
|
|
|
)
|
|
|
|
|
|
|
|
type = 10
|
|
|
|
begin
|
|
|
|
limiter10.performed!
|
|
|
|
type = 60
|
|
|
|
limiter60.performed!
|
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
if GlobalSetting.max_requests_per_ip_mode == "warn"
|
2017-12-20 06:51:23 +08:00
|
|
|
Rails.logger.warn("Global IP rate limit exceeded for #{ip}: #{type} second rate limit")
|
2017-12-11 14:21:00 +08:00
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def log_later(data, host)
|
|
|
|
Scheduler::Defer.later("Track view", _db = nil) do
|
|
|
|
self.class.log_request_on_site(data, host)
|
2015-09-17 09:06:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-05 13:08:52 +08:00
|
|
|
end
|