Class: WebShield::IPShield

Inherits:
Shield
  • Object
show all
Defined in:
lib/web_shield/ip_shield.rb

Constant Summary collapse

OPTION_KEYS =
[:whitelist, :blacklist]

Instance Attribute Summary

Attributes inherited from Shield

#config, #id, #options, #path_matcher, #shield_path

Instance Method Summary collapse

Methods inherited from Shield

#dictatorial?, #shield_name, #write_log

Constructor Details

#initialize(id, shield_path, options, config) ⇒ IPShield

Params:

path:
options:
  whitelist: options, defualt [], like 172.10.10.10 172.10.10.10/16
  blacklist: options, default [], like 172.10.10.10 172.10.10.10/16


13
14
15
16
17
18
19
20
# File 'lib/web_shield/ip_shield.rb', line 13

def initialize(id, shield_path, options, config)
  super

  check_options(@options)
  @options[:dictatorial] = true
  push_to_whitelist(options[:whitelist]) if options[:whitelist]
  push_to_blacklist(options[:blacklist]) if options[:blacklist]
end

Instance Method Details

#filter(request) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/web_shield/ip_shield.rb', line 22

def filter(request)
  req_path = request.path
  return unless path_matcher.match(req_path)

  if in_blacklist?(request.ip)
    user = config.user_parser.call(request)
    write_log(:info, "Blacklist block '#{user}' #{request.request_method} #{req_path}")
    :block
  elsif in_whitelist?(request.ip)
    write_log(:info, "Whitelist pass '#{user}' #{request.request_method} #{req_path}")
    :pass
  else
    nil
  end
end

#in_blacklist?(ip) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/web_shield/ip_shield.rb', line 42

def in_blacklist?(ip)
  in_ip_list?(get_store_key('blacklist'), ip)
end

#in_whitelist?(ip) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/web_shield/ip_shield.rb', line 38

def in_whitelist?(ip)
  in_ip_list?(get_store_key('whitelist'), ip)
end

#push_to_blacklist(ips) ⇒ Object



50
51
52
# File 'lib/web_shield/ip_shield.rb', line 50

def push_to_blacklist(ips)
  config.store.push_to_set(get_store_key('blacklist'), ips)
end

#push_to_whitelist(ips) ⇒ Object



46
47
48
# File 'lib/web_shield/ip_shield.rb', line 46

def push_to_whitelist(ips)
  config.store.push_to_set(get_store_key('whitelist'), ips)
end