Module: T1000

Extended by:
Forwardable, T1000
Included in:
T1000
Defined in:
lib/t-1000.rb,
lib/t-1000/lock.rb,
lib/t-1000/cache.rb,
lib/t-1000/request.rb,
lib/t-1000/version.rb,
lib/t-1000/response.rb,
lib/t-1000/middleware.rb,
lib/t-1000/transaction.rb,
lib/t-1000/transaction_list.rb

Defined Under Namespace

Modules: Refinements Classes: Cache, Lock, Middleware, Request, Response, Transaction, TransactionList

Constant Summary collapse

VERSION =
"0.2.0"

Instance Method Summary collapse

Instance Method Details

#allow_ips(*ips, name: nil, &block) ⇒ Object



61
62
63
64
65
66
# File 'lib/t-1000.rb', line 61

def allow_ips(*ips, name: nil, &block)
  block ||= proc { true }
  whitelist(humanize __method__, name, *ips) do |req|
    ips.include?(req.ip) && block.call(req)
  end
end

#allow_localhostObject



50
51
52
# File 'lib/t-1000.rb', line 50

def allow_localhost
  allow_ips '::1', '127.0.0.1'
end

#allow_user_agents(*user_agents, name: nil, &block) ⇒ Object



97
98
99
100
101
102
# File 'lib/t-1000.rb', line 97

def allow_user_agents(*user_agents, name: nil, &block)
  block ||= proc { true }
  whitelist(humanize __method__, name, *user_agents) do
    user_agents.match_any?(req.user_agent) && block.call(req)
  end
end

#blacklisted_response(&block) ⇒ Object



111
112
113
# File 'lib/t-1000.rb', line 111

def blacklisted_response(&block)
  Middleware.blacklisted_response = block
end

#block_denial_of_service(name: nil, within: 30, requests: 1_000, lock_for: 6000, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/t-1000.rb', line 79

def block_denial_of_service(name: nil, within: 30, requests: 1_000, lock_for: 6000, &block)
  block ||= proc { true }
  blacklist(humanize __method__, name) do |req|
    reqs    = TransactionList.for_ip(req.ip, timeout: within)
    ip_lock = Lock.new(req.ip)
    ip_lock.lock! lock_for if reqs.within(within).count > requests &&
      block.call(req)
    ip_lock.locked?
  end
end

#block_failures(name: nil, within: 30, retries: 10, lock_for: 6000, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/t-1000.rb', line 68

def block_failures(name: nil, within: 30, retries: 10, lock_for: 6000, &block)
  block ||= proc { true }
  blacklist(humanize __method__, name) do |req|
    reqs    = TransactionList.for_ip(req.ip, timeout: within)
    ip_lock = Lock.new(req.ip)
    ip_lock.lock! lock_for if reqs.within(within).with_error.count > retries &&
      block.call(req)
    ip_lock.locked?
  end
end

#block_ips(*ips, name: nil, &block) ⇒ Object



54
55
56
57
58
59
# File 'lib/t-1000.rb', line 54

def block_ips(*ips, name: nil, &block)
  block ||= proc { true }
  blacklist(humanize __method__, name, *ips) do |req|
    ips.include?(req.ip) && block.call(req)
  end
end

#block_strings(*strings, name: nil, &block) ⇒ Object



90
91
92
93
94
95
# File 'lib/t-1000.rb', line 90

def block_strings(*strings, name: nil, &block)
  block ||= proc { true }
  blacklist(humanize __method__, name, *strings) do |req|
    strings.match_any?(req.params.map(&:join).join) && block.call(req)
  end
end

#block_user_agents(*user_agents, name: nil, &block) ⇒ Object



104
105
106
107
108
109
# File 'lib/t-1000.rb', line 104

def block_user_agents(*user_agents, name: nil, &block)
  block ||= proc { true }
  blacklist(humanize __method__, name, *user_agents) do
    user_agents.match_any?(req.user_agent) && block.call(req)
  end
end

#cacheObject



119
120
121
# File 'lib/t-1000.rb', line 119

def cache
  @cache ||= Cache.new
end

#loggerObject



127
128
129
# File 'lib/t-1000.rb', line 127

def logger
  @logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
end

#logger=(logger) ⇒ Object



123
124
125
# File 'lib/t-1000.rb', line 123

def logger=(logger)
  @logger = logger
end

#notify(name, type) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/t-1000.rb', line 131

def notify(name, type)
  logger.warn case type
  when :whitelist
    "T1000 Allowed Request: #{name.inspect}".green
  when :blacklist
    "T1000 Terminated Request: #{name.inspect}".red
  end
end

#throttled_response(&block) ⇒ Object



115
116
117
# File 'lib/t-1000.rb', line 115

def throttled_response(&block)
  Middleware.throttled_response = block
end