* I2PTunnel: Register ports with the PortMapper

* Update: Find the proxy port in the PortMapper
This commit is contained in:
zzz
2012-01-18 16:57:27 +00:00
parent a4a1ed4357
commit 0da70caf7f
11 changed files with 73 additions and 12 deletions

View File

@ -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;

View File

@ -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();

View File

@ -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);

View File

@ -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,6 +79,15 @@ 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() {
@ -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);

View File

@ -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() {

View File

@ -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();

View File

@ -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);

View File

@ -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!!

View File

@ -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);

View File

@ -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<String> urls = getUpdateURLs();
if (urls.isEmpty()) {

View File

@ -53,7 +53,7 @@
</tr><tr><td class= "mediumtags" align="right"><b><%=intl._("eepProxy host")%>:</b></td>
<td><input type="text" size="10" name="proxyHost" value="<jsp:getProperty name="updatehelper" property="proxyHost" />" /></td>
</tr><tr><td class= "mediumtags" align="right"><b><%=intl._("eepProxy port")%>:</b></td>
<td><input type="text" size="4" name="proxyPort" value="<jsp:getProperty name="updatehelper" property="proxyPort" />" /></td></tr>
<td><input type="text" size="10" name="proxyPort" value="<jsp:getProperty name="updatehelper" property="proxyPort" />" /></td></tr>
<% if (updatehelper.canInstall()) { %>
<tr><td class= "mediumtags" align="right"><b><%=intl._("Update URLs")%>:</b></td>
<td><textarea name="updateURL" wrap="off" spellcheck="false"><jsp:getProperty name="updatehelper" property="updateURL" /></textarea></td>