Class: IptablesWeb::Model::AccessRule

Inherits:
Base
  • Object
show all
Defined in:
lib/iptables_web/model/access_rule.rb

Constant Summary collapse

SUPPORTED_PROTOCOLS =
%w(tcp udp)

Instance Method Summary collapse

Methods inherited from Base

configure

Instance Method Details

#mapping(parameter) ⇒ Object



55
56
57
# File 'lib/iptables_web/model/access_rule.rb', line 55

def mapping(parameter)

end

#to_sObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/iptables_web/model/access_rule.rb', line 9

def to_s
  protocols = protocol.to_s.downcase  == 'all' ? SUPPORTED_PROTOCOLS : [protocol]
  protocols.map do |protocol|
    self.resolved_ips.map do |ip|
      command = %w(-A INPUT)
      self.attributes.each do |name, value|
        case name.to_sym
          when :port
            next if  value.to_s.empty? || !value
            if value.include?(',')
              command << '-m'
              command << 'multiport'
              command << '--dports'
              command << value
            else
              command << '--dport'
              command << value
            end
          # when :ip
          #   command << '-s'
          #   command << value
          when :protocol
            next unless protocol
            command << '-p'
            command << protocol
          when :description
            if value
              command << '-m'
              command << 'comment'
              command << '--comment'
              command <<  "\"#{::Shellwords.escape(value)}\""
            end
          else
            #skip
        end
      end
      command << '-s'
      command << ip
      command << '-j'
      command << 'ACCEPT'
      command.join(' ')
    end
  end.join("\n")
  # -A INPUT -s 88.150.233.48/29 -p tcp -m tcp --dport 9200 -j ACCEPT
end