Class: Shugoi::Core
- Inherits:
-
Object
- Object
- Shugoi::Core
- Defined in:
- lib/shugoi/core.rb
Defined Under Namespace
Classes: Decision
Constant Summary collapse
- POW_OK_TTL_MS =
30 * 24 * 3600 * 1000
- POW_TTL_MS =
60_000
Instance Attribute Summary collapse
-
#bot_policy ⇒ Object
readonly
Returns the value of attribute bot_policy.
Instance Method Summary collapse
- #challenge_page(ctx) ⇒ Object
- #evaluate(ctx) ⇒ Object
-
#initialize(config, pow, api_client, config_cache = nil) ⇒ Core
constructor
A new instance of Core.
Constructor Details
#initialize(config, pow, api_client, config_cache = nil) ⇒ Core
Returns a new instance of Core.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/shugoi/core.rb', line 9 def initialize(config, pow, api_client, config_cache = nil) @config = config @pow = pow @api_client = api_client @config_cache = config_cache @key_validator = KeyValidator.new(config, api_client) @used_proofs = {} @mutex = Mutex.new @challenge_limiter = ChallengeLimiter.new @bot_policy = BotPolicy.new(config) end |
Instance Attribute Details
#bot_policy ⇒ Object (readonly)
Returns the value of attribute bot_policy.
3 4 5 |
# File 'lib/shugoi/core.rb', line 3 def bot_policy @bot_policy end |
Instance Method Details
#challenge_page(ctx) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/shugoi/core.rb', line 64 def challenge_page(ctx) loc = Locales.resolve_locale(@config.locale, ctx[:accept_language]) unless @challenge_limiter.allow?(ctx[:ip].to_s) msgs = Locales.(loc) return rate_limit_decision(ctx, msgs, loc, 60, "1 min") end js = ChallengeScript.render(@config.pow_difficulty) html = "<!--\n#{BLOCK_PAGE}-->\n<script>#{js}</script>" Decision.new(status: 200, content_type: "text/html", body: html, headers: {}) end |
#evaluate(ctx) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/shugoi/core.rb', line 21 def evaluate(ctx) path = ctx[:path].to_s if path.match?(%r{/assets/[^?#]+\.(js|css)(\?|$)}) auth_ok = !ctx[:sg_authorized].to_s.empty? && @pow.(ctx[:sg_authorized]) return Decision.new(status: 403, content_type: "text/plain", body: BLOCK_PAGE, headers: {}) unless auth_ok end @key_validator.validate @key_validator.warn_if_failed return nil if @config.allowlisted?(path) return challenge_page(ctx) if path == "/__sg_challenge" ua = ctx[:ua].to_s return nil if path.include?("/__shugoi/") || path.start_with?("/api/") preflight = preflight_decision(ctx, ua) return preflight if preflight flags = @config_cache ? @config_cache.fetch(@config.site_key)[:flags] : {} headless_enabled = flags["enableHeadlessCheck"] != false if flags["enableRateLimit"] == true rl = @api_client.check_rate_limit(@config.site_key, ctx[:ip].to_s, ctx[:ua].to_s) if rl && rl["allowed"] == false reset_at = rl["resetAt"].to_i reset_at = (reset_at / 1000.0).ceil if reset_at > 1_000_000_000_000 remain = [0, reset_at - Utils.now_sec].max time_str = RateLimitFormatter.remaining(remain) loc = @config.locale || Locales.resolve_locale(nil, ctx[:accept_language]) msgs = Locales.(loc) return rate_limit_decision(ctx, msgs, loc, remain, time_str) end end if headless_enabled && !ua.empty? && !@bot_policy.trusted?(ua, ctx[:ip].to_s) && @config.headless?(ua) @api_client.post_event(@config.site_key, "headless", "") return Decision.new(status: @config.block_status, content_type: "text/plain", body: BLOCK_PAGE, headers: {}) end nil end |