i2ptunnel: Fixes and cleanups for command line testing;

catch IAE from getInstance() if i2ptunnel.config isn't found
in app context; log tweaks; config command tweaks
Unit tests: Fix several NPEs in LocalClientManager,
implement HostLookup
This commit is contained in:
zzz
2015-04-22 11:59:40 +00:00
parent 212f6b472a
commit e1d9e05b8d
7 changed files with 124 additions and 25 deletions

View File

@@ -1328,25 +1328,30 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
* @param l logger to receive events and output
*/
private void runConfig(String args[], Logging l) {
if (args.length >= 2) {
if (args.length >= 1) {
int i = 0;
if (args[0].equals("-s")) {
boolean ssl = args[0].equals("-s");
if (ssl) {
_clientOptions.setProperty("i2cp.SSL", "true");
i++;
} else {
_clientOptions.remove("i2cp.SSL");
}
host = args[i++];
listenHost = host;
port = args[i];
if (i < args.length) {
host = args[i++];
listenHost = host;
}
if (i < args.length)
port = args[i];
l.log("New setting: " + host + ' ' + port + (ssl ? " SSL" : " non-SSL"));
notifyEvent("configResult", "ok");
} else {
boolean ssl = Boolean.parseBoolean(_clientOptions.getProperty("i2cp.SSL"));
l.log("Usage:\n" +
" config [-s] <i2phost> <i2pport>\n" +
" sets the connection to the i2p router.\n" +
"Current setting:\n" +
" " + host + ' ' + port + (ssl ? " SSL" : ""));
" config [-s] [<i2phost>] [<i2pport>]\n" +
" Sets the address and port of the I2P router.\n" +
" Use -s for SSL.\n" +
"Current setting: " + host + ' ' + port + (ssl ? " SSL" : " non-SSL"));
notifyEvent("configResult", "error");
}
}
@@ -1750,8 +1755,8 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
*/
public void log(String s) {
System.out.println(s);
if (_log.shouldLog(Log.INFO))
_log.info(getPrefix() + "Display: " + s);
//if (_log.shouldLog(Log.INFO))
// _log.info(getPrefix() + "Display: " + s);
}
/**
@@ -1769,8 +1774,7 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
l.log("Generating new keys...");
I2PClient client = I2PClientFactory.createClient();
Destination d = client.createDestination(writeTo);
l.log("Secret key saved.\n" +
"Public key: " + d.toBase64());
l.log("New destination: " + d.toBase32());
writeTo.flush();
writeTo.close();
writePubKey(d, pubDest, l);
@@ -1793,7 +1797,7 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
try {
Destination d = new Destination();
d.readBytes(readFrom);
l.log("Public key: " + d.toBase64());
l.log("Destination: " + d.toBase32());
readFrom.close();
writePubKey(d, pubDest, l);
} catch (I2PException ex) {
@@ -1808,7 +1812,7 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging {
* Deprecated - only used by CLI
*
* @param d Destination to write
* @param o stream to write the destination to
* @param o stream to write the destination to, or null for noop
* @param l logger to send messages to
*/
private static void writePubKey(Destination d, OutputStream o, Logging l) throws I2PException, IOException {

View File

@@ -102,6 +102,7 @@ public class TunnelControllerGroup implements ClientApp {
* @param mgr may be null
* @param args one arg, the config file, if not absolute will be relative to the context's config dir,
* if empty or null, the default is i2ptunnel.config
* @throws IllegalArgumentException if too many args
* @since 0.9.4
*/
public TunnelControllerGroup(I2PAppContext context, ClientAppManager mgr, String[] args) {
@@ -149,7 +150,19 @@ public class TunnelControllerGroup implements ClientApp {
* @since 0.9.4
*/
public void startup() {
loadControllers(_configFile);
try {
loadControllers(_configFile);
} catch (IllegalArgumentException iae) {
if (DEFAULT_CONFIG_FILE.equals(_configFile) && !_context.isRouterContext()) {
// for i2ptunnel command line
synchronized (_controllersLoadedLock) {
_controllersLoaded = true;
}
_log.logAlways(Log.WARN, "Not in router context and no preconfigured tunnels");
} else {
throw iae;
}
}
startControllers();
if (_mgr != null)
_mgr.register(this);
@@ -274,8 +287,12 @@ public class TunnelControllerGroup implements ClientApp {
synchronized (_controllersLoadedLock) {
_controllersLoaded = true;
}
if (_log.shouldLog(Log.INFO))
_log.info(i + " controllers loaded from " + configFile);
if (i > 0) {
if (_log.shouldLog(Log.INFO))
_log.info(i + " controllers loaded from " + configFile);
} else {
_log.logAlways(Log.WARN, "No i2ptunnel configurations found in " + configFile);
}
}
/**
@@ -294,6 +311,10 @@ public class TunnelControllerGroup implements ClientApp {
synchronized(TunnelControllerGroup.this) {
_controllersLock.readLock().lock();
try {
if (_controllers.size() <= 0) {
_log.logAlways(Log.WARN, "No configured tunnels to start");
return;
}
for (int i = 0; i < _controllers.size(); i++) {
TunnelController controller = _controllers.get(i);
if (controller.getStartOnLoad())