* Build: Add support for bundling router infos in the package

This commit is contained in:
zzz
2014-08-31 16:19:46 +00:00
parent 6826ba05e7
commit 0c5c18a767
4 changed files with 190 additions and 1 deletions

View File

@ -91,3 +91,9 @@ javac.version=1.6
# Optional properties used in tests to enable additional tools.
#with.cobertura=/PATH/TO/cobertura.jar
#with.clover=/PATH/TO/clover.jar
### Bundle router infos ###
# Set to bundle router infos from your local I2P install in the package
#bundle.routerInfos=true
#bundle.routerInfos.count=200
#bundle.routerInfos.i2pConfigDir=/PATH/TO/.i2p

View File

@ -960,7 +960,7 @@
<copy file="installer/lib/wrapper/all/wrapper.jar" todir="pkg-temp/lib" />
</target>
<target name="preppkg-base" depends="build, preplicenses, prepConsoleDocs, prepthemeupdates, prepCertificates">
<target name="preppkg-base" depends="build, preplicenses, prepConsoleDocs, prepthemeupdates, prepCertificates, prepRouterInfos">
<!-- if updater200 was run previously, it left *.pack files in pkg-temp -->
<!-- Also remove deletelist.txt used for updater only -->
<delete>
@ -1054,6 +1054,26 @@
</copy>
</target>
<condition property="no.bundle.routerInfos">
<isfalse value="${bundle.routerInfos}" />
</condition>
<target name="prepRouterInfos" depends="buildrouter" unless="no.bundle.routerInfos">
<mkdir dir="pkg-temp/netDb" />
<java classname="net.i2p.router.networkdb.kademlia.PersistentDataStore" fork="true" failonerror="true">
<classpath>
<pathelement location="build/i2p.jar" />
<pathelement location="build/router.jar" />
</classpath>
<arg value="-i" />
<arg value="${bundle.routerInfos.i2pConfigDir}" />
<arg value="-o" />
<arg value="pkg-temp/netDb" />
<arg value="-c" />
<arg value="${bundle.routerInfos.count}" />
</java>
</target>
<!-- this is no longer required, izpack 4.3.0 supports headless installs with java -jar i2pinstall.exe -console -->
<!-- and this is no longer used by the SlackBuild -->
<target name="tarball" depends="preppkg">

View File

@ -17,6 +17,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@ -25,10 +26,13 @@ import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentHashMap;
import gnu.getopt.Getopt;
import net.i2p.data.Base64;
import net.i2p.data.DatabaseEntry;
import net.i2p.data.DataFormatException;
import net.i2p.data.Hash;
import net.i2p.data.RouterAddress;
import net.i2p.data.RouterInfo;
import net.i2p.router.JobImpl;
import net.i2p.router.Router;
@ -660,4 +664,161 @@ class PersistentDataStore extends TransientDataStore {
return (name.startsWith(ROUTERINFO_PREFIX.toUpperCase(Locale.US)) && name.endsWith(ROUTERINFO_SUFFIX.toUpperCase(Locale.US)));
}
}
/**
* Usage: PersistentDataStore -i configDir -o toDir -c count
*
* Copy a random selection of 'count' router infos from configDir/netDb
* to 'toDir'. Skip your own router info, and old, hidden, unreachable, and
* introduced routers.
*
* Used in the build process, do not comment out.
*
* @since 0.9.15
*/
public static void main(String[] args) {
Getopt g = new Getopt("PersistentDataStore", args, "i:o:c:");
String in = System.getProperty("user.home") + "/.i2p";
String out = "netDb";
int count = 200;
boolean error = false;
int c;
while ((c = g.getopt()) != -1) {
switch (c) {
case 'i':
in = g.getOptarg();
break;
case 'o':
out = g.getOptarg();
break;
case 'c':
String scount = g.getOptarg();
try {
count = Integer.parseInt(scount);
} catch (NumberFormatException nfe) {
error = true;
}
break;
case '?':
case ':':
default:
error = true;
}
}
if (error) {
usage();
System.exit(1);
}
File confDir = new File(in);
File dbDir = new File(confDir, "netDb");
if (!dbDir.exists()) {
System.out.println("NetDB directory " + dbDir + " does not exist");
System.exit(1);
}
File myFile = new File(confDir, "router.info");
File toDir = new File(out);
toDir.mkdirs();
InputStream fis = null;
Hash me = null;
try {
fis = new BufferedInputStream(new FileInputStream(myFile));
RouterInfo ri = new RouterInfo();
ri.readBytes(fis, true); // true = verify sig on read
me = ri.getIdentity().getHash();
} catch (Exception e) {
//System.out.println("Can't determine our identity");
} finally {
if (fis != null) try { fis.close(); } catch (IOException ioe) {}
}
int routerCount = 0;
List<File> toRead = new ArrayList<File>(2048);
for (int j = 0; j < B64.length(); j++) {
File subdir = new File(dbDir, DIR_PREFIX + B64.charAt(j));
File[] files = subdir.listFiles(RouterInfoFilter.getInstance());
if (files == null)
continue;
routerCount += files.length;
for (int i = 0; i < files.length; i++) {
toRead.add(files[i]);
}
}
if (toRead.isEmpty()) {
System.out.println("No files to copy in " + dbDir);
System.exit(1);
}
Collections.shuffle(toRead);
int copied = 0;
long tooOld = System.currentTimeMillis() - 7*24*60*60*1000L;
for (File file : toRead) {
if (copied >= count)
break;
Hash key = getRouterInfoHash(file.getName());
if (key == null) {
System.out.println("Skipping bad " + file);
continue;
}
if (key.equals(me)) {
System.out.println("Skipping my RI");
continue;
}
fis = null;
try {
fis = new BufferedInputStream(new FileInputStream(file));
RouterInfo ri = new RouterInfo();
ri.readBytes(fis, true); // true = verify sig on read
try { fis.close(); } catch (IOException ioe) {}
fis = null;
if (ri.getPublished() < tooOld) {
System.out.println("Skipping too old " + key);
continue;
}
if (ri.getCapabilities().contains("U")) {
System.out.println("Skipping unreachable " + key);
continue;
}
Collection<RouterAddress> addrs = ri.getAddresses();
if (addrs.isEmpty()) {
System.out.println("Skipping hidden " + key);
continue;
}
boolean hasIntro = false;
for (RouterAddress addr : addrs) {
if ("SSU".equals(addr.getTransportStyle()) && addr.getOption("ihost0") != null) {
hasIntro = true;
break;
}
}
if (hasIntro) {
System.out.println("Skipping introduced " + key);
continue;
}
File toFile = new File(toDir, file.getName());
// We could call ri.write() to avoid simultaneous change by the router
boolean ok = FileUtil.copy(file, toFile, true, true);
if (ok)
copied++;
else
System.out.println("Failed copy of " + file + " to " + toDir);
} catch (Exception e) {
System.out.println("Skipping bad " + file);
} finally {
if (fis != null) try { fis.close(); } catch (IOException ioe) {}
}
}
if (copied > 0) {
System.out.println("Copied " + copied + " router info files to " + toDir);
} else {
System.out.println("Failed to copy any files to " + toDir);
System.exit(1);
}
}
private static void usage() {
System.err.println("Usage: PersistentDataStore [-i $HOME/.i2p] [-o netDb/] [-c 200]");
}
}

View File

@ -271,6 +271,8 @@ public class WorkingDir {
// We don't currently have a default addressbook/ in the base distribution,
// but distros might put one in
"addressbook,eepsite," +
// 0.9.15 support bundled router infos
"netDb," +
// base install - files
// We don't currently have a default router.config, logger.config, susimail.config, or webapps.config in the base distribution,
// but distros might put one in