2005-12-15 08:58:30 +00:00
|
|
|
package org.klomp.snark;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
|
|
|
import net.i2p.I2PAppContext;
|
|
|
|
import net.i2p.data.DataHelper;
|
|
|
|
import net.i2p.util.I2PThread;
|
|
|
|
import net.i2p.util.Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage multiple snarks
|
|
|
|
*/
|
2005-12-16 03:00:48 +00:00
|
|
|
public class SnarkManager implements Snark.CompleteListener {
|
2005-12-15 08:58:30 +00:00
|
|
|
private static SnarkManager _instance = new SnarkManager();
|
|
|
|
public static SnarkManager instance() { return _instance; }
|
|
|
|
|
|
|
|
/** map of (canonical) filename to Snark instance (unsynchronized) */
|
|
|
|
private Map _snarks;
|
|
|
|
private String _configFile;
|
|
|
|
private Properties _config;
|
|
|
|
private I2PAppContext _context;
|
|
|
|
private Log _log;
|
|
|
|
private List _messages;
|
|
|
|
|
|
|
|
public static final String PROP_I2CP_HOST = "i2psnark.i2cpHost";
|
|
|
|
public static final String PROP_I2CP_PORT = "i2psnark.i2cpPort";
|
|
|
|
public static final String PROP_I2CP_OPTS = "i2psnark.i2cpOptions";
|
|
|
|
public static final String PROP_EEP_HOST = "i2psnark.eepHost";
|
|
|
|
public static final String PROP_EEP_PORT = "i2psnark.eepPort";
|
|
|
|
public static final String PROP_DIR = "i2psnark.dir";
|
2005-12-21 12:04:54 +00:00
|
|
|
|
|
|
|
public static final String PROP_AUTO_START = "i2snark.autoStart";
|
2005-12-22 12:49:09 +00:00
|
|
|
public static final String DEFAULT_AUTO_START = "false";
|
2005-12-15 08:58:30 +00:00
|
|
|
|
|
|
|
private SnarkManager() {
|
|
|
|
_snarks = new HashMap();
|
|
|
|
_context = I2PAppContext.getGlobalContext();
|
|
|
|
_log = _context.logManager().getLog(SnarkManager.class);
|
|
|
|
_messages = new ArrayList(16);
|
|
|
|
loadConfig("i2psnark.config");
|
2005-12-16 03:00:48 +00:00
|
|
|
int minutes = getStartupDelayMinutes();
|
|
|
|
_messages.add("Starting up torrents in " + minutes + (minutes == 1 ? " minute" : " minutes"));
|
2005-12-15 08:58:30 +00:00
|
|
|
I2PThread monitor = new I2PThread(new DirMonitor(), "Snark DirMonitor");
|
|
|
|
monitor.setDaemon(true);
|
|
|
|
monitor.start();
|
|
|
|
}
|
|
|
|
|
2005-12-16 08:24:21 +00:00
|
|
|
private static final int MAX_MESSAGES = 5;
|
2005-12-15 08:58:30 +00:00
|
|
|
public void addMessage(String message) {
|
|
|
|
synchronized (_messages) {
|
|
|
|
_messages.add(message);
|
|
|
|
while (_messages.size() > MAX_MESSAGES)
|
|
|
|
_messages.remove(0);
|
|
|
|
}
|
2006-01-23 00:51:54 +00:00
|
|
|
if (_log.shouldLog(Log.INFO))
|
|
|
|
_log.info("MSG: " + message);
|
2005-12-15 08:58:30 +00:00
|
|
|
}
|
|
|
|
|
2005-12-16 03:00:48 +00:00
|
|
|
/** newest last */
|
|
|
|
public List getMessages() {
|
|
|
|
synchronized (_messages) {
|
|
|
|
return new ArrayList(_messages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-21 12:04:54 +00:00
|
|
|
public boolean shouldAutoStart() {
|
|
|
|
return Boolean.valueOf(_config.getProperty(PROP_AUTO_START, DEFAULT_AUTO_START+"")).booleanValue();
|
|
|
|
}
|
2005-12-15 08:58:30 +00:00
|
|
|
private int getStartupDelayMinutes() { return 1; }
|
2005-12-16 03:00:48 +00:00
|
|
|
public File getDataDir() {
|
2005-12-15 08:58:30 +00:00
|
|
|
String dir = _config.getProperty(PROP_DIR);
|
|
|
|
if ( (dir == null) || (dir.trim().length() <= 0) )
|
|
|
|
dir = "i2psnark";
|
|
|
|
return new File(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadConfig(String filename) {
|
|
|
|
_configFile = filename;
|
|
|
|
if (_config == null)
|
|
|
|
_config = new Properties();
|
|
|
|
File cfg = new File(filename);
|
|
|
|
if (cfg.exists()) {
|
|
|
|
try {
|
|
|
|
DataHelper.loadProps(_config, cfg);
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
_log.error("Error loading I2PSnark config '" + filename + "'", ioe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// now add sane defaults
|
|
|
|
if (!_config.containsKey(PROP_I2CP_HOST))
|
|
|
|
_config.setProperty(PROP_I2CP_HOST, "localhost");
|
|
|
|
if (!_config.containsKey(PROP_I2CP_PORT))
|
|
|
|
_config.setProperty(PROP_I2CP_PORT, "7654");
|
2006-09-04 06:01:53 +00:00
|
|
|
if (!_config.containsKey(PROP_I2CP_OPTS))
|
|
|
|
_config.setProperty(PROP_I2CP_OPTS, "inbound.length=1 inbound.lengthVariance=1 outbound.length=1 outbound.lengthVariance=1");
|
2005-12-15 08:58:30 +00:00
|
|
|
if (!_config.containsKey(PROP_EEP_HOST))
|
|
|
|
_config.setProperty(PROP_EEP_HOST, "localhost");
|
|
|
|
if (!_config.containsKey(PROP_EEP_PORT))
|
|
|
|
_config.setProperty(PROP_EEP_PORT, "4444");
|
|
|
|
if (!_config.containsKey(PROP_DIR))
|
|
|
|
_config.setProperty(PROP_DIR, "i2psnark");
|
2005-12-21 12:04:54 +00:00
|
|
|
if (!_config.containsKey(PROP_AUTO_START))
|
|
|
|
_config.setProperty(PROP_AUTO_START, DEFAULT_AUTO_START);
|
2005-12-15 08:58:30 +00:00
|
|
|
updateConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateConfig() {
|
|
|
|
String i2cpHost = _config.getProperty(PROP_I2CP_HOST);
|
|
|
|
int i2cpPort = getInt(PROP_I2CP_PORT, 7654);
|
|
|
|
String opts = _config.getProperty(PROP_I2CP_OPTS);
|
2005-12-16 03:00:48 +00:00
|
|
|
Map i2cpOpts = new HashMap();
|
2005-12-15 08:58:30 +00:00
|
|
|
if (opts != null) {
|
|
|
|
StringTokenizer tok = new StringTokenizer(opts, " ");
|
|
|
|
while (tok.hasMoreTokens()) {
|
|
|
|
String pair = tok.nextToken();
|
|
|
|
int split = pair.indexOf('=');
|
2005-12-16 03:00:48 +00:00
|
|
|
if (split > 0)
|
|
|
|
i2cpOpts.put(pair.substring(0, split), pair.substring(split+1));
|
2005-12-15 08:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i2cpHost != null) {
|
|
|
|
I2PSnarkUtil.instance().setI2CPConfig(i2cpHost, i2cpPort, i2cpOpts);
|
|
|
|
_log.debug("Configuring with I2CP options " + i2cpOpts);
|
|
|
|
}
|
|
|
|
//I2PSnarkUtil.instance().setI2CPConfig("66.111.51.110", 7654, new Properties());
|
|
|
|
String eepHost = _config.getProperty(PROP_EEP_HOST);
|
|
|
|
int eepPort = getInt(PROP_EEP_PORT, 4444);
|
|
|
|
if (eepHost != null)
|
|
|
|
I2PSnarkUtil.instance().setProxy(eepHost, eepPort);
|
|
|
|
getDataDir().mkdirs();
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getInt(String prop, int defaultVal) {
|
|
|
|
String p = _config.getProperty(prop);
|
|
|
|
try {
|
|
|
|
if ( (p != null) && (p.trim().length() > 0) )
|
|
|
|
return Integer.parseInt(p.trim());
|
|
|
|
} catch (NumberFormatException nfe) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
return defaultVal;
|
|
|
|
}
|
|
|
|
|
2005-12-16 03:00:48 +00:00
|
|
|
public void updateConfig(String dataDir, boolean autoStart, String seedPct, String eepHost,
|
|
|
|
String eepPort, String i2cpHost, String i2cpPort, String i2cpOpts) {
|
|
|
|
boolean changed = false;
|
|
|
|
if (eepHost != null) {
|
|
|
|
int port = I2PSnarkUtil.instance().getEepProxyPort();
|
|
|
|
try { port = Integer.parseInt(eepPort); } catch (NumberFormatException nfe) {}
|
|
|
|
String host = I2PSnarkUtil.instance().getEepProxyHost();
|
|
|
|
if ( (eepHost.trim().length() > 0) && (port > 0) &&
|
|
|
|
((!host.equals(eepHost) || (port != I2PSnarkUtil.instance().getEepProxyPort()) )) ) {
|
|
|
|
I2PSnarkUtil.instance().setProxy(eepHost, port);
|
|
|
|
changed = true;
|
|
|
|
_config.setProperty(PROP_EEP_HOST, eepHost);
|
|
|
|
_config.setProperty(PROP_EEP_PORT, eepPort+"");
|
|
|
|
addMessage("EepProxy location changed to " + eepHost + ":" + port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i2cpHost != null) {
|
|
|
|
int oldI2CPPort = I2PSnarkUtil.instance().getI2CPPort();
|
|
|
|
String oldI2CPHost = I2PSnarkUtil.instance().getI2CPHost();
|
|
|
|
int port = oldI2CPPort;
|
|
|
|
try { port = Integer.parseInt(i2cpPort); } catch (NumberFormatException nfe) {}
|
|
|
|
String host = oldI2CPHost;
|
|
|
|
Map opts = new HashMap();
|
|
|
|
if (i2cpOpts == null) i2cpOpts = "";
|
|
|
|
StringTokenizer tok = new StringTokenizer(i2cpOpts, " \t\n");
|
|
|
|
while (tok.hasMoreTokens()) {
|
|
|
|
String pair = tok.nextToken();
|
|
|
|
int split = pair.indexOf('=');
|
|
|
|
if (split > 0)
|
|
|
|
opts.put(pair.substring(0, split), pair.substring(split+1));
|
|
|
|
}
|
|
|
|
Map oldOpts = new HashMap();
|
|
|
|
String oldI2CPOpts = _config.getProperty(PROP_I2CP_OPTS);
|
|
|
|
if (oldI2CPOpts == null) oldI2CPOpts = "";
|
|
|
|
tok = new StringTokenizer(oldI2CPOpts, " \t\n");
|
|
|
|
while (tok.hasMoreTokens()) {
|
|
|
|
String pair = tok.nextToken();
|
|
|
|
int split = pair.indexOf('=');
|
|
|
|
if (split > 0)
|
|
|
|
oldOpts.put(pair.substring(0, split), pair.substring(split+1));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (i2cpHost.trim().length() > 0) && (port > 0) &&
|
|
|
|
((!host.equals(i2cpHost) ||
|
|
|
|
(port != I2PSnarkUtil.instance().getI2CPPort()) ||
|
|
|
|
(!oldOpts.equals(opts)))) ) {
|
|
|
|
boolean snarksActive = false;
|
|
|
|
Set names = listTorrentFiles();
|
|
|
|
for (Iterator iter = names.iterator(); iter.hasNext(); ) {
|
|
|
|
Snark snark = getTorrent((String)iter.next());
|
|
|
|
if ( (snark != null) && (!snark.stopped) ) {
|
|
|
|
snarksActive = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (snarksActive) {
|
|
|
|
addMessage("Cannot change the I2CP settings while torrents are active");
|
|
|
|
_log.debug("i2cp host [" + i2cpHost + "] i2cp port " + port + " opts [" + opts
|
|
|
|
+ "] oldOpts [" + oldOpts + "]");
|
|
|
|
} else {
|
|
|
|
if (I2PSnarkUtil.instance().connected()) {
|
|
|
|
I2PSnarkUtil.instance().disconnect();
|
|
|
|
addMessage("Disconnecting old I2CP destination");
|
|
|
|
}
|
|
|
|
Properties p = new Properties();
|
|
|
|
p.putAll(opts);
|
|
|
|
addMessage("I2CP settings changed to " + i2cpHost + ":" + port + " (" + i2cpOpts.trim() + ")");
|
|
|
|
I2PSnarkUtil.instance().setI2CPConfig(i2cpHost, port, p);
|
|
|
|
boolean ok = I2PSnarkUtil.instance().connect();
|
|
|
|
if (!ok) {
|
|
|
|
addMessage("Unable to connect with the new settings, reverting to the old I2CP settings");
|
|
|
|
I2PSnarkUtil.instance().setI2CPConfig(oldI2CPHost, oldI2CPPort, oldOpts);
|
|
|
|
ok = I2PSnarkUtil.instance().connect();
|
|
|
|
if (!ok)
|
|
|
|
addMessage("Unable to reconnect with the old settings!");
|
|
|
|
} else {
|
|
|
|
addMessage("Reconnected on the new I2CP destination");
|
|
|
|
_config.setProperty(PROP_I2CP_HOST, i2cpHost.trim());
|
|
|
|
_config.setProperty(PROP_I2CP_PORT, "" + port);
|
|
|
|
_config.setProperty(PROP_I2CP_OPTS, i2cpOpts.trim());
|
|
|
|
changed = true;
|
|
|
|
// no PeerAcceptors/I2PServerSockets to deal with, since all snarks are inactive
|
|
|
|
for (Iterator iter = names.iterator(); iter.hasNext(); ) {
|
|
|
|
String name = (String)iter.next();
|
|
|
|
Snark snark = getTorrent(name);
|
|
|
|
if ( (snark != null) && (snark.acceptor != null) ) {
|
|
|
|
snark.acceptor.restart();
|
|
|
|
addMessage("I2CP listener restarted for " + snark.meta.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
2005-12-21 12:04:54 +00:00
|
|
|
if (shouldAutoStart() != autoStart) {
|
|
|
|
_config.setProperty(PROP_AUTO_START, autoStart + "");
|
|
|
|
addMessage("Adjusted autostart to " + autoStart);
|
|
|
|
changed = true;
|
|
|
|
}
|
2005-12-16 03:00:48 +00:00
|
|
|
if (changed) {
|
|
|
|
saveConfig();
|
|
|
|
} else {
|
|
|
|
addMessage("Configuration unchanged");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-15 08:58:30 +00:00
|
|
|
public void saveConfig() {
|
|
|
|
try {
|
|
|
|
DataHelper.storeProps(_config, new File(_configFile));
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
addMessage("Unable to save the config to '" + _configFile + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-16 03:00:48 +00:00
|
|
|
public Properties getConfig() { return _config; }
|
|
|
|
|
2005-12-16 08:24:21 +00:00
|
|
|
/** hardcoded for sanity. perhaps this should be customizable, for people who increase their ulimit, etc. */
|
|
|
|
private static final int MAX_FILES_PER_TORRENT = 128;
|
|
|
|
|
2005-12-15 08:58:30 +00:00
|
|
|
/** set of filenames that we are dealing with */
|
|
|
|
public Set listTorrentFiles() { synchronized (_snarks) { return new HashSet(_snarks.keySet()); } }
|
|
|
|
/**
|
|
|
|
* Grab the torrent given the (canonical) filename
|
|
|
|
*/
|
|
|
|
public Snark getTorrent(String filename) { synchronized (_snarks) { return (Snark)_snarks.get(filename); } }
|
2005-12-31 23:40:20 +00:00
|
|
|
public void addTorrent(String filename) { addTorrent(filename, false); }
|
|
|
|
public void addTorrent(String filename, boolean dontAutoStart) {
|
2005-12-16 08:24:21 +00:00
|
|
|
if (!I2PSnarkUtil.instance().connected()) {
|
|
|
|
addMessage("Connecting to I2P");
|
|
|
|
boolean ok = I2PSnarkUtil.instance().connect();
|
|
|
|
if (!ok) {
|
|
|
|
addMessage("Error connecting to I2P - check your I2CP settings");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2005-12-15 08:58:30 +00:00
|
|
|
File sfile = new File(filename);
|
|
|
|
try {
|
|
|
|
filename = sfile.getCanonicalPath();
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
_log.error("Unable to add the torrent " + filename, ioe);
|
|
|
|
addMessage("ERR: Could not add the torrent '" + filename + "': " + ioe.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
File dataDir = getDataDir();
|
|
|
|
Snark torrent = null;
|
|
|
|
synchronized (_snarks) {
|
|
|
|
torrent = (Snark)_snarks.get(filename);
|
|
|
|
if (torrent == null) {
|
2005-12-16 08:24:21 +00:00
|
|
|
FileInputStream fis = null;
|
|
|
|
try {
|
|
|
|
fis = new FileInputStream(sfile);
|
|
|
|
MetaInfo info = new MetaInfo(fis);
|
|
|
|
fis.close();
|
|
|
|
fis = null;
|
|
|
|
|
2005-12-17 03:47:02 +00:00
|
|
|
String rejectMessage = locked_validateTorrent(info);
|
|
|
|
if (rejectMessage != null) {
|
2005-12-16 08:24:21 +00:00
|
|
|
sfile.delete();
|
2005-12-17 03:47:02 +00:00
|
|
|
addMessage(rejectMessage);
|
2005-12-16 08:24:21 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
torrent = new Snark(filename, null, -1, null, null, false, dataDir.getPath());
|
|
|
|
torrent.completeListener = this;
|
|
|
|
_snarks.put(filename, torrent);
|
|
|
|
}
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
addMessage("Torrent in " + sfile.getName() + " is invalid: " + ioe.getMessage());
|
|
|
|
if (sfile.exists())
|
|
|
|
sfile.delete();
|
|
|
|
return;
|
|
|
|
} finally {
|
|
|
|
if (fis != null) try { fis.close(); } catch (IOException ioe) {}
|
|
|
|
}
|
2005-12-15 08:58:30 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ok, snark created, now lets start it up or configure it further
|
2005-12-16 03:00:48 +00:00
|
|
|
File f = new File(filename);
|
2005-12-31 23:40:20 +00:00
|
|
|
if (!dontAutoStart && shouldAutoStart()) {
|
2005-12-15 08:58:30 +00:00
|
|
|
torrent.startTorrent();
|
2005-12-16 03:00:48 +00:00
|
|
|
addMessage("Torrent added and started: '" + f.getName() + "'");
|
2005-12-15 08:58:30 +00:00
|
|
|
} else {
|
2005-12-16 03:00:48 +00:00
|
|
|
addMessage("Torrent added: '" + f.getName() + "'");
|
2005-12-15 08:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-17 03:47:02 +00:00
|
|
|
private String locked_validateTorrent(MetaInfo info) throws IOException {
|
|
|
|
List files = info.getFiles();
|
|
|
|
if ( (files != null) && (files.size() > MAX_FILES_PER_TORRENT) ) {
|
|
|
|
return "Too many files in " + info.getName() + " (" + files.size() + "), deleting it";
|
|
|
|
} else if (info.getPieces() <= 0) {
|
|
|
|
return "No pieces in " + info.getName() + "? deleting it";
|
2006-09-09 22:15:05 +00:00
|
|
|
} else if (info.getPieceLength(0) > 1*1024*1024) {
|
2005-12-17 03:47:02 +00:00
|
|
|
return "Pieces are too large in " + info.getName() + " (" + info.getPieceLength(0)/1024 + "KB, deleting it";
|
|
|
|
} else if (info.getTotalLength() > 10*1024*1024*1024l) {
|
|
|
|
System.out.println("torrent info: " + info.toString());
|
|
|
|
List lengths = info.getLengths();
|
|
|
|
if (lengths != null)
|
|
|
|
for (int i = 0; i < lengths.size(); i++)
|
|
|
|
System.out.println("File " + i + " is " + lengths.get(i) + " long");
|
|
|
|
|
|
|
|
return "Torrents larger than 10GB are not supported yet (because we're paranoid): " + info.getName() + ", deleting it";
|
|
|
|
} else {
|
|
|
|
// ok
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-15 08:58:30 +00:00
|
|
|
/**
|
|
|
|
* Stop the torrent, leaving it on the list of torrents unless told to remove it
|
|
|
|
*/
|
|
|
|
public Snark stopTorrent(String filename, boolean shouldRemove) {
|
|
|
|
File sfile = new File(filename);
|
|
|
|
try {
|
|
|
|
filename = sfile.getCanonicalPath();
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
_log.error("Unable to remove the torrent " + filename, ioe);
|
|
|
|
addMessage("ERR: Could not remove the torrent '" + filename + "': " + ioe.getMessage());
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
int remaining = 0;
|
|
|
|
Snark torrent = null;
|
|
|
|
synchronized (_snarks) {
|
|
|
|
if (shouldRemove)
|
|
|
|
torrent = (Snark)_snarks.remove(filename);
|
|
|
|
else
|
|
|
|
torrent = (Snark)_snarks.get(filename);
|
|
|
|
remaining = _snarks.size();
|
|
|
|
}
|
|
|
|
if (torrent != null) {
|
2005-12-16 03:00:48 +00:00
|
|
|
boolean wasStopped = torrent.stopped;
|
2005-12-15 08:58:30 +00:00
|
|
|
torrent.stopTorrent();
|
|
|
|
if (remaining == 0) {
|
|
|
|
// should we disconnect/reconnect here (taking care to deal with the other thread's
|
|
|
|
// I2PServerSocket.accept() call properly?)
|
|
|
|
////I2PSnarkUtil.instance().
|
|
|
|
}
|
2005-12-16 03:00:48 +00:00
|
|
|
if (!wasStopped)
|
|
|
|
addMessage("Torrent stopped: '" + sfile.getName() + "'");
|
2005-12-15 08:58:30 +00:00
|
|
|
}
|
|
|
|
return torrent;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Stop the torrent and delete the torrent file itself, but leaving the data
|
|
|
|
* behind.
|
|
|
|
*/
|
|
|
|
public void removeTorrent(String filename) {
|
|
|
|
Snark torrent = stopTorrent(filename, true);
|
|
|
|
if (torrent != null) {
|
|
|
|
File torrentFile = new File(filename);
|
|
|
|
torrentFile.delete();
|
2005-12-16 03:00:48 +00:00
|
|
|
addMessage("Torrent removed: '" + torrentFile.getName() + "'");
|
2005-12-15 08:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class DirMonitor implements Runnable {
|
|
|
|
public void run() {
|
|
|
|
try { Thread.sleep(60*1000*getStartupDelayMinutes()); } catch (InterruptedException ie) {}
|
2005-12-16 08:24:21 +00:00
|
|
|
// the first message was a "We are starting up in 1m"
|
|
|
|
synchronized (_messages) {
|
|
|
|
if (_messages.size() == 1)
|
|
|
|
_messages.remove(0);
|
|
|
|
}
|
|
|
|
|
2005-12-15 08:58:30 +00:00
|
|
|
while (true) {
|
|
|
|
File dir = getDataDir();
|
|
|
|
_log.debug("Directory Monitor loop over " + dir.getAbsolutePath());
|
2005-12-16 23:18:56 +00:00
|
|
|
try {
|
|
|
|
monitorTorrents(dir);
|
|
|
|
} catch (Exception e) {
|
|
|
|
_log.error("Error in the DirectoryMonitor", e);
|
|
|
|
}
|
2005-12-15 08:58:30 +00:00
|
|
|
try { Thread.sleep(60*1000); } catch (InterruptedException ie) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-16 03:00:48 +00:00
|
|
|
public void torrentComplete(Snark snark) {
|
|
|
|
File f = new File(snark.torrent);
|
|
|
|
long len = snark.meta.getTotalLength();
|
|
|
|
addMessage("Download complete of " + f.getName()
|
2005-12-16 04:01:05 +00:00
|
|
|
+ (len < 5*1024*1024 ? " (size: " + (len/1024) + "KB)" : " (size: " + (len/(1024*1024l)) + "MB)"));
|
2005-12-16 03:00:48 +00:00
|
|
|
}
|
|
|
|
|
2005-12-15 08:58:30 +00:00
|
|
|
private void monitorTorrents(File dir) {
|
|
|
|
String fileNames[] = dir.list(TorrentFilenameFilter.instance());
|
|
|
|
List foundNames = new ArrayList(0);
|
|
|
|
if (fileNames != null) {
|
|
|
|
for (int i = 0; i < fileNames.length; i++) {
|
|
|
|
try {
|
|
|
|
foundNames.add(new File(dir, fileNames[i]).getCanonicalPath());
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
_log.error("Error resolving '" + fileNames[i] + "' in '" + dir, ioe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Set existingNames = listTorrentFiles();
|
|
|
|
// lets find new ones first...
|
|
|
|
for (int i = 0; i < foundNames.size(); i++) {
|
|
|
|
if (existingNames.contains(foundNames.get(i))) {
|
|
|
|
// already known. noop
|
|
|
|
} else {
|
2005-12-16 03:00:48 +00:00
|
|
|
if (I2PSnarkUtil.instance().connect())
|
|
|
|
addTorrent((String)foundNames.get(i));
|
|
|
|
else
|
|
|
|
addMessage("Unable to connect to I2P");
|
2005-12-15 08:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// now lets see which ones have been removed...
|
|
|
|
for (Iterator iter = existingNames.iterator(); iter.hasNext(); ) {
|
|
|
|
String name = (String)iter.next();
|
|
|
|
if (foundNames.contains(name)) {
|
|
|
|
// known and still there. noop
|
|
|
|
} else {
|
|
|
|
// known, but removed. drop it
|
|
|
|
stopTorrent(name, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-12-31 23:40:20 +00:00
|
|
|
|
|
|
|
private static final String DEFAULT_TRACKERS[] = {
|
|
|
|
"Postman's tracker", "http://YRgrgTLGnbTq2aZOZDJQ~o6Uk5k6TK-OZtx0St9pb0G-5EGYURZioxqYG8AQt~LgyyI~NCj6aYWpPO-150RcEvsfgXLR~CxkkZcVpgt6pns8SRc3Bi-QSAkXpJtloapRGcQfzTtwllokbdC-aMGpeDOjYLd8b5V9Im8wdCHYy7LRFxhEtGb~RL55DA8aYOgEXcTpr6RPPywbV~Qf3q5UK55el6Kex-6VCxreUnPEe4hmTAbqZNR7Fm0hpCiHKGoToRcygafpFqDw5frLXToYiqs9d4liyVB-BcOb0ihORbo0nS3CLmAwZGvdAP8BZ7cIYE3Z9IU9D1G8JCMxWarfKX1pix~6pIA-sp1gKlL1HhYhPMxwyxvuSqx34o3BqU7vdTYwWiLpGM~zU1~j9rHL7x60pVuYaXcFQDR4-QVy26b6Pt6BlAZoFmHhPcAuWfu-SFhjyZYsqzmEmHeYdAwa~HojSbofg0TMUgESRXMw6YThK1KXWeeJVeztGTz25sL8AAAA.i2p/announce.php"
|
2006-11-10 01:44:35 +00:00
|
|
|
, "eBook Tracker", "http://E71FRom6PZNEqTN2Lr8P-sr23b7HJVC32KoGnVQjaX6zJiXwhJy2HsXob36Qmj81TYFZdewFZa9mSJ533UZgGyQkXo2ahctg82JKYZfDe5uDxAn1E9YPjxZCWJaFJh0S~UwSs~9AZ7UcauSJIoNtpxrtbmRNVFLqnkEDdLZi26TeucfOmiFmIWnVblLniWv3tG1boE9Abd-6j3FmYVrRucYuepAILYt6katmVNOk6sXmno1Eynrp~~MBuFq0Ko6~jsc2E2CRVYXDhGHEMdt-j6JUz5D7S2RIVzDRqQyAZLKJ7OdQDmI31przzmne1vOqqqLC~1xUumZVIvF~yOeJUGNjJ1Vx0J8i2BQIusn1pQJ6UCB~ZtZZLQtEb8EPVCfpeRi2ri1M5CyOuxN0V5ekmPHrYIBNevuTCRC26NP7ZS5VDgx1~NaC3A-CzJAE6f1QXi0wMI9aywNG5KGzOPifcsih8eyGyytvgLtrZtV7ykzYpPCS-rDfITncpn5hliPUAAAA.i2p/pub/bt/announce.php"
|
|
|
|
, "Gaytorrents Tracker", "http://uxPWHbK1OIj9HxquaXuhMiIvi21iK0~ZiG9d8G0840ZXIg0r6CbiV71xlsqmdnU6wm0T2LySriM0doW2gUigo-5BNkUquHwOjLROiETnB3ZR0Ml4IGa6QBPn1aAq2d9~g1r1nVjLE~pcFnXB~cNNS7kIhX1d6nLgYVZf0C2cZopEow2iWVUggGGnAA9mHjE86zLEnTvAyhbAMTqDQJhEuLa0ZYSORqzJDMkQt90MV4YMjX1ICY6RfUSFmxEqu0yWTrkHsTtRw48l~dz9wpIgc0a0T9C~eeWvmBFTqlJPtQZwntpNeH~jF7nlYzB58olgV2HHFYpVYD87DYNzTnmNWxCJ5AfDorm6AIUCV2qaE7tZtI1h6fbmGpGlPyW~Kw5GXrRfJwNvr6ajwAVi~bPVnrBwDZezHkfW4slOO8FACPR28EQvaTu9nwhAbqESxV2hCTq6vQSGjuxHeOuzBOEvRWkLKOHWTC09t2DbJ94FSqETmZopTB1ukEmaxRWbKSIaAAAA.i2p/announce.php"
|
2006-03-04 23:50:01 +00:00
|
|
|
, "Orion's tracker", "http://gKik1lMlRmuroXVGTZ~7v4Vez3L3ZSpddrGZBrxVriosCQf7iHu6CIk8t15BKsj~P0JJpxrofeuxtm7SCUAJEr0AIYSYw8XOmp35UfcRPQWyb1LsxUkMT4WqxAT3s1ClIICWlBu5An~q-Mm0VFlrYLIPBWlUFnfPR7jZ9uP5ZMSzTKSMYUWao3ejiykr~mtEmyls6g-ZbgKZawa9II4zjOy-hdxHgP-eXMDseFsrym4Gpxvy~3Fv9TuiSqhpgm~UeTo5YBfxn6~TahKtE~~sdCiSydqmKBhxAQ7uT9lda7xt96SS09OYMsIWxLeQUWhns-C~FjJPp1D~IuTrUpAFcVEGVL-BRMmdWbfOJEcWPZ~CBCQSO~VkuN1ebvIOr9JBerFMZSxZtFl8JwcrjCIBxeKPBmfh~xYh16BJm1BBBmN1fp2DKmZ2jBNkAmnUbjQOqWvUcehrykWk5lZbE7bjJMDFH48v3SXwRuDBiHZmSbsTY6zhGY~GkMQHNGxPMMSIAAAA.i2p/bt/announce.php"
|
2005-12-31 23:40:20 +00:00
|
|
|
// , "The freak's tracker", "http://mHKva9x24E5Ygfey2llR1KyQHv5f8hhMpDMwJDg1U-hABpJ2NrQJd6azirdfaR0OKt4jDlmP2o4Qx0H598~AteyD~RJU~xcWYdcOE0dmJ2e9Y8-HY51ie0B1yD9FtIV72ZI-V3TzFDcs6nkdX9b81DwrAwwFzx0EfNvK1GLVWl59Ow85muoRTBA1q8SsZImxdyZ-TApTVlMYIQbdI4iQRwU9OmmtefrCe~ZOf4UBS9-KvNIqUL0XeBSqm0OU1jq-D10Ykg6KfqvuPnBYT1BYHFDQJXW5DdPKwcaQE4MtAdSGmj1epDoaEBUa9btQlFsM2l9Cyn1hzxqNWXELmx8dRlomQLlV4b586dRzW~fLlOPIGC13ntPXogvYvHVyEyptXkv890jC7DZNHyxZd5cyrKC36r9huKvhQAmNABT2Y~pOGwVrb~RpPwT0tBuPZ3lHYhBFYmD8y~AOhhNHKMLzea1rfwTvovBMByDdFps54gMN1mX4MbCGT4w70vIopS9yAAAA.i2p/bytemonsoon/announce.php"
|
|
|
|
};
|
|
|
|
|
|
|
|
/** comma delimited list of name=announceURL for the trackers to be displayed */
|
|
|
|
public static final String PROP_TRACKERS = "i2psnark.trackers";
|
|
|
|
/** unordered map of announceURL to name */
|
|
|
|
public Map getTrackers() {
|
|
|
|
HashMap rv = new HashMap();
|
|
|
|
String trackers = _config.getProperty(PROP_TRACKERS);
|
|
|
|
if ( (trackers == null) || (trackers.trim().length() <= 0) )
|
|
|
|
trackers = _context.getProperty(PROP_TRACKERS);
|
|
|
|
if ( (trackers == null) || (trackers.trim().length() <= 0) ) {
|
|
|
|
for (int i = 0; i < DEFAULT_TRACKERS.length; i += 2)
|
|
|
|
rv.put(DEFAULT_TRACKERS[i+1], DEFAULT_TRACKERS[i]);
|
|
|
|
} else {
|
|
|
|
StringTokenizer tok = new StringTokenizer(trackers, ",");
|
|
|
|
while (tok.hasMoreTokens()) {
|
|
|
|
String pair = tok.nextToken();
|
|
|
|
int split = pair.indexOf('=');
|
|
|
|
if (split <= 0)
|
|
|
|
continue;
|
|
|
|
String name = pair.substring(0, split).trim();
|
|
|
|
String url = pair.substring(split+1).trim();
|
|
|
|
if ( (name.length() > 0) && (url.length() > 0) )
|
|
|
|
rv.put(url, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2005-12-15 08:58:30 +00:00
|
|
|
|
|
|
|
private static class TorrentFilenameFilter implements FilenameFilter {
|
|
|
|
private static final TorrentFilenameFilter _filter = new TorrentFilenameFilter();
|
|
|
|
public static TorrentFilenameFilter instance() { return _filter; }
|
|
|
|
public boolean accept(File dir, String name) {
|
|
|
|
return (name != null) && (name.endsWith(".torrent"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|