Class: Tablomat::IPTablesBase::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/tablomat/iptables/rule.rb

Overview

IPTables are made of Rules

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chain, description, owned = true) ⇒ Rule

Returns a new instance of Rule.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/tablomat/iptables/rule.rb', line 10

def initialize(chain, description, owned = true)
  @system = chain.table.system
  @chain = chain
  @description = description
  @items = {}
  @owned = owned
  @active = false
  @method = 'APPEND'
  @position = 0
  activate if @chain.active
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



7
8
9
# File 'lib/tablomat/iptables/rule.rb', line 7

def active
  @active
end

#chainObject (readonly)

Returns the value of attribute chain.



8
9
10
# File 'lib/tablomat/iptables/rule.rb', line 8

def chain
  @chain
end

#descriptionObject (readonly)

Returns the value of attribute description.



8
9
10
# File 'lib/tablomat/iptables/rule.rb', line 8

def description
  @description
end

#methodObject

Returns the value of attribute method.



7
8
9
# File 'lib/tablomat/iptables/rule.rb', line 7

def method
  @method
end

#ownedObject

Returns the value of attribute owned.



7
8
9
# File 'lib/tablomat/iptables/rule.rb', line 7

def owned
  @owned
end

#positionObject

Returns the value of attribute position.



7
8
9
# File 'lib/tablomat/iptables/rule.rb', line 7

def position
  @position
end

Instance Method Details

#activate(override = false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/tablomat/iptables/rule.rb', line 22

def activate(override = false)
  return unless @owned || override
  return if @active

  @active = true
  return if override

  @chain.activate unless @chain.active
  apply_create
end

#apply_createObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tablomat/iptables/rule.rb', line 43

def apply_create
  return unless @owned

  method = if @method == 'APPEND'
             "-A #{@chain.name}"
           else
             "-I #{@chain.name} #{@position}"
           end
  command = "#{@system.iptables_bin} -t #{@chain.table.name} #{method} #{@description}"
  @system.exec command
end

#apply_deleteObject



55
56
57
58
59
60
# File 'lib/tablomat/iptables/rule.rb', line 55

def apply_delete
  return unless @owned

  command = "#{@system.iptables_bin} -t #{@chain.table.name} -D #{@chain.name} #{@description}"
  @system.exec command
end

#deactivate(override = false) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/tablomat/iptables/rule.rb', line 33

def deactivate(override = false)
  return unless @owned || override
  return unless @active

  self.active = false
  return if override

  apply_delete
end