Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

*) Adding possibility to specify the interface / IP-Address where YaCy should bind to. - e.g. Port = 192.168.0.1:8080 Port = #eth0:8080 Port = 8080

thelifd58d5f8e6b3
diff --git a/source/de/anomic/server/serverCore.java b/source/de/anomic/server/serverCore.java
index 89de323c4..eb75ce801 100644
--- a/source/de/anomic/server/serverCore.java
+++ b/source/de/anomic/server/serverCore.java
@@ -54,14 +54,17 @@ import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
+import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.channels.ClosedByInterruptException;
import java.security.KeyStore;
+import java.util.Enumeration;
import java.util.Hashtable;
import javax.net.ServerSocketFactory;
@@ -93,7 +96,7 @@ public final class serverCore extends serverAbstractThread implements serverThre
public static Hashtable bfHost = new Hashtable(); // for brute-force prevention
// class variables
- private int port; // the listening port
+ private String port; // the listening port
public static boolean portForwardingEnabled = false;
public static serverPortForwarding portForwarding = null;
@@ -169,14 +172,14 @@ public final class serverCore extends serverAbstractThread implements serverThre
// class initializer
public serverCore(
- int port,
+ String port,
int timeout,
boolean blockAttack,
serverHandler handlerPrototype,
serverSwitch switchboard,
int commandMaxLength
) throws IOException {
- this.port = port;
+ this.port = port.trim();
this.timeout = timeout;
this.commandMaxLength = commandMaxLength;
@@ -199,13 +202,63 @@ public final class serverCore extends serverAbstractThread implements serverThre
// Open a new server-socket channel
try {
+ this.log.logInfo("Trying to bind server to port " + port);
+
+ // parsing the port configuration
+ String bindIP = null;
+ int bindPort;
+
+ int pos = -1;
+ if ((pos = port.indexOf(":"))!= -1) {
+ bindIP = port.substring(0,pos).trim();
+ port = port.substring(pos+1);
+
+ if (bindIP.startsWith("#")) {
+ String interfaceName = bindIP.substring(1);
+ String hostName = null;
+ this.log.logFine("Trying to determine IP address of interface '" + interfaceName + "'.");
+
+ Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
+ if (interfaces != null) {
+ while (interfaces.hasMoreElements()) {
+ NetworkInterface interf = (NetworkInterface) interfaces.nextElement();
+ if (interf.getName().equalsIgnoreCase(interfaceName)) {
+ Enumeration addresses = interf.getInetAddresses();
+ if (addresses != null) {
+ while (addresses.hasMoreElements()) {
+ InetAddress address = (InetAddress)addresses.nextElement();
+ if (address instanceof Inet4Address) {
+ hostName = address.getHostAddress();
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+ if (hostName == null) {
+ this.log.logWarning("Unable to find interface with name '" + interfaceName + "'. Binding server to all interfaces");
+ bindIP = null;
+ } else {
+ this.log.logInfo("Binding server to interface '" + interfaceName + "' with IP '" + hostName + "'.");
+ bindIP = hostName;
+ }
+ }
+ }
+ bindPort = Integer.parseInt(port);
+
// Binds the ServerSocket to a specific address
this.socket = new ServerSocket();
- this.socket.bind(new InetSocketAddress(port));
+ this.socket.bind((bindIP == null)
+ ? new InetSocketAddress(bindPort)
+ : new InetSocketAddress(bindIP, bindPort));
// this.socket = new ServerSocket(port);
} catch (java.net.BindException e) {
- System.out.println("FATAL ERROR: " + e.getMessage() + " - probably root access rights needed. check port number"); System.exit(0);
+ String errorMsg = "FATAL ERROR: " + e.getMessage() + " - probably root access rights needed. check port number";
+ this.log.logSevere(errorMsg);
+ System.out.println(errorMsg);
+ System.exit(0);
}
diff --git a/source/yacy.java b/source/yacy.java
index 5a6deab98..84f343bc5 100644
--- a/source/yacy.java
+++ b/source/yacy.java
@@ -267,7 +267,7 @@ public final class yacy {
// read environment
//new
- final int port = Integer.parseInt(sb.getConfig("port", "8080"));
+ final String port = sb.getConfig("port", "8080");
int timeout = Integer.parseInt(sb.getConfig("httpdTimeout", "60000"));
if (timeout < 60000) timeout = 60000;
diff --git a/yacy.init b/yacy.init
index 008fef66d..e3863b2bc 100644
--- a/yacy.init
+++ b/yacy.init
@@ -9,7 +9,10 @@
# ----------------------------------------------------------------------------
# the http service configurations
-# port number of server
+# port number where the server should bind to
+# e.g. 8080
+# #eth0:8080
+# 192.168.0.1:8080
port = 8080
# time-out of client control socket in milliseconds