Commit e8cc92f4 authored by Dave Cridland's avatar Dave Cridland Committed by GitHub

Merge pull request #628 from guusdk/OF-1170_proxy_addresses

OF-1170 Advertise all proxy addresses
parents 1d859149 352043f7
...@@ -21,12 +21,10 @@ ...@@ -21,12 +21,10 @@
package org.jivesoftware.openfire.filetransfer.proxy; package org.jivesoftware.openfire.filetransfer.proxy;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import org.dom4j.DocumentHelper; import org.dom4j.DocumentHelper;
import org.dom4j.Element; import org.dom4j.Element;
...@@ -94,9 +92,9 @@ public class FileTransferProxy extends BasicModule ...@@ -94,9 +92,9 @@ public class FileTransferProxy extends BasicModule
private IQHandlerInfo info; private IQHandlerInfo info;
private RoutingTable routingTable; private RoutingTable routingTable;
private PacketRouter router; private PacketRouter router;
private String proxyIP;
private ProxyConnectionManager connectionManager; private ProxyConnectionManager connectionManager;
// The address to operate on. Null for any address.
private InetAddress bindInterface; private InetAddress bindInterface;
...@@ -136,14 +134,17 @@ public class FileTransferProxy extends BasicModule ...@@ -136,14 +134,17 @@ public class FileTransferProxy extends BasicModule
IQ reply = IQ.createResultIQ(packet); IQ reply = IQ.createResultIQ(packet);
Element newChild = reply.setChildElement("query", Element newChild = reply.setChildElement("query",
FileTransferManager.NAMESPACE_BYTESTREAMS); FileTransferManager.NAMESPACE_BYTESTREAMS);
Element response = newChild.addElement("streamhost"); for ( InetAddress address : getAddresses() )
response.addAttribute("jid", getServiceDomain()); {
response.addAttribute("host", proxyIP); Element response = newChild.addElement( "streamhost" );
response.addAttribute("port", String.valueOf(connectionManager.getProxyPort())); response.addAttribute( "jid", getServiceDomain() );
response.addAttribute( "host", address.getHostAddress() );
response.addAttribute( "port", String.valueOf( connectionManager.getProxyPort() ) );
}
router.route(reply); router.route(reply);
return true; return true;
} }
else if (packet.getType() == IQ.Type.set && childElement != null) { else if (packet.getType() == IQ.Type.set) {
String sid = childElement.attributeValue("sid"); String sid = childElement.attributeValue("sid");
JID from = packet.getFrom(); JID from = packet.getFrom();
JID to = new JID(childElement.elementTextTrim("activate")); JID to = new JID(childElement.elementTextTrim("activate"));
...@@ -170,39 +171,83 @@ public class FileTransferProxy extends BasicModule ...@@ -170,39 +171,83 @@ public class FileTransferProxy extends BasicModule
} }
@Override @Override
public void initialize(XMPPServer server) { public void initialize( XMPPServer server )
{
super.initialize(server); super.initialize(server);
proxyServiceName = JiveGlobals.getProperty("xmpp.proxy.service", "proxy"); proxyServiceName = JiveGlobals.getProperty("xmpp.proxy.service", "proxy");
routingTable = server.getRoutingTable(); routingTable = server.getRoutingTable();
router = server.getPacketRouter(); router = server.getPacketRouter();
// Load the external IP and port information final String hardCodedProxyIP = JiveGlobals.getProperty( "xmpp.proxy.externalip" );
String interfaceName = JiveGlobals.getXMLProperty("network.interface"); final String interfaceName = JiveGlobals.getXMLProperty( "network.interface" );
bindInterface = null; if ( hardCodedProxyIP != null && !hardCodedProxyIP.trim().isEmpty() )
if (interfaceName != null) { {
if (interfaceName.trim().length() > 0) { // First choice: a hardcoded IP address, if one exists.
try { try
bindInterface = InetAddress.getByName(interfaceName); {
} bindInterface = InetAddress.getByName( hardCodedProxyIP.trim() );
catch (UnknownHostException e) { }
Log.error("Error binding to network.interface", e); catch ( UnknownHostException e )
} {
Log.error( "Error binding to xmpp.proxy.externalip '{}'", interfaceName, e );
} }
} }
else if ( interfaceName != null && !interfaceName.trim().isEmpty() )
try { {
proxyIP = JiveGlobals.getProperty("xmpp.proxy.externalip", // No hardcoded IP? Let's see if we hardcoded a specific interface, then use its address.
(bindInterface != null ? bindInterface.getHostAddress() try
: InetAddress.getLocalHost().getHostAddress())); {
bindInterface = InetAddress.getByName( interfaceName.trim() );
}
catch ( UnknownHostException e )
{
Log.error( "Error binding to network.interface '{}'", interfaceName, e );
}
} }
catch (UnknownHostException e) { else
Log.error("Couldn't discover local host", e); {
// If no configuration is available, use all available addresses.
bindInterface = null;
} }
connectionManager = new ProxyConnectionManager(getFileTransferManager(server)); connectionManager = new ProxyConnectionManager(getFileTransferManager(server));
} }
/**
* Returns the IP address(es) that are used.
*/
private Set<InetAddress> getAddresses()
{
final Set<InetAddress> result = new HashSet<>();
if ( bindInterface != null )
{
result.add( bindInterface );
}
else
{
// When there's no specific address configured, return all available addresses.
try
{
final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while ( networkInterfaces.hasMoreElements() )
{
final NetworkInterface networkInterface = networkInterfaces.nextElement();
final Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while ( inetAddresses.hasMoreElements() )
{
result.add( inetAddresses.nextElement() );
}
}
}
catch ( SocketException e )
{
Log.error( "Error determining all addresses for this server", e );
}
}
return result;
}
private FileTransferManager getFileTransferManager(XMPPServer server) { private FileTransferManager getFileTransferManager(XMPPServer server) {
return server.getFileTransferManager(); return server.getFileTransferManager();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment