2006-03-01 01:14:19 +01:00
|
|
|
// ConfigBasic_p.java
|
|
|
|
// -----------------------
|
|
|
|
// part of YaCy
|
2008-07-20 19:14:51 +02:00
|
|
|
// (C) by Michael Peter Christen; mc@yacy.net
|
2006-03-01 01:14:19 +01:00
|
|
|
// first published on http://www.anomic.de
|
|
|
|
// Frankfurt, Germany, 2006
|
|
|
|
// Created 28.02.2006
|
|
|
|
//
|
|
|
|
// $LastChangedDate: 2005-09-13 00:20:37 +0200 (Di, 13 Sep 2005) $
|
|
|
|
// $LastChangedRevision: 715 $
|
|
|
|
// $LastChangedBy: borg-0300 $
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
// You must compile this file with
|
|
|
|
// javac -classpath .:../classes ConfigBasic_p.java
|
|
|
|
// if the shell's current path is HTROOT
|
|
|
|
|
2006-11-20 21:03:05 +01:00
|
|
|
import java.util.regex.Pattern;
|
2006-03-02 23:25:46 +01:00
|
|
|
|
|
|
|
import de.anomic.data.translator;
|
2008-08-25 20:11:47 +02:00
|
|
|
import de.anomic.http.httpRequestHeader;
|
2008-05-20 15:21:55 +02:00
|
|
|
import de.anomic.http.httpd;
|
2006-08-22 15:35:51 +02:00
|
|
|
import de.anomic.http.httpdFileHandler;
|
2009-02-28 15:36:23 +01:00
|
|
|
import de.anomic.net.UPnP;
|
2006-03-01 23:27:20 +01:00
|
|
|
import de.anomic.plasma.plasmaSwitchboard;
|
2008-08-02 15:57:00 +02:00
|
|
|
import de.anomic.plasma.plasmaSwitchboardConstants;
|
2006-03-01 01:14:19 +01:00
|
|
|
import de.anomic.server.serverCore;
|
2007-07-24 02:46:17 +02:00
|
|
|
import de.anomic.server.serverDomains;
|
2008-03-27 13:03:16 +01:00
|
|
|
import de.anomic.server.serverInstantBusyThread;
|
2006-03-01 01:14:19 +01:00
|
|
|
import de.anomic.server.serverObjects;
|
|
|
|
import de.anomic.server.serverSwitch;
|
2008-06-14 12:24:58 +02:00
|
|
|
import de.anomic.yacy.yacySeed;
|
2006-03-01 01:14:19 +01:00
|
|
|
|
|
|
|
public class ConfigBasic {
|
2006-05-20 16:05:49 +02:00
|
|
|
|
2008-05-15 13:26:43 +02:00
|
|
|
private static final int NEXTSTEP_FINISHED = 0;
|
2008-05-20 15:21:55 +02:00
|
|
|
private static final int NEXTSTEP_PWD = 1;
|
2008-05-15 13:26:43 +02:00
|
|
|
private static final int NEXTSTEP_PEERNAME = 2;
|
|
|
|
private static final int NEXTSTEP_PEERPORT = 3;
|
|
|
|
private static final int NEXTSTEP_RECONNECT = 4;
|
2006-03-01 01:14:19 +01:00
|
|
|
|
2008-08-25 20:11:47 +02:00
|
|
|
public static serverObjects respond(final httpRequestHeader header, final serverObjects post, final serverSwitch<?> env) {
|
2006-08-07 13:31:18 +02:00
|
|
|
|
2006-03-01 01:14:19 +01:00
|
|
|
// return variable that accumulates replacements
|
2008-08-02 14:12:04 +02:00
|
|
|
final plasmaSwitchboard sb = (plasmaSwitchboard) env;
|
|
|
|
final serverObjects prop = new serverObjects();
|
|
|
|
final String langPath = env.getConfigPath("locale.work", "DATA/LOCALE/locales").getAbsolutePath();
|
2007-07-04 12:32:30 +02:00
|
|
|
String lang = env.getConfig("locale.language", "default");
|
2006-03-02 23:25:46 +01:00
|
|
|
|
2008-08-02 14:12:04 +02:00
|
|
|
final int authentication = sb.adminAuthenticated(header);
|
2006-03-01 23:27:20 +01:00
|
|
|
if (authentication < 2) {
|
|
|
|
// must authenticate
|
|
|
|
prop.put("AUTHENTICATE", "admin log-in");
|
|
|
|
return prop;
|
|
|
|
}
|
|
|
|
|
2006-08-07 13:31:18 +02:00
|
|
|
// starting a peer ping
|
2007-06-26 16:37:10 +02:00
|
|
|
|
|
|
|
//boolean doPeerPing = false;
|
2009-03-13 11:34:51 +01:00
|
|
|
if ((sb.webIndex.peers().mySeed().isVirgin()) || (sb.webIndex.peers().mySeed().isJunior())) {
|
2008-03-27 13:03:16 +01:00
|
|
|
serverInstantBusyThread.oneTimeJob(sb.yc, "peerPing", null, 0);
|
2007-06-26 16:37:10 +02:00
|
|
|
//doPeerPing = true;
|
|
|
|
}
|
2006-08-07 13:31:18 +02:00
|
|
|
|
2006-03-02 23:25:46 +01:00
|
|
|
// language settings
|
|
|
|
if ((post != null) && (!(post.get("language", "default").equals(lang)))) {
|
|
|
|
translator.changeLang(env, langPath, post.get("language", "default") + ".lng");
|
|
|
|
}
|
2006-03-01 01:14:19 +01:00
|
|
|
|
|
|
|
// peer name settings
|
2008-08-02 14:12:04 +02:00
|
|
|
final String peerName = (post == null) ? env.getConfig("peerName","") : (String) post.get("peername", "");
|
2006-03-01 01:14:19 +01:00
|
|
|
|
|
|
|
// port settings
|
2008-09-19 13:45:11 +02:00
|
|
|
long port = env.getConfigLong("port", 8080); //this allows a low port, but it will only get one, if the user edits the config himself.
|
2008-06-06 18:01:27 +02:00
|
|
|
if (post != null && Integer.parseInt(post.get("port")) > 1023) {
|
2008-09-19 13:45:11 +02:00
|
|
|
port = post.getLong("port", 8080);
|
2007-04-10 00:05:07 +02:00
|
|
|
}
|
2006-03-01 01:14:19 +01:00
|
|
|
|
|
|
|
// check if peer name already exists
|
2009-03-13 11:34:51 +01:00
|
|
|
final yacySeed oldSeed = sb.webIndex.peers().lookupByName(peerName);
|
2006-11-20 21:03:05 +01:00
|
|
|
if ((oldSeed == null) && (!(env.getConfig("peerName", "").equals(peerName)))) {
|
2006-03-01 01:14:19 +01:00
|
|
|
// the name is new
|
2008-08-02 14:12:04 +02:00
|
|
|
final boolean nameOK = Pattern.compile("[A-Za-z0-9\\-_]{3,80}").matcher(peerName).matches();
|
2006-03-02 23:25:46 +01:00
|
|
|
if (nameOK) env.setConfig("peerName", peerName);
|
2006-03-01 01:14:19 +01:00
|
|
|
}
|
2009-02-14 14:12:08 +01:00
|
|
|
|
|
|
|
// UPnP config
|
|
|
|
boolean upnp = false;
|
|
|
|
if(post != null && post.containsKey("port")) { // hack to allow checkbox
|
|
|
|
if (post.containsKey("enableUpnp")) upnp = true;
|
|
|
|
if (upnp && !sb.getConfigBool(plasmaSwitchboardConstants.UPNP_ENABLED, false)) UPnP.addPortMapping();
|
|
|
|
sb.setConfig(plasmaSwitchboardConstants.UPNP_ENABLED, upnp);
|
|
|
|
if(!upnp) UPnP.deletePortMapping();
|
|
|
|
}
|
|
|
|
|
2006-03-01 01:14:19 +01:00
|
|
|
// check port
|
2006-05-20 16:05:49 +02:00
|
|
|
boolean reconnect = false;
|
2008-09-19 13:45:11 +02:00
|
|
|
if (!(env.getConfigLong("port", port) == port)) {
|
2006-03-01 01:14:19 +01:00
|
|
|
// validate port
|
2008-08-02 14:12:04 +02:00
|
|
|
final serverCore theServerCore = (serverCore) env.getThread("10_httpd");
|
2006-03-01 01:14:19 +01:00
|
|
|
env.setConfig("port", port);
|
2006-05-20 16:05:49 +02:00
|
|
|
|
|
|
|
// redirect the browser to the new port
|
|
|
|
reconnect = true;
|
2006-07-02 15:03:01 +02:00
|
|
|
|
2009-02-14 14:12:08 +01:00
|
|
|
// renew upnp port mapping
|
|
|
|
if (upnp) UPnP.addPortMapping();
|
|
|
|
|
2006-07-02 15:03:01 +02:00
|
|
|
String host = null;
|
2008-08-25 20:11:47 +02:00
|
|
|
if (header.containsKey(httpRequestHeader.HOST)) {
|
|
|
|
host = header.get(httpRequestHeader.HOST);
|
2008-08-02 14:12:04 +02:00
|
|
|
final int idx = host.indexOf(":");
|
2006-07-02 15:03:01 +02:00
|
|
|
if (idx != -1) host = host.substring(0,idx);
|
|
|
|
} else {
|
2007-07-24 02:46:17 +02:00
|
|
|
host = serverDomains.myPublicLocalIP().getHostAddress();
|
2006-07-02 15:03:01 +02:00
|
|
|
}
|
|
|
|
|
2007-10-24 23:38:19 +02:00
|
|
|
prop.put("reconnect", "1");
|
2006-07-02 15:03:01 +02:00
|
|
|
prop.put("reconnect_host", host);
|
|
|
|
prop.put("nextStep_host", host);
|
2006-05-20 16:05:49 +02:00
|
|
|
prop.put("reconnect_port", port);
|
2007-10-24 23:38:19 +02:00
|
|
|
prop.put("nextStep_port", port);
|
|
|
|
prop.put("reconnect_sslSupport", theServerCore.withSSL() ? "1" : "0");
|
|
|
|
prop.put("nextStep_sslSupport", theServerCore.withSSL() ? "1" : "0");
|
2006-05-20 16:05:49 +02:00
|
|
|
|
2008-06-11 21:04:39 +02:00
|
|
|
// generate new shortcut (used for Windows)
|
2008-07-13 09:55:20 +02:00
|
|
|
//yacyAccessible.setNewPortBat(Integer.parseInt(port));
|
|
|
|
//yacyAccessible.setNewPortLink(Integer.parseInt(port));
|
2008-06-11 21:04:39 +02:00
|
|
|
|
2006-05-20 16:05:49 +02:00
|
|
|
// force reconnection in 7 seconds
|
|
|
|
theServerCore.reconnect(7000);
|
|
|
|
} else {
|
2007-10-24 23:38:19 +02:00
|
|
|
prop.put("reconnect", "0");
|
2006-03-01 01:14:19 +01:00
|
|
|
}
|
2008-05-20 15:21:55 +02:00
|
|
|
|
|
|
|
// set a use case
|
2008-09-24 01:30:25 +02:00
|
|
|
String networkName = sb.getConfig(plasmaSwitchboardConstants.NETWORK_NAME, "");
|
2008-05-20 15:21:55 +02:00
|
|
|
if (post != null && post.containsKey("usecase")) {
|
2008-07-20 23:52:51 +02:00
|
|
|
if (post.get("usecase", "").equals("freeworld") && !networkName.equals("freeworld")) {
|
|
|
|
// switch to freeworld network
|
|
|
|
sb.switchNetwork("defaults/yacy.network.freeworld.unit");
|
2008-05-23 11:17:30 +02:00
|
|
|
// switch to p2p mode
|
2008-08-02 15:57:00 +02:00
|
|
|
sb.setConfig(plasmaSwitchboardConstants.INDEX_DIST_ALLOW, true);
|
|
|
|
sb.setConfig(plasmaSwitchboardConstants.INDEX_RECEIVE_ALLOW, true);
|
2008-05-20 15:21:55 +02:00
|
|
|
}
|
2008-07-20 23:52:51 +02:00
|
|
|
if (post.get("usecase", "").equals("portal") && !networkName.equals("webportal")) {
|
|
|
|
// switch to webportal network
|
|
|
|
sb.switchNetwork("defaults/yacy.network.webportal.unit");
|
2008-05-23 11:17:30 +02:00
|
|
|
// switch to robinson mode
|
2008-08-02 15:57:00 +02:00
|
|
|
sb.setConfig(plasmaSwitchboardConstants.INDEX_DIST_ALLOW, false);
|
|
|
|
sb.setConfig(plasmaSwitchboardConstants.INDEX_RECEIVE_ALLOW, false);
|
2008-05-20 15:21:55 +02:00
|
|
|
}
|
2008-07-20 23:52:51 +02:00
|
|
|
if (post.get("usecase", "").equals("intranet") && !networkName.equals("intranet")) {
|
|
|
|
// switch to intranet network
|
|
|
|
sb.switchNetwork("defaults/yacy.network.intranet.unit");
|
2008-05-23 11:17:30 +02:00
|
|
|
// switch to p2p mode: enable ad-hoc networks between intranet users
|
2008-09-23 14:11:19 +02:00
|
|
|
sb.setConfig(plasmaSwitchboardConstants.INDEX_DIST_ALLOW, false);
|
|
|
|
sb.setConfig(plasmaSwitchboardConstants.INDEX_RECEIVE_ALLOW, false);
|
2008-05-20 15:21:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-24 01:30:25 +02:00
|
|
|
networkName = sb.getConfig(plasmaSwitchboardConstants.NETWORK_NAME, "");
|
2008-05-20 15:21:55 +02:00
|
|
|
if (networkName.equals("freeworld")) {
|
|
|
|
prop.put("setUseCase", 1);
|
2008-05-23 11:17:30 +02:00
|
|
|
prop.put("setUseCase_freeworldChecked", 1);
|
|
|
|
} else if (networkName.equals("webportal")) {
|
|
|
|
prop.put("setUseCase", 1);
|
|
|
|
prop.put("setUseCase_portalChecked", 1);
|
2008-05-20 15:21:55 +02:00
|
|
|
} else if (networkName.equals("intranet")) {
|
|
|
|
prop.put("setUseCase", 1);
|
|
|
|
prop.put("setUseCase_intranetChecked", 1);
|
|
|
|
} else {
|
|
|
|
prop.put("setUseCase", 0);
|
|
|
|
}
|
|
|
|
prop.put("setUseCase_port", port);
|
2006-03-01 01:14:19 +01:00
|
|
|
|
|
|
|
// check if values are proper
|
2008-08-02 14:12:04 +02:00
|
|
|
final boolean properPassword = (sb.getConfig(httpd.ADMIN_ACCOUNT_B64MD5, "").length() > 0) || sb.getConfigBool("adminAccountForLocalhost", false);
|
|
|
|
final boolean properName = (env.getConfig("peerName","").length() >= 3) && (!(yacySeed.isDefaultPeerName(env.getConfig("peerName",""))));
|
2009-03-13 11:34:51 +01:00
|
|
|
final boolean properPort = (sb.webIndex.peers().mySeed().isSenior()) || (sb.webIndex.peers().mySeed().isPrincipal());
|
2006-03-01 01:14:19 +01:00
|
|
|
|
2008-05-15 13:26:43 +02:00
|
|
|
if ((env.getConfig("defaultFiles", "").startsWith("ConfigBasic.html,"))) {
|
2008-06-05 22:46:31 +02:00
|
|
|
env.setConfig("defaultFiles", env.getConfig("defaultFiles", "").substring(17));
|
|
|
|
env.setConfig("browserPopUpPage", "Status.html");
|
2006-08-22 15:35:51 +02:00
|
|
|
httpdFileHandler.initDefaultPath();
|
2006-03-04 19:52:04 +01:00
|
|
|
}
|
|
|
|
|
2007-10-24 23:38:19 +02:00
|
|
|
prop.put("statusName", properName ? "1" : "0");
|
|
|
|
prop.put("statusPort", properPort ? "1" : "0");
|
2006-05-20 16:05:49 +02:00
|
|
|
if (reconnect) {
|
|
|
|
prop.put("nextStep", NEXTSTEP_RECONNECT);
|
|
|
|
} else if (!properName) {
|
|
|
|
prop.put("nextStep", NEXTSTEP_PEERNAME);
|
2008-05-20 15:21:55 +02:00
|
|
|
} else if (!properPassword) {
|
|
|
|
prop.put("nextStep", NEXTSTEP_PWD);
|
2006-05-20 16:05:49 +02:00
|
|
|
} else if (!properPort) {
|
|
|
|
prop.put("nextStep", NEXTSTEP_PEERPORT);
|
2006-03-01 01:14:19 +01:00
|
|
|
} else {
|
2006-05-20 16:05:49 +02:00
|
|
|
prop.put("nextStep", NEXTSTEP_FINISHED);
|
2006-03-01 01:14:19 +01:00
|
|
|
}
|
2009-02-14 14:12:08 +01:00
|
|
|
|
2009-03-07 16:31:24 +01:00
|
|
|
final boolean upnp_enabled = env.getConfigBool(plasmaSwitchboardConstants.UPNP_ENABLED, false);
|
2009-02-14 14:12:08 +01:00
|
|
|
prop.put("upnp", "1");
|
2009-03-07 16:31:24 +01:00
|
|
|
prop.put("upnp_enabled", upnp_enabled ? "1" : "0");
|
|
|
|
if (upnp_enabled) prop.put("upnp_success", (UPnP.getMappedPort() > 0) ? "2" : "1");
|
|
|
|
else prop.put("upnp_success", "0");
|
2008-06-29 20:35:15 +02:00
|
|
|
|
2006-05-20 16:05:49 +02:00
|
|
|
// set default values
|
2008-09-19 13:45:11 +02:00
|
|
|
prop.putHTML("defaultName", env.getConfig("peerName", ""));
|
|
|
|
prop.putHTML("defaultPort", env.getConfig("port", "8080"));
|
2007-07-04 12:32:30 +02:00
|
|
|
lang = env.getConfig("locale.language", "default"); // re-assign lang, may have changed
|
2006-03-02 23:25:46 +01:00
|
|
|
if (lang.equals("default")) {
|
2007-10-24 23:38:19 +02:00
|
|
|
prop.put("langDeutsch", "0");
|
|
|
|
prop.put("langEnglish", "1");
|
2006-03-02 23:25:46 +01:00
|
|
|
} else if (lang.equals("de")) {
|
2007-10-24 23:38:19 +02:00
|
|
|
prop.put("langDeutsch", "1");
|
|
|
|
prop.put("langEnglish", "0");
|
2006-03-02 23:25:46 +01:00
|
|
|
} else {
|
2007-10-24 23:38:19 +02:00
|
|
|
prop.put("langDeutsch", "0");
|
|
|
|
prop.put("langEnglish", "0");
|
2006-03-02 23:25:46 +01:00
|
|
|
}
|
2006-03-01 01:14:19 +01:00
|
|
|
return prop;
|
|
|
|
}
|
|
|
|
}
|