2016-06-16 23:09:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module Auth
|
|
|
|
class IpRateLimiter
|
|
|
|
attr_reader :ip
|
|
|
|
|
|
|
|
def initialize(ip)
|
|
|
|
@ip = ip
|
|
|
|
@banned = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def enabled?
|
|
|
|
config.enabled
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def reset!
|
|
|
|
Rack::Attack::Allow2Ban.reset(ip, config)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def register_fail!
|
|
|
|
# Allow2Ban.filter will return false if this IP has not failed too often yet
|
|
|
|
@banned = Rack::Attack::Allow2Ban.filter(ip, config) do
|
|
|
|
# If we return false here, the failure for this IP is ignored by Allow2Ban
|
|
|
|
ip_can_be_banned?
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def banned?
|
|
|
|
@banned
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
private
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def config
|
|
|
|
Gitlab.config.rack_attack.git_basic_auth
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def ip_can_be_banned?
|
|
|
|
config.ip_whitelist.exclude?(ip)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|