From 0da70caf7fd8a0dadfa1992e0ee26f2057f7474f Mon Sep 17 00:00:00 2001 From: zzz Date: Wed, 18 Jan 2012 16:57:27 +0000 Subject: [PATCH] * I2PTunnel: Register ports with the PortMapper * Update: Find the proxy port in the PortMapper --- .../i2p/i2ptunnel/I2PTunnelConnectClient.java | 15 ++++++++++++ .../i2p/i2ptunnel/I2PTunnelHTTPClient.java | 5 ++++ .../net/i2p/i2ptunnel/I2PTunnelIRCClient.java | 10 ++++++++ .../i2p/router/web/ConfigUpdateHandler.java | 17 +++++++++++--- .../i2p/router/web/ConfigUpdateHelper.java | 23 ++++++++++++++++++- .../src/net/i2p/router/web/NewsFetcher.java | 2 +- .../i2p/router/web/PluginUpdateChecker.java | 2 +- .../i2p/router/web/PluginUpdateHandler.java | 2 +- .../i2p/router/web/UnsignedUpdateHandler.java | 2 +- .../src/net/i2p/router/web/UpdateHandler.java | 5 ++-- apps/routerconsole/jsp/configupdate.jsp | 2 +- 11 files changed, 73 insertions(+), 12 deletions(-) diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelConnectClient.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelConnectClient.java index 9953dd205..5ef49d878 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelConnectClient.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelConnectClient.java @@ -25,6 +25,7 @@ import net.i2p.data.Destination; import net.i2p.util.EventDispatcher; import net.i2p.util.FileUtil; import net.i2p.util.Log; +import net.i2p.util.PortMapper; /** * Supports the following: @@ -152,6 +153,20 @@ public class I2PTunnelConnectClient extends I2PTunnelHTTPClientBase implements R return opts; } + @Override + public void startRunning() { + super.startRunning(); + _context.portMapper().register(PortMapper.SVC_HTTPS_PROXY, getLocalPort()); + } + + @Override + public boolean close(boolean forced) { + int reg = _context.portMapper().getPort(PortMapper.SVC_HTTPS_PROXY); + if (reg == getLocalPort()) + _context.portMapper().unregister(PortMapper.SVC_HTTPS_PROXY); + return super.close(forced); + } + protected void clientConnectionRun(Socket s) { InputStream in = null; OutputStream out = null; diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java index bd0f7c54d..b3bce9cbc 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java @@ -39,6 +39,7 @@ import net.i2p.data.Hash; import net.i2p.util.EventDispatcher; import net.i2p.util.FileUtil; import net.i2p.util.Log; +import net.i2p.util.PortMapper; import net.i2p.util.Translate; /** @@ -291,6 +292,7 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn public void startRunning() { super.startRunning(); this.isr = new InternalSocketRunner(this); + _context.portMapper().register(PortMapper.SVC_HTTP_PROXY, getLocalPort()); } /** @@ -298,6 +300,9 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn */ @Override public boolean close(boolean forced) { + int reg = _context.portMapper().getPort(PortMapper.SVC_HTTP_PROXY); + if (reg == getLocalPort()) + _context.portMapper().unregister(PortMapper.SVC_HTTP_PROXY); boolean rv = super.close(forced); if (this.isr != null) this.isr.stopRunning(); diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelIRCClient.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelIRCClient.java index 6811ca4e1..d2cb49cdb 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelIRCClient.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelIRCClient.java @@ -21,6 +21,7 @@ import net.i2p.i2ptunnel.irc.IrcOutboundFilter; import net.i2p.util.EventDispatcher; import net.i2p.util.I2PAppThread; import net.i2p.util.Log; +import net.i2p.util.PortMapper; /** * Todo: Can we extend I2PTunnelClient instead and remove some duplicated code? @@ -151,8 +152,17 @@ public class I2PTunnelIRCClient extends I2PTunnelClientBase { return dests.get(index); } + @Override + public void startRunning() { + super.startRunning(); + _context.portMapper().register(PortMapper.SVC_IRC, getLocalPort()); + } + @Override public boolean close(boolean forced) { + int reg = _context.portMapper().getPort(PortMapper.SVC_IRC); + if (reg == getLocalPort()) + _context.portMapper().unregister(PortMapper.SVC_IRC); synchronized(this) { if (_DCCServer != null) { _DCCServer.close(forced); diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigUpdateHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigUpdateHandler.java index a4d2d068e..a3b30d337 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/ConfigUpdateHandler.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigUpdateHandler.java @@ -3,9 +3,11 @@ package net.i2p.router.web; import java.util.HashMap; import java.util.Map; +import net.i2p.I2PAppContext; import net.i2p.crypto.TrustedUpdate; import net.i2p.data.DataHelper; import net.i2p.util.FileUtil; +import net.i2p.util.PortMapper; /** * @@ -77,7 +79,16 @@ public class ConfigUpdateHandler extends FormHandler { public static final String PROP_TRUSTED_KEYS = "router.trustedUpdateKeys"; - + /** + * Convenience method for updaters + * @return the configured value, else the registered HTTP proxy, else the default + * @since 0.8.13 + */ + static int proxyPort(I2PAppContext ctx) { + return ctx.getProperty(PROP_PROXY_PORT, + ctx.portMapper().getPort(PortMapper.SVC_HTTP_PROXY, DEFAULT_PROXY_PORT_INT)); + } + @Override protected void processForm() { if (_action == null) @@ -112,7 +123,7 @@ public class ConfigUpdateHandler extends FormHandler { } } - if ( (_proxyHost != null) && (_proxyHost.length() > 0) ) { + if (_proxyHost != null && _proxyHost.length() > 0 && !_proxyHost.equals(_("internal"))) { String oldHost = _context.router().getConfigSetting(PROP_PROXY_HOST); if ( (oldHost == null) || (!_proxyHost.equals(oldHost)) ) { changes.put(PROP_PROXY_HOST, _proxyHost); @@ -120,7 +131,7 @@ public class ConfigUpdateHandler extends FormHandler { } } - if ( (_proxyPort != null) && (_proxyPort.length() > 0) ) { + if (_proxyPort != null && _proxyPort.length() > 0 && !_proxyPort.equals(_("internal"))) { String oldPort = _context.router().getConfigSetting(PROP_PROXY_PORT); if ( (oldPort == null) || (!_proxyPort.equals(oldPort)) ) { changes.put(PROP_PROXY_PORT, _proxyPort); diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigUpdateHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigUpdateHelper.java index 54404699f..88aaa70b2 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/ConfigUpdateHelper.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigUpdateHelper.java @@ -3,6 +3,7 @@ package net.i2p.router.web; import net.i2p.I2PAppContext; import net.i2p.crypto.TrustedUpdate; import net.i2p.data.DataHelper; +import net.i2p.util.PortMapper; public class ConfigUpdateHelper extends HelperBase { private boolean _dontInstall; @@ -37,6 +38,7 @@ public class ConfigUpdateHelper extends HelperBase { else return ConfigUpdateHandler.DEFAULT_NEWS_URL; } + public String getUpdateURL() { String url = _context.getProperty(ConfigUpdateHandler.PROP_UPDATE_URL); if (url != null) @@ -44,11 +46,30 @@ public class ConfigUpdateHelper extends HelperBase { else return ConfigUpdateHandler.DEFAULT_UPDATE_URL; } + public String getProxyHost() { + if (isInternal()) + return _("internal") + "\" readonly=\"readonly"; return _context.getProperty(ConfigUpdateHandler.PROP_PROXY_HOST, ConfigUpdateHandler.DEFAULT_PROXY_HOST); } + public String getProxyPort() { - return _context.getProperty(ConfigUpdateHandler.PROP_PROXY_PORT, ConfigUpdateHandler.DEFAULT_PROXY_PORT); + if (isInternal()) + return _("internal") + "\" readonly=\"readonly"; + return Integer.toString(ConfigUpdateHandler.proxyPort(_context)); + } + + /** + * This should almost always be true. + * @return true if settings are at defaults and proxy is registered + * @since 0.8.13 + */ + private boolean isInternal() { + String host = _context.getProperty(ConfigUpdateHandler.PROP_PROXY_HOST); + String port = _context.getProperty(ConfigUpdateHandler.PROP_PROXY_PORT); + return (host == null || host.equals(ConfigUpdateHandler.DEFAULT_PROXY_HOST)) && + (port == null || port.equals(ConfigUpdateHandler.DEFAULT_PROXY_PORT)) && + _context.portMapper().getPort(PortMapper.SVC_HTTP_PROXY) == ConfigUpdateHandler.DEFAULT_PROXY_PORT_INT; } public String getUpdateThroughProxy() { diff --git a/apps/routerconsole/java/src/net/i2p/router/web/NewsFetcher.java b/apps/routerconsole/java/src/net/i2p/router/web/NewsFetcher.java index 6d7550dd2..aa4a52330 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/NewsFetcher.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/NewsFetcher.java @@ -224,7 +224,7 @@ public class NewsFetcher implements Runnable, EepGet.StatusListener { String newsURL = ConfigUpdateHelper.getNewsURL(_context); boolean shouldProxy = Boolean.valueOf(_context.getProperty(ConfigUpdateHandler.PROP_SHOULD_PROXY, ConfigUpdateHandler.DEFAULT_SHOULD_PROXY)).booleanValue(); String proxyHost = _context.getProperty(ConfigUpdateHandler.PROP_PROXY_HOST, ConfigUpdateHandler.DEFAULT_PROXY_HOST); - int proxyPort = _context.getProperty(ConfigUpdateHandler.PROP_PROXY_PORT, ConfigUpdateHandler.DEFAULT_PROXY_PORT_INT); + int proxyPort = ConfigUpdateHandler.proxyPort(_context); if (_tempFile.exists()) _tempFile.delete(); diff --git a/apps/routerconsole/java/src/net/i2p/router/web/PluginUpdateChecker.java b/apps/routerconsole/java/src/net/i2p/router/web/PluginUpdateChecker.java index 73b6850a0..bc4ef05db 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/PluginUpdateChecker.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/PluginUpdateChecker.java @@ -151,7 +151,7 @@ public class PluginUpdateChecker extends UpdateHandler { // always proxy, or else FIXME //boolean shouldProxy = Boolean.valueOf(_context.getProperty(ConfigUpdateHandler.PROP_SHOULD_PROXY, ConfigUpdateHandler.DEFAULT_SHOULD_PROXY)).booleanValue(); String proxyHost = _context.getProperty(ConfigUpdateHandler.PROP_PROXY_HOST, ConfigUpdateHandler.DEFAULT_PROXY_HOST); - int proxyPort = _context.getProperty(ConfigUpdateHandler.PROP_PROXY_PORT, ConfigUpdateHandler.DEFAULT_PROXY_PORT_INT); + int proxyPort = ConfigUpdateHandler.proxyPort(_context); _baos.reset(); try { _get = new PartialEepGet(_context, proxyHost, proxyPort, _baos, _xpi2pURL, TrustedUpdate.HEADER_BYTES); diff --git a/apps/routerconsole/java/src/net/i2p/router/web/PluginUpdateHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/PluginUpdateHandler.java index ecef4bb19..0c1fce48e 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/PluginUpdateHandler.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/PluginUpdateHandler.java @@ -119,7 +119,7 @@ public class PluginUpdateHandler extends UpdateHandler { // use the same settings as for updater boolean shouldProxy = Boolean.valueOf(_context.getProperty(ConfigUpdateHandler.PROP_SHOULD_PROXY, ConfigUpdateHandler.DEFAULT_SHOULD_PROXY)).booleanValue(); String proxyHost = _context.getProperty(ConfigUpdateHandler.PROP_PROXY_HOST, ConfigUpdateHandler.DEFAULT_PROXY_HOST); - int proxyPort = _context.getProperty(ConfigUpdateHandler.PROP_PROXY_PORT, ConfigUpdateHandler.DEFAULT_PROXY_PORT_INT); + int proxyPort = ConfigUpdateHandler.proxyPort(_context); try { if (shouldProxy) // 10 retries!! diff --git a/apps/routerconsole/java/src/net/i2p/router/web/UnsignedUpdateHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/UnsignedUpdateHandler.java index 27a66996b..f72878375 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/UnsignedUpdateHandler.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/UnsignedUpdateHandler.java @@ -70,7 +70,7 @@ public class UnsignedUpdateHandler extends UpdateHandler { // always proxy for now //boolean shouldProxy = Boolean.valueOf(_context.getProperty(ConfigUpdateHandler.PROP_SHOULD_PROXY, ConfigUpdateHandler.DEFAULT_SHOULD_PROXY)).booleanValue(); String proxyHost = _context.getProperty(ConfigUpdateHandler.PROP_PROXY_HOST, ConfigUpdateHandler.DEFAULT_PROXY_HOST); - int proxyPort = _context.getProperty(ConfigUpdateHandler.PROP_PROXY_PORT, ConfigUpdateHandler.DEFAULT_PROXY_PORT_INT); + int proxyPort = ConfigUpdateHandler.proxyPort(_context); try { // 40 retries!! _get = new EepGet(_context, proxyHost, proxyPort, 40, _updateFile, _zipURL, false); diff --git a/apps/routerconsole/java/src/net/i2p/router/web/UpdateHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/UpdateHandler.java index 2253fb75a..6f1bf1c79 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/UpdateHandler.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/UpdateHandler.java @@ -160,16 +160,15 @@ public class UpdateHandler { * If it is, get the whole thing. */ protected void update() { - // TODO: // Do a PartialEepGet on the selected URL, check for version we expect, // and loop if it isn't what we want. - // This will allow us to do a release without waiting for the last host to install the update. + // This will allows us to do a release without waiting for the last host to install the update. // Alternative: In bytesTransferred(), Check the data in the output file after // we've received at least 56 bytes. Need a cancel() method in EepGet ? boolean shouldProxy = Boolean.valueOf(_context.getProperty(ConfigUpdateHandler.PROP_SHOULD_PROXY, ConfigUpdateHandler.DEFAULT_SHOULD_PROXY)).booleanValue(); String proxyHost = _context.getProperty(ConfigUpdateHandler.PROP_PROXY_HOST, ConfigUpdateHandler.DEFAULT_PROXY_HOST); - int proxyPort = _context.getProperty(ConfigUpdateHandler.PROP_PROXY_PORT, ConfigUpdateHandler.DEFAULT_PROXY_PORT_INT); + int proxyPort = ConfigUpdateHandler.proxyPort(_context); List urls = getUpdateURLs(); if (urls.isEmpty()) { diff --git a/apps/routerconsole/jsp/configupdate.jsp b/apps/routerconsole/jsp/configupdate.jsp index f57e995a5..c3066ade6 100644 --- a/apps/routerconsole/jsp/configupdate.jsp +++ b/apps/routerconsole/jsp/configupdate.jsp @@ -53,7 +53,7 @@ <%=intl._("eepProxy host")%>: " /> <%=intl._("eepProxy port")%>: - " /> + " /> <% if (updatehelper.canInstall()) { %> <%=intl._("Update URLs")%>: