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

public class PortMapperextends Object
Map services to internal or external application ports for this context. Not intended for the router's NTCP or SSU ports.
  • Field Details

  • Constructor Details

    • PortMapper

      public PortMapper(I2PAppContext context)
      Create a port mapper.
      Parameters:
      context - unused for now
  • Method Details

    • register

      public boolean register(String service, int port)
      Add the service.
      Parameters:
      service - the service name
      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:
      service - the service name
      host - the hostname to map the service to
      port - > 0
      Returns:
      success, false if already registered
    • isRegistered

      public boolean isRegistered(String service)
      Is the service registered?
      Parameters:
      service - the service name
      Returns:
      true if registered
    • unregister

      public void unregister(String service)
      Remove the service.
      Parameters:
      service - the service name
    • 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().
      Parameters:
      service - the service name
      port - the port number
    • getPort

      public int getPort(String service)
      The registered port for a service.
      Parameters:
      service - the service name
      Returns:
      -1 if not registered
    • getPort

      public int getPort(String service, int def)
      The registered port for a service.
      Parameters:
      service - the service name
      def - value to return when the service is unregistered
      Returns:
      def if not registered
    • getHost

      public String getHost(String service, String def)
      The registered host for a service. Will return "127.0.0.1" if the service was registered without a host.
      Parameters:
      service - the registered service name to look up
      def - value to return when the service is unregistered
      Returns:
      def if not registered
    • getActualHost

      public String getActualHost(String service, String def)
      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:
      service - the registered service name to look up
      def - value to return for an unregistered service or a wildcard with no usable IP
      Returns:
      def if not registered
    • convertWildcard

      private static String convertWildcard(String ip, String def)
      Like the public convertWildcard above, for the case where the caller already knows there is more than one address.
      Parameters:
      ip - the address to convert
      def - the address to use when there is nothing to convert
      Returns:
      def if there are no IPs, otherwise the converted address
    • 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/
      Returns:
      the console URL
    • 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/
      Parameters:
      preferHTTPS - if true, start from the https console URL, which still
      Returns:
      the console URL
    • getHTTPConsoleURL

      private String getHTTPConsoleURL()
      The console URL, https if the console is https only.
      Returns:
      http URL unless console is https only. Default http://127.0.0.1:7657/
    • getHTTPSConsoleURL

      private String getHTTPSConsoleURL()
      The console URL, http if the console is http only.
      Returns:
      https URL unless console is http only. Default http://127.0.0.1:7657/
    • getEepsiteURLs

      public List<String> getEepsiteURLs()
      Eepsite URLs configured in this port mapper.
      Returns:
      unsorted, non-empty, or null if none
    • toURL

      private static String toURL(String svc, String host, int port)
      Control URL for the mapped service.
      Parameters:
      svc - the service name, https:// is used only for the HTTPS eepsite
      host - the bound host, wildcards become 127.0.0.1 and IPv6 hosts get brackets
      port - the port number to append after the host
      Returns:
      the control URL, terminated with a slash
    • getForwardedServices

      public Map<String, InetSocketAddress> getForwardedServices()
      Snapshot of the service names registered for port forwarding with their bound addresses, for the consolidated debug view.
      Returns:
      a copy of the map of service name to address, never null
    • getEepsites

      public Set<String> getEepsites()
      Snapshot of the eepsite URLs registered with the port mapper.
      Returns:
      a copy of the set of URLs, never null
    • displayHost

      public static String displayHost(String ip)
      Render a bind address the way the status tables show it: a wildcard becomes the router's display address or the default host.
      Parameters:
      ip - bound address, may be a wildcard
      Returns:
      displayable host, never null
    • renderStatusHTML

      public void renderStatusHTML(Writer out) throws IOException
      Table of registered services with host and port, plus eepsites.
      Parameters:
      out - writer to write HTML output to
      Throws:
      IOException - if writing to output fails