Class: Netfilter::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/netfilter/tool.rb

Direct Known Subclasses

EbTables, IpTables

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace = nil) {|_self| ... } ⇒ Tool

Returns a new instance of Tool.

Yields:

  • (_self)

Yield Parameters:



50
51
52
53
54
# File 'lib/netfilter/tool.rb', line 50

def initialize(namespace = nil)
  self.namespace = namespace
  self.tables = {}
  yield(self) if block_given?
end

Instance Attribute Details

#namespaceObject

Returns the value of attribute namespace.



3
4
5
# File 'lib/netfilter/tool.rb', line 3

def namespace
  @namespace
end

#tablesObject

Returns the value of attribute tables.



3
4
5
# File 'lib/netfilter/tool.rb', line 3

def tables
  @tables
end

Class Method Details

.delete_chain(name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/netfilter/tool.rb', line 30

def self.delete_chain(name)
  commands = []
  parse.each do |table, chains|
    chains.each do |chain, rules|
      rules.each do |rule|
        if rule.match("-j #{name}")
          commands << "--table #{table} --delete #{chain} #{rule}"
        end
      end
    end

    chains.each do |chain, rules|
      if chain.match(name)
        commands << "--table #{table} --delete-chain #{chain}"
      end
    end
  end
  commands.each{ |command| execute("#{executable} #{command}") }
end

.executableObject



15
16
17
# File 'lib/netfilter/tool.rb', line 15

def self.executable
  name.demodulize.downcase
end

.execute(command) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/netfilter/tool.rb', line 19

def self.execute(command)
  # puts "Executing: #{command}"
  stdout = `#{command} 2>&1`.strip
  status = $?
  if status.exitstatus == 0
    stdout
  else
    raise SystemError, :command => command, :error => stdout
  end
end

.import(data) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/netfilter/tool.rb', line 5

def self.import(data)
  data = data.symbolize_keys
  new(data[:namespace]).tap do |tool|
    data[:tables].each do |data|
      table = Table.import(tool, data)
      tool.tables[table.name.to_s.downcase] = table
    end
  end
end

Instance Method Details

#commandsObject



76
77
78
79
80
81
82
83
84
# File 'lib/netfilter/tool.rb', line 76

def commands
  [].tap do |commands|
    tables.values.each do |table|
      table.commands.each do |command|
        commands << command.unshift(executable)*" "
      end
    end
  end
end

#downObject



97
98
99
100
# File 'lib/netfilter/tool.rb', line 97

def down
  @executed_commands = commands
  rollback
end

#executableObject



109
110
111
# File 'lib/netfilter/tool.rb', line 109

def executable
  self.class.executable
end

#exportObject



102
103
104
105
106
107
# File 'lib/netfilter/tool.rb', line 102

def export
  {
    :namespace => namespace,
    :tables => tables.values.map{ |table| table.export },
  }
end

#ppObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/netfilter/tool.rb', line 64

def pp
  tables.values.sort_by(&:name).each do |table|
    puts [table.name]*"\t"
    table.chains.values.sort_by(&:name).each do |chain|
      puts ["", chain.name_as_argument]*"\t"
      chain.filters.each do |filter|
        puts ["", "", filter]*"\t"
      end
    end
  end
end

#table(name, &block) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/netfilter/tool.rb', line 56

def table(name, &block)
  key = name.to_s.downcase
  (tables[key] || Table.new(self, name)).tap do |table|
    tables[key] = table
    block.call(table) if block
  end
end

#upObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/netfilter/tool.rb', line 86

def up
  @executed_commands = []
  commands.each do |command|
    execute(command)
    @executed_commands << command
  end
rescue SystemError => e
  rollback
  raise e
end