private List<String> getUpdateURLs() { String URLs = _context.getProperty(ConfigUpdateHandler.PROP_UPDATE_URL, ConfigUpdateHandler.DEFAULT_UPDATE_URL); StringTokenizer tok = new StringTokenizer(URLs, " ,\r\n"); List<String> URLList = new ArrayList(); while (tok.hasMoreTokens()) URLList.add(tok.nextToken().trim()); Collections.shuffle(URLList, _context.random()); return URLList; }
public void run() { deferredDeletePlugins(_context); if (_context.getBooleanPropertyDefaultTrue("plugins.autoUpdate") && !NewsHelper.isUpdateInProgress()) { String prev = _context.getProperty("router.previousVersion"); if (prev != null && VersionComparator.comp(RouterVersion.VERSION, prev) > 0) { updateAll(_context, true); } } startPlugins(_context); }
public ProfilePersistenceHelper(RouterContext ctx) { _context = ctx; _log = ctx.logManager().getLog(ProfilePersistenceHelper.class); String dir = _context.getProperty(PROP_PEER_PROFILE_DIR, DEFAULT_PEER_PROFILE_DIR); _profileDir = new SecureDirectory(_context.getRouterDir(), dir); if (!_profileDir.exists()) _profileDir.mkdirs(); for (int j = 0; j < B64.length(); j++) { File subdir = new SecureDirectory(_profileDir, DIR_PREFIX + B64.charAt(j)); if (!subdir.exists()) subdir.mkdir(); } }
/** * The configured SigType to expect on read-in * * @since 0.9.16 */ public static SigType getSigTypeConfig(RouterContext ctx) { SigType cstype = DEFAULT_SIGTYPE; String sstype = ctx.getProperty(PROP_ROUTER_SIGTYPE); if (sstype != null) { SigType ntype = SigType.parseSigType(sstype); if (ntype != null) cstype = ntype; } // fallback? if (cstype != SigType.DSA_SHA1 && !cstype.isAvailable()) cstype = SigType.DSA_SHA1; return cstype; }
/** * this is now misnamed, as it is only used to determine whether to return floodfill peers only */ static boolean onlyQueryFloodfillPeers(RouterContext ctx) { // if (isCongested(ctx)) // return true; // If we are floodfill, we want the FloodfillPeerSelector (in add()) to include // non-ff peers (if required) in DatabaseSearchReplyMessage responses // so that Exploration works. // ExploreJob is disabled if we are floodfill. // The other two places this was called (one below and one in FNDF) // have been commented out. // Returning false essentially enables kademlia as a backup to floodfill for search responses. if (FloodfillNetworkDatabaseFacade.floodfillEnabled(ctx)) return false; return Boolean.valueOf(ctx.getProperty("netDb.floodfillOnly", DEFAULT_FLOODFILL_ONLY + "")) .booleanValue(); }
public UPnPManager(RouterContext context, TransportManager manager) { _context = context; _manager = manager; _log = _context.logManager().getLog(UPnPManager.class); // UPnP wants to bind to IPv6 link local interfaces by default, but what UPnP router // is going to want to talk IPv6 anyway? Just make it easy and force IPv4 only org.cybergarage.upnp.UPnP.setEnable(org.cybergarage.upnp.UPnP.USE_ONLY_IPV4_ADDR); // set up logging in the UPnP package Debug.initialize(context); _upnp = new UPnP(context); _upnp.setHTTPPort(_context.getProperty(PROP_HTTP_PORT, DEFAULT_HTTP_PORT)); _upnp.setSSDPPort(_context.getProperty(PROP_SSDP_PORT, DEFAULT_SSDP_PORT)); _upnpCallback = new UPnPCallback(); _rescanner = new Rescanner(); }
/** @return true on success - caller should call stopPlugin() first */ static boolean deletePlugin(RouterContext ctx, String appName) throws Exception { Log log = ctx.logManager().getLog(PluginStarter.class); File pluginDir = new File(ctx.getConfigDir(), PLUGIN_DIR + '/' + appName); if ((!pluginDir.exists()) || (!pluginDir.isDirectory())) { log.error("Cannot delete nonexistent plugin: " + appName); return false; } // uninstall things in clients.config File clientConfig = new File(pluginDir, "clients.config"); if (clientConfig.exists()) { Properties props = new Properties(); DataHelper.loadProps(props, clientConfig); List<ClientAppConfig> clients = ClientAppConfig.getClientApps(clientConfig); runClientApps(ctx, pluginDir, clients, "uninstall"); } // unregister themes, and switch to default if we are unregistering the current theme File dir = new File(pluginDir, "console/themes"); File[] tfiles = dir.listFiles(); if (tfiles != null) { String current = ctx.getProperty(CSSHelper.PROP_THEME_NAME); Map<String, String> changes = new HashMap<String, String>(); List<String> removes = new ArrayList<String>(); for (int i = 0; i < tfiles.length; i++) { String name = tfiles[i].getName(); if (tfiles[i].isDirectory() && (!Arrays.asList(STANDARD_THEMES).contains(tfiles[i]))) { removes.add(ConfigUIHelper.PROP_THEME_PFX + name); if (name.equals(current)) changes.put(CSSHelper.PROP_THEME_NAME, CSSHelper.DEFAULT_THEME); } } ctx.router().saveConfig(changes, removes); } boolean deleted = FileUtil.rmdir(pluginDir, false); Properties props = pluginProperties(); for (Iterator iter = props.keySet().iterator(); iter.hasNext(); ) { String name = (String) iter.next(); if (name.startsWith(PREFIX + appName + '.')) iter.remove(); } if (!deleted) { // This happens on Windows when there are plugin jars in classpath // Mark it as deleted, we will try again after restart log.logAlways(Log.WARN, "Deletion of " + pluginDir + " failed, will try again at restart"); props.setProperty(PREFIX + appName + ENABLED, DELETED); } storePluginProperties(props); return true; }
/** * @return success if it exists and we have a password, or it was created successfully. * @since 0.8.3 */ private boolean verifyKeyStore(File ks) { if (ks.exists()) { boolean rv = _context.getProperty(PROP_KEY_PASSWORD) != null; if (!rv) System.err.println( "Console SSL error, must set " + PROP_KEY_PASSWORD + " in " + (new File(_context.getConfigDir(), "router.config")).getAbsolutePath()); return rv; } File dir = ks.getParentFile(); if (!dir.exists()) { File sdir = new SecureDirectory(dir.getAbsolutePath()); if (!sdir.mkdir()) return false; } return createKeyStore(ks); }
/** * inline * * @since 0.8.13 */ private static void updateAll(RouterContext ctx, boolean delay) { List<String> plugins = getPlugins(); Map<String, String> toUpdate = new HashMap<String, String>(); for (String appName : plugins) { Properties props = pluginProperties(ctx, appName); String url = props.getProperty("updateURL"); if (url != null) toUpdate.put(appName, url); } if (toUpdate.isEmpty()) return; ConsoleUpdateManager mgr = UpdateHandler.updateManager(ctx); if (mgr == null) return; if (mgr.isUpdateInProgress()) return; if (delay) { // wait for proxy mgr.update(TYPE_DUMMY, 3 * 60 * 1000); mgr.notifyProgress(null, Messages.getString("Checking for plugin updates", ctx)); int loop = 0; do { try { Thread.sleep(5 * 1000); } catch (InterruptedException ie) { } if (loop++ > 40) break; } while (mgr.isUpdateInProgress(TYPE_DUMMY)); } String proxyHost = ctx.getProperty( ConfigUpdateHandler.PROP_PROXY_HOST, ConfigUpdateHandler.DEFAULT_PROXY_HOST); int proxyPort = ConfigUpdateHandler.proxyPort(ctx); if (proxyPort == ConfigUpdateHandler.DEFAULT_PROXY_PORT_INT && proxyHost.equals(ConfigUpdateHandler.DEFAULT_PROXY_HOST) && ctx.portMapper().getPort(PortMapper.SVC_HTTP_PROXY) < 0) { mgr.notifyComplete( null, Messages.getString("Plugin update check failed", ctx) + " - " + Messages.getString("HTTP client proxy tunnel must be running", ctx)); return; } Log log = ctx.logManager().getLog(PluginStarter.class); int updated = 0; for (Map.Entry<String, String> entry : toUpdate.entrySet()) { String appName = entry.getKey(); if (log.shouldLog(Log.WARN)) log.warn("Checking for update plugin: " + appName); // blocking if (mgr.checkAvailable(PLUGIN, appName, 60 * 1000) == null) { if (log.shouldLog(Log.WARN)) log.warn("No update available for plugin: " + appName); continue; } if (log.shouldLog(Log.WARN)) log.warn("Updating plugin: " + appName); // non-blocking mgr.update(PLUGIN, appName, 30 * 60 * 1000); int loop = 0; do { // only wait for 4 minutes, then we will // keep going try { Thread.sleep(5 * 1000); } catch (InterruptedException ie) { } if (loop++ > 48) break; } while (mgr.isUpdateInProgress(PLUGIN, appName)); if (mgr.getUpdateAvailable(PLUGIN, appName) != null) updated++; } if (updated > 0) mgr.notifyComplete(null, ngettext("1 plugin updated", "{0} plugins updated", updated, ctx)); else mgr.notifyComplete(null, Messages.getString("Plugin update check complete", ctx)); }
/** * http://irc.codehaus.org/display/JETTY/Porting+to+jetty6 * * <pre> * Server * HandlerCollection * ContextHandlerCollection * WebAppContext (i.e. ContextHandler) * SessionHandler * SecurityHandler * ServletHandler * servlets... * WebAppContext * ... * DefaultHandler * RequestLogHandler (opt) * </pre> */ public void startConsole() { File workDir = new SecureDirectory(_context.getTempDir(), "jetty-work"); boolean workDirRemoved = FileUtil.rmdir(workDir, false); if (!workDirRemoved) System.err.println("ERROR: Unable to remove Jetty temporary work directory"); boolean workDirCreated = workDir.mkdirs(); if (!workDirCreated) System.err.println("ERROR: Unable to create Jetty temporary work directory"); // try { // Log.setLog(new I2PLogger(_context)); // } catch (Throwable t) { // System.err.println("INFO: I2P Jetty logging class not found, logging to wrapper log"); // } // This way it doesn't try to load Slf4jLog first System.setProperty("org.mortbay.log.class", "net.i2p.jetty.I2PLogger"); // so Jetty can find WebAppConfiguration System.setProperty("jetty.class.path", _context.getBaseDir() + "/lib/routerconsole.jar"); _server = new Server(); _server.setGracefulShutdown(1000); try { ThreadPool ctp = new CustomThreadPoolExecutor(); ctp.prestartAllCoreThreads(); _server.setThreadPool(ctp); } catch (Throwable t) { // class not found... System.out.println("INFO: Jetty concurrent ThreadPool unavailable, using QueuedThreadPool"); QueuedThreadPool qtp = new QueuedThreadPool(MAX_THREADS); qtp.setMinThreads(MIN_THREADS); qtp.setMaxIdleTimeMs(MAX_IDLE_TIME); _server.setThreadPool(qtp); } HandlerCollection hColl = new HandlerCollection(); ContextHandlerCollection chColl = new ContextHandlerCollection(); _server.addHandler(hColl); hColl.addHandler(chColl); hColl.addHandler(new DefaultHandler()); String log = _context.getProperty("routerconsole.log"); if (log != null) { File logFile = new File(log); if (!logFile.isAbsolute()) logFile = new File(_context.getLogDir(), "logs/" + log); try { RequestLogHandler rhl = new RequestLogHandler(); rhl.setRequestLog(new NCSARequestLog(logFile.getAbsolutePath())); hColl.addHandler(rhl); } catch (Exception ioe) { System.err.println("ERROR: Unable to create Jetty log: " + ioe); } } boolean rewrite = false; Properties props = webAppProperties(); if (props.isEmpty()) { props.setProperty(PREFIX + ROUTERCONSOLE + ENABLED, "true"); rewrite = true; } // Get an absolute path with a trailing slash for the webapps dir // We assume relative to the base install dir for backward compatibility File app = new File(_webAppsDir); if (!app.isAbsolute()) { app = new File(_context.getBaseDir(), _webAppsDir); try { _webAppsDir = app.getCanonicalPath(); } catch (IOException ioe) { } } if (!_webAppsDir.endsWith("/")) _webAppsDir += '/'; WebAppContext rootWebApp = null; ServletHandler rootServletHandler = null; List<Connector> connectors = new ArrayList(4); try { int boundAddresses = 0; Set addresses = Addresses.getAllAddresses(); boolean hasIPV4 = addresses.contains("0.0.0.0"); boolean hasIPV6 = addresses.contains("0:0:0:0:0:0:0:0"); // add standard listeners int lport = 0; if (_listenPort != null) { try { lport = Integer.parseInt(_listenPort); } catch (NumberFormatException nfe) { } if (lport <= 0) System.err.println("Bad routerconsole port " + _listenPort); } if (lport > 0) { StringTokenizer tok = new StringTokenizer(_listenHost, " ,"); while (tok.hasMoreTokens()) { String host = tok.nextToken().trim(); try { // Test before we add the connector, because Jetty 6 won't start if any of the // connectors are bad InetAddress test = InetAddress.getByName(host); if ((!hasIPV6) && (!(test instanceof Inet4Address))) throw new IOException("IPv6 addresses unsupported"); if ((!hasIPV4) && (test instanceof Inet4Address)) throw new IOException("IPv4 addresses unsupported"); ServerSocket testSock = null; try { // On Windows, this was passing and Jetty was still failing, // possibly due to %scope_id ??? // https://issues.apache.org/jira/browse/ZOOKEEPER-667 // testSock = new ServerSocket(0, 0, test); // so do exactly what Jetty does in SelectChannelConnector.open() testSock = new ServerSocket(); InetSocketAddress isa = new InetSocketAddress(host, 0); testSock.bind(isa); } finally { if (testSock != null) try { testSock.close(); } catch (IOException ioe) { } } // if (host.indexOf(":") >= 0) // IPV6 - requires patched Jetty 5 // _server.addListener('[' + host + "]:" + _listenPort); // else // _server.addListener(host + ':' + _listenPort); AbstractConnector lsnr; if (SystemVersion.isJava6() && !SystemVersion.isGNU()) { SelectChannelConnector slsnr = new SelectChannelConnector(); slsnr.setUseDirectBuffers(false); // default true seems to be leaky lsnr = slsnr; } else { // Jetty 6 and NIO on Java 5 don't get along that well // Also: http://jira.codehaus.org/browse/JETTY-1238 // "Do not use GCJ with Jetty, it will not work." // Actually it does if you don't use NIO lsnr = new SocketConnector(); } lsnr.setHost(host); lsnr.setPort(lport); lsnr.setMaxIdleTime(90 * 1000); // default 10 sec lsnr.setName("ConsoleSocket"); // all with same name will use the same thread pool // _server.addConnector(lsnr); connectors.add(lsnr); boundAddresses++; } catch (Exception ioe) { System.err.println( "Unable to bind routerconsole to " + host + " port " + _listenPort + ": " + ioe); System.err.println( "You may ignore this warning if the console is still available at http://localhost:" + _listenPort); } } // XXX: what if listenhosts do not include 127.0.0.1? (Should that ever even happen?) _context.portMapper().register(PortMapper.SVC_CONSOLE, lport); } // add SSL listeners int sslPort = 0; if (_sslListenPort != null) { try { sslPort = Integer.parseInt(_sslListenPort); } catch (NumberFormatException nfe) { } if (sslPort <= 0) System.err.println("Bad routerconsole SSL port " + _sslListenPort); } if (sslPort > 0) { File keyStore = new File(_context.getConfigDir(), "keystore/console.ks"); if (verifyKeyStore(keyStore)) { StringTokenizer tok = new StringTokenizer(_sslListenHost, " ,"); while (tok.hasMoreTokens()) { String host = tok.nextToken().trim(); // doing it this way means we don't have to escape an IPv6 host with [] try { // Test before we add the connector, because Jetty 6 won't start if any of the // connectors are bad InetAddress test = InetAddress.getByName(host); if ((!hasIPV6) && (!(test instanceof Inet4Address))) throw new IOException("IPv6 addresses unsupported"); if ((!hasIPV4) && (test instanceof Inet4Address)) throw new IOException("IPv4 addresses unsupported"); ServerSocket testSock = null; try { // see comments above // testSock = new ServerSocket(0, 0, test); testSock = new ServerSocket(); InetSocketAddress isa = new InetSocketAddress(host, 0); testSock.bind(isa); } finally { if (testSock != null) try { testSock.close(); } catch (IOException ioe) { } } // TODO if class not found use SslChannelConnector // Sadly there's no common base class with the ssl methods in it AbstractConnector ssll; if (SystemVersion.isJava6() && !SystemVersion.isGNU()) { SslSelectChannelConnector sssll = new SslSelectChannelConnector(); // the keystore path and password sssll.setKeystore(keyStore.getAbsolutePath()); sssll.setPassword( _context.getProperty(PROP_KEYSTORE_PASSWORD, DEFAULT_KEYSTORE_PASSWORD)); // the X.509 cert password (if not present, verifyKeyStore() returned false) sssll.setKeyPassword(_context.getProperty(PROP_KEY_PASSWORD, "thisWontWork")); sssll.setUseDirectBuffers(false); // default true seems to be leaky ssll = sssll; } else { // Jetty 6 and NIO on Java 5 don't get along that well SslSocketConnector sssll = new SslSocketConnector(); // the keystore path and password sssll.setKeystore(keyStore.getAbsolutePath()); sssll.setPassword( _context.getProperty(PROP_KEYSTORE_PASSWORD, DEFAULT_KEYSTORE_PASSWORD)); // the X.509 cert password (if not present, verifyKeyStore() returned false) sssll.setKeyPassword(_context.getProperty(PROP_KEY_PASSWORD, "thisWontWork")); ssll = sssll; } ssll.setHost(host); ssll.setPort(sslPort); ssll.setMaxIdleTime(90 * 1000); // default 10 sec ssll.setName("ConsoleSocket"); // all with same name will use the same thread pool // _server.addConnector(ssll); connectors.add(ssll); boundAddresses++; } catch (Exception e) { System.err.println( "Unable to bind routerconsole to " + host + " port " + sslPort + " for SSL: " + e); if (SystemVersion.isGNU()) System.err.println("Probably because GNU classpath does not support Sun keystores"); System.err.println( "You may ignore this warning if the console is still available at https://localhost:" + sslPort); } } _context.portMapper().register(PortMapper.SVC_HTTPS_CONSOLE, sslPort); } else { System.err.println( "Unable to create or access keystore for SSL: " + keyStore.getAbsolutePath()); } } if (boundAddresses <= 0) { System.err.println( "Unable to bind routerconsole to any address on port " + _listenPort + (sslPort > 0 ? (" or SSL port " + sslPort) : "")); return; } rootWebApp = new LocaleWebAppHandler(_context, "/", _webAppsDir + ROUTERCONSOLE + ".war"); File tmpdir = new SecureDirectory( workDir, ROUTERCONSOLE + "-" + (_listenPort != null ? _listenPort : _sslListenPort)); tmpdir.mkdir(); rootWebApp.setTempDirectory(tmpdir); rootWebApp.setExtractWAR(false); rootWebApp.setSessionHandler(new SessionHandler()); rootServletHandler = new ServletHandler(); rootWebApp.setServletHandler(rootServletHandler); initialize(_context, rootWebApp); chColl.addHandler(rootWebApp); } catch (Exception ioe) { ioe.printStackTrace(); } try { // start does a mapContexts() _server.start(); } catch (Throwable me) { // NoClassFoundDefError from a webapp is a throwable, not an exception System.err.println("Error starting the Router Console server: " + me); me.printStackTrace(); } if (_server.isRunning()) { // Add and start the connectors one-by-one boolean error = false; for (Connector conn : connectors) { try { _server.addConnector(conn); // start after adding so it gets the right thread pool conn.start(); } catch (Throwable me) { try { _server.removeConnector(conn); } catch (Throwable t) { t.printStackTrace(); } System.err.println("WARNING: Error starting " + conn + ": " + me); me.printStackTrace(); error = true; } } if (error) { System.err.println( "WARNING: Error starting one or more listeners of the Router Console server.\n" + "If your console is still accessible at http://127.0.0.1:" + _listenPort + "/,\n" + "this may be a problem only with binding to the IPV6 address ::1.\n" + "If so, you may ignore this error, or remove the\n" + "\"::1,\" in the \"clientApp.0.args\" line of the clients.config file."); } } // Start all the other webapps after the server is up, // so things start faster. // Jetty 6 starts the connector before the router console is ready // This also prevents one webapp from breaking the whole thing List<String> notStarted = new ArrayList(); if (_server.isRunning()) { File dir = new File(_webAppsDir); String fileNames[] = dir.list(WarFilenameFilter.instance()); if (fileNames != null) { for (int i = 0; i < fileNames.length; i++) { String appName = fileNames[i].substring(0, fileNames[i].lastIndexOf(".war")); String enabled = props.getProperty(PREFIX + appName + ENABLED); if (!"false".equals(enabled)) { try { String path = new File(dir, fileNames[i]).getCanonicalPath(); WebAppStarter.startWebApp(_context, chColl, appName, path); if (enabled == null) { // do this so configclients.jsp knows about all apps from reading the config props.setProperty(PREFIX + appName + ENABLED, "true"); rewrite = true; } } catch (Throwable t) { System.err.println("ERROR: Failed to start " + appName + ' ' + t); t.printStackTrace(); notStarted.add(appName); } } else { notStarted.add(appName); } } changeState(RUNNING); } } else { System.err.println("ERROR: Router console did not start, not starting webapps"); changeState(START_FAILED); } if (rewrite) storeWebAppProperties(_context, props); if (rootServletHandler != null && notStarted.size() > 0) { // map each not-started webapp to the error page ServletHolder noWebApp = rootServletHandler.getServlet("net.i2p.router.web.jsp.nowebapp_jsp"); for (int i = 0; i < notStarted.size(); i++) { // we want a new handler for each one since if the webapp is started we remove the // handler??? try { if (noWebApp != null) { String path = '/' + notStarted.get(i); // LocaleWebAppsHandler adds a .jsp rootServletHandler.addServletWithMapping(noWebApp, path + ".jsp"); rootServletHandler.addServletWithMapping(noWebApp, path + "/*"); } else { System.err.println("Can't find nowebapp.jsp?"); } } catch (Throwable me) { System.err.println(me); me.printStackTrace(); } } } Thread t = new I2PAppThread(new StatSummarizer(), "StatSummarizer", true); t.setPriority(Thread.NORM_PRIORITY - 1); t.start(); ConsoleUpdateManager um = new ConsoleUpdateManager(_context); um.start(); if (PluginStarter.pluginsEnabled(_context)) { t = new I2PAppThread(new PluginStarter(_context), "PluginStarter", true); t.setPriority(Thread.NORM_PRIORITY - 1); t.start(); _context.addShutdownTask(new PluginStopper(_context)); } // stat summarizer registers its own hook _context.addShutdownTask(new ServerShutdown()); ConfigServiceHandler.registerSignalHandler(_context); }
private void updateSettings() { _doLog = _context.getBooleanProperty(PROP_KEEP_MESSAGE_HISTORY); _historyFile = _context.getProperty(PROP_MESSAGE_HISTORY_FILENAME, DEFAULT_MESSAGE_HISTORY_FILENAME); }