Skip to content

Commit

Permalink
when deleting configs, move them to a backup location
Browse files Browse the repository at this point in the history
  • Loading branch information
eyedeekay committed Jun 8, 2019
1 parent e4892c6 commit c1731f6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 55 deletions.
Expand Up @@ -3,7 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
//import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -398,7 +398,7 @@ private boolean migrate(List<Properties> tunnels, File from, File dir) {
for (Map.Entry<Object, Object> e : props.entrySet()) {
String key = (String) e.getKey();
String val = (String) e.getValue();
save.setProperty(PREFIX + key, val);
save.setProperty(key, val);
}
try {
DataHelper.storeProps(save, f);
Expand Down Expand Up @@ -457,10 +457,10 @@ public void run() {
public synchronized void reloadControllers() {
List<File> fileList = listFiles();
unloadControllers();
for (int i = 0; i < fileList.size(); i++) {
String configFile = fileList.get(i).toString();
loadControllers(configFile);
}
// for (int i = 0; i < fileList.size(); i++) {
// String configFile = fileList.get(i).toString();
loadControllers(_configFile);
// }
startControllers();
}

Expand Down Expand Up @@ -615,8 +615,9 @@ public List<String> clearAllMessages() {
List<String> msgs = new ArrayList<String>();
_controllersLock.readLock().lock();
try {
for (int i = 0; i < _controllers.size(); i++) {
TunnelController controller = _controllers.get(i);
// for (int i = 0; i < _controllers.size(); i++) {
// TunnelController controller = _controllers.get(i);
for (TunnelController controller : _controllers) {
msgs.addAll(controller.clearMessages());
}
} finally {
Expand All @@ -635,8 +636,7 @@ public List<String> clearAllMessages() {
public void saveConfig() throws IOException {
_controllersLock.readLock().lock();
try {
for (int i = 0; i < _controllers.size(); i++) {
TunnelController controller = _controllers.get(i);
for (TunnelController controller : _controllers) {
saveConfig(controller);
}
} finally {
Expand All @@ -648,7 +648,7 @@ public void saveConfig() throws IOException {
* Save the configuration of all known tunnels to the given file
* @deprecated
*/
@Deprecated
/* @Deprecated
private synchronized void saveConfig(String configFile) throws IOException {
File cfgFile = new File(configFile);
if (!cfgFile.isAbsolute())
Expand All @@ -670,7 +670,7 @@ private synchronized void saveConfig(String configFile) throws IOException {
}
DataHelper.storeProps(map, cfgFile);
}
}*/

/**
* Save the configuration of this tunnel only, may be new
Expand Down Expand Up @@ -699,8 +699,6 @@ public synchronized void saveConfig(TunnelController tc) throws IOException {
* @since 0.9.34
*/
public synchronized void removeConfig(TunnelController tc) throws IOException {
Properties inputController = tc.getConfig("");
String inputName = inputController.getProperty("name");
Properties map = new OrderedProperties();

File cfgFile = inConfig(tc);
Expand All @@ -712,12 +710,13 @@ public synchronized void removeConfig(TunnelController tc) throws IOException {
saveConfig(c);
}
} 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();
}

if (!FileUtil.rename(cfgFile, new File(cfgFile.getAbsolutePath() + ".bak")))
if (! cfgFile.delete())
_log.debug("could not delete config file" + cfgFile.toString());
}

/**
Expand All @@ -727,6 +726,9 @@ public synchronized void removeConfig(TunnelController tc) throws IOException {
public synchronized File inConfig(TunnelController tc) throws IOException {
Properties inputController = tc.getConfig("");
String configFileName = inputController.getProperty("configFile");
if (_log.shouldLog(Log.WARN))
_log.warn("Found config file" + configFileName);

File file = new File(configFileName);
if (!file.isAbsolute())
file = new File(configFileName, _configDirectory);
Expand Down Expand Up @@ -758,28 +760,6 @@ private List<File> listFiles() {
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
* single-tunnel config file
Expand All @@ -795,7 +775,7 @@ private synchronized List<Properties> loadSplitConfig(File cfgFile) throws IOExc

for (Map.Entry<Object, Object> e : config.entrySet()) {
String key = (String) e.getKey();
key = key.substring(PREFIX.length());
// key = key.substring(PREFIX.length());
String val = (String) e.getValue();
p.setProperty(key, val);
}
Expand Down
35 changes: 20 additions & 15 deletions apps/i2ptunnel/java/src/net/i2p/i2ptunnel/ui/GeneralHelper.java
Expand Up @@ -71,7 +71,7 @@ public static TunnelController getController(TunnelControllerGroup tcg, int tunn
if (tcg == null) return null;
List<TunnelController> controllers = tcg.getControllers();
if (controllers.size() > tunnel)
return controllers.get(tunnel);
return controllers.get(tunnel);
else
return null;
}
Expand Down Expand Up @@ -129,13 +129,13 @@ protected static List<String> updateTunnelConfig(TunnelControllerGroup tcg, int
// config now contains new keystore props
String name = props.getProperty(TunnelController.PROP_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 " +
getTunnelName(tcg, tunnel) + ", check logs: " + ioe);
}
}
}
}
}
}
if (cur == null) {
// creating new
cur = new TunnelController(props, "", true);
Expand Down Expand Up @@ -173,7 +173,7 @@ protected static List<String> updateTunnelConfig(TunnelControllerGroup tcg, int
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();
try {
////////////////
Expand Down Expand Up @@ -209,6 +209,11 @@ public static List<String> deleteTunnel(

////////////////////////
msgs = tcg.removeController(cur);
try {
tcg.removeConfig(cur);
}catch (IOException ioe){
msgs.add(ioe.toString());
}
msgs.addAll(saveConfig(context, tcg));

// Rename private key file if it was a default name in
Expand Down Expand Up @@ -560,7 +565,7 @@ public String getSigner(int tunnel) {
public boolean getEncrypt(int tunnel) {
return getBooleanProperty(tunnel, "i2cp.encryptLeaseSet");
}

/**
* @since 0.9.40
*/
Expand All @@ -576,7 +581,7 @@ public int getEncryptMode(int tunnel) {
}
return 0;
}

/**
* @since 0.9.40
*/
Expand Down Expand Up @@ -663,7 +668,7 @@ public int getAccessMode(int tunnel) {
return 2;
return 0;
}

public String getAccessList(int tunnel) {
return getProperty(tunnel, "i2cp.accessList", "").replace(",", "\n");
}
Expand All @@ -680,12 +685,12 @@ public String getFilterDefinition(int tunnel) {
}
return "";
}

public String getJumpList(int tunnel) {
return getProperty(tunnel, I2PTunnelHTTPClient.PROP_JUMP_SERVERS,
I2PTunnelHTTPClient.DEFAULT_JUMP_SERVERS).replace(",", "\n");
}

public boolean getCloseOnIdle(int tunnel, boolean def) {
return getBooleanProperty(tunnel, "i2cp.closeOnIdle", def);
}
Expand Down Expand Up @@ -736,15 +741,15 @@ public boolean getMultihome(int tunnel) {
public String getProxyAuth(int tunnel) {
return getProperty(tunnel, I2PTunnelHTTPClientBase.PROP_AUTH, "false");
}

public boolean getOutproxyAuth(int tunnel) {
return getBooleanProperty(tunnel, I2PTunnelHTTPClientBase.PROP_OUTPROXY_AUTH);
}

public String getOutproxyUsername(int tunnel) {
return getProperty(tunnel, I2PTunnelHTTPClientBase.PROP_OUTPROXY_USER, "");
}

public String getOutproxyPassword(int tunnel) {
if (getOutproxyUsername(tunnel).length() <= 0)
return "";
Expand Down

0 comments on commit c1731f6

Please sign in to comment.