added UPnP support

YaCy can now automatically forward ports on home routers
off by default

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@5609 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
lotus 16 years ago
parent 62762ba81a
commit 4aad461100

@ -11,39 +11,39 @@
<classpathentry kind="src" path="htroot/api/util"/>
<classpathentry kind="src" path="htroot/api/bookmarks/xbel"/>
<classpathentry kind="src" path="htroot/api/bookmarks/tags"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/tar.jar"/>
<classpathentry kind="lib" path="libx/bzip2.jar"/>
<classpathentry kind="lib" path="libx/commons-discovery.jar"/>
<classpathentry kind="lib" path="libx/commons-jxpath-1.1.jar"/>
<classpathentry kind="lib" path="libx/jakarta-oro-2.0.7.jar"/>
<classpathentry kind="lib" path="libx/jaxrpc.jar"/>
<classpathentry kind="lib" path="libx/jmimemagic-0.1.0.jar"/>
<classpathentry kind="lib" path="libx/jrpm-head.jar"/>
<classpathentry kind="lib" path="libx/jrpm-SNAPSHOT.jar"/>
<classpathentry kind="lib" path="libx/jsch-0.1.21.jar"/>
<classpathentry kind="lib" path="libx/log4j-1.2.9.jar"/>
<classpathentry kind="lib" path="libx/odf_utils_05_11_29.jar"/>
<classpathentry kind="lib" path="libx/sbbi-upnplib-1.0.3.jar"/>
<classpathentry kind="lib" path="libx/tm-extractors-1.0.jar"/>
<classpathentry kind="lib" path="libx/wsdl4j.jar"/>
<classpathentry kind="lib" path="libx/xerces.jar"/>
<classpathentry kind="lib" path="libx/poi-3.0-alpha2-20060616.jar"/>
<classpathentry kind="lib" path="libx/poi-scratchpad-3.0-alpha2-20060616.jar"/>
<classpathentry kind="lib" path="libx/inetlib.jar"/>
<classpathentry kind="lib" path="libx/gnumail.jar"/>
<classpathentry kind="lib" path="libx/activation.jar"/>
<classpathentry kind="lib" path="libx/webcat-0.1-swf.jar"/>
<classpathentry kind="lib" path="libx/J7Zip-modified.jar"/>
<classpathentry kind="lib" path="libx/PDFBox-0.7.3.jar"/>
<classpathentry kind="lib" path="libx/FontBox-0.1.0-dev.jar"/>
<classpathentry kind="lib" path="libx/bcprov-jdk14-139.jar"/>
<classpathentry kind="lib" path="libx/bcmail-jdk14-139.jar"/>
<classpathentry kind="lib" path="libx/commons-codec-1.3.jar"/>
<classpathentry kind="lib" path="lib/commons-httpclient-3.1.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
<classpathentry kind="lib" path="lib/commons-io-1.4.jar"/>
<classpathentry kind="lib" path="lib/commons-fileupload-1.2.1.jar"/>
<classpathentry kind="lib" path="lib/servlet-api.jar"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="lib" path="lib/tar.jar"/>
<classpathentry exported="true" kind="lib" path="libx/bzip2.jar"/>
<classpathentry exported="true" kind="lib" path="libx/commons-discovery.jar"/>
<classpathentry exported="true" kind="lib" path="libx/jakarta-oro-2.0.7.jar"/>
<classpathentry exported="true" kind="lib" path="libx/jaxrpc.jar"/>
<classpathentry exported="true" kind="lib" path="libx/jmimemagic-0.1.0.jar"/>
<classpathentry exported="true" kind="lib" path="libx/jrpm-head.jar"/>
<classpathentry exported="true" kind="lib" path="libx/jrpm-SNAPSHOT.jar"/>
<classpathentry exported="true" kind="lib" path="libx/jsch-0.1.21.jar"/>
<classpathentry exported="true" kind="lib" path="libx/log4j-1.2.9.jar"/>
<classpathentry exported="true" kind="lib" path="libx/odf_utils_05_11_29.jar"/>
<classpathentry exported="true" kind="lib" path="libx/tm-extractors-1.0.jar"/>
<classpathentry exported="true" kind="lib" path="libx/wsdl4j.jar"/>
<classpathentry exported="true" kind="lib" path="libx/xerces.jar"/>
<classpathentry exported="true" kind="lib" path="libx/poi-3.0-alpha2-20060616.jar"/>
<classpathentry exported="true" kind="lib" path="libx/poi-scratchpad-3.0-alpha2-20060616.jar"/>
<classpathentry exported="true" kind="lib" path="libx/inetlib.jar"/>
<classpathentry exported="true" kind="lib" path="libx/gnumail.jar"/>
<classpathentry exported="true" kind="lib" path="libx/activation.jar"/>
<classpathentry exported="true" kind="lib" path="libx/webcat-0.1-swf.jar"/>
<classpathentry exported="true" kind="lib" path="libx/J7Zip-modified.jar"/>
<classpathentry exported="true" kind="lib" path="libx/PDFBox-0.7.3.jar"/>
<classpathentry exported="true" kind="lib" path="libx/FontBox-0.1.0-dev.jar"/>
<classpathentry exported="true" kind="lib" path="libx/bcprov-jdk14-139.jar"/>
<classpathentry exported="true" kind="lib" path="libx/bcmail-jdk14-139.jar"/>
<classpathentry exported="true" kind="lib" path="libx/commons-codec-1.3.jar"/>
<classpathentry exported="true" kind="lib" path="lib/commons-httpclient-3.1.jar"/>
<classpathentry exported="true" kind="lib" path="lib/commons-logging-1.1.1.jar"/>
<classpathentry exported="true" kind="lib" path="lib/commons-io-1.4.jar"/>
<classpathentry exported="true" kind="lib" path="lib/commons-fileupload-1.2.1.jar"/>
<classpathentry exported="true" kind="lib" path="lib/servlet-api.jar"/>
<classpathentry exported="true" kind="lib" path="lib/commons-jxpath-1.1.jar"/>
<classpathentry exported="true" kind="lib" path="lib/sbbi-upnplib-1.0.4.jar"/>
<classpathentry kind="output" path="gen"/>
</classpath>

@ -191,6 +191,9 @@
<pathelement location="${lib}/commons-logging-1.1.1.jar" />
<pathelement location="${lib}/commons-io-1.4.jar" />
<pathelement location="${lib}/servlet-api.jar" />
<!-- upnp (+ commons-logging) -->
<pathelement location="${lib}/sbbi-upnplib-1.0.4.jar" />
<pathelement location="${lib}/commons-jxpath-1.1.jar" />
</path>
<!-- compiling yacy.java -->
@ -889,6 +892,8 @@
<pathelement location="${lib}/commons-logging-1.1.1.jar" />
<pathelement location="${lib}/commons-io-1.4.jar" />
<pathelement location="${lib}/servlet-api.jar" />
<pathelement location="${lib}/sbbi-upnplib-1.0.4.jar" />
<pathelement location="${lib}/commons-jxpath-1.1.jar" />
<pathelement location="${libx}" />
<fileset dir="${libx}" includes="**/*.jar" />
</classpath>

@ -15,6 +15,9 @@
# 192.168.0.1:8080
port = 8080
# use UPnP [true/false]
upnp.enabled =
#sometimes you may want yacy to bind to another port, than the one reachable from outside.
#then set bindPort to the port yacy should bind on, and port to the port, visible from outside
#to run yacy on port 8080, reachable from port 80, set bindPort=8080, port=80 and use

@ -41,6 +41,7 @@ import de.anomic.server.serverDomains;
import de.anomic.server.serverInstantBusyThread;
import de.anomic.server.serverObjects;
import de.anomic.server.serverSwitch;
import de.anomic.tools.UPnP;
import de.anomic.yacy.yacySeed;
public class ConfigBasic {
@ -95,7 +96,16 @@ public class ConfigBasic {
final boolean nameOK = Pattern.compile("[A-Za-z0-9\\-_]{3,80}").matcher(peerName).matches();
if (nameOK) env.setConfig("peerName", peerName);
}
// 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();
}
// check port
boolean reconnect = false;
if (!(env.getConfigLong("port", port) == port)) {
@ -106,6 +116,9 @@ public class ConfigBasic {
// redirect the browser to the new port
reconnect = true;
// renew upnp port mapping
if (upnp) UPnP.addPortMapping();
String host = null;
if (header.containsKey(httpRequestHeader.HOST)) {
host = header.get(httpRequestHeader.HOST);
@ -198,7 +211,9 @@ public class ConfigBasic {
} else {
prop.put("nextStep", NEXTSTEP_FINISHED);
}
prop.put("upnp", "1");
prop.put("upnp_enabled", (env.getConfigBool(plasmaSwitchboardConstants.UPNP_ENABLED, false)) ? "1" : "0");
// set default values
prop.putHTML("defaultName", env.getConfig("peerName", ""));

@ -117,6 +117,7 @@ import de.anomic.crawler.HTTPLoader;
import de.anomic.crawler.ImporterManager;
import de.anomic.crawler.IndexingStack;
import de.anomic.crawler.NoticedURL;
import de.anomic.crawler.ProtocolLoader;
import de.anomic.crawler.ResourceObserver;
import de.anomic.crawler.ResultImages;
import de.anomic.crawler.ResultURLs;
@ -147,10 +148,10 @@ import de.anomic.kelondro.order.DateFormatter;
import de.anomic.kelondro.order.Digest;
import de.anomic.kelondro.order.NaturalOrder;
import de.anomic.kelondro.table.CachedRecords;
import de.anomic.kelondro.util.FileUtils;
import de.anomic.kelondro.util.Log;
import de.anomic.kelondro.util.MemoryControl;
import de.anomic.kelondro.util.SetTools;
import de.anomic.kelondro.util.Log;
import de.anomic.kelondro.util.FileUtils;
import de.anomic.plasma.parser.ParserException;
import de.anomic.server.serverAbstractSwitch;
import de.anomic.server.serverBusyThread;
@ -164,6 +165,7 @@ import de.anomic.server.serverProfiling;
import de.anomic.server.serverSemaphore;
import de.anomic.server.serverSwitch;
import de.anomic.server.serverThread;
import de.anomic.tools.UPnP;
import de.anomic.tools.crypt;
import de.anomic.yacy.yacyClient;
import de.anomic.yacy.yacyCore;
@ -266,6 +268,11 @@ public final class plasmaSwitchboard extends serverAbstractSwitch<IndexingStack.
setLog(new Log("PLASMA"));
if (applyPro) this.log.logInfo("This is the pro-version of YaCy");
// UPnP port mapping
UPnP.setSb(sb);
if (getConfigBool(plasmaSwitchboardConstants.UPNP_ENABLED, false))
serverInstantBusyThread.oneTimeJob(UPnP.class, "addPortMapping", UPnP.log, 0);
// init TrayIcon if possible
yacyTray.init(this);
@ -1113,6 +1120,7 @@ public final class plasmaSwitchboard extends serverAbstractSwitch<IndexingStack.
log.logConfig("SWITCHBOARD SHUTDOWN STEP 3: sending termination signal to database manager (stand by...)");
webIndex.close();
plasmaHTCache.close();
UPnP.deletePortMapping();
yacyTray.removeTray();
log.logConfig("SWITCHBOARD SHUTDOWN TERMINATED");
}

@ -540,4 +540,6 @@ public final class plasmaSwitchboardConstants {
public static final String BROWSER_POP_UP_TRIGGER = "browserPopUpTrigger";
public static final String BROWSER_POP_UP_APPLICATION = "browserPopUpApplication";
public static final String BROWSER_POP_UP_PAGE = "browserPopUpPage";
public static final String UPNP_ENABLED = "upnp.enabled";
}

@ -0,0 +1,136 @@
// UPnP.java
// (C) 2009 by David Wieditz; d.wieditz@gmx.de
// first published 14.02.2009 on http://yacy.net
//
// This is a part of YaCy, a peer-to-peer based web search engine
//
// $LastChangedDate: $
// $LastChangedRevision: $
// $LastChangedBy: $
//
// LICENSE
//
// 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
package de.anomic.tools;
import java.io.IOException;
import java.net.InetAddress;
import net.sbbi.upnp.impls.InternetGatewayDevice;
import net.sbbi.upnp.messages.UPNPResponseException;
import de.anomic.kelondro.util.Log;
import de.anomic.plasma.plasmaSwitchboard;
public class UPnP {
public final static Log log = new Log("UPNP");
private static plasmaSwitchboard sb;
private final static int discoveryTimeout = 5000; // seconds to receive a response from devices
private static InternetGatewayDevice[] IGDs = null;
// mapping variables
private final static String mappedName = "YaCy";
private final static String mappedProtocol = "TCP";
private static int mappedPort = 0;
private static String localHostIP = null;;
public static void setSb(plasmaSwitchboard switchboard) {
sb = switchboard;
}
private static boolean init() {
boolean init = true;
try {
IGDs = InternetGatewayDevice.getDevices(discoveryTimeout);
localHostIP = InetAddress.getLocalHost().getHostAddress();
} catch (IOException e) {
init = false;
}
if (IGDs != null) {
for (InternetGatewayDevice IGD : IGDs) {
log.logInfo("found device: " + IGD.getIGDRootDevice().getFriendlyName());
}
} else {
log.logInfo("no device found");
init = false;
}
return init;
}
/**
* add port mapping for configured port
*/
public static void addPortMapping() {
if (sb == null) return;
addPortMapping(Integer.parseInt(sb.getConfig("port", "0")));
}
/**
* add TCP port mapping to all IGDs on the network<br/>
* latest port mapping will be removed
* @param port
*/
public static void addPortMapping(final int port) { //TODO: don't map already mapped port again
if (port < 1) return;
if (mappedPort > 0) deletePortMapping(); // delete old mapping first
if (mappedPort == 0 && ((IGDs != null && localHostIP != null) || init())) {
mappedPort = port;
for (InternetGatewayDevice IGD : IGDs) {
try {
boolean mapped = IGD.addPortMapping(mappedName, null, mappedPort, mappedPort, localHostIP, 0, mappedProtocol);
String msg = "port " + mappedPort + " on device "+ IGD.getIGDRootDevice().getFriendlyName();
if (mapped) log.logInfo("mapped " + msg);
else log.logWarning("could not map " + msg);
} catch (IOException e) {} catch (UPNPResponseException e) { log.logSevere("mapping error: " + e.getMessage()); }
}
}
}
/**
* delete current port mapping
*/
public static void deletePortMapping() {
if (mappedPort > 0 && IGDs != null && localHostIP != null) {
for (InternetGatewayDevice IGD : IGDs) {
try {
boolean unmapped = IGD.deletePortMapping(null, mappedPort, mappedProtocol);
String msg = "port " + mappedPort + " on device "+ IGD.getIGDRootDevice().getFriendlyName();
if (unmapped) log.logInfo("unmapped " + msg);
else log.logWarning("could not unmap " + msg);
} catch (IOException e) {} catch (UPNPResponseException e) { log.logSevere("unmapping error: " + e.getMessage()); }
}
mappedPort = 0; // reset mapped port
}
}
/**
* @return mapped port or 0
*/
public static int getMappedPort() {
return mappedPort;
}
public static void main(String[] args) {
deletePortMapping(); // nothing
addPortMapping(40000); // map
addPortMapping(40000); // unmap, map
deletePortMapping(); // unmap
deletePortMapping(); // nothing
}
}

@ -24,6 +24,10 @@ CRAWLER-POOL.level = INFO
STACKCRAWL.level = INFO
MEMORY.level = INFO
HTTPC.level = INFO
# UPnP related
UPNP.level = INFO
sun.net.www.protocol.http.HttpURLConnection.level = INFO
# Tray
sun.awt.level = OFF
java.awt.level = OFF

Loading…
Cancel
Save