Commit efdb1618 authored by Daniel Henninger's avatar Daniel Henninger Committed by dhenninger

[JM-1500] Flash cross domain handler now behaves like a proper HTTP handler,...

[JM-1500] Flash cross domain handler now behaves like a proper HTTP handler, should solve a lot of flash related issues for things using port 5229.

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@10926 b35dd754-fafc-0310-a699-88a17e54d16e
parent 528119a6
......@@ -13,99 +13,117 @@
package org.jivesoftware.openfire;
import org.jivesoftware.openfire.container.BasicModule;
import org.jivesoftware.openfire.http.FlashCrossDomainServlet;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.Log;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.ContextHandler;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.servlet.ServletHandler;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Sets up the "legacy" flash cross domain servlet, served off port 5229.
*
* @author Daniel Henninger
*
*/
public class FlashCrossDomainHandler extends BasicModule {
private ServerSocket serverSocket;
public static String CROSS_DOMAIN_TEXT = "<?xml version=\"1.0\"?>" +
"<!DOCTYPE cross-domain-policy SYSTEM \"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd\">" +
"<cross-domain-policy>" +
"<allow-access-from domain=\"*\" to-ports=\"";
public static String CROSS_DOMAIN_END_TEXT = "\" /></cross-domain-policy>";
private Server crossDomainServer;
private Connector crossDomainConnector;
private ContextHandlerCollection contexts;
private int servletPort = 5229;
public FlashCrossDomainHandler() {
super("Flash CrossDomain Handler");
// Configure Jetty logging to a more reasonable default.
System.setProperty("org.mortbay.log.class", "org.jivesoftware.util.log.util.JettyLog");
// JSP 2.0 uses commons-logging, so also override that implementation.
System.setProperty("org.apache.commons.logging.LogFactory", "org.jivesoftware.util.log.util.CommonsLogFactory");
contexts = new ContextHandlerCollection();
}
public Integer getPort() {
if (crossDomainConnector != null) {
return crossDomainConnector.getLocalPort();
}
else {
return null;
}
}
public void start() {
Thread thread = new Thread(new Runnable() {
public void run() {
try {
startServer();
}
catch (Exception e) {
Log.error(e);
}
}
}, "Flash Cross Domain");
configureCrossDomainServer(servletPort);
thread.start();
try {
crossDomainServer.start();
}
catch (Exception e) {
Log.error("Error starting cross domain service", e);
}
}
public void stop() {
try {
if (serverSocket != null) {
serverSocket.close();
if (crossDomainServer != null) {
try {
crossDomainServer.stop();
}
catch (Exception e) {
Log.error("Error stoping cross domain service", e);
}
}
catch (IOException e) {
Log.error(e);
}
}
public int getPort() {
return serverSocket != null ? serverSocket.getLocalPort() : 0;
private String getBindInterface() {
String interfaceName = JiveGlobals.getXMLProperty("network.interface");
String bindInterface = null;
if (interfaceName != null) {
if (interfaceName.trim().length() > 0) {
bindInterface = interfaceName;
}
}
return bindInterface;
}
private void startServer() throws Exception {
try {
private void createConnector(int port) {
crossDomainConnector = null;
if (port > 0) {
SelectChannelConnector connector = new SelectChannelConnector();
// Listen on a specific network interface if it has been set.
String interfaceName = JiveGlobals.getXMLProperty("network.interface");
InetAddress bindInterface = null;
int port = 5229;
if (interfaceName != null) {
if (interfaceName.trim().length() > 0) {
bindInterface = InetAddress.getByName(interfaceName);
}
}
serverSocket = new ServerSocket(port, -1, bindInterface);
Log.debug("Flash cross domain is listening on " + interfaceName + " on port " + port);
connector.setHost(getBindInterface());
connector.setPort(port);
crossDomainConnector = connector;
}
catch (IOException e) {
Log.error("Could not listen on port: 5229.", e);
}
private synchronized void configureCrossDomainServer(int port) {
crossDomainServer = new Server();
createConnector(port);
if (crossDomainConnector == null) {
crossDomainServer = null;
return;
}
else {
crossDomainServer.addConnector(crossDomainConnector);
}
while (true) {
Socket clientSocket;
try {
clientSocket = serverSocket.accept();
createCrossDomainHandler(contexts, "/");
// Validate that we have a license
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
crossDomainServer.setHandlers(new Handler[]{contexts, new DefaultHandler()});
}
out.println(CROSS_DOMAIN_TEXT +
XMPPServer.getInstance().getConnectionManager().getClientListenerPort() +
CROSS_DOMAIN_END_TEXT);
out.println("\n");
out.flush();
out.close();
}
catch (IOException e) {
if (XMPPServer.getInstance().isShuttingDown()) {
break;
}
Log.error(e);
}
}
private void createCrossDomainHandler(ContextHandlerCollection contexts, String crossPath) {
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(FlashCrossDomainServlet.class, "/crossdomain.xml");
ContextHandler crossContextHandler = new ContextHandler(contexts, crossPath);
crossContextHandler.setHandler(handler);
}
}
......@@ -28,14 +28,14 @@ import java.io.IOException;
*/
public class FlashCrossDomainServlet extends HttpServlet {
private static String CROSS_DOMAIN_TEXT = "<?xml version=\"1.0\"?>" +
public static String CROSS_DOMAIN_TEXT = "<?xml version=\"1.0\"?>" +
"<!DOCTYPE cross-domain-policy SYSTEM \"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd\">" +
"<cross-domain-policy>" +
"<site-control permitted-cross-domain-policies=\"all\"/>" +
"<allow-access-from domain=\"*\" to-ports=\"";
private static String CROSS_DOMAIN_MIDDLE_TEXT = "\" secure=\"";
private static String CROSS_DOMAIN_END_TEXT = "\"/></cross-domain-policy>";
public static String CROSS_DOMAIN_MIDDLE_TEXT = "\" secure=\"";
public static String CROSS_DOMAIN_END_TEXT = "\"/></cross-domain-policy>";
private static String CROSS_DOMAIN_SECURE_ENABLED = "httpbind.crossdomain.secure";
private static boolean CROSS_DOMAIN_SECURE_DEFAULT = true;
......
......@@ -14,10 +14,10 @@ package org.jivesoftware.openfire.net;
import org.dom4j.Element;
import org.dom4j.io.XMPPPacketReader;
import org.jivesoftware.openfire.Connection;
import org.jivesoftware.openfire.FlashCrossDomainHandler;
import org.jivesoftware.openfire.PacketRouter;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.auth.UnauthorizedException;
import org.jivesoftware.openfire.http.FlashCrossDomainServlet;
import org.jivesoftware.openfire.session.LocalSession;
import org.jivesoftware.openfire.session.Session;
import org.jivesoftware.util.JiveGlobals;
......@@ -95,9 +95,9 @@ public abstract class StanzaHandler {
if (!initialStream) {
// Allow requests for flash socket policy files directly on the client listener port
if (stanza.startsWith("<policy-file-request/>")) {
String crossDomainText = FlashCrossDomainHandler.CROSS_DOMAIN_TEXT +
String crossDomainText = FlashCrossDomainServlet.CROSS_DOMAIN_TEXT +
XMPPServer.getInstance().getConnectionManager().getClientListenerPort() +
FlashCrossDomainHandler.CROSS_DOMAIN_END_TEXT + '\0';
FlashCrossDomainServlet.CROSS_DOMAIN_END_TEXT + '\0';
connection.deliverRawText(crossDomainText);
return;
}
......
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