net.i2p.util

## Class PortMapper

- java.lang.Object
- - net.i2p.util.PortMapper

-     public class PortMapper
      extends Object

  Map services to internal or external application ports for this context. Not intended for the router's NTCP or SSU ports.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `DEFAULT_CONSOLE_PORT` Default HTTP console port.
    `static String` | `DEFAULT_HOST` Default host for local services.
    `static int` | `DEFAULT_HTTPS_CONSOLE_PORT` Default HTTPS console port.
    `static String` | `PROP_PREFER_HTTPS`
    `static String` | `SVC_BOB`
    `static String` | `SVC_CONSOLE`
    `static String` | `SVC_EEPSITE`
    `static String` | `SVC_HTTP_I2PCONTROL` HTTP I2PControl service name.
    `static String` | `SVC_HTTP_PROXY`
    `static String` | `SVC_HTTPS_CONSOLE`
    `static String` | `SVC_HTTPS_EEPSITE` HTTPS eepsite service name.
    `static String` | `SVC_HTTPS_I2PCONTROL` HTTPS I2PControl service name.
    `static String` | `SVC_HTTPS_PROXY`
    `static String` | `SVC_I2CP` not necessary, already in config?
    `static String` | `SVC_I2CP_SSL` I2CP-SSL service name.
    `static String` | `SVC_I2PSNARK` To indicate presence, alternative to WebAppStarter.isWebappRunning().
    `static String` | `SVC_I2PTUNNEL` To indicate presence, alternative to WebAppStarter.isWebappRunning().
    `static String` | `SVC_IMAGEGEN` To indicate presence, alternative to WebAppStarter.isWebappRunning().
    `static String` | `SVC_IRC`
    `static String` | `SVC_JSONRPC` To indicate presence, alternative to WebAppStarter.isWebappRunning().
    `static String` | `SVC_POP`
    `static String` | `SVC_SAM`
    `static String` | `SVC_SAM_SSL` SAM-SSL service name.
    `static String` | `SVC_SAM_UDP` SAM-UDP service name.
    `static String` | `SVC_SMTP`
    `static String` | `SVC_SOCKS`
    `static String` | `SVC_SUSIDNS` To indicate presence, alternative to WebAppStarter.isWebappRunning().
    `static String` | `SVC_SUSIMAIL` To indicate presence, alternative to WebAppStarter.isWebappRunning().
    `static String` | `SVC_TAHOE`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `PortMapper(I2PAppContext context)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `String` | `getActualHost(String service, String def)` Get the actual host for a service.
    `String` | `getConsoleURL()` If PROP_PREFER_HTTPS is true or unset, return https URL unless console is http only.
    `String` | `getConsoleURL(boolean preferHTTPS)` If preferHTTPS is true, return https URL unless console is http only.
    `List<String>` | `getEepsiteURLs()` Get the list of eepsite URLs.
    `String` | `getHost(String service, String def)` Get the registered host for a service.
    `int` | `getPort(String service)` Get the registered port for a service
    `int` | `getPort(String service, int def)` Get the registered port for a service
    `boolean` | `isRegistered(String service)` Is the service registered?
    `boolean` | `register(String service, int port)` Add the service
    `boolean` | `register(String service, String host, int port)` Add the service.
    `void` | `renderStatusHTML(Writer out)` For debugging only
    `void` | `unregister(String service)` Remove the service
    `void` | `unregister(String service, int port)` Remove the service, only if it is registered with the supplied port.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_CONSOLE_PORT

          public static final int DEFAULT_CONSOLE_PORT

      Default HTTP console port.
      See Also:
        Constant Field Values

    - #### DEFAULT_HOST

          public static final String DEFAULT_HOST

      Default host for local services.
      See Also:
        Constant Field Values

    - #### DEFAULT_HTTPS_CONSOLE_PORT

          public static final int DEFAULT_HTTPS_CONSOLE_PORT

      Default HTTPS console port.
      See Also:
        Constant Field Values

    - #### PROP_PREFER_HTTPS

          public static final String PROP_PREFER_HTTPS

      See Also:
        Constant Field Values

    - #### SVC_BOB

          public static final String SVC_BOB

      See Also:
        Constant Field Values

    - #### SVC_CONSOLE

          public static final String SVC_CONSOLE

      See Also:
        Constant Field Values

    - #### SVC_EEPSITE

          public static final String SVC_EEPSITE

      See Also:
        Constant Field Values

    - #### SVC_HTTP_I2PCONTROL

          public static final String SVC_HTTP_I2PCONTROL

      HTTP I2PControl service name.
      See Also:
        Constant Field Values

    - #### SVC_HTTP_PROXY

          public static final String SVC_HTTP_PROXY

      See Also:
        Constant Field Values

    - #### SVC_HTTPS_CONSOLE

          public static final String SVC_HTTPS_CONSOLE

      See Also:
        Constant Field Values

    - #### SVC_HTTPS_EEPSITE

          public static final String SVC_HTTPS_EEPSITE

      HTTPS eepsite service name.
      See Also:
        Constant Field Values

    - #### SVC_HTTPS_I2PCONTROL

          public static final String SVC_HTTPS_I2PCONTROL

      HTTPS I2PControl service name.
      See Also:
        Constant Field Values

    - #### SVC_HTTPS_PROXY

          public static final String SVC_HTTPS_PROXY

      See Also:
        Constant Field Values

    - #### SVC_I2CP

          public static final String SVC_I2CP

      not necessary, already in config?
      See Also:
        Constant Field Values

    - #### SVC_I2CP_SSL

          public static final String SVC_I2CP_SSL

      I2CP-SSL service name.
      See Also:
        Constant Field Values

    - #### SVC_I2PSNARK

          public static final String SVC_I2PSNARK

      To indicate presence, alternative to WebAppStarter.isWebappRunning(). For actual base URL, use getConsoleURL()
      See Also:
        Constant Field Values

    - #### SVC_I2PTUNNEL

          public static final String SVC_I2PTUNNEL

      To indicate presence, alternative to WebAppStarter.isWebappRunning(). For actual base URL, use getConsoleURL() Note: This is only the webapp. Also requires TCG running to be useful. The webapp does not start TCG.
      See Also:
        Constant Field Values

    - #### SVC_IMAGEGEN

          public static final String SVC_IMAGEGEN

      To indicate presence, alternative to WebAppStarter.isWebappRunning(). For actual base URL, use getConsoleURL()
      See Also:
        Constant Field Values

    - #### SVC_IRC

          public static final String SVC_IRC

      See Also:
        Constant Field Values

    - #### SVC_JSONRPC

          public static final String SVC_JSONRPC

      To indicate presence, alternative to WebAppStarter.isWebappRunning(). For actual base URL, use getConsoleURL()
      See Also:
        Constant Field Values

    - #### SVC_POP

          public static final String SVC_POP

      See Also:
        Constant Field Values

    - #### SVC_SAM

          public static final String SVC_SAM

      See Also:
        Constant Field Values

    - #### SVC_SAM_SSL

          public static final String SVC_SAM_SSL

      SAM-SSL service name.
      See Also:
        Constant Field Values

    - #### SVC_SAM_UDP

          public static final String SVC_SAM_UDP

      SAM-UDP service name.
      See Also:
        Constant Field Values

    - #### SVC_SMTP

          public static final String SVC_SMTP

      See Also:
        Constant Field Values

    - #### SVC_SOCKS

          public static final String SVC_SOCKS

      See Also:
        Constant Field Values

    - #### SVC_SUSIDNS

          public static final String SVC_SUSIDNS

      To indicate presence, alternative to WebAppStarter.isWebappRunning(). For actual base URL, use getConsoleURL()
      See Also:
        Constant Field Values

    - #### SVC_SUSIMAIL

          public static final String SVC_SUSIMAIL

      To indicate presence, alternative to WebAppStarter.isWebappRunning(). For actual base URL, use getConsoleURL()
      See Also:
        Constant Field Values

    - #### SVC_TAHOE

          public static final String SVC_TAHOE

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### PortMapper

          public PortMapper(I2PAppContext context)

      Parameters:
        `context` - unused for now

  - ### Method Detail

    - #### getActualHost

          public String getActualHost(String service,
                                      String def)

      Get the actual host for a service. Will return "127.0.0.1" if the service was registered without a host. If the service was registered with the host "0.0.0.0", "::", or "0:0:0:0:0:0:0:0", it will return a public IP if we have one, else a local IP if we have one, else def. If it was not registered with a wildcard address, it will return the registered host.
      Parameters:
        `def` - default
      Returns:
        def if not registered

    - #### getConsoleURL

          public String getConsoleURL()

      If PROP_PREFER_HTTPS is true or unset, return https URL unless console is http only. Default https://127.0.0.1:7667/ If PROP_PREFER_HTTPS is set to false, return http URL unless console is https only. Default http://127.0.0.1:7657/

    - #### getConsoleURL

          public String getConsoleURL(boolean preferHTTPS)

      If preferHTTPS is true, return https URL unless console is http only. Default https://127.0.0.1:7667/ If preferHTTPS is false, return http URL unless console is https only. Default http://127.0.0.1:7657/

    - #### getEepsiteURLs

          public List<String> getEepsiteURLs()

      Get the list of eepsite URLs.
      Returns:
        unsorted, non-empty, or null if none

    - #### getHost

          public String getHost(String service,
                                String def)

      Get the registered host for a service. Will return "127.0.0.1" if the service was registered without a host.
      Parameters:
        `def` - default
      Returns:
        def if not registered

    - #### getPort

          public int getPort(String service)

      Get the registered port for a service
      Returns:
        -1 if not registered

    - #### getPort

          public int getPort(String service,
                             int def)

      Get the registered port for a service
      Parameters:
        `def` - default
      Returns:
        def if not registered

    - #### isRegistered

          public boolean isRegistered(String service)

      Is the service registered?

    - #### register

          public boolean register(String service,
                                  int port)

      Add the service
      Parameters:
        `port` - > 0
      Returns:
        success, false if already registered

    - #### register

          public boolean register(String service,
                                  String host,
                                  int port)

      Add the service. If service is SVC_EEPSITE or SVC_HTTPS_EEPSITE, the URL will be included in getEepsites()
      Parameters:
        `port` - > 0
      Returns:
        success, false if already registered

    - #### renderStatusHTML

          public void renderStatusHTML(Writer out)
                                throws IOException

      For debugging only
      Throws:
        `IOException`

    - #### unregister

          public void unregister(String service)

      Remove the service

    - #### unregister

          public void unregister(String service,
                                 int port)

      Remove the service, only if it is registered with the supplied port. If service is SVC_EEPSITE or SVC_HTTPS_EEPSITE, the URL will be removed from getEepsites()