use of org.jivesoftware.openfire.SessionManager in project Openfire by igniterealtime.
the class LocalOutgoingServerSession method authenticateDomain.
/**
* Authenticates the local domain to the remote domain. Once authenticated the remote domain can be expected to
* start accepting data from the local domain.
*
* This implementation will attempt to re-use an existing connection. An connection is deemed re-usable when it is either:
* <ul>
* <li>authenticated to the remote domain itself, or:</li>
* <li>authenticated to a sub- or superdomain of the remote domain AND offers dialback.</li>
* </ul>
*
* When no re-usable connection exists, a new connection will be created.
*
* DNS will be used to find hosts for the remote domain. When DNS records do not specify a port, port 5269 will be
* used unless this default is overridden by the <b>xmpp.server.socket.remotePort</b> property.
*
* @param localDomain the local domain to authenticate with the remote server.
* @param remoteDomain the remote server, to which the local domain intends to send data.
* @return True if the domain was authenticated by the remote server.
*/
public static boolean authenticateDomain(final String localDomain, final String remoteDomain) {
final Logger log = LoggerFactory.getLogger(Log.getName() + "[Authenticate local domain: '" + localDomain + "' to remote domain: '" + remoteDomain + "']");
log.debug("Start domain authentication ...");
if (remoteDomain == null || remoteDomain.length() == 0 || remoteDomain.trim().indexOf(' ') > -1) {
// Do nothing if the target domain is empty, null or contains whitespaces
log.warn("Unable to authenticate: remote domain is invalid.");
return false;
}
try {
// Check if the remote domain is in the blacklist
if (!RemoteServerManager.canAccess(remoteDomain)) {
log.info("Unable to authenticate: Remote domain is not accessible according to our configuration (typical causes: server federation is disabled, or domain is blacklisted).");
return false;
}
log.debug("Searching for pre-existing outgoing sessions to the remote domain (if one exists, it will be re-used) ...");
OutgoingServerSession session;
SessionManager sessionManager = SessionManager.getInstance();
if (sessionManager == null) {
// Server is shutting down while we are trying to create a new s2s connection
log.warn("Unable to authenticate: a SessionManager instance is not available. This should not occur unless Openfire is starting up or shutting down.");
return false;
}
session = sessionManager.getOutgoingServerSession(remoteDomain);
if (session == null) {
log.debug("There are no pre-existing outgoing sessions to the remote domain itself. Searching for pre-existing outgoing sessions to super- or subdomains of the remote domain (if one exists, it might be re-usable) ...");
for (IncomingServerSession incomingSession : sessionManager.getIncomingServerSessions(remoteDomain)) {
// These are the remote domains that are allowed to send data to the local domain - expected to be sub- or superdomains of remoteDomain
for (String otherRemoteDomain : incomingSession.getValidatedDomains()) {
// See if there's an outgoing session to any of the (other) domains hosted by the remote domain.
session = sessionManager.getOutgoingServerSession(otherRemoteDomain);
if (session != null) {
log.debug("An outgoing session to a different domain ('{}') hosted on the remote domain was found.", otherRemoteDomain);
// As this sub/superdomain is different from the original remote domain, we need to check if it supports dialback.
if (session.isUsingServerDialback()) {
log.debug("Dialback was used for '{}'. This session can be re-used.", otherRemoteDomain);
break;
} else {
log.debug("Dialback was not used for '{}'. This session cannot be re-used.", otherRemoteDomain);
session = null;
}
}
}
}
if (session == null) {
log.debug("There are no pre-existing session to other domains hosted on the remote domain.");
}
}
if (session != null) {
log.debug("A pre-existing session can be re-used. The session was established using server dialback so it is possible to do piggybacking to authenticate more domains.");
if (session.getAuthenticatedDomains().contains(localDomain) && session.getHostnames().contains(remoteDomain)) {
// Do nothing since the domain has already been authenticated.
log.debug("Authentication successful (domain was already authenticated in the pre-existing session).");
return true;
}
// A session already exists so authenticate the domain using that session.
if (session.authenticateSubdomain(localDomain, remoteDomain)) {
log.debug("Authentication successful (domain authentication was added using a pre-existing session).");
return true;
} else {
log.warn("Unable to authenticate: Unable to add authentication to pre-exising session.");
return false;
}
} else {
log.debug("Unable to re-use an existing session. Creating a new session ...");
int port = RemoteServerManager.getPortForServer(remoteDomain);
session = createOutgoingSession(localDomain, remoteDomain, port);
if (session != null) {
log.debug("Created a new session.");
// Add the validated domain as an authenticated domain
session.addAuthenticatedDomain(localDomain);
// Add the new domain to the list of names that the server may have
session.addHostname(remoteDomain);
// Notify the SessionManager that a new session has been created
sessionManager.outgoingServerSessionCreated((LocalOutgoingServerSession) session);
log.debug("Authentication successful.");
return true;
} else {
log.warn("Unable to authenticate: Fail to create new session.");
return false;
}
}
} catch (Exception e) {
log.error("An exception occurred while authenticating remote domain!", e);
return false;
}
}
use of org.jivesoftware.openfire.SessionManager in project Openfire by igniterealtime.
the class WebSocketPlugin method destroyPlugin.
@Override
public void destroyPlugin() {
// terminate any active websocket sessions
SessionManager sm = XMPPServer.getInstance().getSessionManager();
for (ClientSession session : sm.getSessions()) {
if (session instanceof LocalSession) {
Object ws = ((LocalSession) session).getSessionData("ws");
if (ws != null && (Boolean) ws) {
session.close();
}
}
}
ContextHandlerCollection contexts = HttpBindManager.getInstance().getContexts();
contexts.removeHandler(contextHandler);
contextHandler = null;
pluginClassLoader = null;
}
use of org.jivesoftware.openfire.SessionManager in project Openfire by igniterealtime.
the class FaviconServlet method doGet.
/**
* Retrieve the image based on it's name.
*
* @param request the httpservletrequest.
* @param response the httpservletresponse.
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
final String host = request.getParameter("host");
// Validate that we're connected to the host
final SessionManager sessionManager = SessionManager.getInstance();
final Optional<String> optionalHost = Stream.concat(sessionManager.getIncomingServers().stream(), sessionManager.getOutgoingServers().stream()).filter(remoteServerHost -> remoteServerHost.equalsIgnoreCase(host)).findAny();
if (!optionalHost.isPresent()) {
LOGGER.info("Request to unconnected host {} ignored - using default response", host);
writeBytesToStream(defaultBytes, response);
return;
}
// Check special cases where we need to change host to get a favicon
final String hostToUse = "gmail.com".equals(host) ? "google.com" : host;
byte[] bytes = getImage(hostToUse, defaultBytes);
if (bytes != null) {
writeBytesToStream(bytes, response);
}
}
use of org.jivesoftware.openfire.SessionManager in project Openfire by igniterealtime.
the class OpenfireWebSocketServlet method destroy.
@Override
public void destroy() {
// terminate any active websocket sessions
SessionManager sm = XMPPServer.getInstance().getSessionManager();
for (ClientSession session : sm.getSessions()) {
if (session instanceof LocalSession) {
Object ws = ((LocalSession) session).getSessionData("ws");
if (ws != null && (Boolean) ws) {
Log.debug("Closing session as websocket servlet is being destroyed: {}", session);
session.close();
}
}
}
super.destroy();
}
use of org.jivesoftware.openfire.SessionManager in project Openfire by igniterealtime.
the class TransportInstance method startInstance.
/**
* Starts the transport instance if it's enabled and not already running.
*/
public void startInstance() {
if (!enabled || running) {
return;
}
Log.info("Starting transport service: " + type.toString());
transport = null;
try {
transport = (BaseTransport) Class.forName(nameOfClass).newInstance();
transport.setup(this.type, this.description, sessionRouter);
} catch (ClassNotFoundException e) {
Log.error("Unable to find class: " + nameOfClass);
return;
} catch (InstantiationException e) {
Log.error("Unable to instantiate class: " + nameOfClass);
return;
} catch (IllegalAccessException e) {
Log.error("Unable to access class: " + nameOfClass);
return;
}
// Automatically kill any current s2s connections with the JID we want to use.
SessionManager sessionManager = SessionManager.getInstance();
String fullJID = this.subDomain + "." + XMPPServer.getInstance().getServerInfo().getXMPPDomain();
boolean pause = false;
try {
for (Session sess : sessionManager.getIncomingServerSessions(fullJID)) {
sess.close();
pause = true;
}
} catch (Exception ignored) {
// Session might have disappeared on its own
}
try {
Session sess = sessionManager.getOutgoingServerSession(fullJID);
if (sess != null) {
sess.close();
pause = true;
}
} catch (Exception ignored) {
// Session might have disappeared on its own
}
try {
// Wait one second if we closed something.
if (pause) {
Thread.sleep(1000L);
}
} catch (Exception ignored) {
// Hrm, interrupted? That's odd.
}
try {
componentManager.addComponent(this.subDomain, transport);
PropertyEventDispatcher.addListener(this);
running = true;
} catch (Exception e) {
Log.error("Error while adding component " + this.subDomain + ": ", e);
}
}
Aggregations