From 1ced6043547210c98c2b707af1678c2af9c251a4 Mon Sep 17 00:00:00 2001 From: zzz Date: Sun, 17 Jul 2011 16:31:21 +0000 Subject: [PATCH 1/3] * UDP: Lower max port below 31000 since wrapper uses that --- router/java/src/net/i2p/router/transport/udp/UDPEndpoint.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/router/java/src/net/i2p/router/transport/udp/UDPEndpoint.java b/router/java/src/net/i2p/router/transport/udp/UDPEndpoint.java index b59c40d4b..177309512 100644 --- a/router/java/src/net/i2p/router/transport/udp/UDPEndpoint.java +++ b/router/java/src/net/i2p/router/transport/udp/UDPEndpoint.java @@ -80,11 +80,11 @@ class UDPEndpoint { } ********/ - /** 8998 is monotone, and 32000 is the wrapper, so let's stay between those */ + /** 8998 is monotone, and 31000 is the wrapper outbound, so let's stay between those */ public static final String PROP_MIN_PORT = "i2np.udp.minPort"; public static final String PROP_MAX_PORT = "i2np.udp.maxPort"; private static final int MIN_RANDOM_PORT = 9111; - private static final int MAX_RANDOM_PORT = 31777; + private static final int MAX_RANDOM_PORT = 30777; private static final int MAX_PORT_RETRIES = 20; /** From 7fbb3b12d1f3ea0c5b3c027da3ecc3b9fd335a7b Mon Sep 17 00:00:00 2001 From: zzz Date: Sun, 17 Jul 2011 16:33:40 +0000 Subject: [PATCH 2/3] * FileUtil: Add a rename method and a new copy method --- .../src/net/i2p/router/web/NewsFetcher.java | 2 +- .../i2p/router/web/UnsignedUpdateHandler.java | 6 +-- .../freenet/support/CPUInformation/CPUID.java | 2 +- .../naming/SingleFileNamingService.java | 22 +---------- core/java/src/net/i2p/util/FileUtil.java | 37 +++++++++++++++++++ .../src/net/i2p/util/NativeBigInteger.java | 2 +- 6 files changed, 45 insertions(+), 26 deletions(-) diff --git a/apps/routerconsole/java/src/net/i2p/router/web/NewsFetcher.java b/apps/routerconsole/java/src/net/i2p/router/web/NewsFetcher.java index a11b8adc5..c7a0b672b 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/NewsFetcher.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/NewsFetcher.java @@ -359,7 +359,7 @@ public class NewsFetcher implements Runnable, EepGet.StatusListener { long now = _context.clock().now(); if (_tempFile.exists()) { - boolean copied = FileUtil.copy(_tempFile.getAbsolutePath(), _newsFile.getAbsolutePath(), true); + boolean copied = FileUtil.copy(_tempFile, _newsFile, true, false); if (copied) { _lastUpdated = now; _tempFile.delete(); diff --git a/apps/routerconsole/java/src/net/i2p/router/web/UnsignedUpdateHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/UnsignedUpdateHandler.java index 2eb132b30..90e120af6 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/UnsignedUpdateHandler.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/UnsignedUpdateHandler.java @@ -93,8 +93,8 @@ public class UnsignedUpdateHandler extends UpdateHandler { _log.log(Log.CRIT, "Corrupt zip file from " + url); return; } - String to = (new File(_context.getRouterDir(), Router.UPDATE_FILE)).getAbsolutePath(); - boolean copied = FileUtil.copy(_updateFile, to, true); + File to = new File(_context.getRouterDir(), Router.UPDATE_FILE); + boolean copied = FileUtil.copy(updFile, to, true, false); if (copied) { updFile.delete(); String policy = _context.getProperty(ConfigUpdateHandler.PROP_UPDATE_POLICY); @@ -124,7 +124,7 @@ public class UnsignedUpdateHandler extends UpdateHandler { } } else { _log.log(Log.CRIT, "Failed copy to " + to); - updateStatus("" + _("Failed copy to {0}", to) + ""); + updateStatus("" + _("Failed copy to {0}", to.getAbsolutePath()) + ""); } } } diff --git a/core/java/src/freenet/support/CPUInformation/CPUID.java b/core/java/src/freenet/support/CPUInformation/CPUID.java index 439555fce..62b12952a 100644 --- a/core/java/src/freenet/support/CPUInformation/CPUID.java +++ b/core/java/src/freenet/support/CPUInformation/CPUID.java @@ -402,7 +402,7 @@ public class CPUID { } // copy to install dir, ignore failure File newFile = new File(I2PAppContext.getGlobalContext().getBaseDir(), filename); - FileUtil.copy(outFile.getAbsolutePath(), newFile.getAbsolutePath(), false, true); + FileUtil.copy(outFile, newFile, false, true); return true; } diff --git a/core/java/src/net/i2p/client/naming/SingleFileNamingService.java b/core/java/src/net/i2p/client/naming/SingleFileNamingService.java index 6d7926f40..7013fb77a 100644 --- a/core/java/src/net/i2p/client/naming/SingleFileNamingService.java +++ b/core/java/src/net/i2p/client/naming/SingleFileNamingService.java @@ -193,7 +193,7 @@ public class SingleFileNamingService extends NamingService { out.write(d.toBase64()); out.newLine(); out.close(); - boolean success = rename(tmp, _file); + boolean success = FileUtil.rename(tmp, _file); if (success) { for (NamingServiceListener nsl : _listeners) { nsl.entryChanged(this, hostname, d, options); @@ -284,7 +284,7 @@ public class SingleFileNamingService extends NamingService { tmp.delete(); return false; } - success = rename(tmp, _file); + success = FileUtil.rename(tmp, _file); if (success) { for (NamingServiceListener nsl : _listeners) { nsl.entryRemoved(this, hostname); @@ -442,24 +442,6 @@ public class SingleFileNamingService extends NamingService { } } - private static boolean rename(File from, File to) { - boolean success = false; - boolean isWindows = System.getProperty("os.name").startsWith("Win"); - // overwrite fails on windows - if (!isWindows) - success = from.renameTo(to); - if (!success) { - to.delete(); - success = from.renameTo(to); - if (!success) { - // hard way - success = FileUtil.copy(from.getAbsolutePath(), to.getAbsolutePath(), true, true); - from.delete(); - } - } - return success; - } - private void getReadLock() { _fileLock.readLock().lock(); } diff --git a/core/java/src/net/i2p/util/FileUtil.java b/core/java/src/net/i2p/util/FileUtil.java index de9d354aa..4231eb05d 100644 --- a/core/java/src/net/i2p/util/FileUtil.java +++ b/core/java/src/net/i2p/util/FileUtil.java @@ -400,7 +400,15 @@ public class FileUtil { public static boolean copy(String source, String dest, boolean overwriteExisting, boolean quiet) { File src = new File(source); File dst = new File(dest); + return copy(src, dst, overwriteExisting, quiet); + } + /** + * @param quiet don't log fails to wrapper log if true + * @return true if it was copied successfully + * @since 0.8.8 + */ + public static boolean copy(File src, File dst, boolean overwriteExisting, boolean quiet) { if (dst.exists() && dst.isDirectory()) dst = new File(dst, src.getName()); @@ -429,6 +437,35 @@ public class FileUtil { } } + /** + * Try to rename, if it doesn't work then copy and delete the old. + * Always overwrites any existing "to" file. + * Method moved from SingleFileNamingService. + * + * @return true if it was renamed / copied successfully + * @since 0.8.8 + */ + public static boolean rename(File from, File to) { + if (!from.exists()) + return false; + boolean success = false; + boolean isWindows = System.getProperty("os.name").startsWith("Win"); + // overwrite fails on windows + if (!isWindows) + success = from.renameTo(to); + if (!success) { + to.delete(); + success = from.renameTo(to); + if (!success) { + // hard way + success = copy(from, to, true, true); + if (success) + from.delete(); + } + } + return success; + } + /** * Usage: FileUtil (delete path | copy source dest | unzip path.zip) * diff --git a/core/java/src/net/i2p/util/NativeBigInteger.java b/core/java/src/net/i2p/util/NativeBigInteger.java index b1de68e55..312babe83 100644 --- a/core/java/src/net/i2p/util/NativeBigInteger.java +++ b/core/java/src/net/i2p/util/NativeBigInteger.java @@ -606,7 +606,7 @@ public class NativeBigInteger extends BigInteger { } // copy to install dir, ignore failure File newFile = new File(I2PAppContext.getGlobalContext().getBaseDir(), filename); - FileUtil.copy(outFile.getAbsolutePath(), newFile.getAbsolutePath(), false, true); + FileUtil.copy(outFile, newFile, false, true); return true; } From a8378d0411f5cc67f8a83df104c863f087d83464 Mon Sep 17 00:00:00 2001 From: zzz Date: Sun, 17 Jul 2011 16:38:02 +0000 Subject: [PATCH 3/3] * I2PTunnel: Rename privkey file when deleting tunnel to prevent inadvertent reuse --- .../src/net/i2p/i2ptunnel/web/IndexBean.java | 54 +++++++++++++++---- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/IndexBean.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/IndexBean.java index eb36a64bc..5be985807 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/IndexBean.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/web/IndexBean.java @@ -8,6 +8,7 @@ package net.i2p.i2ptunnel.web; * */ +import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -30,6 +31,7 @@ import net.i2p.i2ptunnel.I2PTunnelHTTPClientBase; import net.i2p.i2ptunnel.TunnelController; import net.i2p.i2ptunnel.TunnelControllerGroup; import net.i2p.util.ConcurrentHashSet; +import net.i2p.util.FileUtil; import net.i2p.util.Log; /** @@ -40,9 +42,9 @@ import net.i2p.util.Log; * Usage by classes outside of i2ptunnel.war is deprecated. */ public class IndexBean { - protected I2PAppContext _context; - protected Log _log; - protected TunnelControllerGroup _group; + protected final I2PAppContext _context; + protected final Log _log; + protected final TunnelControllerGroup _group; private String _action; private int _tunnel; //private long _prevNonce; @@ -74,8 +76,8 @@ public class IndexBean { private boolean _sharedClient; private boolean _privKeyGenerate; private boolean _removeConfirmed; - private Set _booleanOptions; - private Map _otherOptions; + private final Set _booleanOptions; + private final Map _otherOptions; private int _hashCashValue; private int _certType; private String _certSigner; @@ -198,17 +200,17 @@ public class IndexBean { } private String stopAll() { if (_group == null) return ""; - List msgs = _group.stopAllControllers(); + List msgs = _group.stopAllControllers(); return getMessages(msgs); } private String startAll() { if (_group == null) return ""; - List msgs = _group.startAllControllers(); + List msgs = _group.startAllControllers(); return getMessages(msgs); } private String restartAll() { if (_group == null) return ""; - List msgs = _group.restartAllControllers(); + List msgs = _group.restartAllControllers(); return getMessages(msgs); } private String reloadConfig() { @@ -316,6 +318,10 @@ public class IndexBean { return rv; } + /** + * Stop the tunnel, delete from config, + * rename the private key file if in the default directory + */ private String deleteTunnel() { if (!_removeConfirmed) return "Please confirm removal"; @@ -324,8 +330,38 @@ public class IndexBean { if (cur == null) return "Invalid tunnel number"; - List msgs = _group.removeController(cur); + List msgs = _group.removeController(cur); msgs.addAll(doSave()); + + // Rename private key file if it was a default name in + // the default directory, so it doesn't get reused when a new + // tunnel is created. + // Use configured file name if available, not the one from the form. + String pk = cur.getPrivKeyFile(); + if (pk == null) + pk = _privKeyFile; + if (pk != null && pk.startsWith("i2ptunnel") && pk.endsWith("-privKeys.dat")) { + File pkf = new File(_context.getConfigDir(), pk); + if (pkf.exists()) { + String name = cur.getName(); + if (name == null) { + name = cur.getDescription(); + if (name == null) { + name = cur.getType(); + if (name == null) + name = Long.toString(_context.clock().now()); + } + } + name = "i2ptunnel-deleted-" + name.replace(' ', '_') + "-privkeys.dat"; + File to = new File(_context.getConfigDir(), name); + if (to.exists()) + to = new File(_context.getConfigDir(), name + '-' + _context.clock().now()); + boolean success = FileUtil.rename(pkf, to); + if (success) + msgs.add("Private key file " + pkf.getAbsolutePath() + + " renamed to " + to.getAbsolutePath()); + } + } return getMessages(msgs); }