Util: Consolidate console URL generation in PortMapper

This commit is contained in:
zzz
2018-01-18 13:25:21 +00:00
parent 109ac5b61c
commit ae505995ac
7 changed files with 35 additions and 33 deletions

View File

@ -438,21 +438,7 @@ class InternalTrayManager extends TrayManager {
* @since 0.9.26 * @since 0.9.26
*/ */
private void launchBrowser() { private void launchBrowser() {
String unset = "*unset*"; String url = _context.portMapper().getConsoleURL();
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 + '/';
}
try { try {
I2PDesktop.browse(url); I2PDesktop.browse(url);
} catch (BrowseException e1) { } catch (BrowseException e1) {

View File

@ -550,19 +550,9 @@ public abstract class I2PTunnelHTTPClientBase extends I2PTunnelClientBase implem
String rv = out.toString(); String rv = out.toString();
// Do we need to replace http://127.0.0.1:7657 console links in the error page? // 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. // Get the registered host and port from the PortMapper.
final String unset = "*unset*"; String url = ctx.portMapper().getConsoleURL();
final String httpHost = ctx.portMapper().getActualHost(PortMapper.SVC_CONSOLE, unset); if (!url.equals("http://127.0.0.1:7657/")) {
final String httpsHost = ctx.portMapper().getActualHost(PortMapper.SVC_HTTPS_CONSOLE, unset); rv = rv.replace("http://127.0.0.1:7657/", url);
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);
} }
return rv; return rv;
} finally { } finally {

View File

@ -219,6 +219,13 @@ public class ConfigServiceHandler extends FormHandler {
(sdtg == null && (SystemVersion.isWindows() || SystemVersion.isMac())); (sdtg == null && (SystemVersion.isWindows() || SystemVersion.isMac()));
} }
/**
* @since 0.9.33
*/
public String getConsoleURL() {
return _context.portMapper().getConsoleURL();
}
@Override @Override
protected void processForm() { protected void processForm() {
if (_action == null) return; if (_action == null) return;
@ -348,9 +355,9 @@ public class ConfigServiceHandler extends FormHandler {
} }
// releases <= 0.6.5 deleted the entry completely // releases <= 0.6.5 deleted the entry completely
if (shouldLaunchBrowser && !found) { 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", ClientAppConfig ca = new ClientAppConfig(UrlLauncher.class.getName(), "consoleBrowser",
"http://127.0.0.1:" + port + '/', 5, false); url, 5, false);
clients.add(ca); clients.add(ca);
} }
ClientAppConfig.writeClientAppConfig(_context, clients); ClientAppConfig.writeClientAppConfig(_context, clients);

View File

@ -190,7 +190,7 @@ public class HomeHelper extends HelperBase {
String url; String url;
if (app.name.equals(website) && app.url.equals("http://127.0.0.1:7658/")) { if (app.name.equals(website) && app.url.equals("http://127.0.0.1:7658/")) {
// fixup eepsite link // 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) + '/'; ':' + _context.portMapper().getPort(PortMapper.SVC_EEPSITE, 7658) + '/';
} else { } else {
url = app.url; url = app.url;

View File

@ -226,7 +226,7 @@ class SummaryBarRenderer {
.append("</a>\n" + .append("</a>\n" +
"<a href=\"http://") "<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(':')
.append(_context.portMapper().getPort(PortMapper.SVC_EEPSITE, 7658)) .append(_context.portMapper().getPort(PortMapper.SVC_EEPSITE, 7658))
.append("/\" target=\"_blank\" title=\"") .append("/\" target=\"_blank\" title=\"")

View File

@ -83,7 +83,8 @@
<h3 class="ptitle" id="browseronstart"><%=intl._t("Launch browser on router startup?")%></h3> <h3 class="ptitle" id="browseronstart"><%=intl._t("Launch browser on router startup?")%></h3>
<p class="infohelp"> <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> </p>
<hr><div class="formaction" id="browserstart"> <hr><div class="formaction" id="browserstart">
<input type="submit" class="check" name="action" value="<%=intl._t("View console on startup")%>" > <input type="submit" class="check" name="action" value="<%=intl._t("View console on startup")%>" >

View File

@ -173,6 +173,24 @@ public class PortMapper {
return rv; 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 * For debugging only
* @since 0.9.20 * @since 0.9.20