From fea62a529bd8d336fa7a17e7d62a82c11662597f Mon Sep 17 00:00:00 2001 From: jrandom <jrandom> Date: Tue, 3 Aug 2004 08:18:10 +0000 Subject: [PATCH] * don't use static props on the I2PTunnel for I2CP / etc so that we can safely keep multiple instances alive * propogate errors (if the I2CP host isnt reachable, the socket manager won't be created) and handle appropriately --- .../java/src/net/i2p/i2ptunnel/I2PTunnel.java | 38 ++++++++++++------- .../net/i2p/i2ptunnel/I2PTunnelClient.java | 8 +++- .../i2p/i2ptunnel/I2PTunnelClientBase.java | 22 ++++++++--- .../i2p/i2ptunnel/I2PTunnelHTTPClient.java | 10 ++++- .../net/i2p/i2ptunnel/I2PTunnelServer.java | 2 +- 5 files changed, 57 insertions(+), 23 deletions(-) diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnel.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnel.java index 3e1960ec06..782d67d36c 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnel.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnel.java @@ -74,13 +74,13 @@ public class I2PTunnel implements Logging, EventDispatcher { public static final int PACKET_DELAY = 100; - public static boolean ownDest = false; + public boolean ownDest = false; - public static String port = System.getProperty(I2PClient.PROP_TCP_PORT, "7654"); - public static String host = System.getProperty(I2PClient.PROP_TCP_HOST, "127.0.0.1"); - public static String listenHost = host; + public String port = System.getProperty(I2PClient.PROP_TCP_PORT, "7654"); + public String host = System.getProperty(I2PClient.PROP_TCP_HOST, "127.0.0.1"); + public String listenHost = host; - public static long readTimeout = -1; + public long readTimeout = -1; private static final String nocli_args[] = { "-nocli", "-die"}; @@ -408,9 +408,9 @@ public class I2PTunnel implements Logging, EventDispatcher { */ public void runClient(String args[], Logging l) { if (args.length == 2) { - int port = -1; + int portNum = -1; try { - port = Integer.parseInt(args[0]); + portNum = Integer.parseInt(args[0]); } catch (NumberFormatException nfe) { l.log("invalid port"); _log.error(getPrefix() + "Port specified is not valid: " + args[0], nfe); @@ -418,9 +418,15 @@ public class I2PTunnel implements Logging, EventDispatcher { return; } I2PTunnelTask task; - task = new I2PTunnelClient(port, args[1], l, ownDest, (EventDispatcher) this, this); - addtask(task); - notifyEvent("clientTaskId", new Integer(task.getId())); + try { + task = new I2PTunnelClient(portNum, args[1], l, ownDest, (EventDispatcher) this, this); + addtask(task); + notifyEvent("clientTaskId", new Integer(task.getId())); + } catch (IllegalArgumentException iae) { + _log.error(getPrefix() + "Invalid I2PTunnel config to create a client [" + host + ":"+ port + "]", iae); + l.log("Invalid I2PTunnel configuration [" + host + ":" + port + "]"); + notifyEvent("clientTaskId", new Integer(-1)); + } } else { l.log("client <port> <pubkey>|file:<pubkeyfile>"); l.log(" creates a client that forwards port to the pubkey.\n" @@ -455,9 +461,15 @@ public class I2PTunnel implements Logging, EventDispatcher { proxy = args[1]; } I2PTunnelTask task; - task = new I2PTunnelHTTPClient(port, l, ownDest, proxy, (EventDispatcher) this, this); - addtask(task); - notifyEvent("httpclientTaskId", new Integer(task.getId())); + try { + task = new I2PTunnelHTTPClient(port, l, ownDest, proxy, (EventDispatcher) this, this); + addtask(task); + notifyEvent("httpclientTaskId", new Integer(task.getId())); + } catch (IllegalArgumentException iae) { + _log.error(getPrefix() + "Invalid I2PTunnel config to create an httpclient [" + host + ":"+ port + "]", iae); + l.log("Invalid I2PTunnel configuration [" + host + ":" + port + "]"); + notifyEvent("httpclientTaskId", new Integer(-1)); + } } else { l.log("httpclient <port> [<proxy>]"); l.log(" creates a client that distributes HTTP requests."); diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClient.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClient.java index ab62413fdf..d5578265b2 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClient.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClient.java @@ -19,7 +19,13 @@ public class I2PTunnelClient extends I2PTunnelClientBase { private static final long DEFAULT_READ_TIMEOUT = 5*60*1000; // -1 protected long readTimeout = DEFAULT_READ_TIMEOUT; - public I2PTunnelClient(int localPort, String destination, Logging l, boolean ownDest, EventDispatcher notifyThis, I2PTunnel tunnel) { + /** + * @throws IllegalArgumentException if the I2PTunnel does not contain + * valid config to contact the router + */ + public I2PTunnelClient(int localPort, String destination, Logging l, + boolean ownDest, EventDispatcher notifyThis, + I2PTunnel tunnel) throws IllegalArgumentException { super(localPort, ownDest, l, notifyThis, "SynSender", tunnel); if (waitEventValue("openBaseClientResult").equals("error")) { diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClientBase.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClientBase.java index 5a2c3ea041..9c51f7e23f 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClientBase.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClientBase.java @@ -60,7 +60,13 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask implements Runna // I2PTunnelClientBase(localPort, ownDest, l, (EventDispatcher)null); //} - public I2PTunnelClientBase(int localPort, boolean ownDest, Logging l, EventDispatcher notifyThis, String handlerName, I2PTunnel tunnel) { + /** + * @throws IllegalArgumentException if the I2CP configuration is b0rked so + * badly that we cant create a socketManager + */ + public I2PTunnelClientBase(int localPort, boolean ownDest, Logging l, + EventDispatcher notifyThis, String handlerName, + I2PTunnel tunnel) throws IllegalArgumentException{ super(localPort + " (uninitialized)", notifyThis, tunnel); _clientId = ++__clientId; this.localPort = localPort; @@ -74,7 +80,10 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask implements Runna sockMgr = getSocketManager(); } } - if (sockMgr == null) throw new NullPointerException(); + if (sockMgr == null) { + l.log("Invalid I2CP configuration"); + throw new IllegalArgumentException("Socket manager could not be created"); + } l.log("I2P session created"); Thread t = new I2PThread(this); @@ -122,7 +131,8 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask implements Runna props.putAll(System.getProperties()); else props.putAll(tunnel.getClientOptions()); - I2PSocketManager sockManager = I2PSocketManagerFactory.createManager(I2PTunnel.host, Integer.parseInt(I2PTunnel.port), props); + I2PSocketManager sockManager = I2PSocketManagerFactory.createManager(tunnel.host, Integer.parseInt(tunnel.port), props); + if (sockManager == null) return null; sockManager.setName("Client"); return sockManager; } @@ -133,9 +143,9 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask implements Runna protected final InetAddress getListenHost(Logging l) { try { - return InetAddress.getByName(I2PTunnel.listenHost); + return InetAddress.getByName(getTunnel().listenHost); } catch (UnknownHostException uhe) { - l.log("Could not find listen host to bind to [" + I2PTunnel.host + "]"); + l.log("Could not find listen host to bind to [" + getTunnel().host + "]"); _log.error("Error finding host to bind", uhe); notifyEvent("openBaseClientResult", "error"); return null; @@ -212,7 +222,7 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask implements Runna localPort = ss.getLocalPort(); } notifyEvent("clientLocalPort", new Integer(ss.getLocalPort())); - l.log("Listening for clients on port " + localPort + " of " + I2PTunnel.listenHost); + l.log("Listening for clients on port " + localPort + " of " + getTunnel().listenHost); // Notify constructor that port is ready synchronized (this) { diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java index bdee31844e..ee964fb6a1 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java @@ -80,8 +80,14 @@ public class I2PTunnelHTTPClient extends I2PTunnelClientBase implements Runnable /** used to assign unique IDs to the threads / clients. no logic or functionality */ private static volatile long __clientId = 0; - - public I2PTunnelHTTPClient(int localPort, Logging l, boolean ownDest, String wwwProxy, EventDispatcher notifyThis, I2PTunnel tunnel) { + + /** + * @throws IllegalArgumentException if the I2PTunnel does not contain + * valid config to contact the router + */ + public I2PTunnelHTTPClient(int localPort, Logging l, boolean ownDest, + String wwwProxy, EventDispatcher notifyThis, + I2PTunnel tunnel) throws IllegalArgumentException { super(localPort, ownDest, l, notifyThis, "HTTPHandler " + (++__clientId), tunnel); if (waitEventValue("openBaseClientResult").equals("error")) { diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java index 9b0e2b0a04..d40062821c 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java @@ -75,7 +75,7 @@ public class I2PTunnelServer extends I2PTunnelTask implements Runnable { Properties props = new Properties(); props.putAll(getTunnel().getClientOptions()); synchronized (slock) { - sockMgr = I2PSocketManagerFactory.createManager(privData, I2PTunnel.host, Integer.parseInt(I2PTunnel.port), + sockMgr = I2PSocketManagerFactory.createManager(privData, getTunnel().host, Integer.parseInt(getTunnel().port), props); } -- GitLab