Class: Wiki2Go::FirewallBlacklist

Inherits:
Object
  • Object
show all
Defined in:
lib/Wiki2Go/firewall_blacklist.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FirewallBlacklist

Returns a new instance of FirewallBlacklist.



24
25
26
# File 'lib/Wiki2Go/firewall_blacklist.rb', line 24

def initialize(path)
  @storage = Wiki2Go::FileStorage.new(path,path)
end

Class Method Details

.block(ip_address) ⇒ Object

Given a string ip address Return a string suitable for logging and blocking that address with iptables



55
56
57
58
# File 'lib/Wiki2Go/firewall_blacklist.rb', line 55

def FirewallBlacklist.block(ip_address)
  ip_address = clean(ip_address)
  "-A  INPUT -p tcp --dport 80 --source #{ip_address} -j LOG --log-prefix \"spammer! \" --log-level 7\n-A  INPUT -p tcp --dport 80 --source #{ip_address} -j REJECT\n"
end

.block_all(blacklist) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/Wiki2Go/firewall_blacklist.rb', line 60

def FirewallBlacklist.block_all(blacklist)
  out = ""
  blacklist.each do |ip_address|
    out << FirewallBlacklist.block(ip_address)
  end
  out
end

.generate(args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/Wiki2Go/firewall_blacklist.rb', line 28

def FirewallBlacklist.generate(args)
  opts = OptionParser.new

  configuration = BlacklistConfiguration.new      

  opts.on("-h",'--head file',String) { |val| configuration.head = File.expand_path(val) }
  opts.on("-t",'--tail file',String) { |val| configuration.tail = File.expand_path(val) }
  opts.on("-o",'--out file',String) { |val| configuration.out = File.expand_path(val) }
  opts.on("-d",'--directory dir',"default = .",String) { |val| configuration.directory = File.expand_path(val) }
  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
  opts.parse(args)
  
  list = FirewallBlacklist.new(configuration.directory)
  list.write_file(configuration.head, configuration.tail, configuration.out)
end

Instance Method Details

#generate_blocksObject

Reads the blacklist for the given wiki Returns an iptables ruleset for blocking all ip addresses in the blacklist



49
50
51
52
# File 'lib/Wiki2Go/firewall_blacklist.rb', line 49

def generate_blocks
  blacklist = @storage.load_blacklist('user')
  FirewallBlacklist.block_all(blacklist)
end

#write_file(head, tail, out) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/Wiki2Go/firewall_blacklist.rb', line 68

def write_file(head,tail,out)
  header = []
  trailer = []
  File.open(head,'r') {|f| header = f.read }
  File.open(tail,'r') {|f| trailer = f.read }
  
  File.open(out,"w") do |f|
    f.write header
    f.write self.generate_blocks
    f.write trailer
  end
  
  
end