Class: Yast::PortAliasesClass

Inherits:
Module
  • Object
show all
Includes:
Logger
Defined in:
library/network/src/modules/PortAliases.rb

Overview

Module providing full listing of port aliases for services, supporting also multiple aliases for the same port (like "http", "www", "www-http" for port 80)

Defined Under Namespace

Classes: Service

Instance Method Summary collapse

Instance Method Details

#AllowedPortNameOrNumberString

Returns a string describing allowed service names and port numbers

Returns:

  • (String)

    an informative message


59
60
61
62
63
64
65
66
# File 'library/network/src/modules/PortAliases.rb', line 59

def AllowedPortNameOrNumber
  # TRANSLATORS: popup informing message, allowed characters for port-names
  _(
    "A port name may consist of the characters 'a-z', 'A-Z', '0-9', and '*+._-'.\n" \
      "A port number may be a number from 0 to 65535.\n" \
      "No spaces are allowed.\n"
  )
end

#GetListOfServiceAliases(needle) ⇒ Array<String>

Note:

given argument will be also included

Returns list of aliases (including the port number) for service.

Examples:

when number is given

GetListOfServicesAliases("22") #=> ["22", "ssh"]

when name or alias is given

GetListOfServicesAliases("ssh") #=> ["22", "ssh"]

when there is not service for given information

GetListOfServicesAliases("not-exist-yet") => ["not-exist-yet"]

Parameters:

  • needle (String)

    the name, alias or port to look for a service

Returns:

  • (Array<String>)

    list of aliases, including the port number


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'library/network/src/modules/PortAliases.rb', line 83

def GetListOfServiceAliases(needle)
  if numeric?(needle)
    # service is a port number
    service = services[needle.to_i]
  elsif IsAllowedPortName(needle)
    # service is an allowed port name
    service = find_by_alias(needle)
  else
    log.error(format("Port name '%s' is not allowed", needle))
  end

  service&.to_a || [needle]
end

#GetPortNumber(needle) ⇒ Integer?

Note:

when given argument looks like a digit, it will be returned after a proper conversion

Returns the port for requested service (if any)

Parameters:

  • needle (String)

    the name or alias of the service

Returns:

  • (Integer, nil)

    a port number if any


113
114
115
116
117
118
# File 'library/network/src/modules/PortAliases.rb', line 113

def GetPortNumber(needle)
  return needle.to_i if numeric?(needle)

  service = find_by_alias(needle)
  service&.port
end

#IsAllowedPortName(needle) ⇒ Boolean

Whether the given argument is an allowed service name, alias or port

Parameters:

Returns:

  • (Boolean)

    true if given value is allowed; false otherwise


41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'library/network/src/modules/PortAliases.rb', line 41

def IsAllowedPortName(needle)
  if needle.nil?
    log.error(format("Invalid port name: %s", needle))
    false
  elsif numeric?(needle)
    # port is number
    port_number = needle.to_i

    port_number > 0 && port_number <= 65_535
  else
    # port is name
    needle.match?(/^\S+$/)
  end
end

#IsKnownPortName(needle) ⇒ Boolean

Whether the requested argument is a known service

Parameters:

  • needle (String)

    service name, alias or port number

Returns:

  • (Boolean)

    true is found a service; false otherwise


101
102
103
104
105
# File 'library/network/src/modules/PortAliases.rb', line 101

def IsKnownPortName(needle)
  return true if GetListOfServiceAliases(needle).size > 1

  false
end

#mainObject


33
34
35
# File 'library/network/src/modules/PortAliases.rb', line 33

def main
  textdomain "base"
end