Class: Tablomat::IPSet

Inherits:
Object
  • Object
show all
Extended by:
Exec
Defined in:
lib/tablomat/ipset.rb,
lib/tablomat/ipset/set.rb,
lib/tablomat/ipset/entry.rb

Overview

The IPSet interface

Defined Under Namespace

Classes: Entry, Set

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Exec

exec

Constructor Details

#initializeIPSet

Returns a new instance of IPSet.



15
16
17
18
19
# File 'lib/tablomat/ipset.rb', line 15

def initialize
  @ipset_bin = 'ipset'
  @ipset_bin = "sudo #{@ipset_bin}" if Etc.getlogin != 'root'
  @sets = {}
end

Instance Attribute Details

#ipset_binObject

Returns the value of attribute ipset_bin.



13
14
15
# File 'lib/tablomat/ipset.rb', line 13

def ipset_bin
  @ipset_bin
end

Instance Method Details

#add(set_name:, entry_data:, add_options: '', exist: false) ⇒ Object



55
56
57
# File 'lib/tablomat/ipset.rb', line 55

def add(set_name:, entry_data:, add_options: '', exist: false)
  set(set_name).add(entry_data: entry_data, add_options: add_options, exist: exist)
end

#create(set_name:, type:, create_options: '', rangefrom: '', rangeto: '') ⇒ Object



59
60
61
# File 'lib/tablomat/ipset.rb', line 59

def create(set_name:, type:, create_options: '', rangefrom: '', rangeto: '')
  set(set_name).create(type: type, create_options: create_options, rangefrom: rangefrom, rangeto: rangeto)
end

#del(set_name:, entry_data:, exist: false) ⇒ Object



63
64
65
# File 'lib/tablomat/ipset.rb', line 63

def del(set_name:, entry_data:, exist: false)
  set(set_name).del(entry_data: entry_data, exist: exist)
end

#destroy(set_name:) ⇒ Object



67
68
69
# File 'lib/tablomat/ipset.rb', line 67

def destroy(set_name:)
  set(set_name).destroy
end

#destroy_all(really_all = false) ⇒ Object



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

def destroy_all(really_all = false)
  # destroys all sets created in this Instance unless really_all = true
  if really_all
    command = "#{@ipset_bin} destroy"
    exec(command)
  else
    @sets.each do |_name, set|
      set.destroy if set.active
    end
  end
end

#exec(cmd) ⇒ Object



37
38
39
40
41
# File 'lib/tablomat/ipset.rb', line 37

def exec(cmd)
  Exec.exec(cmd)
rescue StandardError => e
  raise IPSetError.new, e.message
end

#matchset(flags:, option: '', negate: false, negate_option: false, set_name:) ⇒ Object



21
22
23
24
25
26
# File 'lib/tablomat/ipset.rb', line 21

def matchset(flags:, option: '', negate: false, negate_option: false, set_name:)
  set_set = "--match-set #{set_name} #{flags}"
  set_set = "! #{set_set}" if negate
  option = "! #{option}" if negate_option
  "set #{set_set} #{option}"
end

#set(name, &block) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/tablomat/ipset.rb', line 28

def set(name, &block)
  name = name.to_s.downcase
  (@sets[name] || Set.new(self, name)).tap do |set|
    @sets[name] = set
    block&.call(set)
  end
  @sets[name]
end