- NPE fix on signed udpates

- More work on snark updater
- Clean up imports
This commit is contained in:
zzz
2012-10-21 13:34:23 +00:00
parent 6331cb2374
commit 6e19854e4c
10 changed files with 71 additions and 55 deletions

View File

@ -46,7 +46,6 @@ public class UpdateHandler implements Updater {
method != UpdateMethod.TORRENT || updateSources.isEmpty()) method != UpdateMethod.TORRENT || updateSources.isEmpty())
return null; return null;
UpdateRunner update = new UpdateRunner(_context, _umgr, _smgr, updateSources, newVersion); UpdateRunner update = new UpdateRunner(_context, _umgr, _smgr, updateSources, newVersion);
// set status before thread to ensure UI feedback
_umgr.notifyProgress(update, "<b>" + _smgr.util().getString("Updating") + "</b>"); _umgr.notifyProgress(update, "<b>" + _smgr.util().getString("Updating") + "</b>");
update.start(); update.start();
return update; return update;

View File

@ -31,10 +31,9 @@ class UpdateRunner implements UpdateTask, CompleteListener {
private ByteArrayOutputStream _baos; private ByteArrayOutputStream _baos;
private URI _currentURI; private URI _currentURI;
private Snark _snark; private Snark _snark;
private boolean _hasMetaInfo;
private static final long CONNECT_TIMEOUT = 55*1000; private static final long MAX_LENGTH = 30*1024*1024;
private static final long INACTIVITY_TIMEOUT = 5*60*1000;
private static final long NOPROXY_INACTIVITY_TIMEOUT = 60*1000;
public UpdateRunner(I2PAppContext ctx, UpdateManager umgr, SnarkManager smgr, public UpdateRunner(I2PAppContext ctx, UpdateManager umgr, SnarkManager smgr,
List<URI> uris, String newVersion) { List<URI> uris, String newVersion) {
@ -101,17 +100,43 @@ class UpdateRunner implements UpdateTask, CompleteListener {
} }
} catch (IllegalArgumentException iae) {} } catch (IllegalArgumentException iae) {}
} }
if (_snark == null) { if (_snark == null)
_umgr.notifyTaskFailed(this, "", null); fatal("No valid URLs");
}
private void fatal(String error) {
if (_snark != null) {
if (_hasMetaInfo) {
_smgr.stopTorrent(_snark, true);
String file = _snark.getName();
_smgr.removeTorrent(file);
// delete torrent file
File f = new File(_smgr.getDataDir(), file);
f.delete();
// delete data
file = _snark.getBaseName();
f = new File(_smgr.getDataDir(), file);
f.delete();
} else {
_smgr.deleteMagnet(_snark);
}
}
_umgr.notifyTaskFailed(this, error, null);
_log.error(error);
_isRunning = false; _isRunning = false;
}
} }
//////// begin CompleteListener methods //////// begin CompleteListener methods
//////// all pass through to SnarkManager //////// all pass through to SnarkManager
public void torrentComplete(Snark snark) { public void torrentComplete(Snark snark) {
String dataFile = snark.getBaseName();
File f = new File(_smgr.getDataDir(), dataFile);
String sudVersion = TrustedUpdate.getVersionString(f);
if (!_newVersion.equals(sudVersion)) {
fatal("version mismatch");
}
_umgr.notifyComplete(this, _newVersion, f);
_smgr.torrentComplete(snark); _smgr.torrentComplete(snark);
} }
@ -121,11 +146,26 @@ class UpdateRunner implements UpdateTask, CompleteListener {
} }
public String gotMetaInfo(Snark snark) { public String gotMetaInfo(Snark snark) {
Storage storage = snark.getStorage();
MetaInfo info = snark.getMetaInfo();
if (info.getFiles() != null) {
fatal("more than 1 file");
return null;
}
if (info.isPrivate()) {
fatal("private torrent");
return null;
}
if (info.getTotalLength() > MAX_LENGTH) {
fatal("too big");
return null;
}
_hasMetaInfo = true;
return _smgr.gotMetaInfo(snark); return _smgr.gotMetaInfo(snark);
} }
public void fatal(Snark snark, String error) { public void fatal(Snark snark, String error) {
fatal(error);
_smgr.fatal(snark, error); _smgr.fatal(snark, error);
} }

View File

@ -30,6 +30,7 @@ import javax.servlet.http.HttpServletResponse;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
import net.i2p.data.Base64; import net.i2p.data.Base64;
import net.i2p.data.DataHelper; import net.i2p.data.DataHelper;
import net.i2p.update.*;
import net.i2p.util.I2PAppThread; import net.i2p.util.I2PAppThread;
import net.i2p.util.Log; import net.i2p.util.Log;
@ -42,6 +43,7 @@ import org.klomp.snark.SnarkManager;
import org.klomp.snark.Storage; import org.klomp.snark.Storage;
import org.klomp.snark.Tracker; import org.klomp.snark.Tracker;
import org.klomp.snark.TrackerClient; import org.klomp.snark.TrackerClient;
import org.klomp.snark.UpdateHandler;
import org.klomp.snark.dht.DHT; import org.klomp.snark.dht.DHT;
import org.mortbay.jetty.servlet.DefaultServlet; import org.mortbay.jetty.servlet.DefaultServlet;
@ -57,6 +59,8 @@ public class I2PSnarkServlet extends DefaultServlet {
private I2PAppContext _context; private I2PAppContext _context;
private Log _log; private Log _log;
private SnarkManager _manager; private SnarkManager _manager;
private UpdateManager _umgr;
private UpdateHandler _uhandler;
private static long _nonce; private static long _nonce;
private Resource _resourceBase; private Resource _resourceBase;
private String _themePath; private String _themePath;
@ -76,6 +80,11 @@ public class I2PSnarkServlet extends DefaultServlet {
configFile = "i2psnark.config"; configFile = "i2psnark.config";
_manager.loadConfig(configFile); _manager.loadConfig(configFile);
_manager.start(); _manager.start();
_umgr = _context.updateManager();
if (_umgr != null) {
_uhandler = new UpdateHandler(_context, _umgr, _manager);
_umgr.register(_uhandler, UpdateType.ROUTER_SIGNED, UpdateMethod.TORRENT, 10);
}
try { try {
_resourceBase = Resource.newResource(_manager.getDataDir().getAbsolutePath()); _resourceBase = Resource.newResource(_manager.getDataDir().getAbsolutePath());
} catch (IOException ioe) {} } catch (IOException ioe) {}
@ -86,6 +95,10 @@ public class I2PSnarkServlet extends DefaultServlet {
public void destroy() { public void destroy() {
if (_manager != null) if (_manager != null)
_manager.stop(); _manager.stop();
if (_umgr != null && _uhandler != null) {
//_uhandler.shutdown();
_umgr.unregister(_uhandler, UpdateType.ROUTER_SIGNED, UpdateMethod.TORRENT);
}
super.destroy(); super.destroy();
} }

View File

@ -1,24 +1,14 @@
package net.i2p.router.update; package net.i2p.router.update;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import net.i2p.crypto.TrustedUpdate;
import net.i2p.router.RouterContext; import net.i2p.router.RouterContext;
import net.i2p.router.web.PluginStarter; import net.i2p.router.web.PluginStarter;
import net.i2p.update.*; import net.i2p.update.*;
import net.i2p.util.EepGet;
import net.i2p.util.I2PAppThread;
import net.i2p.util.PartialEepGet;
import net.i2p.util.SimpleScheduler;
import net.i2p.util.SimpleTimer;
import net.i2p.util.VersionComparator;
/** /**
* Check for or download an updated version of a plugin. * Check for or download an updated version of a plugin.

View File

@ -7,7 +7,6 @@ import java.util.List;
import net.i2p.router.RouterContext; import net.i2p.router.RouterContext;
import net.i2p.router.util.RFC822Date; import net.i2p.router.util.RFC822Date;
import net.i2p.router.web.ConfigUpdateHandler; import net.i2p.router.web.ConfigUpdateHandler;
import net.i2p.router.web.Messages;
import net.i2p.update.*; import net.i2p.update.*;
import net.i2p.util.EepHead; import net.i2p.util.EepHead;
import net.i2p.util.I2PAppThread; import net.i2p.util.I2PAppThread;

View File

@ -6,18 +6,12 @@ import java.net.URISyntaxException;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import net.i2p.router.Router;
import net.i2p.router.RouterContext; import net.i2p.router.RouterContext;
import net.i2p.router.util.RFC822Date;
import net.i2p.router.web.ConfigUpdateHandler; import net.i2p.router.web.ConfigUpdateHandler;
import net.i2p.router.web.NewsHelper; import net.i2p.router.web.NewsHelper;
import net.i2p.update.*; import net.i2p.update.*;
import static net.i2p.update.UpdateType.*; import static net.i2p.update.UpdateType.*;
import static net.i2p.update.UpdateMethod.*; import static net.i2p.update.UpdateMethod.*;
import net.i2p.util.EepGet;
import net.i2p.util.FileUtil;
import net.i2p.util.I2PAppThread;
import net.i2p.util.Log;
/** /**
* <p>Handles the request to update the router by firing off an * <p>Handles the request to update the router by firing off an

View File

@ -1,27 +1,10 @@
package net.i2p.router.update; package net.i2p.router.update;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.net.URI; import java.net.URI;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.StringTokenizer;
import net.i2p.crypto.TrustedUpdate;
import net.i2p.data.DataHelper;
import net.i2p.router.Router;
import net.i2p.router.RouterContext; import net.i2p.router.RouterContext;
import net.i2p.router.RouterVersion;
import net.i2p.router.util.RFC822Date;
import net.i2p.update.*; import net.i2p.update.*;
import net.i2p.util.EepGet;
import net.i2p.util.I2PAppThread;
import net.i2p.util.Log;
import net.i2p.util.PartialEepGet;
import net.i2p.util.VersionComparator;
/** /**
* <p>Handles the request to update the router by firing one or more * <p>Handles the request to update the router by firing one or more

View File

@ -12,7 +12,6 @@ import net.i2p.data.DataHelper;
import net.i2p.router.RouterContext; import net.i2p.router.RouterContext;
import net.i2p.router.RouterVersion; import net.i2p.router.RouterVersion;
import net.i2p.router.web.ConfigUpdateHandler; import net.i2p.router.web.ConfigUpdateHandler;
import net.i2p.router.web.Messages;
import net.i2p.update.*; import net.i2p.update.*;
import net.i2p.util.EepGet; import net.i2p.util.EepGet;
import net.i2p.util.I2PAppThread; import net.i2p.util.I2PAppThread;
@ -40,7 +39,8 @@ class UpdateRunner extends I2PAppThread implements UpdateTask, EepGet.StatusList
protected boolean _isPartial; protected boolean _isPartial;
/** set by the listeners on completion */ /** set by the listeners on completion */
protected String _newVersion; protected String _newVersion;
private ByteArrayOutputStream _baos; // 56 byte header, only used for suds
private final ByteArrayOutputStream _baos;
protected URI _currentURI; protected URI _currentURI;
private static final String SIGNED_UPDATE_FILE = "i2pupdate.sud"; private static final String SIGNED_UPDATE_FILE = "i2pupdate.sud";
@ -56,6 +56,7 @@ class UpdateRunner extends I2PAppThread implements UpdateTask, EepGet.StatusList
_log = ctx.logManager().getLog(getClass()); _log = ctx.logManager().getLog(getClass());
_mgr = mgr; _mgr = mgr;
_urls = uris; _urls = uris;
_baos = new ByteArrayOutputStream(TrustedUpdate.HEADER_BYTES);
_updateFile = (new File(ctx.getTempDir(), "update" + ctx.random().nextInt() + ".tmp")).getAbsolutePath(); _updateFile = (new File(ctx.getTempDir(), "update" + ctx.random().nextInt() + ".tmp")).getAbsolutePath();
} }
@ -110,14 +111,11 @@ class UpdateRunner extends I2PAppThread implements UpdateTask, EepGet.StatusList
if (_urls.isEmpty()) { if (_urls.isEmpty()) {
// not likely, don't bother translating // not likely, don't bother translating
updateStatus("<b>Update source list is empty, cannot download update</b>"); updateStatus("<b>Update source list is empty, cannot download update</b>");
_log.log(Log.CRIT, "Update source list is empty - cannot download update"); _log.error("Update source list is empty - cannot download update");
_mgr.notifyTaskFailed(this, "", null); _mgr.notifyTaskFailed(this, "", null);
return; return;
} }
ByteArrayOutputStream baos = null;
if (shouldProxy)
baos = new ByteArrayOutputStream(TrustedUpdate.HEADER_BYTES);
for (URI uri : _urls) { for (URI uri : _urls) {
_currentURI = uri; _currentURI = uri;
String updateURL = uri.toString(); String updateURL = uri.toString();
@ -128,10 +126,10 @@ class UpdateRunner extends I2PAppThread implements UpdateTask, EepGet.StatusList
// Check the first 56 bytes for the version // Check the first 56 bytes for the version
if (shouldProxy) { if (shouldProxy) {
_isPartial = true; _isPartial = true;
baos.reset(); _baos.reset();
try { try {
// no retries // no retries
_get = new PartialEepGet(_context, proxyHost, proxyPort, baos, updateURL, TrustedUpdate.HEADER_BYTES); _get = new PartialEepGet(_context, proxyHost, proxyPort, _baos, updateURL, TrustedUpdate.HEADER_BYTES);
_get.addStatusListener(UpdateRunner.this); _get.addStatusListener(UpdateRunner.this);
_get.fetch(CONNECT_TIMEOUT); _get.fetch(CONNECT_TIMEOUT);
} catch (Throwable t) { } catch (Throwable t) {

View File

@ -1016,7 +1016,7 @@ public class I2PAppContext {
/** /**
* The controller of router, plugin, and other updates. * The controller of router, plugin, and other updates.
* @return always null in I2PAppContext, the update manager if in RouterContext and it is registered * @return always null in I2PAppContext, the update manager if in RouterContext and it is registered
* @since 0.9.2 * @since 0.9.4
*/ */
public UpdateManager updateManager() { public UpdateManager updateManager() {
return null; return null;

View File

@ -522,7 +522,7 @@ public class RouterContext extends I2PAppContext {
/** /**
* The controller of router, plugin, and other updates. * The controller of router, plugin, and other updates.
* @return The manager if it is registered, else null * @return The manager if it is registered, else null
* @since 0.9.2 * @since 0.9.4
*/ */
@Override @Override
public UpdateManager updateManager() { public UpdateManager updateManager() {
@ -532,7 +532,7 @@ public class RouterContext extends I2PAppContext {
/** /**
* Register as the update manager. * Register as the update manager.
* @throws IllegalStateException if one was already registered * @throws IllegalStateException if one was already registered
* @since 0.9.2 * @since 0.9.4
*/ */
public void registerUpdateManager(UpdateManager mgr) { public void registerUpdateManager(UpdateManager mgr) {
synchronized(_lock3) { synchronized(_lock3) {
@ -545,7 +545,7 @@ public class RouterContext extends I2PAppContext {
/** /**
* Unregister the update manager. * Unregister the update manager.
* @throws IllegalStateException if it was not registered * @throws IllegalStateException if it was not registered
* @since 0.9.2 * @since 0.9.4
*/ */
public void unregisterUpdateManager(UpdateManager mgr) { public void unregisterUpdateManager(UpdateManager mgr) {
synchronized(_lock3) { synchronized(_lock3) {