2004-07-24 02:06:07 +00:00
|
|
|
package net.i2p.router.web;
|
|
|
|
|
2009-11-27 13:26:38 +00:00
|
|
|
import java.util.ArrayList;
|
2011-02-11 19:12:19 +00:00
|
|
|
import java.awt.GraphicsEnvironment;
|
2004-09-29 19:34:02 +00:00
|
|
|
import java.io.File;
|
2008-06-16 12:26:36 +00:00
|
|
|
import java.io.FilenameFilter;
|
2004-07-24 02:06:07 +00:00
|
|
|
import java.io.IOException;
|
2010-12-14 14:34:27 +00:00
|
|
|
import java.security.KeyStore;
|
2004-08-10 19:51:11 +00:00
|
|
|
import java.util.List;
|
2008-06-16 12:26:36 +00:00
|
|
|
import java.util.Properties;
|
2009-04-08 01:34:12 +00:00
|
|
|
import java.util.StringTokenizer;
|
2011-12-24 17:09:01 +00:00
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.SynchronousQueue;
|
2011-12-23 00:56:48 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2011-12-24 17:09:01 +00:00
|
|
|
import java.util.concurrent.ThreadFactory;
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
2004-08-10 19:51:11 +00:00
|
|
|
|
2005-03-24 01:19:52 +00:00
|
|
|
import net.i2p.I2PAppContext;
|
2011-02-27 13:53:39 +00:00
|
|
|
import net.i2p.apps.systray.SysTray;
|
2010-12-05 19:04:33 +00:00
|
|
|
import net.i2p.data.Base32;
|
2008-06-16 12:26:36 +00:00
|
|
|
import net.i2p.data.DataHelper;
|
2011-02-27 13:53:39 +00:00
|
|
|
import net.i2p.desktopgui.Main;
|
2011-12-30 19:44:57 +00:00
|
|
|
import net.i2p.jetty.I2PLogger;
|
2008-07-16 13:42:54 +00:00
|
|
|
import net.i2p.router.RouterContext;
|
2004-09-29 19:34:02 +00:00
|
|
|
import net.i2p.util.FileUtil;
|
2009-08-26 22:16:29 +00:00
|
|
|
import net.i2p.util.I2PAppThread;
|
2011-12-15 23:36:32 +00:00
|
|
|
import net.i2p.util.PortMapper;
|
2010-07-06 15:22:48 +00:00
|
|
|
import net.i2p.util.SecureDirectory;
|
2010-12-05 19:04:33 +00:00
|
|
|
import net.i2p.util.SecureFileOutputStream;
|
|
|
|
import net.i2p.util.ShellCommand;
|
2011-01-20 17:10:30 +00:00
|
|
|
import net.i2p.util.VersionComparator;
|
2004-08-10 19:51:11 +00:00
|
|
|
|
2011-12-23 00:56:48 +00:00
|
|
|
import org.mortbay.jetty.AbstractConnector;
|
|
|
|
import org.mortbay.jetty.Connector;
|
|
|
|
import org.mortbay.jetty.Handler;
|
|
|
|
import org.mortbay.jetty.NCSARequestLog;
|
2008-07-16 13:42:54 +00:00
|
|
|
import org.mortbay.jetty.Server;
|
2011-12-23 00:56:48 +00:00
|
|
|
import org.mortbay.jetty.handler.ContextHandlerCollection;
|
|
|
|
import org.mortbay.jetty.handler.DefaultHandler;
|
|
|
|
import org.mortbay.jetty.handler.HandlerCollection;
|
|
|
|
import org.mortbay.jetty.handler.RequestLogHandler;
|
|
|
|
import org.mortbay.jetty.nio.SelectChannelConnector;
|
|
|
|
import org.mortbay.jetty.security.DigestAuthenticator;
|
|
|
|
import org.mortbay.jetty.security.HashUserRealm;
|
|
|
|
import org.mortbay.jetty.security.Constraint;
|
|
|
|
import org.mortbay.jetty.security.ConstraintMapping;
|
|
|
|
import org.mortbay.jetty.security.SecurityHandler;
|
|
|
|
import org.mortbay.jetty.security.SslSelectChannelConnector;
|
|
|
|
import org.mortbay.jetty.servlet.ServletHandler;
|
2011-12-24 05:26:56 +00:00
|
|
|
import org.mortbay.jetty.servlet.ServletHolder;
|
2011-12-23 00:56:48 +00:00
|
|
|
import org.mortbay.jetty.servlet.SessionHandler;
|
|
|
|
import org.mortbay.jetty.webapp.WebAppContext;
|
2011-12-30 19:44:57 +00:00
|
|
|
import org.mortbay.log.Log;
|
2011-12-23 00:56:48 +00:00
|
|
|
import org.mortbay.thread.QueuedThreadPool;
|
|
|
|
import org.mortbay.thread.concurrent.ThreadPool;
|
2004-07-24 02:06:07 +00:00
|
|
|
|
2011-12-23 00:56:48 +00:00
|
|
|
/**
|
|
|
|
* Start the router console.
|
|
|
|
*/
|
2004-07-24 02:06:07 +00:00
|
|
|
public class RouterConsoleRunner {
|
2011-12-23 00:56:48 +00:00
|
|
|
private static Server _server;
|
2010-12-05 19:04:33 +00:00
|
|
|
private String _listenPort;
|
|
|
|
private String _listenHost;
|
|
|
|
private String _sslListenPort;
|
|
|
|
private String _sslListenHost;
|
|
|
|
private String _webAppsDir;
|
2011-12-23 00:56:48 +00:00
|
|
|
|
2008-06-16 12:26:36 +00:00
|
|
|
private static final String PROP_WEBAPP_CONFIG_FILENAME = "router.webappsConfigFile";
|
|
|
|
private static final String DEFAULT_WEBAPP_CONFIG_FILENAME = "webapps.config";
|
2010-02-27 18:02:56 +00:00
|
|
|
private static final DigestAuthenticator authenticator = new DigestAuthenticator();
|
2008-06-16 12:26:36 +00:00
|
|
|
public static final String ROUTERCONSOLE = "routerconsole";
|
|
|
|
public static final String PREFIX = "webapps.";
|
|
|
|
public static final String ENABLED = ".startOnLoad";
|
2010-12-05 19:04:33 +00:00
|
|
|
private static final String PROP_KEYSTORE_PASSWORD = "routerconsole.keystorePassword";
|
|
|
|
private static final String DEFAULT_KEYSTORE_PASSWORD = "changeit";
|
|
|
|
private static final String PROP_KEY_PASSWORD = "routerconsole.keyPassword";
|
|
|
|
private static final String DEFAULT_LISTEN_PORT = "7657";
|
|
|
|
private static final String DEFAULT_LISTEN_HOST = "127.0.0.1";
|
|
|
|
private static final String DEFAULT_WEBAPPS_DIR = "./webapps/";
|
|
|
|
private static final String USAGE = "Bad RouterConsoleRunner arguments, check clientApp.0.args in your clients.config file! " +
|
|
|
|
"Usage: [[port host[,host]] [-s sslPort [host[,host]]] [webAppsDir]]";
|
2011-12-23 00:56:48 +00:00
|
|
|
|
|
|
|
private static final int MIN_THREADS = 1;
|
|
|
|
private static final int MAX_THREADS = 24;
|
|
|
|
private static final int MAX_IDLE_TIME = 90*1000;
|
2011-12-24 17:09:01 +00:00
|
|
|
private static final String THREAD_NAME = "RouterConsole Jetty";
|
2004-07-24 02:06:07 +00:00
|
|
|
|
2004-09-30 06:57:22 +00:00
|
|
|
static {
|
|
|
|
System.setProperty("org.mortbay.http.Version.paranoid", "true");
|
|
|
|
}
|
|
|
|
|
2009-04-08 01:34:12 +00:00
|
|
|
/**
|
2010-12-05 19:04:33 +00:00
|
|
|
* <pre>
|
|
|
|
* non-SSL:
|
|
|
|
* RouterConsoleRunner
|
|
|
|
* RouterConsoleRunner 7657
|
|
|
|
* RouterConsoleRunner 7657 127.0.0.1
|
|
|
|
* RouterConsoleRunner 7657 127.0.0.1,::1
|
|
|
|
* RouterConsoleRunner 7657 127.0.0.1,::1 ./webapps/
|
|
|
|
*
|
|
|
|
* SSL:
|
|
|
|
* RouterConsoleRunner -s 7657
|
|
|
|
* RouterConsoleRunner -s 7657 127.0.0.1
|
|
|
|
* RouterConsoleRunner -s 7657 127.0.0.1,::1
|
|
|
|
* RouterConsoleRunner -s 7657 127.0.0.1,::1 ./webapps/
|
|
|
|
*
|
|
|
|
* If using both, non-SSL must be first:
|
|
|
|
* RouterConsoleRunner 7657 127.0.0.1 -s 7667
|
|
|
|
* RouterConsoleRunner 7657 127.0.0.1 -s 7667 127.0.0.1
|
|
|
|
* RouterConsoleRunner 7657 127.0.0.1,::1 -s 7667 127.0.0.1,::1
|
|
|
|
* RouterConsoleRunner 7657 127.0.0.1,::1 -s 7667 127.0.0.1,::1 ./webapps/
|
|
|
|
* </pre>
|
|
|
|
*
|
2009-04-08 01:34:12 +00:00
|
|
|
* @param args second arg may be a comma-separated list of bind addresses,
|
|
|
|
* for example ::1,127.0.0.1
|
|
|
|
* On XP, the other order (127.0.0.1,::1) fails the IPV6 bind,
|
|
|
|
* because 127.0.0.1 will bind ::1 also. But even though it's bound
|
|
|
|
* to both, we can't connect to [::1]:7657 for some reason.
|
|
|
|
* So the wise choice is ::1,127.0.0.1
|
|
|
|
*/
|
2004-07-24 02:06:07 +00:00
|
|
|
public RouterConsoleRunner(String args[]) {
|
2010-12-05 19:04:33 +00:00
|
|
|
if (args.length == 0) {
|
2010-12-14 14:34:27 +00:00
|
|
|
// _listenHost and _webAppsDir are defaulted below
|
2010-12-05 19:04:33 +00:00
|
|
|
_listenPort = DEFAULT_LISTEN_PORT;
|
|
|
|
} else {
|
|
|
|
boolean ssl = false;
|
|
|
|
for (int i = 0; i < args.length; i++) {
|
|
|
|
if (args[i].equals("-s"))
|
|
|
|
ssl = true;
|
|
|
|
else if ((!ssl) && _listenPort == null)
|
|
|
|
_listenPort = args[i];
|
|
|
|
else if ((!ssl) && _listenHost == null)
|
|
|
|
_listenHost = args[i];
|
|
|
|
else if (ssl && _sslListenPort == null)
|
|
|
|
_sslListenPort = args[i];
|
|
|
|
else if (ssl && _sslListenHost == null)
|
|
|
|
_sslListenHost = args[i];
|
|
|
|
else if (_webAppsDir == null)
|
|
|
|
_webAppsDir = args[i];
|
|
|
|
else {
|
|
|
|
System.err.println(USAGE);
|
|
|
|
throw new IllegalArgumentException(USAGE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_listenHost == null)
|
|
|
|
_listenHost = DEFAULT_LISTEN_HOST;
|
|
|
|
if (_sslListenHost == null)
|
|
|
|
_sslListenHost = _listenHost;
|
|
|
|
if (_webAppsDir == null)
|
|
|
|
_webAppsDir = DEFAULT_WEBAPPS_DIR;
|
|
|
|
// _listenPort and _sslListenPort are not defaulted, if one or the other is null, do not enable
|
|
|
|
if (_listenPort == null && _sslListenPort == null) {
|
|
|
|
System.err.println(USAGE);
|
|
|
|
throw new IllegalArgumentException(USAGE);
|
2004-07-24 02:06:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String args[]) {
|
2011-02-27 13:53:39 +00:00
|
|
|
startTrayApp();
|
2004-07-24 02:06:07 +00:00
|
|
|
RouterConsoleRunner runner = new RouterConsoleRunner(args);
|
|
|
|
runner.startConsole();
|
|
|
|
}
|
|
|
|
|
2011-12-23 00:56:48 +00:00
|
|
|
/**
|
|
|
|
* SInce _server is now static
|
|
|
|
* @return may be null or stopped perhaps
|
|
|
|
* @since Jetty 6 since it doesn't have Server.getServers()
|
|
|
|
*/
|
|
|
|
static Server getConsoleServer() {
|
|
|
|
return _server;
|
|
|
|
}
|
|
|
|
|
2011-02-27 13:53:39 +00:00
|
|
|
private static void startTrayApp() {
|
|
|
|
try {
|
|
|
|
//TODO: move away from routerconsole into a separate application.
|
|
|
|
//ApplicationManager?
|
|
|
|
VersionComparator v = new VersionComparator();
|
|
|
|
boolean recentJava = v.compare(System.getProperty("java.runtime.version"), "1.6") >= 0;
|
|
|
|
// default false for now
|
|
|
|
boolean desktopguiEnabled = I2PAppContext.getGlobalContext().getBooleanProperty("desktopgui.enabled");
|
|
|
|
if (recentJava && desktopguiEnabled) {
|
|
|
|
//Check if we are in a headless environment, set properties accordingly
|
|
|
|
System.setProperty("java.awt.headless", Boolean.toString(GraphicsEnvironment.isHeadless()));
|
|
|
|
String[] args = new String[0];
|
|
|
|
net.i2p.desktopgui.Main.beginStartup(args);
|
|
|
|
} else {
|
|
|
|
// required true for jrobin to work
|
|
|
|
System.setProperty("java.awt.headless", "true");
|
|
|
|
SysTray.getInstance();
|
|
|
|
}
|
|
|
|
} catch (Throwable t) {
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-23 00:56:48 +00:00
|
|
|
/**
|
|
|
|
* http://irc.codehaus.org/display/JETTY/Porting+to+jetty6
|
|
|
|
*
|
|
|
|
*<pre>
|
|
|
|
* Server
|
|
|
|
* HandlerCollection
|
|
|
|
* ContextHandlerCollection
|
|
|
|
* WebAppContext (i.e. ContextHandler)
|
|
|
|
* SessionHandler
|
|
|
|
* SecurityHandler
|
|
|
|
* ServletHandler
|
2011-12-24 05:26:56 +00:00
|
|
|
* servlets...
|
2011-12-23 00:56:48 +00:00
|
|
|
* WebAppContext
|
|
|
|
* ...
|
|
|
|
* DefaultHandler
|
|
|
|
* RequestLogHandler (opt)
|
|
|
|
*</pre>
|
|
|
|
*/
|
2004-07-24 02:06:07 +00:00
|
|
|
public void startConsole() {
|
2010-07-06 15:22:48 +00:00
|
|
|
File workDir = new SecureDirectory(I2PAppContext.getGlobalContext().getTempDir(), "jetty-work");
|
2004-09-29 19:34:02 +00:00
|
|
|
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");
|
|
|
|
|
2011-12-30 19:44:57 +00:00
|
|
|
Log.setLog(new I2PLogger(I2PAppContext.getGlobalContext()));
|
2010-02-06 18:40:55 +00:00
|
|
|
// so Jetty can find WebAppConfiguration
|
|
|
|
System.setProperty("jetty.class.path", I2PAppContext.getGlobalContext().getBaseDir() + "/lib/routerconsole.jar");
|
2004-07-24 02:06:07 +00:00
|
|
|
_server = new Server();
|
2011-12-24 17:09:01 +00:00
|
|
|
_server.setGracefulShutdown(1000);
|
2010-12-31 13:14:15 +00:00
|
|
|
|
2011-12-23 00:56:48 +00:00
|
|
|
try {
|
2011-12-24 17:09:01 +00:00
|
|
|
ThreadPool ctp = new CustomThreadPoolExecutor();
|
|
|
|
ctp.prestartAllCoreThreads();
|
|
|
|
_server.setThreadPool(ctp);
|
2011-12-23 00:56:48 +00:00
|
|
|
} 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);
|
|
|
|
}
|
2011-12-24 17:09:01 +00:00
|
|
|
|
2011-12-23 00:56:48 +00:00
|
|
|
HandlerCollection hColl = new HandlerCollection();
|
|
|
|
ContextHandlerCollection chColl = new ContextHandlerCollection();
|
|
|
|
_server.addHandler(hColl);
|
|
|
|
hColl.addHandler(chColl);
|
|
|
|
hColl.addHandler(new DefaultHandler());
|
|
|
|
|
2010-12-31 13:14:15 +00:00
|
|
|
String log = I2PAppContext.getGlobalContext().getProperty("routerconsole.log");
|
|
|
|
if (log != null) {
|
|
|
|
File logFile = new File(log);
|
|
|
|
if (!logFile.isAbsolute())
|
|
|
|
logFile = new File(I2PAppContext.getGlobalContext().getLogDir(), "logs/" + log);
|
|
|
|
try {
|
2011-12-23 00:56:48 +00:00
|
|
|
RequestLogHandler rhl = new RequestLogHandler();
|
|
|
|
rhl.setRequestLog(new NCSARequestLog(logFile.getAbsolutePath()));
|
|
|
|
hColl.addHandler(rhl);
|
|
|
|
} catch (Exception ioe) {
|
2010-12-31 13:14:15 +00:00
|
|
|
System.err.println("ERROR: Unable to create Jetty log: " + ioe);
|
|
|
|
}
|
|
|
|
}
|
2008-06-16 12:26:36 +00:00
|
|
|
boolean rewrite = false;
|
|
|
|
Properties props = webAppProperties();
|
2010-05-05 17:50:28 +00:00
|
|
|
if (props.isEmpty()) {
|
2008-06-16 12:26:36 +00:00
|
|
|
props.setProperty(PREFIX + ROUTERCONSOLE + ENABLED, "true");
|
|
|
|
rewrite = true;
|
|
|
|
}
|
2009-06-13 23:47:08 +00:00
|
|
|
|
|
|
|
// 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(I2PAppContext.getGlobalContext().getBaseDir(), _webAppsDir);
|
|
|
|
try {
|
|
|
|
_webAppsDir = app.getCanonicalPath();
|
|
|
|
} catch (IOException ioe) {}
|
|
|
|
}
|
|
|
|
if (!_webAppsDir.endsWith("/"))
|
|
|
|
_webAppsDir += '/';
|
|
|
|
|
2011-12-24 05:26:56 +00:00
|
|
|
WebAppContext rootWebApp = null;
|
|
|
|
ServletHandler rootServletHandler = null;
|
2004-07-24 02:06:07 +00:00
|
|
|
try {
|
2009-04-08 01:34:12 +00:00
|
|
|
int boundAddresses = 0;
|
2010-12-05 19:04:33 +00:00
|
|
|
|
|
|
|
// add standard listeners
|
|
|
|
if (_listenPort != null) {
|
2011-12-15 23:36:32 +00:00
|
|
|
Integer lport = Integer.parseInt(_listenPort);
|
2010-12-05 19:04:33 +00:00
|
|
|
StringTokenizer tok = new StringTokenizer(_listenHost, " ,");
|
|
|
|
while (tok.hasMoreTokens()) {
|
|
|
|
String host = tok.nextToken().trim();
|
|
|
|
try {
|
2011-01-19 20:16:18 +00:00
|
|
|
//if (host.indexOf(":") >= 0) // IPV6 - requires patched Jetty 5
|
|
|
|
// _server.addListener('[' + host + "]:" + _listenPort);
|
|
|
|
//else
|
|
|
|
// _server.addListener(host + ':' + _listenPort);
|
2011-12-23 00:56:48 +00:00
|
|
|
// Use AbstractConnector instead of Connector so we can do setName()
|
|
|
|
AbstractConnector lsnr = new SelectChannelConnector();
|
|
|
|
lsnr.setHost(host);
|
|
|
|
lsnr.setPort(lport);
|
|
|
|
lsnr.setMaxIdleTime(90*1000); // default 10 sec
|
2011-01-19 20:16:18 +00:00
|
|
|
lsnr.setName("ConsoleSocket"); // all with same name will use the same thread pool
|
2011-12-23 00:56:48 +00:00
|
|
|
_server.addConnector(lsnr);
|
2010-12-05 19:04:33 +00:00
|
|
|
boundAddresses++;
|
2011-01-19 20:16:18 +00:00
|
|
|
} catch (NumberFormatException nfe) {
|
|
|
|
System.err.println("Unable to bind routerconsole to " + host + " port " + _listenPort + ' ' + nfe);
|
2011-12-23 00:56:48 +00:00
|
|
|
} catch (Exception ioe) { // this doesn't seem to work, exceptions don't happen until start() below
|
2010-12-05 19:04:33 +00:00
|
|
|
System.err.println("Unable to bind routerconsole to " + host + " port " + _listenPort + ' ' + ioe);
|
|
|
|
}
|
|
|
|
}
|
2011-12-15 23:36:32 +00:00
|
|
|
// XXX: what if listenhosts do not include 127.0.0.1? (Should that ever even happen?)
|
|
|
|
I2PAppContext.getGlobalContext().portMapper().register(PortMapper.SVC_CONSOLE,lport);
|
2010-12-05 19:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add SSL listeners
|
|
|
|
int sslPort = 0;
|
|
|
|
if (_sslListenPort != null) {
|
2009-04-08 01:34:12 +00:00
|
|
|
try {
|
2010-12-05 19:04:33 +00:00
|
|
|
sslPort = Integer.parseInt(_sslListenPort);
|
|
|
|
} catch (NumberFormatException nfe) {}
|
|
|
|
if (sslPort <= 0)
|
|
|
|
System.err.println("Bad routerconsole SSL port " + _sslListenPort);
|
|
|
|
}
|
|
|
|
if (sslPort > 0) {
|
|
|
|
I2PAppContext ctx = I2PAppContext.getGlobalContext();
|
|
|
|
File keyStore = new File(ctx.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 {
|
2011-12-23 00:56:48 +00:00
|
|
|
// TODO if class not found use SslChannelConnector
|
|
|
|
// Sadly there's no common base class with the ssl methods in it
|
|
|
|
SslSelectChannelConnector ssll = new SslSelectChannelConnector();
|
|
|
|
ssll.setHost(host);
|
|
|
|
ssll.setPort(sslPort);
|
2010-12-05 19:04:33 +00:00
|
|
|
// the keystore path and password
|
|
|
|
ssll.setKeystore(keyStore.getAbsolutePath());
|
|
|
|
ssll.setPassword(ctx.getProperty(PROP_KEYSTORE_PASSWORD, DEFAULT_KEYSTORE_PASSWORD));
|
|
|
|
// the X.509 cert password (if not present, verifyKeyStore() returned false)
|
|
|
|
ssll.setKeyPassword(ctx.getProperty(PROP_KEY_PASSWORD, "thisWontWork"));
|
2011-12-23 00:56:48 +00:00
|
|
|
ssll.setMaxIdleTime(90*1000); // default 10 sec
|
2011-01-19 20:16:18 +00:00
|
|
|
ssll.setName("ConsoleSocket"); // all with same name will use the same thread pool
|
2011-12-23 00:56:48 +00:00
|
|
|
_server.addConnector(ssll);
|
2010-12-05 19:04:33 +00:00
|
|
|
boundAddresses++;
|
|
|
|
} catch (Exception e) { // probably no exceptions at this point
|
2010-12-27 15:56:19 +00:00
|
|
|
System.err.println("Unable to bind routerconsole to " + host + " port " + sslPort + " for SSL: " + e);
|
2010-12-05 19:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-15 23:36:32 +00:00
|
|
|
I2PAppContext.getGlobalContext().portMapper().register(PortMapper.SVC_HTTPS_CONSOLE,sslPort);
|
2010-12-05 19:04:33 +00:00
|
|
|
} else {
|
|
|
|
System.err.println("Unable to create or access keystore for SSL: " + keyStore.getAbsolutePath());
|
2009-04-08 01:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-05 19:04:33 +00:00
|
|
|
|
2009-04-08 01:34:12 +00:00
|
|
|
if (boundAddresses <= 0) {
|
2010-12-05 19:04:33 +00:00
|
|
|
System.err.println("Unable to bind routerconsole to any address on port " + _listenPort + (sslPort > 0 ? (" or SSL port " + sslPort) : ""));
|
2009-04-08 01:34:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-12-23 00:56:48 +00:00
|
|
|
|
2011-12-24 05:26:56 +00:00
|
|
|
rootWebApp = new LocaleWebAppHandler(I2PAppContext.getGlobalContext(),
|
2011-12-23 00:56:48 +00:00
|
|
|
"/", _webAppsDir + ROUTERCONSOLE + ".war");
|
2010-12-27 15:56:19 +00:00
|
|
|
File tmpdir = new SecureDirectory(workDir, ROUTERCONSOLE + "-" +
|
|
|
|
(_listenPort != null ? _listenPort : _sslListenPort));
|
Big directory rework.
Eliminate all uses of the current working directory, and
set up multiple directories specified by absolute paths for various uses.
Add a WorkingDir class to create a user config directory and
migrate files to it for new installs.
The directory will be $HOME/.i2p on linux and %APPDIR%\I2P on Windows,
or as specified in the system property -Di2p.dir.config=/path/to/i2pdir
All files except for the base install and temp files will be
in the config directory by default.
Temp files will be in a i2p-xxxxx subdirectory of the system temp directory
specified by the system property java.io.tmpdir.
Convert all file opens in the code to be relative to a specific directory,
as specified in the context. Code and applications should never open
files relative to the current working directory (e.g. new File("foo")).
All files should be accessed in the appropriate context directory,
e.g. new File(_context.getAppDir(), "foo").
The router.config file location may be specified as a system property on the
java command line with -Drouter.configLocation=/path/to/router.config
All directories may be specified as properties in the router.config file.
The migration will copy all files from an existing installation,
except i2psnark/, with the system property -Di2p.dir.migrate=true.
Otherwise it will just set up a new directory with a minimal configuration.
The migration will also create a modified wrapper.config and (on linux only)
a modified i2prouter script, and place them in the config directory.
There are no changes to the installer or the default i2prouter, i2prouter.bat,
i2prouter, wrapper.config, runplain.sh, windows service installer/uninstaller,
etc. in this checkin.
* Directories. These are all set at instantiation and will not be changed by
* subsequent property changes.
* All properties, if set, should be absolute paths.
*
* Name Property Method Files
* ----- -------- ----- -----
* Base i2p.dir.base getBaseDir() lib/, webapps/, docs/, geoip/, licenses/, ...
* Temp i2p.dir.temp getTempDir() Temporary files
* Config i2p.dir.config getConfigDir() *.config, hosts.txt, addressbook/, ...
*
* (the following all default to the same as Config)
*
* Router i2p.dir.router getRouterDir() netDb/, peerProfiles/, router.*, keyBackup/, ...
* Log i2p.dir.log getLogDir() wrapper.log*, logs/
* PID i2p.dir.pid getPIDDir() wrapper *.pid files, router.ping
* App i2p.dir.app getAppDir() eepsite/, ...
*
* Note that we can't control where the wrapper actually puts its files.
All these will be set appropriately in a Router Context.
In an I2P App Context, all except Temp will be the current working directory.
Lightly tested so far, needs much more testing.
2009-06-04 19:14:40 +00:00
|
|
|
tmpdir.mkdir();
|
2011-12-24 05:26:56 +00:00
|
|
|
rootWebApp.setTempDirectory(tmpdir);
|
|
|
|
rootWebApp.setSessionHandler(new SessionHandler());
|
|
|
|
rootServletHandler = new ServletHandler();
|
|
|
|
rootWebApp.setServletHandler(rootServletHandler);
|
|
|
|
initialize(rootWebApp);
|
|
|
|
chColl.addHandler(rootWebApp);
|
2011-12-23 00:56:48 +00:00
|
|
|
|
2011-12-29 23:25:27 +00:00
|
|
|
} 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("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:7657/,\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.\n" +
|
|
|
|
"Exception: " + me);
|
|
|
|
me.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()) {
|
2009-06-13 23:47:08 +00:00
|
|
|
File dir = new File(_webAppsDir);
|
2008-06-16 12:26:36 +00:00
|
|
|
String fileNames[] = dir.list(WarFilenameFilter.instance());
|
|
|
|
if (fileNames != null) {
|
|
|
|
for (int i = 0; i < fileNames.length; i++) {
|
2011-12-29 23:25:27 +00:00
|
|
|
String appName = fileNames[i].substring(0, fileNames[i].lastIndexOf(".war"));
|
|
|
|
String enabled = props.getProperty(PREFIX + appName + ENABLED);
|
|
|
|
if (! "false".equals(enabled)) {
|
|
|
|
try {
|
2008-06-16 12:26:36 +00:00
|
|
|
String path = new File(dir, fileNames[i]).getCanonicalPath();
|
2011-12-29 23:25:27 +00:00
|
|
|
WebAppStarter.startWebApp(I2PAppContext.getGlobalContext(), chColl, appName, path);
|
2008-06-16 12:26:36 +00:00
|
|
|
if (enabled == null) {
|
|
|
|
// do this so configclients.jsp knows about all apps from reading the config
|
|
|
|
props.setProperty(PREFIX + appName + ENABLED, "true");
|
|
|
|
rewrite = true;
|
|
|
|
}
|
2011-12-29 23:25:27 +00:00
|
|
|
} catch (Throwable t) {
|
|
|
|
System.err.println("ERROR: Failed to start " + appName + ' ' + t);
|
|
|
|
t.printStackTrace();
|
2009-11-27 13:26:38 +00:00
|
|
|
notStarted.add(appName);
|
2008-06-16 12:26:36 +00:00
|
|
|
}
|
2011-12-29 23:25:27 +00:00
|
|
|
} else {
|
|
|
|
notStarted.add(appName);
|
2008-06-16 12:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-10 19:51:11 +00:00
|
|
|
}
|
2011-12-29 23:25:27 +00:00
|
|
|
} else {
|
|
|
|
System.err.println("ERROR: Router console did not start, not starting webapps");
|
2004-07-24 02:06:07 +00:00
|
|
|
}
|
2011-12-23 00:56:48 +00:00
|
|
|
|
2008-06-16 12:26:36 +00:00
|
|
|
if (rewrite)
|
|
|
|
storeWebAppProperties(props);
|
2009-11-27 13:26:38 +00:00
|
|
|
|
2011-12-24 05:26:56 +00:00
|
|
|
if (rootServletHandler != null && notStarted.size() > 0) {
|
2009-11-27 13:26:38 +00:00
|
|
|
// map each not-started webapp to the error page
|
2011-12-24 05:26:56 +00:00
|
|
|
ServletHolder noWebApp = rootServletHandler.getServlet("net.i2p.router.web.jsp.nowebapp_jsp");
|
2009-11-27 13:26:38 +00:00
|
|
|
for (int i = 0; i < notStarted.size(); i++) {
|
2011-12-24 05:26:56 +00:00
|
|
|
// we want a new handler for each one since if the webapp is started we remove the handler???
|
2009-11-27 13:26:38 +00:00
|
|
|
try {
|
2011-12-24 05:26:56 +00:00
|
|
|
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?");
|
|
|
|
}
|
2009-11-27 13:26:38 +00:00
|
|
|
} catch (Throwable me) {
|
|
|
|
System.err.println(me);
|
2011-12-24 05:26:56 +00:00
|
|
|
me.printStackTrace();
|
2009-11-27 13:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-16 19:04:23 +00:00
|
|
|
Thread t = new I2PAppThread(new StatSummarizer(), "StatSummarizer", true);
|
2011-12-05 16:18:35 +00:00
|
|
|
t.setPriority(Thread.NORM_PRIORITY - 1);
|
2010-02-07 13:32:49 +00:00
|
|
|
t.start();
|
|
|
|
|
|
|
|
List<RouterContext> contexts = RouterContext.listContexts();
|
|
|
|
if (contexts != null) {
|
2010-03-29 21:20:48 +00:00
|
|
|
RouterContext ctx = contexts.get(0);
|
2011-11-09 18:38:39 +00:00
|
|
|
|
|
|
|
NewsFetcher fetcher = NewsFetcher.getInstance(ctx);
|
|
|
|
Thread newsThread = new I2PAppThread(fetcher, "NewsFetcher", true);
|
2011-12-05 16:18:35 +00:00
|
|
|
newsThread.setPriority(Thread.NORM_PRIORITY - 1);
|
2011-11-09 18:38:39 +00:00
|
|
|
newsThread.start();
|
|
|
|
|
2010-03-29 21:20:48 +00:00
|
|
|
if (PluginStarter.pluginsEnabled(ctx)) {
|
|
|
|
t = new I2PAppThread(new PluginStarter(ctx), "PluginStarter", true);
|
2011-12-05 16:18:35 +00:00
|
|
|
t.setPriority(Thread.NORM_PRIORITY - 1);
|
2010-02-10 19:09:35 +00:00
|
|
|
t.start();
|
2010-03-29 21:20:48 +00:00
|
|
|
ctx.addShutdownTask(new PluginStopper(ctx));
|
2010-02-10 19:09:35 +00:00
|
|
|
}
|
2011-06-16 19:04:23 +00:00
|
|
|
ctx.addShutdownTask(new NewsShutdown(fetcher, newsThread));
|
|
|
|
// stat summarizer registers its own hook
|
|
|
|
ctx.addShutdownTask(new ServerShutdown());
|
2011-11-09 18:38:39 +00:00
|
|
|
} // else log CRIT ?
|
2004-07-24 02:06:07 +00:00
|
|
|
}
|
|
|
|
|
2010-12-05 19:04:33 +00:00
|
|
|
/**
|
|
|
|
* @return success if it exists and we have a password, or it was created successfully.
|
|
|
|
* @since 0.8.3
|
|
|
|
*/
|
|
|
|
private static boolean verifyKeyStore(File ks) {
|
|
|
|
if (ks.exists()) {
|
|
|
|
I2PAppContext ctx = I2PAppContext.getGlobalContext();
|
|
|
|
boolean rv = ctx.getProperty(PROP_KEY_PASSWORD) != null;
|
|
|
|
if (!rv)
|
|
|
|
System.err.println("Console SSL error, must set " + PROP_KEY_PASSWORD + " in " + (new File(ctx.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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call out to keytool to create a new keystore with a keypair in it.
|
|
|
|
* Trying to do this programatically is a nightmare, requiring either BouncyCastle
|
|
|
|
* libs or using proprietary Sun libs, and it's a huge mess.
|
|
|
|
*
|
|
|
|
* @return success
|
|
|
|
* @since 0.8.3
|
|
|
|
*/
|
|
|
|
private static boolean createKeyStore(File ks) {
|
|
|
|
I2PAppContext ctx = I2PAppContext.getGlobalContext();
|
|
|
|
// make a random 48 character password (30 * 8 / 5)
|
|
|
|
byte[] rand = new byte[30];
|
|
|
|
ctx.random().nextBytes(rand);
|
|
|
|
String keyPassword = Base32.encode(rand);
|
|
|
|
// and one for the cname
|
|
|
|
ctx.random().nextBytes(rand);
|
|
|
|
String cname = Base32.encode(rand) + ".console.i2p.net";
|
|
|
|
|
|
|
|
String keytool = (new File(System.getProperty("java.home"), "bin/keytool")).getAbsolutePath();
|
|
|
|
String[] args = new String[] {
|
|
|
|
keytool,
|
|
|
|
"-genkey", // -genkeypair preferred in newer keytools, but this works with more
|
2010-12-14 14:34:27 +00:00
|
|
|
"-storetype", KeyStore.getDefaultType(),
|
2010-12-05 19:04:33 +00:00
|
|
|
"-keystore", ks.getAbsolutePath(),
|
|
|
|
"-storepass", DEFAULT_KEYSTORE_PASSWORD,
|
|
|
|
"-alias", "console",
|
|
|
|
"-dname", "CN=" + cname + ",OU=Console,O=I2P Anonymous Network,L=XX,ST=XX,C=XX",
|
|
|
|
"-validity", "3652", // 10 years
|
|
|
|
"-keyalg", "DSA",
|
|
|
|
"-keysize", "1024",
|
|
|
|
"-keypass", keyPassword};
|
|
|
|
boolean success = (new ShellCommand()).executeSilentAndWaitTimed(args, 30); // 30 secs
|
|
|
|
if (success) {
|
|
|
|
success = ks.exists();
|
|
|
|
if (success) {
|
|
|
|
SecureFileOutputStream.setPerms(ks);
|
|
|
|
try {
|
|
|
|
RouterContext rctx = (RouterContext) ctx;
|
|
|
|
rctx.router().setConfigSetting(PROP_KEYSTORE_PASSWORD, DEFAULT_KEYSTORE_PASSWORD);
|
|
|
|
rctx.router().setConfigSetting(PROP_KEY_PASSWORD, keyPassword);
|
|
|
|
rctx.router().saveConfig();
|
|
|
|
} catch (Exception e) {} // class cast exception
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (success) {
|
|
|
|
System.err.println("Created self-signed certificate for " + cname + " in keystore: " + ks.getAbsolutePath() + "\n" +
|
|
|
|
"The certificate name was generated randomly, and is not associated with your " +
|
|
|
|
"IP address, host name, router identity, or destination keys.");
|
|
|
|
} else {
|
|
|
|
System.err.println("Failed to create console SSL keystore using command line:");
|
|
|
|
StringBuilder buf = new StringBuilder(256);
|
|
|
|
for (int i = 0; i < args.length; i++) {
|
|
|
|
buf.append('"').append(args[i]).append("\" ");
|
|
|
|
}
|
|
|
|
System.err.println(buf.toString());
|
|
|
|
System.err.println("This is for the Sun/Oracle keytool, others may be incompatible.\n" +
|
|
|
|
"If you create the keystore manually, you must add " + PROP_KEYSTORE_PASSWORD + " and " + PROP_KEY_PASSWORD +
|
|
|
|
" to " + (new File(ctx.getConfigDir(), "router.config")).getAbsolutePath());
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2011-12-23 00:56:48 +00:00
|
|
|
static void initialize(WebAppContext context) {
|
|
|
|
SecurityHandler sec = new SecurityHandler();
|
|
|
|
List<ConstraintMapping> constraints = new ArrayList(4);
|
2004-08-10 19:51:11 +00:00
|
|
|
String password = getPassword();
|
|
|
|
if (password != null) {
|
2005-03-01 17:50:52 +00:00
|
|
|
HashUserRealm realm = new HashUserRealm("i2prouter");
|
2004-08-10 19:51:11 +00:00
|
|
|
realm.put("admin", password);
|
|
|
|
realm.addUserToRole("admin", "routerAdmin");
|
2011-12-23 00:56:48 +00:00
|
|
|
sec.setUserRealm(realm);
|
|
|
|
sec.setAuthenticator(authenticator);
|
|
|
|
Constraint constraint = new Constraint("admin", "routerAdmin");
|
2004-08-10 19:51:11 +00:00
|
|
|
constraint.setAuthenticate(true);
|
2011-12-23 00:56:48 +00:00
|
|
|
ConstraintMapping cm = new ConstraintMapping();
|
|
|
|
cm.setConstraint(constraint);
|
|
|
|
cm.setPathSpec("/");
|
|
|
|
constraints.add(cm);
|
2004-08-10 19:51:11 +00:00
|
|
|
}
|
2010-06-29 02:29:42 +00:00
|
|
|
|
|
|
|
// This forces a '403 Forbidden' response for TRACE and OPTIONS unless the
|
|
|
|
// WAC handler handles it.
|
|
|
|
// (LocaleWebAppHandler returns a '405 Method Not Allowed')
|
|
|
|
// TRACE and OPTIONS aren't really security issues...
|
|
|
|
// TRACE doesn't echo stuff unless you call setTrace(true)
|
|
|
|
// But it might bug some people
|
|
|
|
// The other strange methods - PUT, DELETE, MOVE - are disabled by default
|
|
|
|
// See also:
|
|
|
|
// http://old.nabble.com/Disable-HTTP-TRACE-in-Jetty-5.x-td12412607.html
|
2011-12-23 00:56:48 +00:00
|
|
|
|
|
|
|
Constraint sc = new Constraint();
|
|
|
|
sc.setName("No trace");
|
|
|
|
ConstraintMapping cm = new ConstraintMapping();
|
|
|
|
cm.setMethod("TRACE");
|
|
|
|
cm.setConstraint(sc);
|
|
|
|
cm.setPathSpec("/");
|
|
|
|
constraints.add(cm);
|
|
|
|
|
|
|
|
sc = new Constraint();
|
|
|
|
sc.setName("No options");
|
|
|
|
cm = new ConstraintMapping();
|
|
|
|
cm.setMethod("OPTIONS");
|
|
|
|
cm.setConstraint(sc);
|
|
|
|
cm.setPathSpec("/");
|
|
|
|
constraints.add(cm);
|
|
|
|
|
|
|
|
ConstraintMapping cmarr[] = constraints.toArray(new ConstraintMapping[constraints.size()]);
|
|
|
|
sec.setConstraintMappings(cmarr);
|
|
|
|
|
|
|
|
context.setSecurityHandler(sec);
|
2004-08-10 19:51:11 +00:00
|
|
|
}
|
|
|
|
|
2010-02-06 18:40:55 +00:00
|
|
|
static String getPassword() {
|
2010-02-07 13:32:49 +00:00
|
|
|
List<RouterContext> contexts = RouterContext.listContexts();
|
2004-08-10 19:51:11 +00:00
|
|
|
if (contexts != null) {
|
|
|
|
for (int i = 0; i < contexts.size(); i++) {
|
2010-02-07 13:32:49 +00:00
|
|
|
RouterContext ctx = contexts.get(i);
|
2004-08-10 19:51:11 +00:00
|
|
|
String password = ctx.getProperty("consolePassword");
|
|
|
|
if (password != null) {
|
|
|
|
password = password.trim();
|
|
|
|
if (password.length() > 0) {
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// no password in any context
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
// no contexts?!
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-16 19:04:23 +00:00
|
|
|
/** @since 0.8.8 */
|
2011-12-23 00:56:48 +00:00
|
|
|
private static class ServerShutdown implements Runnable {
|
2011-06-16 19:04:23 +00:00
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
_server.stop();
|
2011-12-23 00:56:48 +00:00
|
|
|
} catch (Exception ie) {}
|
2004-07-24 02:06:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-16 19:04:23 +00:00
|
|
|
/** @since 0.8.8 */
|
|
|
|
private static class NewsShutdown implements Runnable {
|
|
|
|
private final NewsFetcher _fetcher;
|
|
|
|
private final Thread _newsThread;
|
|
|
|
|
|
|
|
public NewsShutdown(NewsFetcher fetcher, Thread t) {
|
|
|
|
_fetcher = fetcher;
|
|
|
|
_newsThread = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
_fetcher.shutdown();
|
|
|
|
_newsThread.interrupt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-16 12:26:36 +00:00
|
|
|
public static Properties webAppProperties() {
|
2010-02-06 18:40:55 +00:00
|
|
|
return webAppProperties(I2PAppContext.getGlobalContext().getConfigDir().getAbsolutePath());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Properties webAppProperties(String dir) {
|
2008-06-16 12:26:36 +00:00
|
|
|
Properties rv = new Properties();
|
|
|
|
// String webappConfigFile = ctx.getProperty(PROP_WEBAPP_CONFIG_FILENAME, DEFAULT_WEBAPP_CONFIG_FILENAME);
|
|
|
|
String webappConfigFile = DEFAULT_WEBAPP_CONFIG_FILENAME;
|
2010-02-06 18:40:55 +00:00
|
|
|
File cfgFile = new File(dir, webappConfigFile);
|
2008-06-16 12:26:36 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
DataHelper.loadProps(rv, cfgFile);
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
// _log.warn("Error loading the client app properties from " + cfgFile.getName(), ioe);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void storeWebAppProperties(Properties props) {
|
|
|
|
// String webappConfigFile = ctx.getProperty(PROP_WEBAPP_CONFIG_FILENAME, DEFAULT_WEBAPP_CONFIG_FILENAME);
|
|
|
|
String webappConfigFile = DEFAULT_WEBAPP_CONFIG_FILENAME;
|
Big directory rework.
Eliminate all uses of the current working directory, and
set up multiple directories specified by absolute paths for various uses.
Add a WorkingDir class to create a user config directory and
migrate files to it for new installs.
The directory will be $HOME/.i2p on linux and %APPDIR%\I2P on Windows,
or as specified in the system property -Di2p.dir.config=/path/to/i2pdir
All files except for the base install and temp files will be
in the config directory by default.
Temp files will be in a i2p-xxxxx subdirectory of the system temp directory
specified by the system property java.io.tmpdir.
Convert all file opens in the code to be relative to a specific directory,
as specified in the context. Code and applications should never open
files relative to the current working directory (e.g. new File("foo")).
All files should be accessed in the appropriate context directory,
e.g. new File(_context.getAppDir(), "foo").
The router.config file location may be specified as a system property on the
java command line with -Drouter.configLocation=/path/to/router.config
All directories may be specified as properties in the router.config file.
The migration will copy all files from an existing installation,
except i2psnark/, with the system property -Di2p.dir.migrate=true.
Otherwise it will just set up a new directory with a minimal configuration.
The migration will also create a modified wrapper.config and (on linux only)
a modified i2prouter script, and place them in the config directory.
There are no changes to the installer or the default i2prouter, i2prouter.bat,
i2prouter, wrapper.config, runplain.sh, windows service installer/uninstaller,
etc. in this checkin.
* Directories. These are all set at instantiation and will not be changed by
* subsequent property changes.
* All properties, if set, should be absolute paths.
*
* Name Property Method Files
* ----- -------- ----- -----
* Base i2p.dir.base getBaseDir() lib/, webapps/, docs/, geoip/, licenses/, ...
* Temp i2p.dir.temp getTempDir() Temporary files
* Config i2p.dir.config getConfigDir() *.config, hosts.txt, addressbook/, ...
*
* (the following all default to the same as Config)
*
* Router i2p.dir.router getRouterDir() netDb/, peerProfiles/, router.*, keyBackup/, ...
* Log i2p.dir.log getLogDir() wrapper.log*, logs/
* PID i2p.dir.pid getPIDDir() wrapper *.pid files, router.ping
* App i2p.dir.app getAppDir() eepsite/, ...
*
* Note that we can't control where the wrapper actually puts its files.
All these will be set appropriately in a Router Context.
In an I2P App Context, all except Temp will be the current working directory.
Lightly tested so far, needs much more testing.
2009-06-04 19:14:40 +00:00
|
|
|
File cfgFile = new File(I2PAppContext.getGlobalContext().getConfigDir(), webappConfigFile);
|
2008-06-16 12:26:36 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
DataHelper.storeProps(props, cfgFile);
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
// _log.warn("Error loading the client app properties from " + cfgFile.getName(), ioe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-07 13:32:49 +00:00
|
|
|
static class WarFilenameFilter implements FilenameFilter {
|
2008-06-16 12:26:36 +00:00
|
|
|
private static final WarFilenameFilter _filter = new WarFilenameFilter();
|
|
|
|
public static WarFilenameFilter instance() { return _filter; }
|
|
|
|
public boolean accept(File dir, String name) {
|
|
|
|
return (name != null) && (name.endsWith(".war") && !name.equals(ROUTERCONSOLE + ".war"));
|
|
|
|
}
|
|
|
|
}
|
2010-02-07 13:32:49 +00:00
|
|
|
|
2011-12-24 17:09:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Just to set the name and set Daemon
|
|
|
|
* @since Jetty 6
|
|
|
|
*/
|
|
|
|
private static class CustomThreadPoolExecutor extends ThreadPool {
|
|
|
|
public CustomThreadPoolExecutor() {
|
|
|
|
super(MIN_THREADS, MAX_THREADS, MAX_IDLE_TIME, TimeUnit.MILLISECONDS,
|
|
|
|
new SynchronousQueue(), new CustomThreadFactory(),
|
|
|
|
new ThreadPoolExecutor.CallerRunsPolicy());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Just to set the name and set Daemon
|
|
|
|
* @since Jetty 6
|
|
|
|
*/
|
|
|
|
private static class CustomThreadFactory implements ThreadFactory {
|
|
|
|
|
|
|
|
public Thread newThread(Runnable r) {
|
|
|
|
Thread rv = Executors.defaultThreadFactory().newThread(r);
|
|
|
|
rv.setName(THREAD_NAME);
|
|
|
|
rv.setDaemon(true);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-24 02:06:07 +00:00
|
|
|
}
|