Class: IPScriptables::Rule

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ipscriptables/rule.rb,
lib/ipscriptables/pretty_print.rb

Constant Summary collapse

OPTION_SYNONYMS =
Hashie::Mash[
  :p => :protocol,
  :s => :source,
  :d => :destination,
  :j => :jump,
  :g => :goto,
  :i => :in_interface,
  :o => :out_interface,
  :f => :fragment,
  :m => :match,
  :sport => :source_port,
  :dport => :destination_port,
  :sports => :source_ports,
  :dports => :destination_ports
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chain, rule, counters = nil) ⇒ Rule

rubocop:disable MethodLength



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ipscriptables/rule.rb', line 26

def initialize(chain, rule, counters = nil) # rubocop:disable MethodLength
  @chain, @rule, @counters = chain, rule, counters
  @parsed = Hashie::Mash.new

  @counters ||= original.counters if original
  @counters ||= [0, 0] if opts[:counters]

  key = nil
  Shellwords.shellsplit(rule).each do |word|
    case word
    when /^-+(.*)$/
      self[key] = true if key
      key = Regexp.last_match[1].gsub('-', '_')
    else
      self[key] = word
      key = nil
    end
  end
end

Instance Attribute Details

#chainObject (readonly)

Returns the value of attribute chain.



23
24
25
# File 'lib/ipscriptables/rule.rb', line 23

def chain
  @chain
end

#countersObject (readonly)

Returns the value of attribute counters.



23
24
25
# File 'lib/ipscriptables/rule.rb', line 23

def counters
  @counters
end

#ruleObject (readonly)

Returns the value of attribute rule.



23
24
25
# File 'lib/ipscriptables/rule.rb', line 23

def rule
  @rule
end

Instance Method Details

#==(other) ⇒ Object



50
51
52
53
# File 'lib/ipscriptables/rule.rb', line 50

def ==(other)
  other = other.rule if other.respond_to?(:rule)
  rule == other
end

#=~(other) ⇒ Object



72
73
74
# File 'lib/ipscriptables/rule.rb', line 72

def =~(other)
  rule =~ other
end

#[](k) ⇒ Object



55
56
57
58
# File 'lib/ipscriptables/rule.rb', line 55

def [](k)
  k = k.to_s.sub(/^-+/, '').gsub('-', '_')
  @parsed[OPTION_SYNONYMS.fetch(k, k)]
end

#inspectObject



50
51
52
# File 'lib/ipscriptables/pretty_print.rb', line 50

def inspect
  "#<#{self.class} #{render_counters}#{rule}>"
end

#matchObject



64
65
66
# File 'lib/ipscriptables/rule.rb', line 64

def match
  Array(self[:m])
end

#originalObject



46
47
48
# File 'lib/ipscriptables/rule.rb', line 46

def original
  chain.original.find { |rule| rule == self } if chain.original
end

#pretty_print(q) ⇒ Object



54
55
56
# File 'lib/ipscriptables/pretty_print.rb', line 54

def pretty_print(q)
  q.text("#{render_counters}#{rule}")
end

#protoObject



60
61
62
# File 'lib/ipscriptables/rule.rb', line 60

def proto
  self[:protocol]
end

#renderObject



76
77
78
# File 'lib/ipscriptables/rule.rb', line 76

def render
  "#{render_counters}-A #{chain.name} #{rule}"
end

#render_countersObject



80
81
82
# File 'lib/ipscriptables/rule.rb', line 80

def render_counters
  "[#{counters.join(':')}] " if counters
end

#targetObject



68
69
70
# File 'lib/ipscriptables/rule.rb', line 68

def target
  self[:jump]
end