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