Class: Netfilter::Tool
- Inherits:
-
Object
- Object
- Netfilter::Tool
- Defined in:
- lib/netfilter/tool.rb
Instance Attribute Summary collapse
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#tables ⇒ Object
Returns the value of attribute tables.
Class Method Summary collapse
Instance Method Summary collapse
- #commands ⇒ Object
- #down ⇒ Object
- #executable ⇒ Object
- #export ⇒ Object
-
#initialize(namespace = nil) {|_self| ... } ⇒ Tool
constructor
A new instance of Tool.
- #pp ⇒ Object
- #table(name, &block) ⇒ Object
- #up ⇒ Object
Constructor Details
#initialize(namespace = nil) {|_self| ... } ⇒ Tool
Returns a new instance of Tool.
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
#namespace ⇒ Object
Returns the value of attribute namespace.
3 4 5 |
# File 'lib/netfilter/tool.rb', line 3 def namespace @namespace end |
#tables ⇒ Object
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 |
.executable ⇒ Object
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 |
Instance Method Details
#commands ⇒ Object
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 |
#down ⇒ Object
97 98 99 100 |
# File 'lib/netfilter/tool.rb', line 97 def down @executed_commands = commands rollback end |
#executable ⇒ Object
109 110 111 |
# File 'lib/netfilter/tool.rb', line 109 def executable self.class.executable end |
#export ⇒ Object
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 |
#pp ⇒ Object
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 |
#up ⇒ Object
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 |