public static File configFile(I2PAppContext ctx) { String clientConfigFile = ctx.getProperty(PROP_CLIENT_CONFIG_FILENAME, DEFAULT_CLIENT_CONFIG_FILENAME); File cfgFile = new File(clientConfigFile); if (!cfgFile.isAbsolute()) cfgFile = new File(ctx.getConfigDir(), clientConfigFile); return cfgFile; }
public void setConfig(String filename) { _locationFile = new File(filename); if (!_locationFile.isAbsolute()) _locationFile = new File(_context.getConfigDir(), filename); loadConfig(); }
/** * Does not necessarily exist. * @param f relative or absolute path, may be null * @return absolute path or null * @since 0.9.30 */ static File filenameToFile(String f) { if (f == null) return null; f = f.trim(); if (f.length() == 0) return null; File rv = new File(f); if (!rv.isAbsolute()) rv = new File(I2PAppContext.getGlobalContext().getConfigDir(), f); return rv; }
public static final void writeSeed(byte buf[]) { File f = new File(I2PAppContext.getGlobalContext().getConfigDir(), SEEDFILE); FileOutputStream fos = null; try { fos = new SecureFileOutputStream(f); fos.write(buf); } catch (IOException ioe) { // ignore } finally { if (fos != null) try { fos.close(); } catch (IOException ioe) {} } }
/** * Save the configuration of all known tunnels to the given file * */ public synchronized void saveConfig(String configFile) throws IOException { File cfgFile = new File(configFile); if (!cfgFile.isAbsolute()) cfgFile = new File(I2PAppContext.getGlobalContext().getConfigDir(), configFile); File parent = cfgFile.getParentFile(); if ( (parent != null) && (!parent.exists()) ) parent.mkdirs(); Properties map = new OrderedProperties(); _controllersLock.readLock().lock(); try { for (int i = 0; i < _controllers.size(); i++) { TunnelController controller = _controllers.get(i); Properties cur = controller.getConfig("tunnel." + i + "."); map.putAll(cur); } } finally { _controllersLock.readLock().unlock(); } DataHelper.storeProps(map, cfgFile); }
/** * @since 0.9.26 * @return key or null */ public SigningPrivateKey getSigningPrivateKey(int tunnel) { TunnelController tun = getController(tunnel); if (tun == null) return null; String keyFile = tun.getPrivKeyFile(); if (keyFile != null && keyFile.trim().length() > 0) { File f = new File(keyFile); if (!f.isAbsolute()) f = new File(_context.getConfigDir(), keyFile); PrivateKeyFile pkf = new PrivateKeyFile(f); return pkf.getSigningPrivKey(); } return null; }
/** * @return path, non-null, non-empty */ public String getPrivateKeyFile(TunnelControllerGroup tcg, int tunnel) { TunnelController tun = getController(tcg, tunnel); if (tun != null) { String rv = tun.getPrivKeyFile(); if (rv != null) return rv; } if (tunnel < 0) tunnel = tcg == null ? 999 : tcg.getControllers().size(); String rv = "i2ptunnel" + tunnel + "-privKeys.dat"; // Don't default to a file that already exists, // which could happen after other tunnels are deleted. int i = 0; while ((new File(_context.getConfigDir(), rv)).exists()) { rv = "i2ptunnel" + tunnel + '.' + (++i) + "-privKeys.dat"; } return rv; }
/** * Load up the config data from the file * * @return properties loaded * @throws IllegalArgumentException if unable to load from file */ private synchronized Properties loadConfig(String configFile) { File cfgFile = new File(configFile); if (!cfgFile.isAbsolute()) cfgFile = new File(I2PAppContext.getGlobalContext().getConfigDir(), configFile); if (!cfgFile.exists()) { if (_log.shouldLog(Log.ERROR)) _log.error("Unable to load the controllers from " + cfgFile.getAbsolutePath()); throw new IllegalArgumentException("Unable to load the controllers from " + cfgFile.getAbsolutePath()); } Properties props = new Properties(); try { DataHelper.loadProps(props, cfgFile); return props; } catch (IOException ioe) { if (_log.shouldLog(Log.ERROR)) _log.error("Error reading the controllers from " + cfgFile.getAbsolutePath(), ioe); throw new IllegalArgumentException("Error reading the controllers from " + cfgFile.getAbsolutePath(), ioe); } }
/** * Load up the keys from the persistFilename. */ @SuppressWarnings("unchecked") private void loadKeys() { synchronized (nameToPrivKeys) { nameToPrivKeys.clear(); 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 { 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 " + file); } catch (IOException ioe) { _log.error("Unable to read the keys from " + file, ioe); } } }
/** * Pull the cert back OUT of the keystore and save it as ascii * so the clients can get to it. * * @param name used to generate output file name * @param opts must contain PROP_KEY_ALIAS */ private static void exportCert(File ks, String name, Properties opts) { File sdir = new SecureDirectory(I2PAppContext.getGlobalContext().getConfigDir(), CERT_DIR); if (sdir.exists() || sdir.mkdirs()) { String keyAlias = opts.getProperty(PROP_KEY_ALIAS); String ksPass = opts.getProperty(PROP_KEYSTORE_PASSWORD, KeyStoreUtil.DEFAULT_KEYSTORE_PASSWORD); File out = new File(sdir, PREFIX + name + ASCII_KEYFILE_SUFFIX); boolean success = KeyStoreUtil.exportCert(ks, ksPass, keyAlias, out); if (!success) error("Error getting SSL cert to save as ASCII"); } else { error("Error saving ASCII SSL keys"); } }
/** * Pull the cert back OUT of the keystore and save it as ascii * so the clients can get to it. * * @param name used to generate output file name * @param opts must contain PROP_KEY_ALIAS */ private static void exportCert(File ks, String name, Properties opts) { File sdir = new SecureDirectory(I2PAppContext.getGlobalContext().getConfigDir(), CERT_DIR); if (sdir.exists() || sdir.mkdirs()) { String keyAlias = opts.getProperty(PROP_KEY_ALIAS); String ksPass = opts.getProperty(PROP_KEYSTORE_PASSWORD, KeyStoreUtil.DEFAULT_KEYSTORE_PASSWORD); File out = new File(sdir, PREFIX + name + ASCII_KEYFILE_SUFFIX); boolean success = KeyStoreUtil.exportCert(ks, ksPass, keyAlias, out); if (!success) error("Error getting SSL cert to save as ASCII"); } else { error("Error saving ASCII SSL keys"); } }
boolean diff = true; try { diff = !ctx.getBaseDir().getCanonicalPath().equals(ctx.getConfigDir().getCanonicalPath()); } catch (IOException ioe) {} if (diff) { File dir2 = new File(ctx.getConfigDir(), CERT_DIR); dir2 = new File(dir2, REVOCATION_DIR); loadCRLs(crls, dir2);
/** * Store the current keys to disk in the location specified on creation. */ private void storeKeys() { synchronized (nameToPrivKeys) { 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 { Properties props = new OrderedProperties(); props.putAll(nameToPrivKeys); DataHelper.storeProps(props, file); if (_log.shouldInfo()) _log.info("Saved " + nameToPrivKeys.size() + " private keys to " + file); } catch (IOException ioe) { _log.error("Error writing out the SAM keys to " + file, ioe); } } }
/** * Pull the cert back OUT of the keystore and save it as ascii * so the clients can get to it. * * @param name used to generate output file name * @param opts must contain optPfx + PROP_KEY_ALIAS * @param optPfx add this prefix when getting options */ private static void exportCert(File ks, String name, Properties opts, String optPfx) { File sdir = new SecureDirectory(I2PAppContext.getGlobalContext().getConfigDir(), CERT_DIR); if (sdir.exists() || sdir.mkdirs()) { String keyAlias = opts.getProperty(optPfx + PROP_KEY_ALIAS); String ksPass = opts.getProperty(optPfx + PROP_KEYSTORE_PASSWORD, KeyStoreUtil.DEFAULT_KEYSTORE_PASSWORD); File out = new File(sdir, PREFIX + name + ASCII_KEYFILE_SUFFIX); boolean success = KeyStoreUtil.exportCert(ks, ksPass, keyAlias, out); if (!success) error("Error getting SSL cert to save as ASCII"); } else { error("Error saving ASCII SSL keys"); } }
error("Failed to create SAM SSL keystore.\n" + "If you create the keystore manually, you must add " + PROP_KEYSTORE_PASSWORD + " and " + PROP_KEY_PASSWORD + " to " + (new File(I2PAppContext.getGlobalContext().getConfigDir(), DEFAULT_SAMCLIENT_CONFIGFILE)).getAbsolutePath());
error("Failed to create SAM SSL keystore.\n" + "If you create the keystore manually, you must add " + PROP_KEYSTORE_PASSWORD + " and " + PROP_KEY_PASSWORD + " to " + (new File(I2PAppContext.getGlobalContext().getConfigDir(), SAMBridge.DEFAULT_SAM_CONFIGFILE)).getAbsolutePath());
error("Failed to create I2PTunnel SSL keystore.\n" + "If you create the keystore manually, you must add " + optPfx + PROP_KEYSTORE_PASSWORD + " and " + optPfx + PROP_KEY_PASSWORD + " to " + (new File(I2PAppContext.getGlobalContext().getConfigDir(), "i2ptunnel.config")).getAbsolutePath());
/** * For webapp. * @param ctxPath generally "/i2psnark" * @param ctxName generally "i2psnark" * @since 0.9.6 */ public SnarkManager(I2PAppContext ctx, String ctxPath, String ctxName) { _snarks = new ConcurrentHashMap<String, Snark>(); _magnets = new ConcurrentHashSet<String>(); _addSnarkLock = new Object(); _context = ctx; _contextPath = ctxPath; _contextName = ctxName; _log = _context.logManager().getLog(SnarkManager.class); _messages = new UIMessages(MAX_MESSAGES); _util = new I2PSnarkUtil(_context, ctxName); String cfile = ctxName + CONFIG_FILE_SUFFIX; File configFile = new File(cfile); if (!configFile.isAbsolute()) configFile = new File(_context.getConfigDir(), cfile); _configDir = migrateConfig(configFile); _configFile = new File(_configDir, CONFIG_FILE); _trackerMap = new ConcurrentHashMap<String, Tracker>(4); loadConfig(null); if (!ctx.isRouterContext()) Runtime.getRuntime().addShutdownHook(new Thread(new TempDeleter(_util.getTempDir()), "Snark Temp Dir Deleter")); }
ok = seedFromFile(new File("/dev/urandom"), buf) || ok; File localFile = new File(_context.getConfigDir(), SEEDFILE); ok = seedFromFile(localFile, buf) || ok; return ok;
/** * Signal the wrapper to asynchronously dump threads to wrapper.log. * It waits for the signal to complete (which should be fast) * but does not wait for the dump itself. * * @param secondsToWait if <= 0, don't wait * @return success, false if windows or no wrapper, true if secondsToWait <= 0, false if timed out, dump result otherwise */ public static boolean dump(I2PAppContext context, int secondsToWait) { if (SystemVersion.isWindows() || !context.hasWrapper()) return false; ShellCommand sc = new ShellCommand(); File i2pr = new File(context.getBaseDir(), "i2prouter"); String[] args = new String[2]; args[0] = i2pr.getAbsolutePath(); args[1] = "dump"; boolean success = sc.executeSilentAndWaitTimed(args, secondsToWait); if (secondsToWait <= 0) success = true; if (success) { Log log = context.logManager().getLog(ThreadDump.class); File f = new File(context.getConfigDir(), "wrapper.log"); String loc = f.exists() ? f.getAbsolutePath() : "wrapper.log"; log.log(Log.CRIT, "Threads dumped to " + loc); } return success; } }