2004-07-24 02:06:07 +00:00
|
|
|
package net.i2p.router.web;
|
|
|
|
|
2004-09-29 19:34:02 +00:00
|
|
|
import java.io.File;
|
2004-07-24 02:06:07 +00:00
|
|
|
import java.io.IOException;
|
2004-08-10 19:51:11 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import net.i2p.router.RouterContext;
|
2004-08-29 21:06:15 +00:00
|
|
|
import net.i2p.apps.systray.SysTray;
|
2004-09-29 19:34:02 +00:00
|
|
|
import net.i2p.util.FileUtil;
|
2004-08-10 19:51:11 +00:00
|
|
|
|
2004-07-24 02:06:07 +00:00
|
|
|
import org.mortbay.jetty.Server;
|
2004-08-10 19:51:11 +00:00
|
|
|
import org.mortbay.jetty.servlet.WebApplicationContext;
|
2004-08-29 22:35:55 +00:00
|
|
|
import org.mortbay.http.DigestAuthenticator;
|
2004-08-10 19:51:11 +00:00
|
|
|
import org.mortbay.http.handler.SecurityHandler;
|
|
|
|
import org.mortbay.http.HashUserRealm;
|
|
|
|
import org.mortbay.http.HttpRequest;
|
|
|
|
import org.mortbay.http.SecurityConstraint;
|
2004-07-24 02:06:07 +00:00
|
|
|
import org.mortbay.util.MultiException;
|
|
|
|
|
|
|
|
public class RouterConsoleRunner {
|
|
|
|
private Server _server;
|
|
|
|
private String _listenPort = "7657";
|
2004-08-10 19:51:11 +00:00
|
|
|
private String _listenHost = "127.0.0.1";
|
2004-07-24 02:06:07 +00:00
|
|
|
private String _webAppsDir = "./webapps/";
|
|
|
|
|
2004-09-30 06:57:22 +00:00
|
|
|
static {
|
|
|
|
System.setProperty("org.mortbay.http.Version.paranoid", "true");
|
|
|
|
}
|
|
|
|
|
2004-07-24 02:06:07 +00:00
|
|
|
public RouterConsoleRunner(String args[]) {
|
|
|
|
if (args.length == 3) {
|
|
|
|
_listenPort = args[0].trim();
|
|
|
|
_listenHost = args[1].trim();
|
|
|
|
_webAppsDir = args[2].trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String args[]) {
|
|
|
|
RouterConsoleRunner runner = new RouterConsoleRunner(args);
|
|
|
|
runner.startConsole();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void startConsole() {
|
2004-09-29 19:34:02 +00:00
|
|
|
File workDir = new File("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");
|
|
|
|
|
2004-07-24 02:06:07 +00:00
|
|
|
_server = new Server();
|
2004-08-10 19:51:11 +00:00
|
|
|
WebApplicationContext contexts[] = null;
|
2004-07-24 02:06:07 +00:00
|
|
|
try {
|
|
|
|
_server.addListener(_listenHost + ':' + _listenPort);
|
|
|
|
_server.setRootWebApp("routerconsole");
|
2004-08-10 19:51:11 +00:00
|
|
|
contexts = _server.addWebApplications(_webAppsDir);
|
|
|
|
if (contexts != null) {
|
|
|
|
for (int i = 0; i < contexts.length; i++)
|
|
|
|
initialize(contexts[i]);
|
|
|
|
}
|
2004-07-24 02:06:07 +00:00
|
|
|
} catch (IOException ioe) {
|
|
|
|
ioe.printStackTrace();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
_server.start();
|
|
|
|
} catch (MultiException me) {
|
|
|
|
me.printStackTrace();
|
|
|
|
}
|
2004-08-29 21:06:15 +00:00
|
|
|
try {
|
|
|
|
SysTray tray = SysTray.getInstance();
|
|
|
|
} catch (Throwable t) {
|
|
|
|
t.printStackTrace();
|
|
|
|
}
|
2004-07-24 02:06:07 +00:00
|
|
|
}
|
|
|
|
|
2004-08-10 19:51:11 +00:00
|
|
|
private void initialize(WebApplicationContext context) {
|
|
|
|
String password = getPassword();
|
|
|
|
if (password != null) {
|
|
|
|
HashUserRealm realm = new HashUserRealm();
|
|
|
|
realm.put("admin", password);
|
|
|
|
realm.addUserToRole("admin", "routerAdmin");
|
|
|
|
context.setRealm(realm);
|
2004-08-29 22:35:55 +00:00
|
|
|
context.setAuthenticator(new DigestAuthenticator());
|
2004-08-10 19:51:11 +00:00
|
|
|
context.addHandler(0, new SecurityHandler());
|
|
|
|
SecurityConstraint constraint = new SecurityConstraint("admin", "routerAdmin");
|
|
|
|
constraint.setAuthenticate(true);
|
|
|
|
context.addSecurityConstraint("/", constraint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getPassword() {
|
|
|
|
List contexts = RouterContext.listContexts();
|
|
|
|
if (contexts != null) {
|
|
|
|
for (int i = 0; i < contexts.size(); i++) {
|
|
|
|
RouterContext ctx = (RouterContext)contexts.get(i);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-24 02:06:07 +00:00
|
|
|
public void stopConsole() {
|
|
|
|
try {
|
|
|
|
_server.stop();
|
|
|
|
} catch (InterruptedException ie) {
|
|
|
|
ie.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|