- Use DataHelper to load/store sam.keys
 - Move sam.keys file to config dir (ticket #677)
This commit is contained in:
zzz
2015-11-27 22:39:19 +00:00
parent b1668bbc11
commit a03339b120

View File

@ -42,6 +42,7 @@ import net.i2p.data.Destination;
import net.i2p.util.I2PAppThread;
import net.i2p.util.I2PSSLSocketFactory;
import net.i2p.util.Log;
import net.i2p.util.OrderedProperties;
import net.i2p.util.PortMapper;
import net.i2p.util.SystemVersion;
@ -255,59 +256,51 @@ public class SAMBridge implements Runnable, ClientApp {
/**
* Load up the keys from the persistFilename.
* TODO use DataHelper
* TODO store in config dir, not base dir
*/
@SuppressWarnings("unchecked")
private void loadKeys() {
synchronized (nameToPrivKeys) {
nameToPrivKeys.clear();
BufferedReader br = null;
File file = new File(persistFilename);
// now in config dir but check base dir too...
if (!file.exists()) {
if (file.isAbsolute())
return;
file = new File(I2PAppContext.getGlobalContext().getConfigDir(), persistFilename);
if (!file.exists())
return;
}
try {
br = new BufferedReader(new InputStreamReader(
new FileInputStream(persistFilename), "UTF-8"));
String line = null;
while ( (line = br.readLine()) != null) {
int eq = line.indexOf('=');
String name = line.substring(0, eq);
String privKeys = line.substring(eq+1);
nameToPrivKeys.put(name, privKeys);
}
Properties props = new Properties();
DataHelper.loadProps(props, file);
// unchecked
Map foo = props;
nameToPrivKeys.putAll(foo);
if (_log.shouldInfo())
_log.info("Loaded " + nameToPrivKeys.size() + " private keys from " + persistFilename);
} catch (FileNotFoundException fnfe) {
_log.warn("Key file does not exist at " + persistFilename);
_log.info("Loaded " + nameToPrivKeys.size() + " private keys from " + file);
} catch (IOException ioe) {
_log.error("Unable to read the keys from " + persistFilename, ioe);
} finally {
if (br != null) try { br.close(); } catch (IOException ioe) {}
_log.error("Unable to read the keys from " + file, ioe);
}
}
}
/**
* Store the current keys to disk in the location specified on creation.
* TODO use DataHelper
* TODO store in config dir, not base dir
*/
private void storeKeys() {
synchronized (nameToPrivKeys) {
FileOutputStream out = null;
File file = new File(persistFilename);
// now in config dir but check base dir too...
if (!file.exists() && !file.isAbsolute())
file = new File(I2PAppContext.getGlobalContext().getConfigDir(), persistFilename);
try {
out = new FileOutputStream(persistFilename);
for (Map.Entry<String, String> entry : nameToPrivKeys.entrySet()) {
String name = entry.getKey();
String privKeys = entry.getValue();
out.write(name.getBytes("UTF-8"));
out.write('=');
out.write(privKeys.getBytes("UTF-8"));
out.write('\n');
}
Properties props = new OrderedProperties();
props.putAll(nameToPrivKeys);
DataHelper.storeProps(props, file);
if (_log.shouldInfo())
_log.info("Saved " + nameToPrivKeys.size() + " private keys to " + persistFilename);
_log.info("Saved " + nameToPrivKeys.size() + " private keys to " + file);
} catch (IOException ioe) {
_log.error("Error writing out the SAM keys to " + persistFilename, ioe);
} finally {
if (out != null) try { out.close(); } catch (IOException ioe) {}
_log.error("Error writing out the SAM keys to " + file, ioe);
}
}
}