Search in sources :

Example 1 with FileSuffixFilter

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;
}
Also used : Log(net.i2p.util.Log) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException) ConcurrentHashSet(net.i2p.util.ConcurrentHashSet) ClientAppConfig(net.i2p.router.startup.ClientAppConfig) FileSuffixFilter(net.i2p.util.FileSuffixFilter) File(java.io.File)

Example 2 with FileSuffixFilter

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);
                }
            }
        }
    }
}
Also used : X509CRL(java.security.cert.X509CRL) GeneralSecurityException(java.security.GeneralSecurityException) FileSuffixFilter(net.i2p.util.FileSuffixFilter) IOException(java.io.IOException) File(java.io.File)

Example 3 with FileSuffixFilter

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);
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FixCRLFOutputStream(i2p.susi.util.FixCRLFOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileSuffixFilter(net.i2p.util.FileSuffixFilter) IOException(java.io.IOException) GzipFileBuffer(i2p.susi.util.GzipFileBuffer) FixCRLFOutputStream(i2p.susi.util.FixCRLFOutputStream) SecureFile(net.i2p.util.SecureFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with FileSuffixFilter

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;
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) WebAppClassLoader(org.eclipse.jetty.webapp.WebAppClassLoader) URLClassLoader(java.net.URLClassLoader) FileSuffixFilter(net.i2p.util.FileSuffixFilter) File(java.io.File) HashSet(java.util.HashSet)

Example 5 with FileSuffixFilter

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 "";
}
Also used : FileSuffixFilter(net.i2p.util.FileSuffixFilter) IOException(java.io.IOException) File(java.io.File)

Aggregations

File (java.io.File)8 FileSuffixFilter (net.i2p.util.FileSuffixFilter)8 IOException (java.io.IOException)6 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 Log (net.i2p.util.Log)2 FixCRLFOutputStream (i2p.susi.util.FixCRLFOutputStream)1 GzipFileBuffer (i2p.susi.util.GzipFileBuffer)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 GeneralSecurityException (java.security.GeneralSecurityException)1 X509CRL (java.security.cert.X509CRL)1