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,27 +37,44 @@ 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();
for (int i = 0; i < filenames.size(); i++) {
String hostsfile = (String)filenames.get(i);
Properties hosts = new Properties(); Properties hosts = new Properties();
FileInputStream fis = null; FileInputStream fis = null;
try { try {
File f = new File(hostsfile); File f = new File(hostsfile);
if (f.canRead()) { if ( (f.exists()) && (f.canRead()) ) {
fis = new FileInputStream(f); fis = new FileInputStream(f);
hosts.load(fis); hosts.load(fis);
String key = hosts.getProperty(hostname);
if ( (key != null) && (key.trim().length() > 0) ) {
return lookupBase64(key);
}
} else { } else {
_log.error("Hosts file " + hostsfile + " does not exist."); _log.warn("Hosts file " + hostsfile + " does not exist.");
} }
} catch (Exception ioe) { } catch (Exception ioe) {
_log.error("Error loading hosts file " + hostsfile, ioe); _log.error("Error loading hosts file " + hostsfile, ioe);
@ -64,12 +84,11 @@ public class HostsTxtNamingService extends NamingService {
} catch (IOException ioe) { // nop } catch (IOException ioe) { // nop
} }
} }
String res = hosts.getProperty(hostname); // not found, continue to the next file
// If we can't find name in hosts, assume it's a key.
if ((res == null) || (res.trim().length() == 0)) {
res = hostname;
} }
return lookupBase64(res); // If we can't find name in any of the hosts files,
// assume it's a key.
return lookupBase64(hostname);
} }
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);