diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/Connection.java b/xmppserver/src/main/java/org/jivesoftware/openfire/Connection.java index 20103f04eb..b31cf19daf 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/Connection.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/Connection.java @@ -137,10 +137,18 @@ public interface Connection extends Closeable { Certificate[] getPeerCertificates(); /** - * Keeps track if the other peer of this session presented a self-signed certificate. When - * using self-signed certificate for server-2-server sessions then SASL EXTERNAL will not be - * used and instead server-dialback will be preferred for verifying the identify of the remote - * server. + * Returns the remote port used by the connection. + * + * @return the remote port, or 0 when unavailable. + */ + default int getRemotePort() { + return 0; + } + + /** + * Keeps track of whether the other peer of this session presented a self-signed certificate. When + * using a self-signed certificate for server-to-server sessions, SASL EXTERNAL will not be + * used and instead server dialback will be preferred for verifying the identity of the remote * * @param isSelfSigned true if the other peer presented a self-signed certificate. */ diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/net/SocketConnection.java b/xmppserver/src/main/java/org/jivesoftware/openfire/net/SocketConnection.java index 748ba2e507..bcbae337ee 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/net/SocketConnection.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/net/SocketConnection.java @@ -263,11 +263,18 @@ public String getHostName() throws UnknownHostException { } /** - * Returns the port that the connection uses. + * Returns the remote port used by the connection. * - * @return the port that the connection uses. + * @return the remote port, or 0 when unavailable. + * @deprecated This method is replaced by {@link #getRemotePort()} */ + @Deprecated(forRemoval = true, since = "5.1.0") // Remove in or after Openfire 5.2.0. public int getPort() { + return getRemotePort(); + } + + @Override + public int getRemotePort() { return socket.getPort(); } diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/nio/NettyConnection.java b/xmppserver/src/main/java/org/jivesoftware/openfire/nio/NettyConnection.java index d17504411a..b26bf09665 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/nio/NettyConnection.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/nio/NettyConnection.java @@ -120,6 +120,15 @@ public String getHostAddress() throws UnknownHostException { return inetAddress.getHostAddress(); } + @Override + public int getRemotePort() { + final SocketAddress remoteAddress = channelHandlerContext.channel().remoteAddress(); + if (remoteAddress != null && remoteAddress instanceof InetSocketAddress) { + return ((InetSocketAddress) remoteAddress).getPort(); + } + return 0; + } + @Override public String getHostName() throws UnknownHostException { final SocketAddress remoteAddress = channelHandlerContext.channel().remoteAddress(); diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/session/IncomingServerSessionTask.java b/xmppserver/src/main/java/org/jivesoftware/openfire/session/IncomingServerSessionTask.java index 58688cd1da..5870a851a1 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/session/IncomingServerSessionTask.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/session/IncomingServerSessionTask.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2009 Jive Software, 2021-2023 Ignite Realtime Foundation. All rights reserved. + * Copyright (C) 2007-2009 Jive Software, 2021-2026 Ignite Realtime Foundation. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/session/LocalSession.java b/xmppserver/src/main/java/org/jivesoftware/openfire/session/LocalSession.java index d907248ecf..29468b44cb 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/session/LocalSession.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/session/LocalSession.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004-2009 Jive Software, 2017-2025 Ignite Realtime Foundation. All rights reserved. + * Copyright (C) 2004-2009 Jive Software, 2017-2026 Ignite Realtime Foundation. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -504,6 +504,15 @@ public String getHostAddress() throws UnknownHostException { return connection.getHostAddress(); } + @Override + public int getRemotePort() { + Connection connection = conn; + if (connection == null) { + return 0; + } + return connection.getRemotePort(); + } + @Override public String getHostName() throws UnknownHostException { Connection connection = conn; diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/session/RemoteSession.java b/xmppserver/src/main/java/org/jivesoftware/openfire/session/RemoteSession.java index 567cfc2896..81b519dcba 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/session/RemoteSession.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/session/RemoteSession.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2009 Jive Software, 2021-2025 Ignite Realtime Foundation. All rights reserved. + * Copyright (C) 2007-2009 Jive Software, 2021-2026 Ignite Realtime Foundation. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,6 +51,7 @@ public abstract class RemoteSession implements Session { private String serverName; private String hostAddress; private String hostName; + private int remotePort = -1; public RemoteSession(byte[] nodeID, JID address) { this.nodeID = nodeID; @@ -180,6 +181,17 @@ public String getHostName() throws UnknownHostException { return hostName; } + @Override + public int getRemotePort() { + if (remotePort == -1) { + ClusterTask task = getRemoteSessionTask(RemoteSessionTask.Operation.getRemotePort); + Object result = doSynchronousClusterTask(task); + remotePort = result == null ? 0 : (Integer) result; + } + return remotePort; + } + + public void deliverRawText(String text) { doClusterTask(getDeliverRawTextTask(text)); } diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/session/RemoteSessionTask.java b/xmppserver/src/main/java/org/jivesoftware/openfire/session/RemoteSessionTask.java index 201b87b1f9..81cba3c331 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/session/RemoteSessionTask.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/session/RemoteSessionTask.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2009 Jive Software, 2021-2025 Ignite Realtime Foundation. All rights reserved. + * Copyright (C) 2007-2009 Jive Software, 2021-2026 Ignite Realtime Foundation. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -143,6 +143,13 @@ else if (operation == Operation.getHostName) { Log.error("Error getting address of session: {}", getSession(), e); } } + else if (operation == Operation.getRemotePort) { + if (getSession().isDetached()) { + Log.debug("Unable to get remote port of detached session: {}", getSession()); + } else { + result = getSession().getRemotePort(); + } + } else if (operation == Operation.validate) { result = getSession().validate(); } @@ -246,6 +253,7 @@ public enum Operation { */ getLocalDomain, getAddress, - getValidatedDomains + getValidatedDomains, + getRemotePort } } diff --git a/xmppserver/src/main/java/org/jivesoftware/openfire/session/Session.java b/xmppserver/src/main/java/org/jivesoftware/openfire/session/Session.java index 2e0f0a3ecb..62e76d7a6b 100644 --- a/xmppserver/src/main/java/org/jivesoftware/openfire/session/Session.java +++ b/xmppserver/src/main/java/org/jivesoftware/openfire/session/Session.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2008 Jive Software, 2017-2025 Ignite Realtime Foundation. All rights reserved. + * Copyright (C) 2005-2008 Jive Software, 2017-2026 Ignite Realtime Foundation. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -195,6 +195,15 @@ default boolean isAuthenticated() { * @throws java.net.UnknownHostException if IP address of host could not be determined. */ String getHostAddress() throws UnknownHostException; + + /** + * Returns the remote port used by the connection. + * + * @return the remote port, or 0 when unavailable. + */ + default int getRemotePort() { + return 0; + } /** * Gets the host name for this IP address. diff --git a/xmppserver/src/main/webapp/server-session-details.jsp b/xmppserver/src/main/webapp/server-session-details.jsp index 0476135415..a52d45cc31 100644 --- a/xmppserver/src/main/webapp/server-session-details.jsp +++ b/xmppserver/src/main/webapp/server-session-details.jsp @@ -1,7 +1,7 @@ <%@ page contentType="text/html; charset=UTF-8" %> <%-- - - - Copyright (C) 2004-2008 Jive Software, 2017-2025 Ignite Realtime Foundation. All rights reserved. + - Copyright (C) 2004-2008 Jive Software, 2017-2026 Ignite Realtime Foundation. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. @@ -268,6 +268,7 @@ + @@ -313,6 +314,14 @@ + + + + + +   + + @@ -333,6 +342,7 @@ + @@ -378,6 +388,14 @@ + + + + + +   + +