2018-04-18 14:58:40 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-09 11:08:42 +08:00
|
|
|
require_dependency "mobile_detection"
|
2014-04-29 08:48:09 +08:00
|
|
|
require_dependency "crawler_detection"
|
2017-06-16 02:09:33 +08:00
|
|
|
require_dependency "guardian"
|
2014-01-09 11:08:42 +08:00
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
module Middleware
|
|
|
|
class AnonymousCache
|
|
|
|
|
|
|
|
def self.anon_cache(env, duration)
|
|
|
|
env["ANON_CACHE_DURATION"] = duration
|
|
|
|
end
|
|
|
|
|
|
|
|
class Helper
|
2018-04-18 14:58:40 +08:00
|
|
|
USER_AGENT = "HTTP_USER_AGENT"
|
|
|
|
RACK_SESSION = "rack.session"
|
|
|
|
ACCEPT_ENCODING = "HTTP_ACCEPT_ENCODING"
|
2014-04-29 08:48:09 +08:00
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
def initialize(env)
|
|
|
|
@env = env
|
2017-06-16 02:09:33 +08:00
|
|
|
@request = Rack::Request.new(@env)
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
2018-07-04 09:14:43 +08:00
|
|
|
def blocked_crawler?
|
|
|
|
@request.get? &&
|
|
|
|
!@request.xhr? &&
|
|
|
|
!@request.path.ends_with?('robots.txt') &&
|
2018-07-18 10:33:06 +08:00
|
|
|
!@request.path.ends_with?('srv/status') &&
|
2018-09-15 03:34:21 +08:00
|
|
|
@request[Auth::DefaultCurrentUserProvider::API_KEY].nil? &&
|
|
|
|
@env[Auth::DefaultCurrentUserProvider::USER_API_KEY].nil? &&
|
2018-07-04 09:14:43 +08:00
|
|
|
CrawlerDetection.is_blocked_crawler?(@request.env['HTTP_USER_AGENT'])
|
|
|
|
end
|
|
|
|
|
2014-01-09 12:11:04 +08:00
|
|
|
def is_mobile=(val)
|
|
|
|
@is_mobile = val ? :true : :false
|
|
|
|
end
|
|
|
|
|
2014-01-09 11:08:42 +08:00
|
|
|
def is_mobile?
|
|
|
|
@is_mobile ||=
|
|
|
|
begin
|
2014-04-29 08:48:09 +08:00
|
|
|
session = @env[RACK_SESSION]
|
2014-01-09 13:49:12 +08:00
|
|
|
# don't initialize params until later otherwise
|
|
|
|
# you get a broken params on the request
|
|
|
|
params = {}
|
2017-07-28 09:20:09 +08:00
|
|
|
user_agent = @env[USER_AGENT]
|
2014-01-09 11:08:42 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
MobileDetection.resolve_mobile_view!(user_agent, params, session) ? :true : :false
|
2014-04-29 08:48:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@is_mobile == :true
|
|
|
|
end
|
|
|
|
|
2016-12-05 10:57:09 +08:00
|
|
|
def has_brotli?
|
|
|
|
@has_brotli ||=
|
|
|
|
begin
|
|
|
|
@env[ACCEPT_ENCODING].to_s =~ /br/ ? :true : :false
|
|
|
|
end
|
|
|
|
@has_brotli == :true
|
|
|
|
end
|
|
|
|
|
2014-04-29 08:48:09 +08:00
|
|
|
def is_crawler?
|
|
|
|
@is_crawler ||=
|
|
|
|
begin
|
2017-07-28 09:20:09 +08:00
|
|
|
user_agent = @env[USER_AGENT]
|
2014-04-29 08:48:09 +08:00
|
|
|
CrawlerDetection.crawler?(user_agent) ? :true : :false
|
|
|
|
end
|
|
|
|
@is_crawler == :true
|
2014-01-09 11:08:42 +08:00
|
|
|
end
|
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
def cache_key
|
2018-08-08 12:46:34 +08:00
|
|
|
@cache_key ||= "ANON_CACHE_#{@env["HTTP_ACCEPT"]}_#{@env["HTTP_HOST"]}#{@env["REQUEST_URI"]}|m=#{is_mobile?}|c=#{is_crawler?}|b=#{has_brotli?}|t=#{theme_ids.join(",")}"
|
2017-06-15 21:36:27 +08:00
|
|
|
end
|
|
|
|
|
2018-08-08 12:46:34 +08:00
|
|
|
def theme_ids
|
2018-07-12 12:18:21 +08:00
|
|
|
ids, _ = @request.cookies['theme_ids']&.split('|')
|
|
|
|
ids = ids&.split(",")&.map(&:to_i)
|
|
|
|
if ids && Guardian.new.allow_themes?(ids)
|
2018-08-08 12:46:34 +08:00
|
|
|
Theme.transform_ids(ids)
|
2017-06-15 21:36:27 +08:00
|
|
|
else
|
2018-08-08 12:46:34 +08:00
|
|
|
[]
|
2017-06-15 21:36:27 +08:00
|
|
|
end
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def cache_key_body
|
|
|
|
@cache_key_body ||= "#{cache_key}_body"
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_key_other
|
|
|
|
@cache_key_other || "#{cache_key}_other"
|
|
|
|
end
|
|
|
|
|
|
|
|
def get?
|
|
|
|
@env["REQUEST_METHOD"] == "GET"
|
|
|
|
end
|
|
|
|
|
2015-02-04 13:14:56 +08:00
|
|
|
def has_auth_cookie?
|
|
|
|
CurrentUser.has_auth_cookie?(@env)
|
|
|
|
end
|
|
|
|
|
2015-10-29 05:16:56 +08:00
|
|
|
def no_cache_bypass
|
|
|
|
request = Rack::Request.new(@env)
|
2018-04-18 14:58:40 +08:00
|
|
|
request.cookies['_bypass_cache'].nil? &&
|
|
|
|
request[Auth::DefaultCurrentUserProvider::API_KEY].nil? &&
|
|
|
|
@env[Auth::DefaultCurrentUserProvider::USER_API_KEY].nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def force_anonymous!
|
|
|
|
@env[Auth::DefaultCurrentUserProvider::USER_API_KEY] = nil
|
|
|
|
@env['HTTP_COOKIE'] = nil
|
|
|
|
@env['rack.request.cookie.hash'] = {}
|
|
|
|
@env['rack.request.cookie.string'] = ''
|
|
|
|
@env['_bypass_cache'] = nil
|
|
|
|
request = Rack::Request.new(@env)
|
|
|
|
request.delete_param('api_username')
|
|
|
|
request.delete_param('api_key')
|
|
|
|
end
|
|
|
|
|
2018-04-23 09:54:58 +08:00
|
|
|
def logged_in_anon_limiter
|
|
|
|
@logged_in_anon_limiter ||= RateLimiter.new(
|
2018-04-18 14:58:40 +08:00
|
|
|
nil,
|
|
|
|
"logged_in_anon_cache_#{@env["HOST"]}/#{@env["REQUEST_URI"]}",
|
|
|
|
GlobalSetting.force_anonymous_min_per_10_seconds,
|
|
|
|
10
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-04-23 09:54:58 +08:00
|
|
|
def check_logged_in_rate_limit!
|
|
|
|
!logged_in_anon_limiter.performed!(raise_error: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
MIN_TIME_TO_CHECK = 0.05
|
|
|
|
|
2018-04-18 14:58:40 +08:00
|
|
|
def should_force_anonymous?
|
2018-04-23 09:54:58 +08:00
|
|
|
if (queue_time = @env['REQUEST_QUEUE_SECONDS']) && get?
|
|
|
|
if queue_time > GlobalSetting.force_anonymous_min_queue_seconds
|
2018-04-18 14:58:40 +08:00
|
|
|
return check_logged_in_rate_limit!
|
2018-04-23 09:54:58 +08:00
|
|
|
elsif queue_time >= MIN_TIME_TO_CHECK
|
|
|
|
if !logged_in_anon_limiter.can_perform?
|
|
|
|
return check_logged_in_rate_limit!
|
|
|
|
end
|
2018-04-18 14:58:40 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2015-10-29 05:16:56 +08:00
|
|
|
end
|
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
def cacheable?
|
2015-10-29 05:16:56 +08:00
|
|
|
!!(!has_auth_cookie? && get? && no_cache_bypass)
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def cached
|
|
|
|
if body = $redis.get(cache_key_body)
|
|
|
|
if other = $redis.get(cache_key_other)
|
|
|
|
other = JSON.parse(other)
|
|
|
|
[other[0], other[1], [body]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_duration
|
|
|
|
@env["ANON_CACHE_DURATION"]
|
|
|
|
end
|
|
|
|
|
|
|
|
# NOTE in an ideal world cache still serves out cached content except for one magic worker
|
|
|
|
# that fills it up, this avoids a herd killing you, we can probably do this using a job or redis tricks
|
|
|
|
# but coordinating this is tricky
|
|
|
|
def cache(result)
|
2017-07-28 09:20:09 +08:00
|
|
|
status, headers, response = result
|
2013-10-16 13:39:18 +08:00
|
|
|
|
|
|
|
if status == 200 && cache_duration
|
2017-07-28 09:20:09 +08:00
|
|
|
headers_stripped = headers.dup.delete_if { |k, _| ["Set-Cookie", "X-MiniProfiler-Ids"].include? k }
|
2015-06-16 08:30:06 +08:00
|
|
|
headers_stripped["X-Discourse-Cached"] = "true"
|
2013-10-16 13:39:18 +08:00
|
|
|
parts = []
|
|
|
|
response.each do |part|
|
|
|
|
parts << part
|
|
|
|
end
|
|
|
|
|
|
|
|
$redis.setex(cache_key_body, cache_duration, parts.join)
|
2017-07-28 09:20:09 +08:00
|
|
|
$redis.setex(cache_key_other, cache_duration, [status, headers_stripped].to_json)
|
2013-10-16 13:39:18 +08:00
|
|
|
else
|
|
|
|
parts = response
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
[status, headers, parts]
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def clear_cache
|
|
|
|
$redis.del(cache_key_body)
|
|
|
|
$redis.del(cache_key_other)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def initialize(app, settings = {})
|
2013-10-16 13:39:18 +08:00
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
helper = Helper.new(env)
|
2018-04-18 14:58:40 +08:00
|
|
|
force_anon = false
|
|
|
|
|
2018-07-04 09:14:43 +08:00
|
|
|
if helper.blocked_crawler?
|
|
|
|
env["discourse.request_tracker.skip"] = true
|
2018-09-15 03:39:24 +08:00
|
|
|
return [403, {}, ["Crawler is not allowed!"]]
|
2018-07-04 09:14:43 +08:00
|
|
|
end
|
|
|
|
|
2018-04-18 14:58:40 +08:00
|
|
|
if helper.should_force_anonymous?
|
|
|
|
force_anon = env["DISCOURSE_FORCE_ANON"] = true
|
|
|
|
helper.force_anonymous!
|
|
|
|
end
|
2013-10-16 13:39:18 +08:00
|
|
|
|
2018-04-18 14:58:40 +08:00
|
|
|
result =
|
|
|
|
if helper.cacheable?
|
|
|
|
helper.cached || helper.cache(@app.call(env))
|
|
|
|
else
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
if force_anon
|
2018-04-24 11:24:26 +08:00
|
|
|
result[1]["Set-Cookie"] = "dosp=1; Path=/"
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
2018-04-18 14:58:40 +08:00
|
|
|
result
|
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|