From 3bf95e566c9b5f20f0d03fdb1b6137cf97f57ddd Mon Sep 17 00:00:00 2001 From: zzz Date: Thu, 21 Jan 2010 14:17:37 +0000 Subject: [PATCH] * I2PTunnelServer: Fix bug preventing connection retries at startup from working --- .../net/i2p/i2ptunnel/I2PTunnelServer.java | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java index ff21c27c4..a00fc7d8d 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java @@ -4,6 +4,7 @@ package net.i2p.i2ptunnel; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -98,6 +99,7 @@ public class I2PTunnelServer extends I2PTunnelTask implements Runnable { else _usePool = DEFAULT_USE_POOL; } + private void init(InetAddress host, int port, InputStream privData, String privkeyname, Logging l) { this.l = l; this.remoteHost = host; @@ -113,15 +115,26 @@ public class I2PTunnelServer extends I2PTunnelTask implements Runnable { } } + // copy the privData to a new BAIS, so we can always reset() it if we have to retry + ByteArrayInputStream privDataCopy; + try { + privDataCopy = copyOfInputStream(privData); + } catch (IOException ioe) { + _log.log(Log.CRIT, "Cannot read private key data for " + privkeyname, ioe); + return; + } + + // Todo: Can't stop a tunnel from the UI while it's in this loop (no session yet) while (sockMgr == null) { synchronized (slock) { - sockMgr = I2PSocketManagerFactory.createManager(privData, getTunnel().host, portNum, + sockMgr = I2PSocketManagerFactory.createManager(privDataCopy, getTunnel().host, portNum, props); } if (sockMgr == null) { _log.log(Log.CRIT, "Unable to create socket manager"); try { Thread.sleep(10*1000); } catch (InterruptedException ie) {} + privDataCopy.reset(); } } @@ -132,6 +145,24 @@ public class I2PTunnelServer extends I2PTunnelTask implements Runnable { open = true; } + /** + * Copy input stream to a byte array, so we can retry + * @since 0.7.10 + */ + private static ByteArrayInputStream copyOfInputStream(InputStream is) throws IOException { + byte[] buf = new byte[128]; + ByteArrayOutputStream os = new ByteArrayOutputStream(768); + try { + int read; + while ((read = is.read(buf)) >= 0) { + os.write(buf, 0, read); + } + } finally { + try { is.close(); } catch (IOException ioe) {} + // don't need to close BAOS + } + return new ByteArrayInputStream(os.toByteArray()); + } /** * Start running the I2PTunnelServer.