when deleting configs, move them to a backup location

This commit is contained in:
hankhill19580
2019-06-08 00:51:59 +00:00
parent e4892c6d5c
commit c1731f68fa
2 changed files with 40 additions and 55 deletions

View File

@ -3,7 +3,7 @@ package net.i2p.i2ptunnel;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; //import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -398,7 +398,7 @@ public class TunnelControllerGroup implements ClientApp {
for (Map.Entry<Object, Object> e : props.entrySet()) { for (Map.Entry<Object, Object> e : props.entrySet()) {
String key = (String) e.getKey(); String key = (String) e.getKey();
String val = (String) e.getValue(); String val = (String) e.getValue();
save.setProperty(PREFIX + key, val); save.setProperty(key, val);
} }
try { try {
DataHelper.storeProps(save, f); DataHelper.storeProps(save, f);
@ -457,10 +457,10 @@ public class TunnelControllerGroup implements ClientApp {
public synchronized void reloadControllers() { public synchronized void reloadControllers() {
List<File> fileList = listFiles(); List<File> fileList = listFiles();
unloadControllers(); unloadControllers();
for (int i = 0; i < fileList.size(); i++) { // for (int i = 0; i < fileList.size(); i++) {
String configFile = fileList.get(i).toString(); // String configFile = fileList.get(i).toString();
loadControllers(configFile); loadControllers(_configFile);
} // }
startControllers(); startControllers();
} }
@ -615,8 +615,9 @@ public class TunnelControllerGroup implements ClientApp {
List<String> msgs = new ArrayList<String>(); List<String> msgs = new ArrayList<String>();
_controllersLock.readLock().lock(); _controllersLock.readLock().lock();
try { try {
for (int i = 0; i < _controllers.size(); i++) { // for (int i = 0; i < _controllers.size(); i++) {
TunnelController controller = _controllers.get(i); // TunnelController controller = _controllers.get(i);
for (TunnelController controller : _controllers) {
msgs.addAll(controller.clearMessages()); msgs.addAll(controller.clearMessages());
} }
} finally { } finally {
@ -635,8 +636,7 @@ public class TunnelControllerGroup implements ClientApp {
public void saveConfig() throws IOException { public void saveConfig() throws IOException {
_controllersLock.readLock().lock(); _controllersLock.readLock().lock();
try { try {
for (int i = 0; i < _controllers.size(); i++) { for (TunnelController controller : _controllers) {
TunnelController controller = _controllers.get(i);
saveConfig(controller); saveConfig(controller);
} }
} finally { } finally {
@ -648,7 +648,7 @@ public class TunnelControllerGroup implements ClientApp {
* Save the configuration of all known tunnels to the given file * Save the configuration of all known tunnels to the given file
* @deprecated * @deprecated
*/ */
@Deprecated /* @Deprecated
private synchronized void saveConfig(String configFile) throws IOException { private synchronized void saveConfig(String configFile) throws IOException {
File cfgFile = new File(configFile); File cfgFile = new File(configFile);
if (!cfgFile.isAbsolute()) if (!cfgFile.isAbsolute())
@ -670,7 +670,7 @@ public class TunnelControllerGroup implements ClientApp {
} }
DataHelper.storeProps(map, cfgFile); DataHelper.storeProps(map, cfgFile);
} }*/
/** /**
* Save the configuration of this tunnel only, may be new * Save the configuration of this tunnel only, may be new
@ -699,8 +699,6 @@ public class TunnelControllerGroup implements ClientApp {
* @since 0.9.34 * @since 0.9.34
*/ */
public synchronized void removeConfig(TunnelController tc) throws IOException { public synchronized void removeConfig(TunnelController tc) throws IOException {
Properties inputController = tc.getConfig("");
String inputName = inputController.getProperty("name");
Properties map = new OrderedProperties(); Properties map = new OrderedProperties();
File cfgFile = inConfig(tc); File cfgFile = inConfig(tc);
@ -712,12 +710,13 @@ public class TunnelControllerGroup implements ClientApp {
saveConfig(c); saveConfig(c);
} }
} finally { } finally {
if (!FileUtil.rename(cfgFile, new File(cfgFile.getAbsolutePath() + ".bak")))
if (! cfgFile.delete())
if (_log.shouldLog(Log.WARN))
_log.warn("could not delete config file" + cfgFile.toString());
_controllersLock.readLock().unlock(); _controllersLock.readLock().unlock();
} }
if (!FileUtil.rename(cfgFile, new File(cfgFile.getAbsolutePath() + ".bak")))
if (! cfgFile.delete())
_log.debug("could not delete config file" + cfgFile.toString());
} }
/** /**
@ -727,6 +726,9 @@ public class TunnelControllerGroup implements ClientApp {
public synchronized File inConfig(TunnelController tc) throws IOException { public synchronized File inConfig(TunnelController tc) throws IOException {
Properties inputController = tc.getConfig(""); Properties inputController = tc.getConfig("");
String configFileName = inputController.getProperty("configFile"); String configFileName = inputController.getProperty("configFile");
if (_log.shouldLog(Log.WARN))
_log.warn("Found config file" + configFileName);
File file = new File(configFileName); File file = new File(configFileName);
if (!file.isAbsolute()) if (!file.isAbsolute())
file = new File(configFileName, _configDirectory); file = new File(configFileName, _configDirectory);
@ -758,28 +760,6 @@ public class TunnelControllerGroup implements ClientApp {
return files; return files;
} }
/**
* Load up the config data from the file, try and determine whether it's a
* split or monolithic config automatically by attempting to detect a
* tunnel.0 config
*
* @return non-null, properties loaded, one for each tunnel
* @throws IOException if unable to load from file
*/
private synchronized List<Properties> loadConfig(File cfgFile) throws IOException {
Properties config = new Properties();
DataHelper.loadProps(config, cfgFile);
for (String key : config.stringPropertyNames()){
if (key.startsWith(PREFIX+"0"))
return loadMonolithicConfig(cfgFile);
else
break;
}
return loadSplitConfig(cfgFile);
}
/** /**
* Load up the config data from the file, a "Split Config" is a non-numbered * Load up the config data from the file, a "Split Config" is a non-numbered
* single-tunnel config file * single-tunnel config file
@ -795,7 +775,7 @@ public class TunnelControllerGroup implements ClientApp {
for (Map.Entry<Object, Object> e : config.entrySet()) { for (Map.Entry<Object, Object> e : config.entrySet()) {
String key = (String) e.getKey(); String key = (String) e.getKey();
key = key.substring(PREFIX.length()); // key = key.substring(PREFIX.length());
String val = (String) e.getValue(); String val = (String) e.getValue();
p.setProperty(key, val); p.setProperty(key, val);
} }

View File

@ -71,7 +71,7 @@ public class GeneralHelper {
if (tcg == null) return null; if (tcg == null) return null;
List<TunnelController> controllers = tcg.getControllers(); List<TunnelController> controllers = tcg.getControllers();
if (controllers.size() > tunnel) if (controllers.size() > tunnel)
return controllers.get(tunnel); return controllers.get(tunnel);
else else
return null; return null;
} }
@ -129,13 +129,13 @@ public class GeneralHelper {
// config now contains new keystore props // config now contains new keystore props
String name = props.getProperty(TunnelController.PROP_NAME, ""); String name = props.getProperty(TunnelController.PROP_NAME, "");
msgs.add("Created new self-signed certificate for tunnel " + name); msgs.add("Created new self-signed certificate for tunnel " + name);
} }
} catch (IOException ioe) { } catch (IOException ioe) {
msgs.add("Failed to create new self-signed certificate for tunnel " + msgs.add("Failed to create new self-signed certificate for tunnel " +
getTunnelName(tcg, tunnel) + ", check logs: " + ioe); getTunnelName(tcg, tunnel) + ", check logs: " + ioe);
} }
} }
} }
if (cur == null) { if (cur == null) {
// creating new // creating new
cur = new TunnelController(props, "", true); cur = new TunnelController(props, "", true);
@ -173,7 +173,7 @@ public class GeneralHelper {
return msgs; return msgs;
} }
protected static List<String> saveConfig(I2PAppContext context, TunnelControllerGroup tcg) { protected static List<String> saveConfig(I2PAppContext context, TunnelControllerGroup tcg) {
List<String> rv = tcg.clearAllMessages(); List<String> rv = tcg.clearAllMessages();
try { try {
//////////////// ////////////////
@ -209,6 +209,11 @@ public class GeneralHelper {
//////////////////////// ////////////////////////
msgs = tcg.removeController(cur); msgs = tcg.removeController(cur);
try {
tcg.removeConfig(cur);
}catch (IOException ioe){
msgs.add(ioe.toString());
}
msgs.addAll(saveConfig(context, tcg)); msgs.addAll(saveConfig(context, tcg));
// Rename private key file if it was a default name in // Rename private key file if it was a default name in
@ -560,7 +565,7 @@ public class GeneralHelper {
public boolean getEncrypt(int tunnel) { public boolean getEncrypt(int tunnel) {
return getBooleanProperty(tunnel, "i2cp.encryptLeaseSet"); return getBooleanProperty(tunnel, "i2cp.encryptLeaseSet");
} }
/** /**
* @since 0.9.40 * @since 0.9.40
*/ */
@ -576,7 +581,7 @@ public class GeneralHelper {
} }
return 0; return 0;
} }
/** /**
* @since 0.9.40 * @since 0.9.40
*/ */
@ -663,7 +668,7 @@ public class GeneralHelper {
return 2; return 2;
return 0; return 0;
} }
public String getAccessList(int tunnel) { public String getAccessList(int tunnel) {
return getProperty(tunnel, "i2cp.accessList", "").replace(",", "\n"); return getProperty(tunnel, "i2cp.accessList", "").replace(",", "\n");
} }
@ -680,12 +685,12 @@ public class GeneralHelper {
} }
return ""; return "";
} }
public String getJumpList(int tunnel) { public String getJumpList(int tunnel) {
return getProperty(tunnel, I2PTunnelHTTPClient.PROP_JUMP_SERVERS, return getProperty(tunnel, I2PTunnelHTTPClient.PROP_JUMP_SERVERS,
I2PTunnelHTTPClient.DEFAULT_JUMP_SERVERS).replace(",", "\n"); I2PTunnelHTTPClient.DEFAULT_JUMP_SERVERS).replace(",", "\n");
} }
public boolean getCloseOnIdle(int tunnel, boolean def) { public boolean getCloseOnIdle(int tunnel, boolean def) {
return getBooleanProperty(tunnel, "i2cp.closeOnIdle", def); return getBooleanProperty(tunnel, "i2cp.closeOnIdle", def);
} }
@ -736,15 +741,15 @@ public class GeneralHelper {
public String getProxyAuth(int tunnel) { public String getProxyAuth(int tunnel) {
return getProperty(tunnel, I2PTunnelHTTPClientBase.PROP_AUTH, "false"); return getProperty(tunnel, I2PTunnelHTTPClientBase.PROP_AUTH, "false");
} }
public boolean getOutproxyAuth(int tunnel) { public boolean getOutproxyAuth(int tunnel) {
return getBooleanProperty(tunnel, I2PTunnelHTTPClientBase.PROP_OUTPROXY_AUTH); return getBooleanProperty(tunnel, I2PTunnelHTTPClientBase.PROP_OUTPROXY_AUTH);
} }
public String getOutproxyUsername(int tunnel) { public String getOutproxyUsername(int tunnel) {
return getProperty(tunnel, I2PTunnelHTTPClientBase.PROP_OUTPROXY_USER, ""); return getProperty(tunnel, I2PTunnelHTTPClientBase.PROP_OUTPROXY_USER, "");
} }
public String getOutproxyPassword(int tunnel) { public String getOutproxyPassword(int tunnel) {
if (getOutproxyUsername(tunnel).length() <= 0) if (getOutproxyUsername(tunnel).length() <= 0)
return ""; return "";