From ae505995ac48a234a6594c1cba706ee47c37cf31 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Thu, 18 Jan 2018 13:25:21 +0000
Subject: [PATCH] Util: Consolidate console URL generation in PortMapper

---
 .../i2p/desktopgui/InternalTrayManager.java    | 16 +---------------
 .../i2p/i2ptunnel/I2PTunnelHTTPClientBase.java | 16 +++-------------
 .../i2p/router/web/ConfigServiceHandler.java   | 11 +++++++++--
 .../net/i2p/router/web/helpers/HomeHelper.java |  2 +-
 .../router/web/helpers/SummaryBarRenderer.java |  2 +-
 apps/routerconsole/jsp/configservice.jsp       |  3 ++-
 core/java/src/net/i2p/util/PortMapper.java     | 18 ++++++++++++++++++
 7 files changed, 35 insertions(+), 33 deletions(-)

diff --git a/apps/desktopgui/src/net/i2p/desktopgui/InternalTrayManager.java b/apps/desktopgui/src/net/i2p/desktopgui/InternalTrayManager.java
index 852c9cca0c..3b7984abeb 100644
--- a/apps/desktopgui/src/net/i2p/desktopgui/InternalTrayManager.java
+++ b/apps/desktopgui/src/net/i2p/desktopgui/InternalTrayManager.java
@@ -438,21 +438,7 @@ class InternalTrayManager extends TrayManager {
      * @since 0.9.26
      */
     private void launchBrowser() {
-        String unset = "*unset*";
-        PortMapper pm = _context.portMapper();
-        String httpHost = pm.getActualHost(PortMapper.SVC_CONSOLE, unset);
-        String httpsHost = pm.getActualHost(PortMapper.SVC_HTTPS_CONSOLE, unset);
-        int httpPort = pm.getPort(PortMapper.SVC_CONSOLE, 7657);
-        int httpsPort = pm.getPort(PortMapper.SVC_HTTPS_CONSOLE, -1);
-        boolean httpsOnly = httpsPort > 0 && httpHost.equals(unset) && !httpsHost.equals(unset);
-        String url;
-        if (httpsOnly) {
-            url = "https://" + httpsHost + ':' + httpsPort + '/';
-        } else {
-            if (httpHost.equals(unset))
-                httpHost = "127.0.0.1";
-            url = "http://" + httpHost + ':' + httpPort + '/';
-        }
+        String url = _context.portMapper().getConsoleURL();
         try {
             I2PDesktop.browse(url);
         } catch (BrowseException e1) {
diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClientBase.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClientBase.java
index 92cb2dca78..c8aafbd667 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClientBase.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClientBase.java
@@ -550,19 +550,9 @@ public abstract class I2PTunnelHTTPClientBase extends I2PTunnelClientBase implem
             String rv = out.toString();
             // Do we need to replace http://127.0.0.1:7657 console links in the error page?
             // Get the registered host and port from the PortMapper.
-            final String unset = "*unset*";
-            final String httpHost = ctx.portMapper().getActualHost(PortMapper.SVC_CONSOLE, unset);
-            final String httpsHost = ctx.portMapper().getActualHost(PortMapper.SVC_HTTPS_CONSOLE, unset);
-            final int httpPort = ctx.portMapper().getPort(PortMapper.SVC_CONSOLE, 7657);
-            final int httpsPort = ctx.portMapper().getPort(PortMapper.SVC_HTTPS_CONSOLE, -1);
-            final boolean httpsOnly = httpsPort > 0 && httpHost.equals(unset) && !httpsHost.equals(unset);
-            final int port = httpsOnly ? httpsPort : httpPort;
-            String host = httpsOnly ? httpsHost : httpHost;
-            if (host.equals(unset))
-                host = "127.0.0.1";
-            if (httpsOnly || port != 7657 || !host.equals("127.0.0.1")) {
-                String url = (httpsOnly ? "https://" : "http://") + host + ':' + port;
-                rv = rv.replace("http://127.0.0.1:7657", url);
+            String url = ctx.portMapper().getConsoleURL();
+            if (!url.equals("http://127.0.0.1:7657/")) {
+                rv = rv.replace("http://127.0.0.1:7657/", url);
             }
             return rv;
         } finally {
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigServiceHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigServiceHandler.java
index 7bfc470fe9..857d16cb57 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/ConfigServiceHandler.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigServiceHandler.java
@@ -219,6 +219,13 @@ public class ConfigServiceHandler extends FormHandler {
                (sdtg == null && (SystemVersion.isWindows() || SystemVersion.isMac()));
     }
 
+    /**
+     *  @since 0.9.33
+     */
+    public String getConsoleURL() {
+        return _context.portMapper().getConsoleURL();
+    }
+
     @Override
     protected void processForm() {
         if (_action == null) return;
@@ -348,9 +355,9 @@ public class ConfigServiceHandler extends FormHandler {
         }
         // releases <= 0.6.5 deleted the entry completely
         if (shouldLaunchBrowser && !found) {
-            int port = _context.portMapper().getPort(PortMapper.SVC_CONSOLE, RouterConsoleRunner.DEFAULT_LISTEN_PORT);
+            String url = _context.portMapper().getConsoleURL();
             ClientAppConfig ca = new ClientAppConfig(UrlLauncher.class.getName(), "consoleBrowser",
-                                                     "http://127.0.0.1:" + port + '/', 5, false);
+                                                     url, 5, false);
             clients.add(ca);
         }
         ClientAppConfig.writeClientAppConfig(_context, clients);
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/HomeHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/HomeHelper.java
index 752edacdbf..45ff222f37 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/HomeHelper.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/HomeHelper.java
@@ -190,7 +190,7 @@ public class HomeHelper extends HelperBase {
             String url;
             if (app.name.equals(website) && app.url.equals("http://127.0.0.1:7658/")) {
                 // fixup eepsite link
-                url = "http://" + _context.portMapper().getHost(PortMapper.SVC_EEPSITE, "127.0.0.1") +
+                url = "http://" + _context.portMapper().getActualHost(PortMapper.SVC_EEPSITE, "127.0.0.1") +
                       ':' + _context.portMapper().getPort(PortMapper.SVC_EEPSITE, 7658) + '/';
             } else {
                 url = app.url;
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/SummaryBarRenderer.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/SummaryBarRenderer.java
index 05936e89a9..4003fcebf3 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/SummaryBarRenderer.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/SummaryBarRenderer.java
@@ -226,7 +226,7 @@ class SummaryBarRenderer {
            .append("</a>\n" +
 
                    "<a href=\"http://")
-           .append(_context.portMapper().getHost(PortMapper.SVC_EEPSITE, "127.0.0.1"))
+           .append(_context.portMapper().getActualHost(PortMapper.SVC_EEPSITE, "127.0.0.1"))
            .append(':')
            .append(_context.portMapper().getPort(PortMapper.SVC_EEPSITE, 7658))
            .append("/\" target=\"_blank\" title=\"")
diff --git a/apps/routerconsole/jsp/configservice.jsp b/apps/routerconsole/jsp/configservice.jsp
index 8892b5bbd2..38ea13d8c8 100644
--- a/apps/routerconsole/jsp/configservice.jsp
+++ b/apps/routerconsole/jsp/configservice.jsp
@@ -83,7 +83,8 @@
 
  <h3 class="ptitle" id="browseronstart"><%=intl._t("Launch browser on router startup?")%></h3>
  <p class="infohelp">
-    <%=intl._t("I2P''s main configuration interface is this web console, so for your convenience I2P can launch a web browser on startup pointing at {0}.", "<a href=\"http://127.0.0.1:7657/\">http://127.0.0.1:7657/</a>")%>
+    <% String consoleURL = formhandler.getConsoleURL(); %>
+    <%=intl._t("I2P''s main configuration interface is this web console, so for your convenience I2P can launch a web browser on startup pointing at {0}.", "<a href=\"" + consoleURL + "\">" + consoleURL + "</a>")%>
  </p>
  <hr><div class="formaction" id="browserstart">
  <input type="submit" class="check" name="action" value="<%=intl._t("View console on startup")%>" >
diff --git a/core/java/src/net/i2p/util/PortMapper.java b/core/java/src/net/i2p/util/PortMapper.java
index 105dc1f074..3548bc35f2 100644
--- a/core/java/src/net/i2p/util/PortMapper.java
+++ b/core/java/src/net/i2p/util/PortMapper.java
@@ -173,6 +173,24 @@ public class PortMapper {
         return rv;
     }
 
+    /*
+     *  @return http URL unless console is https only. Default http://127.0.0.1:7657/
+     *  @since 0.9.33 consolidated from i2ptunnel and desktopgui
+     */
+    public String getConsoleURL() {
+        String unset = "*unset*";
+        String httpHost = getActualHost(SVC_CONSOLE, unset);
+        String httpsHost = getActualHost(SVC_HTTPS_CONSOLE, unset);
+        int httpPort = getPort(SVC_CONSOLE, 7657);
+        int httpsPort = getPort(SVC_HTTPS_CONSOLE, -1);
+        boolean httpsOnly = httpsPort > 0 && httpHost.equals(unset) && !httpsHost.equals(unset);
+        if (httpsOnly)
+            return "https://" + httpsHost + ':' + httpsPort + '/';
+        if (httpHost.equals(unset))
+            httpHost = "127.0.0.1";
+        return "http://" + httpHost + ':' + httpPort + '/';
+    }
+
     /**
      *  For debugging only
      *  @since 0.9.20
-- 
GitLab