Class: Mt::Wall::Model::Service

Inherits:
Data
  • Object
show all
Defined in:
lib/mt/wall/model/service.rb

Overview

A protocol/port definition referenced when granting access, e.g. Service.new(name: “https”, protocols: [:tcp], ports: [443]). Compiles into the ‘protocol` / `dst-port` fields of a filter rule.

MULTI-PROTOCOL: a service may carry MORE THAN ONE protocol (e.g. DNS is tcp+udp). A grant using such a service compiles to ONE filter rule PER protocol (RouterOS filter rows match a single protocol each). The legacy singular ‘protocol:` keyword is still accepted and folded into the `protocols` array; the `#protocol` reader returns the first protocol for callers that only need one.

PORTS: stored as the validated spec (Integer / Array / Range / “a-b” or “n” String), so a port RANGE round-trips to RouterOS as a range (‘dst-port=8000-8100`) rather than being exploded into a long list. Empty for portless protocols (e.g. icmp).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, protocol: nil, protocols: nil, ports: []) ⇒ Service

Returns a new instance of Service.



26
27
28
29
30
31
32
# File 'lib/mt/wall/model/service.rb', line 26

def initialize(name:, protocol: nil, protocols: nil, ports: [])
  list = protocols || protocol
  # A Range is a single port-spec value — wrap it so Array() does not
  # explode it into discrete integers (we want `dst-port=8000-8100`).
  port_list = ports.is_a?(Range) ? [ports] : Array(ports)
  super(name: name, protocols: Array(list), ports: port_list)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mt/wall/model/service.rb', line 25

Service = Data.define(:name, :protocols, :ports) do
  def initialize(name:, protocol: nil, protocols: nil, ports: [])
    list = protocols || protocol
    # A Range is a single port-spec value — wrap it so Array() does not
    # explode it into discrete integers (we want `dst-port=8000-8100`).
    port_list = ports.is_a?(Range) ? [ports] : Array(ports)
    super(name: name, protocols: Array(list), ports: port_list)
  end

  # Backward-compatible single-protocol reader: the first declared protocol.
  # @return [Symbol, nil]
  def protocol
    protocols.first
  end
end

#portsObject (readonly)

Returns the value of attribute ports

Returns:

  • (Object)

    the current value of ports



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mt/wall/model/service.rb', line 25

Service = Data.define(:name, :protocols, :ports) do
  def initialize(name:, protocol: nil, protocols: nil, ports: [])
    list = protocols || protocol
    # A Range is a single port-spec value — wrap it so Array() does not
    # explode it into discrete integers (we want `dst-port=8000-8100`).
    port_list = ports.is_a?(Range) ? [ports] : Array(ports)
    super(name: name, protocols: Array(list), ports: port_list)
  end

  # Backward-compatible single-protocol reader: the first declared protocol.
  # @return [Symbol, nil]
  def protocol
    protocols.first
  end
end

#protocolsObject (readonly)

Returns the value of attribute protocols

Returns:

  • (Object)

    the current value of protocols



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mt/wall/model/service.rb', line 25

Service = Data.define(:name, :protocols, :ports) do
  def initialize(name:, protocol: nil, protocols: nil, ports: [])
    list = protocols || protocol
    # A Range is a single port-spec value — wrap it so Array() does not
    # explode it into discrete integers (we want `dst-port=8000-8100`).
    port_list = ports.is_a?(Range) ? [ports] : Array(ports)
    super(name: name, protocols: Array(list), ports: port_list)
  end

  # Backward-compatible single-protocol reader: the first declared protocol.
  # @return [Symbol, nil]
  def protocol
    protocols.first
  end
end

Instance Method Details

#protocolSymbol?

Backward-compatible single-protocol reader: the first declared protocol.

Returns:

  • (Symbol, nil)


36
37
38
# File 'lib/mt/wall/model/service.rb', line 36

def protocol
  protocols.first
end