use of net.i2p.util.FileSuffixFilter in project i2p.i2p by i2p.
the class PluginStarter method startPlugin.
/**
* @return true on success
* @throws Exception just about anything, caller would be wise to catch Throwable
*/
@SuppressWarnings("deprecation")
public static boolean startPlugin(RouterContext ctx, String appName) throws Exception {
Log log = ctx.logManager().getLog(PluginStarter.class);
File pluginDir = new File(ctx.getConfigDir(), PLUGIN_DIR + '/' + appName);
String iconfile = null;
if ((!pluginDir.exists()) || (!pluginDir.isDirectory())) {
log.error("Cannot start nonexistent plugin: " + appName);
disablePlugin(appName);
return false;
}
// Do we need to extract an update?
File pluginUpdate = new File(ctx.getConfigDir(), PLUGIN_DIR + '/' + appName + "/app.xpi2p.zip");
if (pluginUpdate.exists()) {
// Compare the start time of the router with the plugin.
if (ctx.router().getWhenStarted() > pluginUpdate.lastModified()) {
if (!FileUtil.extractZip(pluginUpdate, pluginDir)) {
pluginUpdate.delete();
String foo = "Plugin '" + appName + "' failed to update! File '" + pluginUpdate + "' deleted. You may need to remove and install the plugin again.";
log.error(foo);
disablePlugin(appName);
throw new Exception(foo);
} else {
pluginUpdate.delete();
// Need to always log this, and log.logAlways() did not work for me.
System.err.println("INFO: Plugin updated: " + appName);
}
}
// silently fail to update, because we have not restarted.
}
Properties props = pluginProperties(ctx, appName);
// For the following, we use the exact same translated strings as in PluginUpdateRunner
// to avoid duplication
String minVersion = stripHTML(props, "min-i2p-version");
if (minVersion != null && VersionComparator.comp(CoreVersion.VERSION, minVersion) < 0) {
String foo = "Plugin " + appName + " requires I2P version " + minVersion + " or higher";
log.error(foo);
disablePlugin(appName);
foo = gettext("This plugin requires I2P version {0} or higher", minVersion, ctx);
throw new Exception(foo);
}
minVersion = stripHTML(props, "min-java-version");
if (minVersion != null && VersionComparator.comp(System.getProperty("java.version"), minVersion) < 0) {
String foo = "Plugin " + appName + " requires Java version " + minVersion + " or higher";
log.error(foo);
disablePlugin(appName);
foo = gettext("This plugin requires Java version {0} or higher", minVersion, ctx);
throw new Exception(foo);
}
String jVersion = RouterConsoleRunner.jettyVersion();
minVersion = stripHTML(props, "min-jetty-version");
if (minVersion != null && VersionComparator.comp(minVersion, jVersion) > 0) {
String foo = "Plugin " + appName + " requires Jetty version " + minVersion + " or higher";
log.error(foo);
disablePlugin(appName);
foo = gettext("Plugin requires Jetty version {0} or higher", minVersion, ctx);
throw new Exception(foo);
}
String blacklistVersion = jetty9Blacklist.get(appName);
String curVersion = stripHTML(props, "version");
if (blacklistVersion != null && VersionComparator.comp(curVersion, blacklistVersion) <= 0) {
String foo = "Plugin " + appName + " requires Jetty version 8.9999 or lower";
log.error(foo);
disablePlugin(appName);
foo = gettext("Plugin requires Jetty version {0} or lower", "8.9999", ctx);
throw new Exception(foo);
}
String maxVersion = stripHTML(props, "max-jetty-version");
if (maxVersion != null && VersionComparator.comp(maxVersion, jVersion) < 0) {
String foo = "Plugin " + appName + " requires Jetty version " + maxVersion + " or lower";
log.error(foo);
disablePlugin(appName);
foo = gettext("Plugin requires Jetty version {0} or lower", maxVersion, ctx);
throw new Exception(foo);
}
if (log.shouldLog(Log.INFO))
log.info("Starting plugin: " + appName);
// register themes
File dir = new File(pluginDir, "console/themes");
File[] tfiles = dir.listFiles();
if (tfiles != null) {
for (int i = 0; i < tfiles.length; i++) {
String name = tfiles[i].getName();
if (tfiles[i].isDirectory() && (!Arrays.asList(STANDARD_THEMES).contains(tfiles[i]))) {
// deprecated
ctx.router().setConfigSetting(CSSHelper.PROP_THEME_PFX + name, tfiles[i].getAbsolutePath());
// we don't need to save
}
}
}
// handle console icons for plugins without web-resources through prop icon-code
String fullprop = props.getProperty("icon-code");
if (fullprop != null && fullprop.length() > 1) {
byte[] decoded = Base64.decode(fullprop);
if (decoded != null) {
NavHelper.setBinary(appName, decoded);
iconfile = "/Plugins/pluginicon?plugin=" + appName;
} else {
iconfile = "/themes/console/images/plugin.png";
}
}
// load and start things in clients.config
File clientConfig = new File(pluginDir, "clients.config");
if (clientConfig.exists()) {
Properties cprops = new Properties();
DataHelper.loadProps(cprops, clientConfig);
List<ClientAppConfig> clients = ClientAppConfig.getClientApps(clientConfig);
runClientApps(ctx, pluginDir, clients, "start");
}
// start console webapps in console/webapps
ContextHandlerCollection server = WebAppStarter.getConsoleServer();
if (server != null) {
File consoleDir = new File(pluginDir, "console");
Properties wprops = RouterConsoleRunner.webAppProperties(consoleDir.getAbsolutePath());
File webappDir = new File(consoleDir, "webapps");
File[] files = webappDir.listFiles(RouterConsoleRunner.WAR_FILTER);
if (files != null) {
if (!pluginWars.containsKey(appName))
pluginWars.put(appName, new ConcurrentHashSet<String>());
for (int i = 0; i < files.length; i++) {
try {
String warName = files[i].getName();
warName = warName.substring(0, warName.lastIndexOf(".war"));
// check for duplicates in $I2P
if (Arrays.asList(STANDARD_WEBAPPS).contains(warName)) {
log.error("Skipping duplicate webapp " + warName + " in plugin " + appName);
continue;
}
String enabled = wprops.getProperty(RouterConsoleRunner.PREFIX + warName + ENABLED);
if (!"false".equals(enabled)) {
if (log.shouldLog(Log.INFO))
log.info("Starting webapp: " + warName);
String path = files[i].getCanonicalPath();
WebAppStarter.startWebApp(ctx, server, warName, path);
pluginWars.get(appName).add(warName);
}
} catch (IOException ioe) {
log.error("Error resolving '" + files[i] + "' in '" + webappDir, ioe);
}
}
// Check for iconfile in plugin.properties
String icfile = stripHTML(props, "console-icon");
if (icfile != null && !icfile.contains("..")) {
StringBuilder buf = new StringBuilder(32);
buf.append('/').append(appName);
if (!icfile.startsWith("/"))
buf.append('/');
buf.append(icfile);
iconfile = buf.toString();
}
}
} else {
log.error("No console web server to start plugins?");
}
// add translation jars in console/locale
// These will not override existing resource bundles since we are adding them
// later in the classpath.
File localeDir = new File(pluginDir, "console/locale");
if (localeDir.exists() && localeDir.isDirectory()) {
File[] files = localeDir.listFiles(new FileSuffixFilter(".jar"));
if (files != null) {
boolean added = false;
for (int i = 0; i < files.length; i++) {
File f = files[i];
try {
addPath(f.toURI().toURL());
log.info("INFO: Adding translation plugin to classpath: " + f);
added = true;
} catch (ClassCastException e) {
log.logAlways(Log.WARN, "Java version: " + System.getProperty("java.version") + " does not support adding classpath element: " + f + " for plugin " + appName);
} catch (RuntimeException e) {
log.error("Plugin " + appName + " bad classpath element: " + f, e);
}
}
if (added)
Translate.clearCache();
}
}
// add summary bar link
String name = stripHTML(props, "consoleLinkName_" + Messages.getLanguage(ctx));
if (name == null)
name = stripHTML(props, "consoleLinkName");
String url = stripHTML(props, "consoleLinkURL");
if (name != null && url != null && name.length() > 0 && url.length() > 0) {
String tip = stripHTML(props, "consoleLinkTooltip_" + Messages.getLanguage(ctx));
if (tip == null)
tip = stripHTML(props, "consoleLinkTooltip");
NavHelper.registerApp(name, url, tip, iconfile);
}
return true;
}
use of net.i2p.util.FileSuffixFilter in project i2p.i2p by i2p.
the class CertUtil method loadCRLs.
/**
* Load CRLs from the directory into the set.
*
* @since 0.9.25
*/
private static void loadCRLs(Set<X509CRL> crls, File dir) {
if (dir.exists() && dir.isDirectory()) {
File[] files = dir.listFiles(new FileSuffixFilter(".crl"));
if (files != null) {
for (int i = 0; i < files.length; i++) {
File f = files[i];
try {
X509CRL crl = loadCRL(f);
crls.add(crl);
} catch (IOException ioe) {
error("Cannot load CRL from " + f, ioe);
} catch (GeneralSecurityException crle) {
error("Cannot load CRL from " + f, crle);
}
}
}
}
}
use of net.i2p.util.FileSuffixFilter in project i2p.i2p by i2p.
the class PersistentMailCache method importMail.
/**
* For debugging. Import .eml files from the import/ directory
* @since 0.9.34
*/
private void importMail() {
File importDir = new File(_cacheDir.getParentFile(), DIR_IMPORT);
if (importDir.exists() && importDir.isDirectory()) {
File[] files = importDir.listFiles(new FileSuffixFilter(".eml"));
if (files == null)
return;
for (int i = 0; i < files.length; i++) {
File f = files[i];
// Read in the headers to get the X-UIDL that Thunderbird stuck in there
String uidl = Long.toString(_context.random().nextLong());
InputStream in = null;
try {
in = new FileInputStream(f);
for (int j = 0; j < 20; j++) {
String line = DataHelper.readLine(in);
if (line.length() < 2)
break;
if (line.startsWith("X-UIDL:")) {
uidl = line.substring(7).trim();
break;
}
}
} catch (IOException ioe) {
Debug.debug(Debug.ERROR, "Import failed " + f, ioe);
continue;
} finally {
if (in != null)
try {
in.close();
} catch (IOException ioe) {
}
}
if (uidl == null)
uidl = Long.toString(_context.random().nextLong());
File to = getFullFile(uidl);
if (to.exists()) {
Debug.debug(Debug.DEBUG, "Already have " + f + " as UIDL " + uidl);
f.delete();
continue;
}
in = null;
OutputStream out = null;
try {
in = new FileInputStream(f);
GzipFileBuffer gb = new GzipFileBuffer(to);
// Thunderbird exports aren't CRLF terminated
out = new FixCRLFOutputStream(gb.getOutputStream());
DataHelper.copy(in, out);
} catch (IOException ioe) {
Debug.debug(Debug.ERROR, "Import failed " + f, ioe);
continue;
} finally {
if (in != null)
try {
in.close();
} catch (IOException ioe) {
}
if (out != null)
try {
out.close();
} catch (IOException ioe) {
}
}
f.delete();
Debug.debug(Debug.DEBUG, "Imported " + f + " as UIDL " + uidl);
}
}
}
use of net.i2p.util.FileSuffixFilter in project i2p.i2p by i2p.
the class WebAppConfiguration method getSystemClassPath.
/**
* Convert URL to URI so there's no blocking equals(),
* not that there's really any hostnames in here,
* but keep findbugs happy.
* @since 0.9
*/
private static Set<URI> getSystemClassPath(I2PAppContext ctx) {
Set<URI> rv = new HashSet<URI>(32);
ClassLoader loader = ClassLoader.getSystemClassLoader();
if (loader instanceof URLClassLoader) {
// through Java 8, not available in Java 9
URLClassLoader urlClassLoader = (URLClassLoader) loader;
URL[] urls = urlClassLoader.getURLs();
for (int i = 0; i < urls.length; i++) {
try {
rv.add(urls[i].toURI());
} catch (URISyntaxException use) {
}
}
} else {
// Java 9 - assume everything in lib/ is in the classpath
// except addressbook.jar
File libDir = new File(ctx.getBaseDir(), "lib");
File[] files = libDir.listFiles(new FileSuffixFilter(".jar"));
if (files != null) {
for (int i = 0; i < files.length; i++) {
String name = files[i].getName();
if (!name.equals("addressbook.jar"))
rv.add(files[i].toURI());
}
}
}
return rv;
}
use of net.i2p.util.FileSuffixFilter in project i2p.i2p by i2p.
the class CertHelper method getSummary.
public String getSummary() {
File dir = new File(_context.getConfigDir(), DIR);
try {
_out.write("<h3>");
_out.write(_t("Local SSL Certificates"));
_out.write("</h3>\n");
// console
output("Console", new File(dir, CONSOLE));
// I2CP
output("I2CP", new File(dir, I2CP));
// i2ptunnel clients
File tunnelDir = new File(_context.getConfigDir(), I2PTUNNEL_DIR);
boolean hasTunnels = false;
File[] tunnels = tunnelDir.listFiles(new FileSuffixFilter("i2ptunnel-", ".local.crt"));
if (tunnels != null) {
for (int i = 0; i < tunnels.length; i++) {
File f = tunnels[i];
String name = f.getName();
String b32 = name.substring(10, name.length() - 10);
output(_t("I2PTunnel") + ' ' + b32, f);
hasTunnels = true;
}
}
if (!hasTunnels)
output(_t("I2PTunnel"), null);
// SAM
tunnelDir = new File(dir, SAM_DIR);
hasTunnels = false;
tunnels = tunnelDir.listFiles(new FileSuffixFilter("sam-", ".local.crt"));
if (tunnels != null) {
for (int i = 0; i < tunnels.length; i++) {
File f = tunnels[i];
output("SAM", f);
hasTunnels = true;
}
}
if (!hasTunnels)
output(_t("SAM"), null);
// Family
_out.write("<h3>");
_out.write(_t("Local Router Family Certificate"));
_out.write("</h3>\n");
String family = _context.getProperty(FamilyKeyCrypto.PROP_FAMILY_NAME);
if (family != null) {
File f = new File(dir, "family");
f = new File(f, family + ".crt");
output(_t("Family") + ": " + DataHelper.escapeHTML(family), f);
} else {
_out.write("<p>");
_out.write(_t("none"));
_out.write("</p>\n");
}
// Eepsite
_out.write("<h3>");
_out.write(_t("Website"));
_out.write("</h3>\n");
File ks = new File(_context.getConfigDir(), EEPSITE);
if (ks.exists()) {
// TODO
} else {
_out.write("<p>");
_out.write(_t("none"));
_out.write("</p>\n");
}
// anything else? plugins?
} catch (IOException ioe) {
ioe.printStackTrace();
}
return "";
}
Aggregations