2004-09-21 jrandom

* Have two tiers of hosts.txt files - the standard "hosts.txt" and
      the new "userhosts.txt".  Updates to I2P will only overwrite the former,
      but values stored in the later take precedence.  Both are queried on
      lookup.
This commit is contained in:
jrandom
2004-09-22 00:10:26 +00:00
committed by zzz
parent e686c0e0a2
commit 54dce61a95
3 changed files with 56 additions and 31 deletions

View File

@ -10,7 +10,10 @@ package net.i2p.client.naming;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.StringTokenizer;
import net.i2p.I2PAppContext; import net.i2p.I2PAppContext;
import net.i2p.data.Destination; import net.i2p.data.Destination;
@ -34,42 +37,58 @@ public class HostsTxtNamingService extends NamingService {
* If this system property is specified, the tunnel will read the * If this system property is specified, the tunnel will read the
* given file for hostname=destKey values when resolving names * given file for hostname=destKey values when resolving names
*/ */
public final static String PROP_HOSTS_FILE = "i2p.hostsfile"; public final static String PROP_HOSTS_FILE = "i2p.hostsfilelist";
/** default hosts.txt filename */ /** default hosts.txt filename */
public final static String DEFAULT_HOSTS_FILE = "hosts.txt"; public final static String DEFAULT_HOSTS_FILE = "userhosts.txt,hosts.txt";
private final static Log _log = new Log(HostsTxtNamingService.class); private final static Log _log = new Log(HostsTxtNamingService.class);
private List getFilenames() {
String list = _context.getProperty(PROP_HOSTS_FILE, DEFAULT_HOSTS_FILE);
StringTokenizer tok = new StringTokenizer(list, ",");
List rv = new ArrayList(tok.countTokens());
while (tok.hasMoreTokens())
rv.add(tok.nextToken());
return rv;
}
public Destination lookup(String hostname) { public Destination lookup(String hostname) {
// Try to look it up in hosts.txt // check the list each time, reloading the file on each
// Reload file each time to catch changes. // lookup
// (and it's easier :P
String hostsfile = _context.getProperty(PROP_HOSTS_FILE, DEFAULT_HOSTS_FILE); List filenames = getFilenames();
Properties hosts = new Properties(); for (int i = 0; i < filenames.size(); i++) {
FileInputStream fis = null; String hostsfile = (String)filenames.get(i);
try { Properties hosts = new Properties();
File f = new File(hostsfile); FileInputStream fis = null;
if (f.canRead()) { try {
fis = new FileInputStream(f); File f = new File(hostsfile);
hosts.load(fis); if ( (f.exists()) && (f.canRead()) ) {
} else { fis = new FileInputStream(f);
_log.error("Hosts file " + hostsfile + " does not exist."); hosts.load(fis);
}
} catch (Exception ioe) { String key = hosts.getProperty(hostname);
_log.error("Error loading hosts file " + hostsfile, ioe); if ( (key != null) && (key.trim().length() > 0) ) {
} finally { return lookupBase64(key);
if (fis != null) try { }
fis.close();
} catch (IOException ioe) { // nop } else {
_log.warn("Hosts file " + hostsfile + " does not exist.");
}
} catch (Exception ioe) {
_log.error("Error loading hosts file " + hostsfile, ioe);
} finally {
if (fis != null) try {
fis.close();
} catch (IOException ioe) { // nop
}
} }
// not found, continue to the next file
} }
String res = hosts.getProperty(hostname); // If we can't find name in any of the hosts files,
// If we can't find name in hosts, assume it's a key. // assume it's a key.
if ((res == null) || (res.trim().length() == 0)) { return lookupBase64(hostname);
res = hostname;
}
return lookupBase64(res);
} }
public String reverseLookup(Destination dest) { public String reverseLookup(Destination dest) {

View File

@ -1,4 +1,10 @@
$Id: history.txt,v 1.17 2004/09/12 22:08:17 jrandom Exp $ $Id: history.txt,v 1.18 2004/09/16 18:55:12 jrandom Exp $
2004-09-21 jrandom
* Have two tiers of hosts.txt files - the standard "hosts.txt" and
the new "userhosts.txt". Updates to I2P will only overwrite the former,
but values stored in the later take precedence. Both are queried on
lookup.
2004-09-16 jrandom 2004-09-16 jrandom
* Refactor the TCP transport to deal with changing identities gracefully, * Refactor the TCP transport to deal with changing identities gracefully,

View File

@ -15,9 +15,9 @@ import net.i2p.CoreVersion;
* *
*/ */
public class RouterVersion { public class RouterVersion {
public final static String ID = "$Revision: 1.30 $ $Date: 2004/09/12 22:08:16 $"; public final static String ID = "$Revision: 1.31 $ $Date: 2004/09/16 18:55:12 $";
public final static String VERSION = "0.4.0.1"; public final static String VERSION = "0.4.0.1";
public final static long BUILD = 2; public final static long BUILD = 3;
public static void main(String args[]) { public static void main(String args[]) {
System.out.println("I2P Router version: " + VERSION); System.out.println("I2P Router version: " + VERSION);
System.out.println("Router ID: " + RouterVersion.ID); System.out.println("Router ID: " + RouterVersion.ID);